Skip to content

Commit 3c7d75c

Browse files
feat: Add originator post-condition mode and maybe-sent condition (#1841)
* feat: Add originator post-condition mode and maybe-sent condition * docs: add better doc strings
1 parent b31202a commit 3c7d75c

7 files changed

Lines changed: 271 additions & 2 deletions

File tree

‎packages/transactions/src/constants.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ export enum PostConditionMode {
108108
Allow = 0x01,
109109
/** `Deny` — Do not allow unspecified transfers */
110110
Deny = 0x02,
111+
/**
112+
* `Originator` (SIP-040) — Deny for the transaction origin, Allow for everyone else
113+
*
114+
* **⚠︎ Attention**: Enabled with [Epoch 3.4](https://forum.stacks.org/t/clarity-5-and-epoch-3-4/18659)
115+
*
116+
* @see [SIP-039](https://github.com/stacksgov/sips/pull/256/changes)
117+
* @see [SIP-040](https://github.com/stacksgov/sips/pull/257/changes)
118+
*/
119+
Originator = 0x03,
111120
}
112121

113122
/**
@@ -188,6 +197,15 @@ export enum FungibleConditionCode {
188197
export enum NonFungibleConditionCode {
189198
Sends = 0x10,
190199
DoesNotSend = 0x11,
200+
/**
201+
* `MaybeSent` (SIP-040) — The NFT may or may not be sent; always passes
202+
*
203+
* **⚠︎ Attention**: Enabled with [Epoch 3.4](https://forum.stacks.org/t/clarity-5-and-epoch-3-4/18659)
204+
*
205+
* @see [SIP-039](https://github.com/stacksgov/sips/pull/256/changes)
206+
* @see [SIP-040](https://github.com/stacksgov/sips/pull/257/changes)
207+
*/
208+
MaybeSent = 0x12,
191209
}
192210

193211
/**

‎packages/transactions/src/pc.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ class PartialPcWithPrincipal {
166166
willNotSendAsset() {
167167
return new PartialPcNftWithCode(this.address, 'not-sent');
168168
}
169+
170+
/**
171+
* ### NFT Post Condition
172+
* A post-condition where the NFT `NonFungibleConditionCode.MaybeSent` (may or may not be sent).
173+
* Finalize with the chained `.nft(…)` method.
174+
*
175+
* **⚠︎ Attention**: Enabled with [Epoch 3.4](https://forum.stacks.org/t/clarity-5-and-epoch-3-4/18659)
176+
*
177+
* @see [SIP-039](https://github.com/stacksgov/sips/pull/256/changes)
178+
* @see [SIP-040](https://github.com/stacksgov/sips/pull/257/changes)
179+
*/
180+
willMaybeSendAsset() {
181+
return new PartialPcNftWithCode(this.address, 'maybe-sent');
182+
}
169183
}
170184

171185
/**

‎packages/transactions/src/postcondition-types.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@ export type FungiblePostCondition = {
2525
amount: string | bigint | number;
2626
};
2727

28-
export type NonFungibleComparator = 'sent' | 'not-sent';
28+
/**
29+
* The type of non-fungible token post-condition comparison.
30+
*
31+
* - `sent`: The NFT MUST have been sent by the principal.
32+
* - `not-sent`: The NFT MUST NOT have been sent by the principal.
33+
* - `maybe-sent`: The NFT may or may not have been sent by the principal.
34+
*
35+
* **⚠︎ Attention**: `maybe-sent` is only enabled starting with [Epoch 3.4](https://forum.stacks.org/t/clarity-5-and-epoch-3-4/18659)
36+
*
37+
* @see [SIP-039](https://github.com/stacksgov/sips/pull/256/changes)
38+
* @see [SIP-040](https://github.com/stacksgov/sips/pull/257/changes)
39+
*/
40+
export type NonFungibleComparator = 'sent' | 'not-sent' | 'maybe-sent';
2941

