Smart Signature Testing
Test Algorand smart signatures (LogicSigs) with ease using the Algorand TypeScript Testing framework.
import * as algots from '@algorandfoundation/algorand-typescript';import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing';
// Create the context manager for snippets belowconst ctx = new TestExecutionContext();Define a LogicSig
Section titled “Define a LogicSig”Extend algots.LogicSig class to create a LogicSig:
import * as algots from '@algorandfoundation/algorand-typescript';
class HashedTimeLockedLogicSig extends LogicSig { program(): boolean { // LogicSig code here return true; // Approve transaction }}Execute and Test
Section titled “Execute and Test”Use ctx.executeLogicSig() to run and verify LogicSigs:
ctx.txn.createScope([ctx.any.txn.payment()]).execute(() => { const result = ctx.executeLogicSig(new HashedTimeLockedLogicSig(), Bytes('secret')); expect(result).toBe(true);});executeLogicSig() returns a boolean:
true: Transaction approvedfalse: Transaction rejected
Pass Arguments
Section titled “Pass Arguments”Provide arguments to LogicSigs using executeLogicSig():
const result = ctx.executeLogicSig(new HashedTimeLockedLogicSig(), Bytes('secret'));Access arguments in the LogicSig with algots.op.arg() opcode:
import * as algots from '@algorandfoundation/algorand-typescript';
class HashedTimeLockedLogicSig extends LogicSig { program(): boolean { // LogicSig code here const secret = algots.op.arg(0); const expectedHash = algots.op.sha256(algots.Bytes('secret')); return algots.op.sha256(secret) === expectedHash; }}
// Example usageconst secret = algots.Bytes('secret');expect(ctx.executeLogicSig(new HashedTimeLockedLogicSig(), secret));For more details on available operations, see the coverage.
// test cleanupctx.reset();