-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement stubs for LogicSig and arg, len op codes
- Loading branch information
Showing
13 changed files
with
1,416 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Account, Bytes, Global, LogicSig, op, TransactionType, Txn, Uint64, uint64 } from '@algorandfoundation/algorand-typescript' | ||
import algosdk from 'algosdk' | ||
|
||
export default class HashedTimeLockedLogicSig extends LogicSig { | ||
program(): boolean | uint64 { | ||
// Participants | ||
const sellerAddress = Bytes(algosdk.decodeAddress('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY').publicKey) | ||
const buyerAddress = Bytes(algosdk.decodeAddress('7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M').publicKey) | ||
const seller = Account(sellerAddress) | ||
const buyer = Account(buyerAddress) | ||
|
||
// Contract parameters | ||
const feeLimit = Uint64(1000) | ||
const secretHash = Bytes.fromHex('2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b') | ||
const timeout = Uint64(3000) | ||
|
||
// Transaction conditions | ||
const isPayment = Txn.typeEnum === TransactionType.Payment | ||
const isFeeAcceptable = Txn.fee < feeLimit | ||
const isNoCloseTo = Txn.closeRemainderTo === Global.zeroAddress | ||
const isNoRekey = Txn.rekeyTo === Global.zeroAddress | ||
|
||
// Safety conditions | ||
const safetyConditions = isPayment && isNoCloseTo && isNoRekey | ||
|
||
// Seller receives payment if correct secret is provided | ||
const isToSeller = Txn.receiver === seller | ||
const isSecretCorrect = op.sha256(op.arg(0)) === secretHash | ||
Check failure on line 28 in examples/htlc-logicsig/signature.algo.ts
|
||
const sellerReceives = isToSeller && isSecretCorrect | ||
|
||
// Buyer receives refund after timeout | ||
const isToBuyer = Txn.receiver === buyer | ||
const isAfterTimeout = Txn.firstValid > timeout | ||
const buyerReceivesRefund = isToBuyer && isAfterTimeout | ||
|
||
// Final contract logic | ||
return isFeeAcceptable && safetyConditions && (sellerReceives || buyerReceivesRefund) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Account, Bytes } from '@algorandfoundation/algorand-typescript' | ||
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' | ||
import algosdk from 'algosdk' | ||
import { afterEach, describe, expect, it } from 'vitest' | ||
import { ZERO_ADDRESS } from '../../src/constants' | ||
import HashedTimeLockedLogicSig from './signature.algo' | ||
|
||
describe('HTLC LogicSig', () => { | ||
const ctx = new TestExecutionContext() | ||
|
||
afterEach(() => { | ||
ctx.reset() | ||
}) | ||
|
||
it('seller receives payment if correct secret is provided', () => { | ||
const receiverAddress = Bytes(algosdk.decodeAddress('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY').publicKey) | ||
ctx.txn | ||
.createScope([ | ||
ctx.any.txn.payment({ | ||
fee: 500, | ||
firstValid: 1000, | ||
closeRemainderTo: Account(ZERO_ADDRESS), | ||
rekeyTo: Account(ZERO_ADDRESS), | ||
receiver: Account(receiverAddress), | ||
}), | ||
]) | ||
.execute(() => { | ||
const result = ctx.executeLogicSig(new HashedTimeLockedLogicSig(), Bytes('secret')) | ||
expect(result).toBe(true) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { bytes, internal } from '@algorandfoundation/algorand-typescript' | ||
import { lazyContext } from '../context-helpers/internal-context' | ||
import { asNumber } from '../util' | ||
|
||
export const arg = (a: internal.primitives.StubUint64Compat): bytes => { | ||
const index = asNumber(a) | ||
return lazyContext.value.activeLogicSigArgs[index] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.