3042
export type NonFungiblePostCondition = {
3143
type: 'nft-postcondition';
@@ -41,4 +53,17 @@ export type NonFungiblePostCondition = {
4153

4254
export type PostCondition = StxPostCondition | FungiblePostCondition | NonFungiblePostCondition;
4355

44-
export type PostConditionModeName = 'allow' | 'deny';
56+
/**
57+
* Describes how unspecified asset transfers are handled in a transaction:
58+
* - `'allow'`: Allow unspecified asset transfers.
59+
* - `'deny'`: Do not allow unspecified asset transfers.
60+
* - `'originator'`: Deny unspecified asset transfers for the transaction origin, allow for others (e.g. smart contracts).
61+
*
62+
* **Note**: Specified post-conditions are always checked, regardless of _mode_.
63+
*
64+
* **⚠︎ Attention**: `originator` is only enabled starting with [Epoch 3.4](https://forum.stacks.org/t/clarity-5-and-epoch-3-4/18659)
65+
*
66+
* @see {@link https://github.com/stacksgov/sips/pull/256/changes SIP-039}
67+
* @see {@link https://github.com/stacksgov/sips/pull/257/changes SIP-040}
68+
*/
69+
export type PostConditionModeName = 'allow' | 'deny' | 'originator';

‎packages/transactions/src/postcondition.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum PostConditionCodeWireType {
4747

4848
sent = NonFungibleConditionCode.Sends,
4949
'not-sent' = NonFungibleConditionCode.DoesNotSend,
50+
'maybe-sent' = NonFungibleConditionCode.MaybeSent,
5051
}
5152

5253
export function postConditionToWire(postcondition: PostCondition): PostConditionWire {
@@ -178,6 +179,7 @@ export function postConditionModeFrom(
178179
if (typeof mode === 'number') return mode;
179180
if (mode === 'allow') return PostConditionMode.Allow;
180181
if (mode === 'deny') return PostConditionMode.Deny;
182+
if (mode === 'originator') return PostConditionMode.Originator;
181183
throw new Error(`Invalid post condition mode: ${mode}`);
182184
}
183185

‎packages/transactions/tests/builder.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,3 +2971,26 @@ describe('multi-sig', () => {
29712971
await expect(txMismatch).rejects.toThrow();
29722972
});
29732973
});
2974+
2975+
test('Build transaction with originator post-condition mode', async () => {
2976+
const senderKey = 'edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc01';
2977+
const publicKey = privateKeyToPublic(senderKey);
2978+
2979+
const transaction = await makeUnsignedContractCall({
2980+
contractAddress: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
2981+
contractName: 'test-contract',
2982+
functionName: 'test-function',
2983+
functionArgs: [],
2984+
publicKey,
2985+
fee: 0,
2986+
nonce: 0,
2987+
network: STACKS_TESTNET,
2988+
postConditionMode: 'originator',
2989+
});
2990+
2991+
const serialized = transaction.serializeBytes();
2992+
const deserializedTx = deserializeTransaction(new BytesReader(serialized));
2993+
2994+
expect(deserializedTx.postConditionMode).toBe(PostConditionMode.Originator);
2995+
expect(deserializedTx.postConditionMode).toBe(0x03);
2996+
});

‎packages/transactions/tests/pc.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ describe('pc -- post condition builder', () => {
242242
} as NonFungiblePostCondition;
243243
expect(pc).toEqual(postCondition);
244244
});
245+
246+
test('will maybe send nft', () => {
247+
const pc = Pc.principal(STANDARD_ADDRESS)
248+
.willMaybeSendAsset()
249+
.nft(`${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}`, NFT_TOKEN_NAME, NFT_ASSET_ID);
250+
const postCondition = {
251+
type: 'nft-postcondition',
252+
address: STANDARD_ADDRESS,
253+
condition: 'maybe-sent',
254+
asset: `${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}::${NFT_TOKEN_NAME}`,
255+
assetId: NFT_ASSET_ID,
256+
} as NonFungiblePostCondition;
257+
expect(pc).toEqual(postCondition);
258+
});
245259
});
246260
});
247261

