Skip to content

Commit

Permalink
Merge pull request #20 from hirosystems/feat/clarity3
Browse files Browse the repository at this point in the history
feat: parse Clarity3 version smart contract tx payloads
  • Loading branch information
zone117x authored Jul 31, 2024
2 parents 09fd8ca + a5aad21 commit ce052e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export enum TxSpendingConditionMultiSigHashMode {
export enum ClarityVersion {
Clarity1 = 1,
Clarity2 = 2,
Clarity3 = 3,
}

export interface DecodedTxSpendingConditionSingleSig {
Expand Down
2 changes: 2 additions & 0 deletions src/stacks_tx/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ impl ClarityVersion {
match n {
x if x == ClarityVersion::Clarity1 as u8 => Some(ClarityVersion::Clarity1),
x if x == ClarityVersion::Clarity2 as u8 => Some(ClarityVersion::Clarity2),
x if x == ClarityVersion::Clarity3 as u8 => Some(ClarityVersion::Clarity3),
_ => None,
}
}
Expand Down Expand Up @@ -687,6 +688,7 @@ pub enum TransactionPublicKeyEncoding {
pub enum ClarityVersion {
Clarity1 = 1,
Clarity2 = 2,
Clarity3 = 3,
}

#[repr(u8)]
Expand Down
40 changes: 39 additions & 1 deletion tests/tx-decode-3.0.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AnchorModeID,
ClarityVersion,
decodeTransaction,
PostConditionAuthFlag,
PostConditionModeID,
Expand All @@ -10,7 +11,8 @@ import {
TxPayloadNakamotoCoinbase,
TxPayloadTypeID,
TxPublicKeyEncoding,
TxSpendingConditionMultiSigHashMode
TxSpendingConditionMultiSigHashMode,
TxSpendingConditionSingleSigHashMode
} from '../index.js';

test('stacks3.0 - decode tx - tenure change', () => {
Expand Down Expand Up @@ -235,3 +237,39 @@ test("stacks 3.0 - decode tx - non-sequential multi-sig", () => {
},
});
});

test('stacks 3.0 - decode tx - versioned smart contract Clariy 3', () => {
const tx = '8080000000040005572d04565d56f67e84ad7e20deedd8e7bba2fd00000000000000000000000000000bb800010c3287ab0587cf952022e079d261baeacc533d8c92b754136271450eb121ba4c0849f3c47f5a6f967dd1088e8242a9f9ed0454bfadaa19c71d25c8812e47160a030200000000060307636f756e7465720000004e28646566696e652d646174612d76617220636f756e742075696e74207530290a28646566696e652d726561642d6f6e6c7920286765742d636f756e742920287661722d67657420636f756e742929';
const decoded = decodeTransaction(tx);

expect(decoded).toEqual({
"tx_id": "0xcd544ee70fe7f15043245217d1ec16cc8d7f68b739adc6ab2925c1c6fd9edc3b",
"version": TransactionVersion.Testnet,
"chain_id": 2147483648,
"auth": {
"type_id": PostConditionAuthFlag.Standard,
"origin_condition": {
"hash_mode": TxSpendingConditionSingleSigHashMode.P2PKH,
"signer": {
"address_version": 26,
"address_hash_bytes": "0x05572d04565d56f67e84ad7e20deedd8e7bba2fd",
"address": "ST2NEB84ASENDXKYGJPQW86YXQCEFEX2ZQPG87ND"
},
"nonce": "0",
"tx_fee": "3000",
"key_encoding": TxPublicKeyEncoding.Compressed,
"signature": "0x010c3287ab0587cf952022e079d261baeacc533d8c92b754136271450eb121ba4c0849f3c47f5a6f967dd1088e8242a9f9ed0454bfadaa19c71d25c8812e47160a"
}
},
"anchor_mode": AnchorModeID.Any,
"post_condition_mode": PostConditionModeID.Deny,
"post_conditions": [],
"post_conditions_buffer": "0x0200000000",
"payload": {
"type_id": TxPayloadTypeID.VersionedSmartContract,
"clarity_version": ClarityVersion.Clarity3,
"contract_name": "counter",
"code_body": "(define-data-var count uint u0)\n(define-read-only (get-count) (var-get count))"
}
});
});

0 comments on commit ce052e3

Please sign in to comment.