@@ -13,12 +13,14 @@ import { BufferCV, bufferCVFromString } from '../src/clarity';
1313import {
1414 FungibleConditionCode ,
1515 NonFungibleConditionCode ,
16+ PostConditionMode ,
1617 PostConditionPrincipalId ,
1718 PostConditionType ,
1819} from '../src/constants' ;
1920import {
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+
134174test ( '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+
213282describe ( '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