@@ -403,6 +417,20 @@ describe('pc -- post condition builder', () => {
403417
} as NonFungiblePostCondition;
404418
expect(pc).toEqual(postCondition);
405419
});
420+
421+
test('will maybe send nft', () => {
422+
const pc = Pc.principal(`${CONTRACT_ADDRESS}.${CONTRACT_NAME}`)
423+
.willMaybeSendAsset()
424+
.nft(`${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}`, NFT_TOKEN_NAME, NFT_ASSET_ID);
425+
const postCondition = {
426+
type: 'nft-postcondition',
427+
address: `${CONTRACT_ADDRESS}.${CONTRACT_NAME}`,
428+
condition: 'maybe-sent',
429+
asset: `${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}::${NFT_TOKEN_NAME}`,
430+
assetId: NFT_ASSET_ID,
431+
} as NonFungiblePostCondition;
432+
expect(pc).toEqual(postCondition);
433+
});
406434
});
407435
});
408436

@@ -416,6 +444,19 @@ describe('pc -- post condition builder', () => {
416444
amount: '12345',
417445
});
418446
});
447+
448+
test('origin will maybe send nft', () => {
449+
const pc = Pc.origin()
450+
.willMaybeSendAsset()
451+
.nft(`${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}`, NFT_TOKEN_NAME, NFT_ASSET_ID);
452+
expect(pc).toEqual({
453+
type: 'nft-postcondition',
454+
address: 'origin',
455+
condition: 'maybe-sent',
456+
asset: `${NFT_CONTRACT_ADDRESS}.${NFT_CONTRACT_NAME}::${NFT_TOKEN_NAME}`,
457+
assetId: NFT_ASSET_ID,
458+
});
459+
});
419460
});
420461

421462
describe('fromHex function', () => {

‎packages/transactions/tests/postcondition.test.ts‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import { BufferCV, bufferCVFromString } from '../src/clarity';
1313
import {
1414
FungibleConditionCode,
1515
NonFungibleConditionCode,
16+
PostConditionMode,
1617
PostConditionPrincipalId,
1718
PostConditionType,
1819
} from '../src/constants';
1920
import {
2021
conditionByteToType,
2122
conditionTypeToByte,
23+
postConditionModeFrom,
2224
postConditionToHex,
2325
postConditionToWire,
2426
wireToPostCondition,
@@ -131,6 +133,44 @@ test('Non-fungible post condition serialization and deserialization', () => {
131133
expect(bytesToUtf8(hexToBytes((deserialized.assetName as BufferCV).value))).toEqual(nftAssetName);
132134
});
133135

136+
test('Non-fungible post condition with maybe-sent serialization and deserialization', () => {
137+
const postConditionType = PostConditionType.NonFungible;
138+
139+
const address = 'SP2JXKMSH007NPYAQHKJPQMAQYAD90NQGTVJVQ02B';
140+
const contractName = 'contract-name';
141+
142+
const conditionCode = NonFungibleConditionCode.MaybeSent;
143+
144+
const assetAddress = 'SP2ZP4GJDZJ1FDHTQ963F0292PE9J9752TZJ68F21';
145+
const assetContractName = 'contract_name';
146+
const assetName = 'asset_name';
147+
148+
const nftAssetName = 'nft_asset_name';
149+
150+
const postCondition = postConditionToWire({
151+
type: 'nft-postcondition',
152+
address: `${address}.${contractName}`,
153+
condition: 'maybe-sent',
154+
asset: `${assetAddress}.${assetContractName}::${assetName}`,
155+
assetId: bufferCVFromString(nftAssetName),
156+
});
157+
158+
const deserialized = serializeDeserialize(
159+
postCondition,
160+
StacksWireType.PostCondition
161+
) as NonFungiblePostConditionWire;
162+
expect(deserialized.conditionType).toBe(postConditionType);
163+
expect(deserialized.principal.prefix).toBe(PostConditionPrincipalId.Contract);
164+
if (!('address' in deserialized.principal)) throw TypeError;
165+
expect(addressToString(deserialized.principal.address)).toBe(address);
166+
expect((deserialized.principal as ContractPrincipalWire).contractName.content).toBe(contractName);
167+
expect(deserialized.conditionCode).toBe(conditionCode);
168+
expect(addressToString(deserialized.asset.address)).toBe(assetAddress);
169+
expect(deserialized.asset.contractName.content).toBe(assetContractName);
170+
expect(deserialized.asset.assetName.content).toBe(assetName);
171+
expect(bytesToUtf8(hexToBytes((deserialized.assetName as BufferCV).value))).toEqual(nftAssetName);
172+
});
173+
134174
test('Non-fungible post condition with string IDs serialization and deserialization', () => {
135175
const postConditionType = PostConditionType.NonFungible;
136176

@@ -202,6 +242,17 @@ describe(postConditionToHex.name, () => {
202242
expected:
203243
'02021a164247d6f2b425ac5771423ae6c80c754f7172b01a164247d6f2b425ac5771423ae6c80c754f7172b005746f6b656e03746f6b010000000000000000000000000000002011',
204244
},
245+
{
246+
repr: {
247+
type: 'nft-postcondition',
248+
address: 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6',
249+
condition: 'maybe-sent',
250+
asset: 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6.token::tok',
251+
assetId: Cl.uint(32),
252+
} as const,
253+
expected:
254+
'02021a164247d6f2b425ac5771423ae6c80c754f7172b01a164247d6f2b425ac5771423ae6c80c754f7172b005746f6b656e03746f6b010000000000000000000000000000002012',
255+
},
205256
];
206257

207258
test.each(TEST_CASES)('postConditionToHex %p', ({ repr, expected }) => {
@@ -210,6 +261,24 @@ describe(postConditionToHex.name, () => {
210261
});
211262
});
212263

264+
describe('postConditionModeFrom', () => {
265+
test('converts originator string to PostConditionMode.Originator', () => {
266+
expect(postConditionModeFrom('originator')).toBe(PostConditionMode.Originator);
267+
});
268+
269+
test('converts allow string to PostConditionMode.Allow', () => {
270+
expect(postConditionModeFrom('allow')).toBe(PostConditionMode.Allow);
271+
});
272+
273+
test('converts deny string to PostConditionMode.Deny', () => {
274+
expect(postConditionModeFrom('deny')).toBe(PostConditionMode.Deny);
275+
});
276+
277+
test('passes through numeric enum values', () => {
278+
expect(postConditionModeFrom(PostConditionMode.Originator)).toBe(PostConditionMode.Originator);
279+
});
280+
});
281+
213282
describe('origin postcondition', () => {
214283
test('origin postcondition to wire', () => {
215284
const pc = {
@@ -322,6 +391,7 @@ describe('conditionTypeToByte', () => {
322391
const nonFungibleTestCases = [
323392
{ name: 'sent', expectedCode: NonFungibleConditionCode.Sends },
324393
{ name: 'not-sent', expectedCode: NonFungibleConditionCode.DoesNotSend },
394+
{ name: 'maybe-sent', expectedCode: NonFungibleConditionCode.MaybeSent },
325395
] as const;
326396

327397
test.each(fungibleTestCases)(
@@ -353,6 +423,7 @@ describe('conditionBytesToType', () => {
353423
const nonFungibleTestCases = [
354424
{ code: NonFungibleConditionCode.Sends, expectedName: 'sent' },
355425
{ code: NonFungibleConditionCode.DoesNotSend, expectedName: 'not-sent' },
426+
{ code: NonFungibleConditionCode.MaybeSent, expectedName: 'maybe-sent' },
356427
] as const;
357428

358429
test.each(fungibleTestCases)(
@@ -561,3 +632,78 @@ describe('post-condition amount u64 validation', () => {
561632
});
562633
});
563634
});
635+
636+
describe('SIP-040 stacks-core test vectors', () => {
637+
test('Vector 1: Originator mode + STX SentEq post-condition', () => {
638+
const txHex =
639+
'80800000000400143e543243dfcd8c02a12ad7ea371bd07bc91df90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003030000000100010100000000000003e801047465737400000009286f6b207472756529';
640+
const tx = deserializeTransaction(txHex);
641+
expect(tx.postConditionMode).toBe(PostConditionMode.Originator);
642+
expect(tx.postConditions.values).toHaveLength(1);
643+
const pc = wireToPostCondition(tx.postConditions.values[0]);
644+
expect(pc.type).toBe('stx-postcondition');
645+
expect(pc.address).toBe('origin');
646+
expect(pc.condition).toBe('eq');
647+
expect((pc as StxPostCondition).amount).toBe('1000');
648+
});
649+
650+
test('Vector 2: Originator mode + Fungible SentGe post-condition', () => {
651+
const txHex =
652+
'80800000000400143e543243dfcd8c02a12ad7ea371bd07bc91df900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030300000001010101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0d746573742d636f6e747261637408746573742d6e667403000000000000138801047465737400000009286f6b207472756529';
653+
const tx = deserializeTransaction(txHex);
654+
expect(tx.postConditionMode).toBe(PostConditionMode.Originator);
655+
const pc = wireToPostCondition(tx.postConditions.values[0]);
656+
expect(pc.type).toBe('ft-postcondition');
657+
expect(pc.address).toBe('origin');
658+
expect(pc.condition).toBe('gte');
659+
});
660+
661+
test('Vector 3: Originator mode + NFT MaybeSent post-condition', () => {
662+
const txHex =
663+
'80800000000400143e543243dfcd8c02a12ad7ea371bd07bc91df900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030300000001020101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0d746573742d636f6e747261637408746573742d6e667401000000000000000000000000000000011201047465737400000009286f6b207472756529';
664+
const tx = deserializeTransaction(txHex);
665+
expect(tx.postConditionMode).toBe(PostConditionMode.Originator);
666+
const pc = wireToPostCondition(tx.postConditions.values[0]);
667+
expect(pc.type).toBe('nft-postcondition');
668+
expect(pc.address).toBe('origin');
669+
expect(pc.condition).toBe('maybe-sent');
670+
});
671+
672+
test('Vector 4: Deny mode + NFT MaybeSent post-condition', () => {
673+
const txHex =
674+
'80800000000400143e543243dfcd8c02a12ad7ea371bd07bc91df900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030200000001020101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0d746573742d636f6e747261637408746573742d6e667401000000000000000000000000000000011201047465737400000009286f6b207472756529';
675+
const tx = deserializeTransaction(txHex);
676+
expect(tx.postConditionMode).toBe(PostConditionMode.Deny);
677+
const pc = wireToPostCondition(tx.postConditions.values[0]);
678+
expect(pc.type).toBe('nft-postcondition');
679+
expect(pc.condition).toBe('maybe-sent');
680+
});
681+
682+
test('Vector 5: Originator mode + multiple post-conditions (STX + NFT MaybeSent)', () => {
683+
const txHex =
684+
'80800000000400143e543243dfcd8c02a12ad7ea371bd07bc91df90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003030000000200010500000000000007d0020101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0d746573742d636f6e747261637408746573742d6e6674010000000000000000000000000000002a1201047465737400000009286f6b207472756529';
685+
const tx = deserializeTransaction(txHex);
686+
expect(tx.postConditionMode).toBe(PostConditionMode.Originator);
687+
expect(tx.postConditions.values).toHaveLength(2);
688+
const pc0 = wireToPostCondition(tx.postConditions.values[0]);
689+
expect(pc0.type).toBe('stx-postcondition');
690+
expect(pc0.address).toBe('origin');
691+
expect(pc0.condition).toBe('lte');
692+
expect((pc0 as StxPostCondition).amount).toBe('2000');
693+
const pc1 = wireToPostCondition(tx.postConditions.values[1]);
694+
expect(pc1.type).toBe('nft-postcondition');
695+
expect(pc1.address).toBe('origin');
696+
expect(pc1.condition).toBe('maybe-sent');
697+
});
698+
699+
test('Vector 6: Standalone MaybeSent NFT post-condition bytes', () => {
700+
const hex =
701+
'020101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0d746573742d636f6e747261637408746573742d6e6674010000000000000000000000000000000112';
702+
const pc = Pc.fromHex(hex);
703+
expect(pc.type).toBe('nft-postcondition');
704+
expect(pc.condition).toBe('maybe-sent');
705+
if (pc.type === 'nft-postcondition') {
706+
expect(pc.assetId).toEqual(Cl.uint(1));
707+
}
708+
});
709+
});

0 commit comments

Comments
 (0)