@stacks/codec 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,404 @@
1
+ # Stacks Codec JS
2
+
3
+ `@stacks/codec` is a Node.js [native addon](https://nodejs.org/api/addons.html) library written in
4
+ Rust, which provides functions for decoding binary/wire formats used in the Stacks blockchain.
5
+ Features include Clarity values, transactions, post-conditions, Stacks and Bitcoin addresses, and
6
+ more.
7
+
8
+ Various ASM/SIMD optimizations are used in areas which are prone to causing CPU bottlenecks when
9
+ used in hot paths, e.g. decoding raw Clarity values on the fly.
10
+
11
+ _This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon)._
12
+
13
+ ### Runtime Support
14
+ * Node.js v16+
15
+
16
+ ### Platform Support
17
+
18
+ * Linux (glibc) x86_64
19
+ * Linux (glibc) aarch64
20
+ * Linux (musl) x86_64
21
+ * Linux (musl) aarch64
22
+ * MacOS x86_64
23
+ * MacOS aarch64 (Apple Silicon M1)
24
+ * Windows x86_64
25
+
26
+ ## Installation and Usage
27
+
28
+ ```shell
29
+ npm install @stacks/codec
30
+ ```
31
+
32
+ ### Decoding serialized Clarity values
33
+ _Example Clarity value:_
34
+ <details>
35
+ <summary>Expand</summary>
36
+
37
+ ```clar
38
+ (tuple
39
+ (active true)
40
+ (address 'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW)
41
+ (alias "Alice")
42
+ (balance u2000)
43
+ (ping (ok 250))
44
+ (public_key (some 0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58))
45
+ (tags (list u"contributor" u"og \u{e2ad90}" u"clarity"))
46
+ )
47
+ ```
48
+ </details>
49
+
50
+ #### Decode serialized Clarity value to repr string
51
+ ```ts
52
+ import * as assert from 'node:assert';
53
+ import { decodeClarityValueToRepr } from '@stacks/codec';
54
+
55
+ // Serialized hex string of the example Clarity value (0x-prefix optional, Buffer / Uint8Array also accepted)
56
+ const hex = '0x0c00000007066163746976650307616464726573730516142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b05616c6961730d00000005416c6963650762616c616e636501000000000000000000000000000007d00470696e670700000000000000000000000000000000fa0a7075626c69635f6b65790a020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e5804746167730b000000030e0000000b636f6e7472696275746f720e000000066f6720e2ad900e00000007636c6172697479';
57
+
58
+ const reprStr = decodeClarityValueToRepr(hex);
59
+
60
+ assert.strictEqual(
61
+ reprStr,
62
+ `(tuple (active true) (address 'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW) (alias "Alice") (balance u2000) (ping (ok 250)) (public_key (some 0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58)) (tags (list u"contributor" u"og \\u{e2ad90}" u"clarity")))`
63
+ );
64
+ ```
65
+
66
+ #### Decode serialized Clarity value to object
67
+ ```ts
68
+ import * as assert from 'node:assert';
69
+ import { decodeClarityValue } from '@stacks/codec';
70
+
71
+ // Serialized hex string of the example Clarity value (0x-prefix optional, Buffer / Uint8Array also accepted)
72
+ const hex = '0x0c00000007066163746976650307616464726573730516142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b05616c6961730d00000005416c6963650762616c616e636501000000000000000000000000000007d00470696e670700000000000000000000000000000000fa0a7075626c69635f6b65790a020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e5804746167730b000000030e0000000b636f6e7472696275746f720e000000066f6720e2ad900e00000007636c6172697479';
73
+
74
+ // Decode into JSON object
75
+ const decoded = decodeClarityValue(hex);
76
+ ```
77
+
78
+ Decoded Clarity value output result:
79
+
80
+ <details>
81
+ <summary>Expand</summary>
82
+
83
+ ```ts
84
+ // Result object
85
+ assert.deepStrictEqual(decoded, {
86
+ repr: `(tuple (active true) (address 'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW) (alias "Alice") (balance u2000) (ping (ok 250)) (public_key (some 0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58)) (tags (list u"contributor" u"og \\u{e2ad90}" u"clarity")))`,
87
+ hex: '0x0c00000007066163746976650307616464726573730516142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b05616c6961730d00000005416c6963650762616c616e636501000000000000000000000000000007d00470696e670700000000000000000000000000000000fa0a7075626c69635f6b65790a020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e5804746167730b000000030e0000000b636f6e7472696275746f720e000000066f6720e2ad900e00000007636c6172697479',
88
+ type_id: 12,
89
+ data: {
90
+ active: { repr: 'true', hex: '0x03', type_id: 3, value: true },
91
+ address: {
92
+ repr: "'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW",
93
+ hex: '0x0516142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b',
94
+ type_id: 5,
95
+ address_version: 22,
96
+ address_hash_bytes: '0x142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b',
97
+ address: 'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW'
98
+ },
99
+ alias: {
100
+ repr: '"Alice"',
101
+ hex: '0x0d00000005416c696365',
102
+ type_id: 13,
103
+ data: 'Alice'
104
+ },
105
+ balance: {
106
+ repr: 'u2000',
107
+ hex: '0x01000000000000000000000000000007d0',
108
+ type_id: 1,
109
+ value: '2000'
110
+ },
111
+ ping: {
112
+ repr: '(ok 250)',
113
+ hex: '0x0700000000000000000000000000000000fa',
114
+ type_id: 7,
115
+ value: {
116
+ repr: '250',
117
+ hex: '0x00000000000000000000000000000000fa',
118
+ type_id: 0,
119
+ value: '250'
120
+ }
121
+ },
122
+ public_key: {
123
+ repr: '(some 0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58)',
124
+ hex: '0x0a020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58',
125
+ type_id: 10,
126
+ value: {
127
+ repr: '0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58',
128
+ hex: '0x020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58',
129
+ type_id: 2,
130
+ buffer: '0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58'
131
+ }
132
+ },
133
+ tags: {
134
+ repr: '(list u"contributor" u"og \\u{e2ad90}" u"clarity")',
135
+ hex: '0x0b000000030e0000000b636f6e7472696275746f720e000000066f6720e2ad900e00000007636c6172697479',
136
+ type_id: 11,
137
+ list: [
138
+ {
139
+ repr: 'u"contributor"',
140
+ hex: '0x0e0000000b636f6e7472696275746f72',
141
+ type_id: 14,
142
+ data: 'contributor'
143
+ },
144
+ {
145
+ repr: 'u"og \\u{e2ad90}"',
146
+ hex: '0x0e000000066f6720e2ad90',
147
+ type_id: 14,
148
+ data: 'og ⭐'
149
+ },
150
+ {
151
+ repr: 'u"clarity"',
152
+ hex: '0x0e00000007636c6172697479',
153
+ type_id: 14,
154
+ data: 'clarity'
155
+ }
156
+ ]
157
+ }
158
+ }
159
+ });
160
+ ```
161
+ </details>
162
+
163
+ #### Decode serialized Clarity value to typed object
164
+
165
+ ```ts
166
+ import * as assert from 'node:assert';
167
+ import {
168
+ decodeClarityValue,
169
+ ClarityTypeID,
170
+ ClarityValueTuple,
171
+ ClarityValuePrincipalStandard,
172
+ ClarityValueStringAscii,
173
+ ClarityValueInt,
174
+ ClarityValueBoolTrue,
175
+ ClarityValueResponseOk,
176
+ ClarityValueBuffer,
177
+ ClarityValueList,
178
+ ClarityValueStringUtf8,
179
+ ClarityValueOptionalSome,
180
+ ClarityValueUInt,
181
+ } from '@stacks/codec';
182
+
183
+ // Serialized hex string of the example Clarity value (0x-prefix optional, Buffer / Uint8Array also accepted)
184
+ const hex = '0x0c00000007066163746976650307616464726573730516142a7f9b4d4c7d2fdbe69c0b6733a484f37bbc3b05616c6961730d00000005416c6963650762616c616e636501000000000000000000000000000007d00470696e670700000000000000000000000000000000fa0a7075626c69635f6b65790a020000002102d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e5804746167730b000000030e0000000b636f6e7472696275746f720e000000066f6720e2ad900e00000007636c6172697479';
185
+
186
+ // Provide a typed view of the decoded value
187
+ const decoded = decodeClarityValue<ClarityValueTuple<{
188
+ active: ClarityValueBoolTrue;
189
+ address: ClarityValuePrincipalStandard;
190
+ alias: ClarityValueStringAscii;
191
+ balance: ClarityValueUInt;
192
+ ping: ClarityValueResponseOk<ClarityValueInt>;
193
+ public_key: ClarityValueOptionalSome<ClarityValueBuffer>;
194
+ tags: ClarityValueList<ClarityValueStringUtf8>;
195
+ }>>(hex);
196
+
197
+ assert.deepStrictEqual(decoded.data.tags.list.map(v => v.data), ['contributor', 'og ⭐', 'clarity']);
198
+ ```
199
+
200
+ The annotated types are _not_ automatically checked at runtime, so type checks are needed for error handling:
201
+
202
+ <details>
203
+ <summary>Expand</summary>
204
+
205
+ ```ts
206
+ assert.strictEqual(decoded.type_id, ClarityTypeID.Tuple);
207
+ assert.strictEqual(decoded.data.active.type_id, ClarityTypeID.BoolTrue);
208
+ assert.strictEqual(decoded.data.address.type_id, ClarityTypeID.PrincipalStandard);
209
+ assert.strictEqual(decoded.data.alias.type_id, ClarityTypeID.StringAscii);
210
+ assert.strictEqual(decoded.data.balance.type_id, ClarityTypeID.UInt);
211
+ assert.strictEqual(decoded.data.ping.type_id, ClarityTypeID.ResponseOk);
212
+ assert.strictEqual(decoded.data.ping.value.type_id, ClarityTypeID.Int);
213
+ assert.strictEqual(decoded.data.public_key.type_id, ClarityTypeID.OptionalSome);
214
+ assert.strictEqual(decoded.data.public_key.value.type_id, ClarityTypeID.Buffer);
215
+ assert.strictEqual(decoded.data.tags.type_id, ClarityTypeID.List);
216
+ assert.strictEqual(decoded.data.tags.list[0].type_id, ClarityTypeID.StringUtf8);
217
+
218
+ // Now we can safely access typed properties
219
+ assert.strictEqual(decoded.data.active.value, true);
220
+ assert.strictEqual(decoded.data.address.address, 'SPA2MZWV9N67TBYVWTE0PSSKMJ2F6YXW7CBE6YPW');
221
+ assert.strictEqual(decoded.data.alias.data, 'Alice');
222
+ assert.strictEqual(decoded.data.balance.value, '2000');
223
+ assert.strictEqual(decoded.data.ping.value.value, '250');
224
+ assert.strictEqual(decoded.data.public_key.value.buffer, '0x02d4dada83bff981f0cb7ebafcfc6fc7cb5e078b9ee2302a93aae19fb90f872e58');
225
+ assert.deepStrictEqual(decoded.data.tags.list.map(v => v.data), ['contributor', 'og ⭐', 'clarity']);
226
+ ```
227
+ </details>
228
+
229
+ ### Decoding principals
230
+
231
+ #### Principal from serialized Clarity value
232
+
233
+ ```ts
234
+ import * as assert from 'node:assert';
235
+ import { decodeClarityValueToPrincipal } from '@stacks/codec';
236
+
237
+ // Serialized hex string of an example Clarity value (0x-prefix optional, Buffer / Uint8Array also accepted)
238
+ const standardPrincipal = decodeClarityValueToPrincipal('0x0516a13dce8114be0f707f94470a2e5e86eb402f2923');
239
+ assert.strictEqual(principal, 'SP2GKVKM12JZ0YW3ZJH3GMBJYGVNM0BS94ERA45AM');
240
+
241
+ const contractPrincipal = decodeClarityValueToPrincipal('0x0616a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c616273');
242
+ assert.strictEqual(contractPrincipal, 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs');
243
+ ```
244
+
245
+ #### Stacks address from parts
246
+
247
+ ```ts
248
+ import * as assert from 'node:assert';
249
+ import { stacksAddressFromParts } from '@stacks/codec';
250
+
251
+ const stacksAddressData = {
252
+ version: 26,
253
+ // Serialized hex string (0x-prefix optional, Buffer / Uint8Array also accepted)
254
+ hash160: '0xcd1f5bc9aa49e7417cee3e5dba1a92567da41af6'
255
+ };
256
+
257
+ const stacksAddress = stacksAddressFromParts(stacksAddressData.version, stacksAddressData.hash160);
258
+ assert.strictEqual(stacksAddress, 'ST36HYPY9N94YEGBWXRZ5VEGTJ9B7V90TYTM9HGTJ');
259
+ ```
260
+
261
+ #### Stacks address to parts
262
+
263
+ ```ts
264
+ import * as assert from 'node:assert';
265
+ import { decodeStacksAddress } from '@stacks/codec';
266
+
267
+ const [version, hash160] = decodeStacksAddress('ST36HYPY9N94YEGBWXRZ5VEGTJ9B7V90TYTM9HGTJ');
268
+ assert.strictEqual(version, 26);
269
+ assert.strictEqual(hash160, '0xcd1f5bc9aa49e7417cee3e5dba1a92567da41af6');
270
+ ```
271
+
272
+ ### Decoding transactions
273
+
274
+ ```ts
275
+ import * as assert from 'node:assert';
276
+ import {
277
+ decodeTransaction,
278
+ AnchorModeID,
279
+ TransactionVersion,
280
+ TxPayloadTypeID,
281
+ TxSpendingConditionSingleSigHashMode,
282
+ TxPublicKeyEncoding,
283
+ ClarityTypeID,
284
+ PostConditionModeID,
285
+ PostConditionAuthFlag,
286
+ PostConditionAssetInfoID,
287
+ PostConditionNonfungibleConditionCodeID,
288
+ PostConditionNonFungibleConditionName
289
+ } from '@stacks/codec';
290
+
291
+ // Serialized hex string (0x-prefix optional, Buffer / Uint8Array also accepted)
292
+ const rawTx = '0000000001040089f5fd1f719e4449c980de38e3504be6770a2698000000000000014500000000000001f400008b510c9e20dc22040953d9d7eabf2038008fa4d89a5a6cb78bb9d513e75cd0df3924af9ce3b5f185705bc2f6ba3071710ec6a8803ed6da4addc40a05a01ee0f503020000000102021689f5fd1f719e4449c980de38e3504be6770a269816a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c61627314626974636f696e2d6d6f6e6b6579732d6c61627301000000000000000000000000000008ba1002162bcf9762d5b90bc36dc1b4759b1727690f92ddd30e6d61726b6574706c6163652d76340a6c6973742d6173736574000000040616a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c61627301000000000000000000000000000008ba010000000000000000000000000c84588001000000000000000000000000000000c8';
293
+
294
+ const decoded = decodeTransaction(rawTx);
295
+ ```
296
+
297
+ Decoded transaction output result:
298
+ <details>
299
+ <summary>Expand</summary>
300
+
301
+ ```ts
302
+ assert.deepStrictEqual(decoded, {
303
+ tx_id: "0x49bcdba540d5c486f6f7a71de639a128a65f0378d7571e20c75a61661dd5b469",
304
+ chain_id: 1,
305
+ version: TransactionVersion.Mainnet,
306
+ anchor_mode: AnchorModeID.Any,
307
+ auth: {
308
+ type_id: PostConditionAuthFlag.Standard,
309
+ origin_condition: {
310
+ hash_mode: TxSpendingConditionSingleSigHashMode.P2PKH,
311
+ key_encoding: TxPublicKeyEncoding.Compressed,
312
+ nonce: "325",
313
+ signature: "0x008b510c9e20dc22040953d9d7eabf2038008fa4d89a5a6cb78bb9d513e75cd0df3924af9ce3b5f185705bc2f6ba3071710ec6a8803ed6da4addc40a05a01ee0f5",
314
+ signer: {
315
+ address: "SP24ZBZ8ZE6F48JE9G3F3HRTG9FK7E2H6K2QZ3Q1K",
316
+ address_hash_bytes: "0x89f5fd1f719e4449c980de38e3504be6770a2698",
317
+ address_version: 22
318
+ },
319
+ tx_fee: "500"
320
+ }
321
+ },
322
+ payload: {
323
+ type_id: TxPayloadTypeID.ContractCall,
324
+ address: "SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S",
325
+ address_hash_bytes: "0x2bcf9762d5b90bc36dc1b4759b1727690f92ddd3",
326
+ address_version: 22,
327
+ contract_name: "marketplace-v4",
328
+ function_name: "list-asset",
329
+ function_args: [
330
+ {
331
+ type_id: ClarityTypeID.PrincipalContract,
332
+ hex: "0x0616a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c616273",
333
+ repr: "'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs"
334
+ },
335
+ {
336
+ type_id: ClarityTypeID.UInt,
337
+ hex: "0x01000000000000000000000000000008ba",
338
+ repr: "u2234"
339
+ },
340
+ {
341
+ type_id: ClarityTypeID.UInt,
342
+ hex: "0x010000000000000000000000000c845880",
343
+ repr: "u210000000"
344
+ },
345
+ {
346
+ type_id: ClarityTypeID.UInt,
347
+ hex: "0x01000000000000000000000000000000c8",
348
+ repr: "u200"
349
+ }
350
+ ],
351
+ function_args_buffer: "0x000000040616a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c61627301000000000000000000000000000008ba010000000000000000000000000c84588001000000000000000000000000000000c8"
352
+ },
353
+ post_condition_mode: PostConditionModeID.Deny,
354
+ post_conditions: [
355
+ {
356
+ condition_code: PostConditionNonfungibleConditionCodeID.Sent,
357
+ condition_name: PostConditionNonFungibleConditionName.Sent,
358
+ asset: {
359
+ asset_name: "bitcoin-monkeys-labs",
360
+ contract_address: "SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C",
361
+ contract_name: "bitcoin-monkeys-labs"
362
+ },
363
+ asset_info_id: PostConditionAssetInfoID.NonfungibleAsset,
364
+ asset_value: {
365
+ hex: "0x01000000000000000000000000000008ba",
366
+ repr: "u2234",
367
+ type_id: ClarityTypeID.UInt
368
+ },
369
+ principal: {
370
+ address: "SP24ZBZ8ZE6F48JE9G3F3HRTG9FK7E2H6K2QZ3Q1K",
371
+ address_hash_bytes: "0x89f5fd1f719e4449c980de38e3504be6770a2698",
372
+ address_version: 22,
373
+ type_id: ClarityTypeID.Buffer
374
+ }
375
+ }
376
+ ],
377
+ post_conditions_buffer: "0x020000000102021689f5fd1f719e4449c980de38e3504be6770a269816a6a7a70f41adbe8eae708ed7ec2cbf41a272182014626974636f696e2d6d6f6e6b6579732d6c61627314626974636f696e2d6d6f6e6b6579732d6c61627301000000000000000000000000000008ba10"
378
+ });
379
+ ```
380
+ </details>
381
+
382
+ ## Project Layout
383
+
384
+ The directory structure of this project is:
385
+
386
+ <pre>
387
+ /
388
+ ├── Cargo.toml # The Cargo <a href="https://doc.rust-lang.org/cargo/reference/manifest.html">manifest file</a>
389
+ ├── package.json # The npm <a href="https://docs.npmjs.com/cli/v7/configuring-npm/package-json">manifest file</a>
390
+ ├── native/ # The <a href="https://nodejs.org/api/addons.html">Node addon</a> modules built by this project, these are <a href="https://en.wikipedia.org/wiki/Library_(computing)#Shared_libraries">dynamically-linked shared objects</a>
391
+ ├── src/**/*.rs # Directory containing the Rust source code for the project
392
+ |── index.ts # Typescript definitions for the js interface exposed by the Node addon
393
+ |── loader.js # Script to determine which addon file to load based on the executing target platform
394
+ |── loader.d.ts # Type defintions for the functions exported by the Node addon
395
+ |── builder.js # Script to build the native Node addon for the executing host platform
396
+ ├── tests/*.ts # Js/ts unit tests, primarily testing the Neon (rust<->nodejs) interop layer
397
+ └── perf-tests/ # Scripts to run performance benchmarks used by commands in package.json
398
+ </pre>
399
+
400
+ The Rust source code inside the `src/**/deserialize.rs` files are responsible for deserializing the Stacks blockchain wire/binary formats defined in [SIP-005](https://github.com/stacksgov/sips/blob/main/sips/sip-005/sip-005-blocks-and-transactions.md).
401
+
402
+ ## NPM Library Bundling
403
+
404
+ The Node addon modules for all supported platforms are compiled by [CI](.github/workflows/build.yml) and bundled inside the npm package. The native binary files are small enough that the bundled npm package is an acceptable ~20 MB in size.
package/index.d.ts ADDED
@@ -0,0 +1,521 @@
1
+ import * as bindings from './loader';
2
+ export * from './loader';
3
+ export declare const StacksNativeEncodingBindings: typeof bindings;
4
+ export default StacksNativeEncodingBindings;
5
+ export declare type TxPostCondition = PostConditionStx | PostConditionFungible | PostConditionNonfungible;
6
+ export interface DecodedPostConditionsResult {
7
+ post_condition_mode: PostConditionModeID;
8
+ post_conditions: TxPostCondition[];
9
+ }
10
+ export interface DecodedTxResult {
11
+ /** Hex encoded string of the serialized transaction */
12
+ tx_id: string;
13
+ version: TransactionVersion;
14
+ chain_id: number;
15
+ auth: TxAuthStandard | TxAuthSponsored;
16
+ anchor_mode: AnchorModeID;
17
+ post_condition_mode: PostConditionModeID;
18
+ post_conditions: TxPostCondition[];
19
+ /** Hex string */
20
+ post_conditions_buffer: string;
21
+ payload: TxPayloadTokenTransfer | TxPayloadSmartContract | TxPayloadContractCall | TxPayloadPoisonMicroblock | TxPayloadCoinbase | TxPayloadCoinbaseToAltRecipient | TxPayloadVersionedSmartContract | TxPayloadTenureChange | TxPayloadNakamotoCoinbase;
22
+ }
23
+ export declare enum PostConditionAssetInfoID {
24
+ STX = 0,
25
+ FungibleAsset = 1,
26
+ NonfungibleAsset = 2
27
+ }
28
+ export interface PostConditionStx {
29
+ asset_info_id: PostConditionAssetInfoID.STX;
30
+ principal: PostConditionPrincipal;
31
+ condition_code: PostConditionFungibleConditionCodeID;
32
+ condition_name: PostConditionFungibleConditionCodeName;
33
+ amount: string;
34
+ }
35
+ export interface PostConditionFungible {
36
+ asset_info_id: PostConditionAssetInfoID.FungibleAsset;
37
+ principal: PostConditionPrincipal;
38
+ asset: PostConditionAssetInfo;
39
+ condition_code: PostConditionFungibleConditionCodeID;
40
+ condition_name: PostConditionFungibleConditionCodeName;
41
+ amount: string;
42
+ }
43
+ export interface PostConditionNonfungible {
44
+ asset_info_id: PostConditionAssetInfoID.NonfungibleAsset;
45
+ principal: PostConditionPrincipal;
46
+ asset: PostConditionAssetInfo;
47
+ asset_value: ClarityValueAbstract;
48
+ condition_code: PostConditionNonfungibleConditionCodeID;
49
+ condition_name: PostConditionNonFungibleConditionName;
50
+ }
51
+ export interface PostConditionAssetInfo {
52
+ contract_address: string;
53
+ contract_name: string;
54
+ asset_name: string;
55
+ }
56
+ export declare enum PostConditionNonfungibleConditionCodeID {
57
+ Sent = 16,
58
+ NotSent = 17
59
+ }
60
+ export declare enum PostConditionNonFungibleConditionName {
61
+ Sent = "sent",
62
+ NotSent = "not_sent"
63
+ }
64
+ export declare enum PostConditionFungibleConditionCodeID {
65
+ SentEq = 1,
66
+ SentGt = 2,
67
+ SentGe = 3,
68
+ SentLt = 4,
69
+ SentLe = 5
70
+ }
71
+ export declare enum PostConditionFungibleConditionCodeName {
72
+ SentEq = "sent_equal_to",
73
+ SentGt = "sent_greater_than",
74
+ SentGe = "sent_greater_than_or_equal_to",
75
+ SentLt = "sent_less_than",
76
+ SentLe = "sent_less_than_or_equal_to"
77
+ }
78
+ export declare enum PostConditionPrincipalTypeID {
79
+ /** A STX post-condition, which pertains to the origin account's STX. */
80
+ Origin = 1,
81
+ /** A Fungible token post-condition, which pertains to one of the origin account's fungible tokens. */
82
+ Standard = 2,
83
+ /** A Non-fungible token post-condition, which pertains to one of the origin account's non-fungible tokens. */
84
+ Contract = 3
85
+ }
86
+ export declare type PostConditionPrincipal = PostConditionPrincipalOrigin | PostConditionPrincipalStandard | PostConditionPrincipalContract;
87
+ export interface PostConditionPrincipalOrigin {
88
+ type_id: PostConditionPrincipalTypeID.Origin;
89
+ }
90
+ export interface PostConditionPrincipalStandard {
91
+ type_id: PostConditionPrincipalTypeID.Standard;
92
+ address_version: number;
93
+ /** Hex string */
94
+ address_hash_bytes: string;
95
+ address: string;
96
+ }
97
+ export interface PostConditionPrincipalContract {
98
+ type_id: PostConditionPrincipalTypeID.Contract;
99
+ address_version: number;
100
+ /** Hex string */
101
+ address_hash_bytes: string;
102
+ address: string;
103
+ contract_name: string;
104
+ }
105
+ export interface TxPayloadTokenTransfer {
106
+ type_id: TxPayloadTypeID.TokenTransfer;
107
+ recipient: PrincipalStandardData | PrincipalContractData;
108
+ amount: string;
109
+ /** Hex encoded string of the 34-bytes */
110
+ memo_hex: string;
111
+ }
112
+ export declare enum PrincipalTypeID {
113
+ Standard = 5,
114
+ Contract = 6
115
+ }
116
+ export interface PrincipalStandardData {
117
+ type_id: PrincipalTypeID.Standard;
118
+ address_version: number;
119
+ /** Hex string */
120
+ address_hash_bytes: string;
121
+ address: string;
122
+ }
123
+ export interface PrincipalContractData {
124
+ type_id: PrincipalTypeID.Contract;
125
+ contract_name: string;
126
+ address_version: number;
127
+ /** Hex string */
128
+ address_hash_bytes: string;
129
+ address: string;
130
+ }
131
+ export interface TxPayloadSmartContract {
132
+ type_id: TxPayloadTypeID.SmartContract;
133
+ contract_name: string;
134
+ code_body: string;
135
+ }
136
+ export interface TxPayloadContractCall {
137
+ type_id: TxPayloadTypeID.ContractCall;
138
+ address_version: number;
139
+ /** Hex string */
140
+ address_hash_bytes: string;
141
+ address: string;
142
+ contract_name: string;
143
+ function_name: string;
144
+ function_args: ClarityValueAbstract[];
145
+ /** Hex string */
146
+ function_args_buffer: string;
147
+ }
148
+ export interface TxPayloadPoisonMicroblock {
149
+ type_id: TxPayloadTypeID.PoisonMicroblock;
150
+ microblock_header_1: TxMicroblockHeader;
151
+ microblock_header_2: TxMicroblockHeader;
152
+ }
153
+ export interface TxPayloadCoinbase {
154
+ type_id: TxPayloadTypeID.Coinbase;
155
+ /** Hex string */
156
+ payload_buffer: string;
157
+ }
158
+ export interface TxPayloadCoinbaseToAltRecipient {
159
+ type_id: TxPayloadTypeID.CoinbaseToAltRecipient;
160
+ /** Hex string */
161
+ payload_buffer: string;
162
+ recipient: PrincipalStandardData | PrincipalContractData;
163
+ }
164
+ export interface TxPayloadNakamotoCoinbase {
165
+ type_id: TxPayloadTypeID.NakamotoCoinbase;
166
+ /** Hex string */
167
+ payload_buffer: string;
168
+ /** Optional, null if not specified */
169
+ recipient: PrincipalStandardData | PrincipalContractData | null;
170
+ /** Hex string */
171
+ vrf_proof: string;
172
+ }
173
+ export interface TxPayloadVersionedSmartContract {
174
+ type_id: TxPayloadTypeID.VersionedSmartContract;
175
+ clarity_version: ClarityVersion;
176
+ contract_name: string;
177
+ code_body: string;
178
+ }
179
+ export interface TxPayloadTenureChange {
180
+ type_id: TxPayloadTypeID.TenureChange;
181
+ /** Consensus hash of this tenure. Corresponds to the sortition in which the miner of this
182
+ * block was chosen. It may be the case that this miner's tenure gets _extended_ across
183
+ * subsequent sortitions; if this happens, then this `consensus_hash` value _remains the same_
184
+ * as the sortition in which the winning block-commit was mined. */
185
+ tenure_consensus_hash: string;
186
+ /** Consensus hash of the previous tenure. Corresponds to the sortition of the previous winning block-commit. */
187
+ prev_tenure_consensus_hash: string;
188
+ /** Current consensus hash on the underlying burnchain. Corresponds to the last-seen sortition. */
189
+ burn_view_consensus_hash: string;
190
+ /** The StacksBlockId of the last block from the previous tenure */
191
+ previous_tenure_end: string;
192
+ /** The number of blocks produced since the last sortition-linked tenure */
193
+ previous_tenure_blocks: number;
194
+ /** Cause of change in mining tenure. Depending on cause, tenure can be ended or extended. */
195
+ cause: TenureChangeCause;
196
+ /** (Hex string) The ECDSA public key hash of the current tenure */
197
+ pubkey_hash: string;
198
+ }
199
+ export declare enum TenureChangeCause {
200
+ /** A valid winning block-commit */
201
+ BlockFound = 0,
202
+ /** The next burnchain block is taking too long, so extend the runtime budget */
203
+ Extended = 1,
204
+ /** SIP-034: extend specific dimensions - runtime */
205
+ ExtendedRuntime = 2,
206
+ /** SIP-034: extend specific dimensions - read count */
207
+ ExtendedReadCount = 3,
208
+ /** SIP-034: extend specific dimensions - read length */
209
+ ExtendedReadLength = 4,
210
+ /** SIP-034: extend specific dimensions - write count */
211
+ ExtendedWriteCount = 5,
212
+ /** SIP-034: extend specific dimensions - write length */
213
+ ExtendedWriteLength = 6
214
+ }
215
+ export declare enum TxPayloadTypeID {
216
+ TokenTransfer = 0,
217
+ SmartContract = 1,
218
+ ContractCall = 2,
219
+ PoisonMicroblock = 3,
220
+ Coinbase = 4,
221
+ CoinbaseToAltRecipient = 5,
222
+ VersionedSmartContract = 6,
223
+ TenureChange = 7,
224
+ NakamotoCoinbase = 8
225
+ }
226
+ export declare enum PostConditionAuthFlag {
227
+ Standard = 4,
228
+ Sponsored = 5
229
+ }
230
+ export interface TxAuthStandard {
231
+ type_id: PostConditionAuthFlag.Standard;
232
+ origin_condition: DecodedTxSpendingConditionSingleSig | DecodedTxSpendingConditionMultiSig;
233
+ }
234
+ export interface TxAuthSponsored {
235
+ type_id: PostConditionAuthFlag.Sponsored;
236
+ origin_condition: DecodedTxSpendingConditionSingleSig | DecodedTxSpendingConditionMultiSig;
237
+ sponsor_condition: DecodedTxSpendingConditionSingleSig | DecodedTxSpendingConditionMultiSig;
238
+ }
239
+ export declare enum TxSpendingConditionSingleSigHashMode {
240
+ /** hash160(public-key), same as bitcoin's p2pkh */
241
+ P2PKH = 0,
242
+ /** hash160(segwit-program-00(p2pkh)), same as bitcoin's p2sh-p2wpkh */
243
+ P2WPKH = 2
244
+ }
245
+ export declare enum TxSpendingConditionMultiSigHashMode {
246
+ /** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh */
247
+ P2SH = 1,
248
+ /** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh (non-sequential signing) */
249
+ P2SHNonSequential = 5,
250
+ /** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh */
251
+ P2WSH = 3,
252
+ /** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh (non-sequential signing) */
253
+ P2WSHNonSequential = 7
254
+ }
255
+ export declare enum ClarityVersion {
256
+ Clarity1 = 1,
257
+ Clarity2 = 2,
258
+ Clarity3 = 3,
259
+ Clarity4 = 4,
260
+ Clarity5 = 5
261
+ }
262
+ export interface DecodedTxSpendingConditionSingleSig {
263
+ hash_mode: TxSpendingConditionSingleSigHashMode;
264
+ signer: DecodedStacksAddress;
265
+ nonce: string;
266
+ tx_fee: string;
267
+ /** A 1-byte public key encoding field to indicate whether or not the public key should be compressed before hashing. */
268
+ key_encoding: TxPublicKeyEncoding;
269
+ signature: string;
270
+ }
271
+ export interface DecodedTxSpendingConditionMultiSig {
272
+ hash_mode: TxSpendingConditionMultiSigHashMode;
273
+ signer: DecodedStacksAddress;
274
+ nonce: string;
275
+ tx_fee: string;
276
+ fields: (TxAuthFieldPublicKey | TxAuthFieldSignature)[];
277
+ signatures_required: number;
278
+ }
279
+ export declare enum TxAuthFieldTypeID {
280
+ /** The next 33 bytes are a compressed secp256k1 public key. If the field ID is 0x00, the key will be loaded as a compressed secp256k1 public key. */
281
+ PublicKeyCompressed = 0,
282
+ /** The next 33 bytes are a compressed secp256k1 public key. If it is 0x01, then the key will be loaded as an uncompressed secp256k1 public key. */
283
+ PublicKeyUncompressed = 1,
284
+ /** The next 65 bytes are a recoverable secp256k1 ECDSA signature. If the field ID is 0x02, then the recovered public key will be loaded as a compressed public key. */
285
+ SignatureCompressed = 2,
286
+ /** The next 65 bytes are a recoverable secp256k1 ECDSA signature. If it is 0x03, then the recovered public key will be loaded as an uncompressed public key. */
287
+ SignatureUncompressed = 3
288
+ }
289
+ export interface TxAuthFieldPublicKey {
290
+ type_id: TxAuthFieldTypeID.PublicKeyCompressed | TxAuthFieldTypeID.PublicKeyUncompressed;
291
+ /** Hex encoded public key bytes. */
292
+ public_key: string;
293
+ }
294
+ export interface TxAuthFieldSignature {
295
+ type_id: TxAuthFieldTypeID.SignatureCompressed | TxAuthFieldTypeID.SignatureUncompressed;
296
+ /** Hex encoded signatures bytes. */
297
+ signature: string;
298
+ }
299
+ export interface TxMicroblockHeader {
300
+ /** Hex string */
301
+ buffer: string;
302
+ version: number;
303
+ sequence: number;
304
+ /** Hex string */
305
+ prev_block: string;
306
+ /** Hex string */
307
+ tx_merkle_root: string;
308
+ /** Hex string */
309
+ signature: string;
310
+ }
311
+ export declare enum TxPublicKeyEncoding {
312
+ Compressed = 0,
313
+ Uncompressed = 1
314
+ }
315
+ export interface DecodedStacksAddress {
316
+ address_version: number;
317
+ /** Hex-encoded string of the hash160 signer address. */
318
+ address_hash_bytes: string;
319
+ address: string;
320
+ }
321
+ export declare enum TransactionVersion {
322
+ Mainnet = 0,
323
+ Testnet = 128
324
+ }
325
+ export declare enum AnchorModeID {
326
+ /** The transaction MUST be included in an anchored block. */
327
+ OnChainOnly = 1,
328
+ /** The transaction MUST be included in a microblock. */
329
+ OffChainOnly = 2,
330
+ /** The leader can choose where to include the transaction. */
331
+ Any = 3
332
+ }
333
+ export declare enum PostConditionModeID {
334
+ /** This transaction may affect other assets not listed in the post-conditions. */
335
+ Allow = 1,
336
+ /** This transaction may NOT affect other assets besides those listed in the post-conditions. */
337
+ Deny = 2
338
+ }
339
+ export interface ClarityValueCommon {
340
+ /** Clarity repr value */
341
+ repr: string;
342
+ /** Hex encoded string of the serialized Clarity value */
343
+ hex: string;
344
+ }
345
+ export interface ClarityValueAbstract extends ClarityValueCommon {
346
+ type_id: number;
347
+ }
348
+ export declare enum ClarityTypeID {
349
+ Int = 0,
350
+ UInt = 1,
351
+ Buffer = 2,
352
+ BoolTrue = 3,
353
+ BoolFalse = 4,
354
+ PrincipalStandard = 5,
355
+ PrincipalContract = 6,
356
+ ResponseOk = 7,
357
+ ResponseError = 8,
358
+ OptionalNone = 9,
359
+ OptionalSome = 10,
360
+ List = 11,
361
+ Tuple = 12,
362
+ StringAscii = 13,
363
+ StringUtf8 = 14
364
+ }
365
+ export interface ClarityValueInt extends ClarityValueCommon {
366
+ type_id: ClarityTypeID.Int;
367
+ /** String-quoted signed integer */
368
+ value: string;
369
+ }
370
+ export interface ClarityValueUInt extends ClarityValueCommon {
371
+ type_id: ClarityTypeID.UInt;
372
+ /** String-quoted unsigned integer */
373
+ value: string;
374
+ }
375
+ export interface ClarityValueBoolTrue extends ClarityValueCommon {
376
+ type_id: ClarityTypeID.BoolTrue;
377
+ value: true;
378
+ }
379
+ export interface ClarityValueBoolFalse extends ClarityValueCommon {
380
+ type_id: ClarityTypeID.BoolFalse;
381
+ value: false;
382
+ }
383
+ export interface ClarityValueBuffer extends ClarityValueCommon {
384
+ type_id: ClarityTypeID.Buffer;
385
+ /** Hex string */
386
+ buffer: string;
387
+ }
388
+ export interface ClarityValueList<T extends ClarityValue = ClarityValue> extends ClarityValueCommon {
389
+ type_id: ClarityTypeID.List;
390
+ list: T[];
391
+ }
392
+ export interface ClarityValueStringAscii extends ClarityValueCommon {
393
+ type_id: ClarityTypeID.StringAscii;
394
+ data: string;
395
+ }
396
+ export interface ClarityValueStringUtf8 extends ClarityValueCommon {
397
+ type_id: ClarityTypeID.StringUtf8;
398
+ data: string;
399
+ }
400
+ export interface ClarityValuePrincipalStandard extends ClarityValueCommon {
401
+ type_id: ClarityTypeID.PrincipalStandard;
402
+ address: string;
403
+ address_version: number;
404
+ /** Hex string */
405
+ address_hash_bytes: string;
406
+ }
407
+ export interface ClarityValuePrincipalContract extends ClarityValueCommon {
408
+ type_id: ClarityTypeID.PrincipalContract;
409
+ address: string;
410
+ address_version: number;
411
+ /** Hex string */
412
+ address_hash_bytes: string;
413
+ contract_name: string;
414
+ }
415
+ export declare type ClarityTupleData<T extends ClarityValue = ClarityValue> = {
416
+ [key: string]: T;
417
+ };
418
+ export interface ClarityValueTuple<T extends ClarityTupleData = ClarityTupleData> extends ClarityValueCommon {
419
+ type_id: ClarityTypeID.Tuple;
420
+ data: T;
421
+ }
422
+ export interface ClarityValueOptionalSome<T extends ClarityValue = ClarityValue> extends ClarityValueCommon {
423
+ type_id: ClarityTypeID.OptionalSome;
424
+ value: T;
425
+ }
426
+ export interface ClarityValueOptionalNone extends ClarityValueCommon {
427
+ type_id: ClarityTypeID.OptionalNone;
428
+ }
429
+ export interface ClarityValueResponseOk<T extends ClarityValue = ClarityValue> extends ClarityValueCommon {
430
+ type_id: ClarityTypeID.ResponseOk;
431
+ value: T;
432
+ }
433
+ export interface ClarityValueResponseError<T extends ClarityValue = ClarityValue> extends ClarityValueCommon {
434
+ type_id: ClarityTypeID.ResponseError;
435
+ value: T;
436
+ }
437
+ export declare type ClarityValue = ClarityValueInt | ClarityValueUInt | ClarityValueBoolTrue | ClarityValueBoolFalse | ClarityValueBuffer | ClarityValueList | ClarityValueStringAscii | ClarityValueStringUtf8 | ClarityValuePrincipalStandard | ClarityValuePrincipalContract | ClarityValueTuple | ClarityValueOptionalSome | ClarityValueOptionalNone | ClarityValueResponseOk | ClarityValueResponseError;
438
+ export declare type ClarityValueOptional<T extends ClarityValue = ClarityValue> = ClarityValueOptionalSome<T> | ClarityValueOptionalNone;
439
+ export declare type ClarityValueBool = ClarityValueBoolTrue | ClarityValueBoolFalse;
440
+ export declare type ClarityValueResponse<TOk extends ClarityValue = ClarityValue, TError extends ClarityValue = ClarityValue> = ClarityValueResponseOk<TOk> | ClarityValueResponseError<TError>;
441
+ /**
442
+ * Type for commonly used `(optional bool)`
443
+ */
444
+ export declare type ClarityValueOptionalBool = ClarityValueOptional<ClarityValueBool>;
445
+ /**
446
+ * Type for commonly used `(optional uint)`
447
+ */
448
+ export declare type ClarityValueOptionalUInt = ClarityValueOptional<ClarityValueUInt>;
449
+ export interface DecodedNakamotoBlockResult {
450
+ /** Hex encoded string of the block ID (index block hash) */
451
+ block_id: string;
452
+ header: NakamotoBlockHeader;
453
+ txs: DecodedTxResult[];
454
+ }
455
+ export interface NakamotoBlockHeader {
456
+ version: number;
457
+ /** String-quoted unsigned integer - total blocks preceding this one */
458
+ chain_length: string;
459
+ /** String-quoted unsigned integer - total BTC spent in sortition */
460
+ burn_spent: string;
461
+ /** Hex string (20 bytes) - consensus hash of the burnchain block */
462
+ consensus_hash: string;
463
+ /** Hex string (32 bytes) - parent block ID */
464
+ parent_block_id: string;
465
+ /** Hex string (32 bytes) - merkle root of transactions */
466
+ tx_merkle_root: string;
467
+ /** Hex string (32 bytes) - MARF trie root hash */
468
+ state_index_root: string;
469
+ /** String-quoted unsigned integer - Unix timestamp */
470
+ timestamp: string;
471
+ /** Hex string (65 bytes) - miner's ECDSA signature */
472
+ miner_signature: string;
473
+ /** Array of hex strings (65 bytes each) - signer signatures */
474
+ signer_signature: string[];
475
+ /** PoX treatment bitvec */
476
+ pox_treatment: BitVec;
477
+ /** Hex string (32 bytes) - computed block hash */
478
+ block_hash: string;
479
+ /** Hex string (32 bytes) - computed index block hash */
480
+ index_block_hash: string;
481
+ }
482
+ export interface BitVec {
483
+ /** Number of bits */
484
+ len: number;
485
+ /** Hex encoded data bytes */
486
+ data: string;
487
+ /** Array of boolean values for each bit */
488
+ bits: boolean[];
489
+ }
490
+ export interface DecodedStacksBlockResult {
491
+ /** Hex encoded string of the block hash */
492
+ block_hash: string;
493
+ header: StacksBlockHeader;
494
+ txs: DecodedTxResult[];
495
+ }
496
+ export interface StacksBlockHeader {
497
+ version: number;
498
+ total_work: StacksWorkScore;
499
+ /** Hex string (80 bytes) - VRF proof */
500
+ proof: string;
501
+ /** Hex string (32 bytes) - parent block hash */
502
+ parent_block: string;
503
+ /** Hex string (32 bytes) - parent microblock hash */
504
+ parent_microblock: string;
505
+ /** Parent microblock sequence number */
506
+ parent_microblock_sequence: number;
507
+ /** Hex string (32 bytes) - merkle root of transactions */
508
+ tx_merkle_root: string;
509
+ /** Hex string (32 bytes) - MARF trie root hash */
510
+ state_index_root: string;
511
+ /** Hex string (20 bytes) - hash160 of microblock public key */
512
+ microblock_pubkey_hash: string;
513
+ /** Hex string (32 bytes) - computed block hash */
514
+ block_hash: string;
515
+ }
516
+ export interface StacksWorkScore {
517
+ /** String-quoted unsigned integer - burn amount */
518
+ burn: string;
519
+ /** String-quoted unsigned integer - work score */
520
+ work: string;
521
+ }
package/index.js ADDED
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.ClarityTypeID = exports.PostConditionModeID = exports.AnchorModeID = exports.TransactionVersion = exports.TxPublicKeyEncoding = exports.TxAuthFieldTypeID = exports.ClarityVersion = exports.TxSpendingConditionMultiSigHashMode = exports.TxSpendingConditionSingleSigHashMode = exports.PostConditionAuthFlag = exports.TxPayloadTypeID = exports.TenureChangeCause = exports.PrincipalTypeID = exports.PostConditionPrincipalTypeID = exports.PostConditionFungibleConditionCodeName = exports.PostConditionFungibleConditionCodeID = exports.PostConditionNonFungibleConditionName = exports.PostConditionNonfungibleConditionCodeID = exports.PostConditionAssetInfoID = exports.StacksNativeEncodingBindings = void 0;
18
+ const bindings = require("./loader");
19
+ __exportStar(require("./loader"), exports);
20
+ exports.StacksNativeEncodingBindings = bindings;
21
+ exports.default = exports.StacksNativeEncodingBindings;
22
+ var PostConditionAssetInfoID;
23
+ (function (PostConditionAssetInfoID) {
24
+ PostConditionAssetInfoID[PostConditionAssetInfoID["STX"] = 0] = "STX";
25
+ PostConditionAssetInfoID[PostConditionAssetInfoID["FungibleAsset"] = 1] = "FungibleAsset";
26
+ PostConditionAssetInfoID[PostConditionAssetInfoID["NonfungibleAsset"] = 2] = "NonfungibleAsset";
27
+ })(PostConditionAssetInfoID = exports.PostConditionAssetInfoID || (exports.PostConditionAssetInfoID = {}));
28
+ var PostConditionNonfungibleConditionCodeID;
29
+ (function (PostConditionNonfungibleConditionCodeID) {
30
+ PostConditionNonfungibleConditionCodeID[PostConditionNonfungibleConditionCodeID["Sent"] = 16] = "Sent";
31
+ PostConditionNonfungibleConditionCodeID[PostConditionNonfungibleConditionCodeID["NotSent"] = 17] = "NotSent";
32
+ })(PostConditionNonfungibleConditionCodeID = exports.PostConditionNonfungibleConditionCodeID || (exports.PostConditionNonfungibleConditionCodeID = {}));
33
+ var PostConditionNonFungibleConditionName;
34
+ (function (PostConditionNonFungibleConditionName) {
35
+ PostConditionNonFungibleConditionName["Sent"] = "sent";
36
+ PostConditionNonFungibleConditionName["NotSent"] = "not_sent";
37
+ })(PostConditionNonFungibleConditionName = exports.PostConditionNonFungibleConditionName || (exports.PostConditionNonFungibleConditionName = {}));
38
+ var PostConditionFungibleConditionCodeID;
39
+ (function (PostConditionFungibleConditionCodeID) {
40
+ PostConditionFungibleConditionCodeID[PostConditionFungibleConditionCodeID["SentEq"] = 1] = "SentEq";
41
+ PostConditionFungibleConditionCodeID[PostConditionFungibleConditionCodeID["SentGt"] = 2] = "SentGt";
42
+ PostConditionFungibleConditionCodeID[PostConditionFungibleConditionCodeID["SentGe"] = 3] = "SentGe";
43
+ PostConditionFungibleConditionCodeID[PostConditionFungibleConditionCodeID["SentLt"] = 4] = "SentLt";
44
+ PostConditionFungibleConditionCodeID[PostConditionFungibleConditionCodeID["SentLe"] = 5] = "SentLe";
45
+ })(PostConditionFungibleConditionCodeID = exports.PostConditionFungibleConditionCodeID || (exports.PostConditionFungibleConditionCodeID = {}));
46
+ var PostConditionFungibleConditionCodeName;
47
+ (function (PostConditionFungibleConditionCodeName) {
48
+ PostConditionFungibleConditionCodeName["SentEq"] = "sent_equal_to";
49
+ PostConditionFungibleConditionCodeName["SentGt"] = "sent_greater_than";
50
+ PostConditionFungibleConditionCodeName["SentGe"] = "sent_greater_than_or_equal_to";
51
+ PostConditionFungibleConditionCodeName["SentLt"] = "sent_less_than";
52
+ PostConditionFungibleConditionCodeName["SentLe"] = "sent_less_than_or_equal_to";
53
+ })(PostConditionFungibleConditionCodeName = exports.PostConditionFungibleConditionCodeName || (exports.PostConditionFungibleConditionCodeName = {}));
54
+ var PostConditionPrincipalTypeID;
55
+ (function (PostConditionPrincipalTypeID) {
56
+ /** A STX post-condition, which pertains to the origin account's STX. */
57
+ PostConditionPrincipalTypeID[PostConditionPrincipalTypeID["Origin"] = 1] = "Origin";
58
+ /** A Fungible token post-condition, which pertains to one of the origin account's fungible tokens. */
59
+ PostConditionPrincipalTypeID[PostConditionPrincipalTypeID["Standard"] = 2] = "Standard";
60
+ /** A Non-fungible token post-condition, which pertains to one of the origin account's non-fungible tokens. */
61
+ PostConditionPrincipalTypeID[PostConditionPrincipalTypeID["Contract"] = 3] = "Contract";
62
+ })(PostConditionPrincipalTypeID = exports.PostConditionPrincipalTypeID || (exports.PostConditionPrincipalTypeID = {}));
63
+ var PrincipalTypeID;
64
+ (function (PrincipalTypeID) {
65
+ PrincipalTypeID[PrincipalTypeID["Standard"] = 5] = "Standard";
66
+ PrincipalTypeID[PrincipalTypeID["Contract"] = 6] = "Contract";
67
+ })(PrincipalTypeID = exports.PrincipalTypeID || (exports.PrincipalTypeID = {}));
68
+ var TenureChangeCause;
69
+ (function (TenureChangeCause) {
70
+ /** A valid winning block-commit */
71
+ TenureChangeCause[TenureChangeCause["BlockFound"] = 0] = "BlockFound";
72
+ /** The next burnchain block is taking too long, so extend the runtime budget */
73
+ TenureChangeCause[TenureChangeCause["Extended"] = 1] = "Extended";
74
+ /** SIP-034: extend specific dimensions - runtime */
75
+ TenureChangeCause[TenureChangeCause["ExtendedRuntime"] = 2] = "ExtendedRuntime";
76
+ /** SIP-034: extend specific dimensions - read count */
77
+ TenureChangeCause[TenureChangeCause["ExtendedReadCount"] = 3] = "ExtendedReadCount";
78
+ /** SIP-034: extend specific dimensions - read length */
79
+ TenureChangeCause[TenureChangeCause["ExtendedReadLength"] = 4] = "ExtendedReadLength";
80
+ /** SIP-034: extend specific dimensions - write count */
81
+ TenureChangeCause[TenureChangeCause["ExtendedWriteCount"] = 5] = "ExtendedWriteCount";
82
+ /** SIP-034: extend specific dimensions - write length */
83
+ TenureChangeCause[TenureChangeCause["ExtendedWriteLength"] = 6] = "ExtendedWriteLength";
84
+ })(TenureChangeCause = exports.TenureChangeCause || (exports.TenureChangeCause = {}));
85
+ var TxPayloadTypeID;
86
+ (function (TxPayloadTypeID) {
87
+ TxPayloadTypeID[TxPayloadTypeID["TokenTransfer"] = 0] = "TokenTransfer";
88
+ TxPayloadTypeID[TxPayloadTypeID["SmartContract"] = 1] = "SmartContract";
89
+ TxPayloadTypeID[TxPayloadTypeID["ContractCall"] = 2] = "ContractCall";
90
+ TxPayloadTypeID[TxPayloadTypeID["PoisonMicroblock"] = 3] = "PoisonMicroblock";
91
+ TxPayloadTypeID[TxPayloadTypeID["Coinbase"] = 4] = "Coinbase";
92
+ TxPayloadTypeID[TxPayloadTypeID["CoinbaseToAltRecipient"] = 5] = "CoinbaseToAltRecipient";
93
+ TxPayloadTypeID[TxPayloadTypeID["VersionedSmartContract"] = 6] = "VersionedSmartContract";
94
+ TxPayloadTypeID[TxPayloadTypeID["TenureChange"] = 7] = "TenureChange";
95
+ TxPayloadTypeID[TxPayloadTypeID["NakamotoCoinbase"] = 8] = "NakamotoCoinbase";
96
+ })(TxPayloadTypeID = exports.TxPayloadTypeID || (exports.TxPayloadTypeID = {}));
97
+ var PostConditionAuthFlag;
98
+ (function (PostConditionAuthFlag) {
99
+ PostConditionAuthFlag[PostConditionAuthFlag["Standard"] = 4] = "Standard";
100
+ PostConditionAuthFlag[PostConditionAuthFlag["Sponsored"] = 5] = "Sponsored";
101
+ })(PostConditionAuthFlag = exports.PostConditionAuthFlag || (exports.PostConditionAuthFlag = {}));
102
+ var TxSpendingConditionSingleSigHashMode;
103
+ (function (TxSpendingConditionSingleSigHashMode) {
104
+ /** hash160(public-key), same as bitcoin's p2pkh */
105
+ TxSpendingConditionSingleSigHashMode[TxSpendingConditionSingleSigHashMode["P2PKH"] = 0] = "P2PKH";
106
+ /** hash160(segwit-program-00(p2pkh)), same as bitcoin's p2sh-p2wpkh */
107
+ TxSpendingConditionSingleSigHashMode[TxSpendingConditionSingleSigHashMode["P2WPKH"] = 2] = "P2WPKH";
108
+ })(TxSpendingConditionSingleSigHashMode = exports.TxSpendingConditionSingleSigHashMode || (exports.TxSpendingConditionSingleSigHashMode = {}));
109
+ var TxSpendingConditionMultiSigHashMode;
110
+ (function (TxSpendingConditionMultiSigHashMode) {
111
+ /** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh */
112
+ TxSpendingConditionMultiSigHashMode[TxSpendingConditionMultiSigHashMode["P2SH"] = 1] = "P2SH";
113
+ /** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh (non-sequential signing) */
114
+ TxSpendingConditionMultiSigHashMode[TxSpendingConditionMultiSigHashMode["P2SHNonSequential"] = 5] = "P2SHNonSequential";
115
+ /** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh */
116
+ TxSpendingConditionMultiSigHashMode[TxSpendingConditionMultiSigHashMode["P2WSH"] = 3] = "P2WSH";
117
+ /** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh (non-sequential signing) */
118
+ TxSpendingConditionMultiSigHashMode[TxSpendingConditionMultiSigHashMode["P2WSHNonSequential"] = 7] = "P2WSHNonSequential";
119
+ })(TxSpendingConditionMultiSigHashMode = exports.TxSpendingConditionMultiSigHashMode || (exports.TxSpendingConditionMultiSigHashMode = {}));
120
+ var ClarityVersion;
121
+ (function (ClarityVersion) {
122
+ ClarityVersion[ClarityVersion["Clarity1"] = 1] = "Clarity1";
123
+ ClarityVersion[ClarityVersion["Clarity2"] = 2] = "Clarity2";
124
+ ClarityVersion[ClarityVersion["Clarity3"] = 3] = "Clarity3";
125
+ ClarityVersion[ClarityVersion["Clarity4"] = 4] = "Clarity4";
126
+ ClarityVersion[ClarityVersion["Clarity5"] = 5] = "Clarity5";
127
+ })(ClarityVersion = exports.ClarityVersion || (exports.ClarityVersion = {}));
128
+ var TxAuthFieldTypeID;
129
+ (function (TxAuthFieldTypeID) {
130
+ /** The next 33 bytes are a compressed secp256k1 public key. If the field ID is 0x00, the key will be loaded as a compressed secp256k1 public key. */
131
+ TxAuthFieldTypeID[TxAuthFieldTypeID["PublicKeyCompressed"] = 0] = "PublicKeyCompressed";
132
+ /** The next 33 bytes are a compressed secp256k1 public key. If it is 0x01, then the key will be loaded as an uncompressed secp256k1 public key. */
133
+ TxAuthFieldTypeID[TxAuthFieldTypeID["PublicKeyUncompressed"] = 1] = "PublicKeyUncompressed";
134
+ /** The next 65 bytes are a recoverable secp256k1 ECDSA signature. If the field ID is 0x02, then the recovered public key will be loaded as a compressed public key. */
135
+ TxAuthFieldTypeID[TxAuthFieldTypeID["SignatureCompressed"] = 2] = "SignatureCompressed";
136
+ /** The next 65 bytes are a recoverable secp256k1 ECDSA signature. If it is 0x03, then the recovered public key will be loaded as an uncompressed public key. */
137
+ TxAuthFieldTypeID[TxAuthFieldTypeID["SignatureUncompressed"] = 3] = "SignatureUncompressed";
138
+ })(TxAuthFieldTypeID = exports.TxAuthFieldTypeID || (exports.TxAuthFieldTypeID = {}));
139
+ var TxPublicKeyEncoding;
140
+ (function (TxPublicKeyEncoding) {
141
+ TxPublicKeyEncoding[TxPublicKeyEncoding["Compressed"] = 0] = "Compressed";
142
+ TxPublicKeyEncoding[TxPublicKeyEncoding["Uncompressed"] = 1] = "Uncompressed";
143
+ })(TxPublicKeyEncoding = exports.TxPublicKeyEncoding || (exports.TxPublicKeyEncoding = {}));
144
+ var TransactionVersion;
145
+ (function (TransactionVersion) {
146
+ TransactionVersion[TransactionVersion["Mainnet"] = 0] = "Mainnet";
147
+ TransactionVersion[TransactionVersion["Testnet"] = 128] = "Testnet";
148
+ })(TransactionVersion = exports.TransactionVersion || (exports.TransactionVersion = {}));
149
+ var AnchorModeID;
150
+ (function (AnchorModeID) {
151
+ /** The transaction MUST be included in an anchored block. */
152
+ AnchorModeID[AnchorModeID["OnChainOnly"] = 1] = "OnChainOnly";
153
+ /** The transaction MUST be included in a microblock. */
154
+ AnchorModeID[AnchorModeID["OffChainOnly"] = 2] = "OffChainOnly";
155
+ /** The leader can choose where to include the transaction. */
156
+ AnchorModeID[AnchorModeID["Any"] = 3] = "Any";
157
+ })(AnchorModeID = exports.AnchorModeID || (exports.AnchorModeID = {}));
158
+ var PostConditionModeID;
159
+ (function (PostConditionModeID) {
160
+ /** This transaction may affect other assets not listed in the post-conditions. */
161
+ PostConditionModeID[PostConditionModeID["Allow"] = 1] = "Allow";
162
+ /** This transaction may NOT affect other assets besides those listed in the post-conditions. */
163
+ PostConditionModeID[PostConditionModeID["Deny"] = 2] = "Deny";
164
+ })(PostConditionModeID = exports.PostConditionModeID || (exports.PostConditionModeID = {}));
165
+ var ClarityTypeID;
166
+ (function (ClarityTypeID) {
167
+ ClarityTypeID[ClarityTypeID["Int"] = 0] = "Int";
168
+ ClarityTypeID[ClarityTypeID["UInt"] = 1] = "UInt";
169
+ ClarityTypeID[ClarityTypeID["Buffer"] = 2] = "Buffer";
170
+ ClarityTypeID[ClarityTypeID["BoolTrue"] = 3] = "BoolTrue";
171
+ ClarityTypeID[ClarityTypeID["BoolFalse"] = 4] = "BoolFalse";
172
+ ClarityTypeID[ClarityTypeID["PrincipalStandard"] = 5] = "PrincipalStandard";
173
+ ClarityTypeID[ClarityTypeID["PrincipalContract"] = 6] = "PrincipalContract";
174
+ ClarityTypeID[ClarityTypeID["ResponseOk"] = 7] = "ResponseOk";
175
+ ClarityTypeID[ClarityTypeID["ResponseError"] = 8] = "ResponseError";
176
+ ClarityTypeID[ClarityTypeID["OptionalNone"] = 9] = "OptionalNone";
177
+ ClarityTypeID[ClarityTypeID["OptionalSome"] = 10] = "OptionalSome";
178
+ ClarityTypeID[ClarityTypeID["List"] = 11] = "List";
179
+ ClarityTypeID[ClarityTypeID["Tuple"] = 12] = "Tuple";
180
+ ClarityTypeID[ClarityTypeID["StringAscii"] = 13] = "StringAscii";
181
+ ClarityTypeID[ClarityTypeID["StringUtf8"] = 14] = "StringUtf8";
182
+ })(ClarityTypeID = exports.ClarityTypeID || (exports.ClarityTypeID = {}));
package/loader.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ import type { DecodedPostConditionsResult, DecodedTxResult, DecodedNakamotoBlockResult, DecodedStacksBlockResult, ClarityValue, ClarityValueAbstract } from ".";
2
+
3
+ export function getVersion(): string;
4
+
5
+ export function decodeTransaction(arg: string | Buffer): DecodedTxResult;
6
+
7
+ /**
8
+ * Decode a Nakamoto block (Stacks 3.x+).
9
+ * The input should be the raw binary block data as returned by /v3/blocks/{block_id} endpoint.
10
+ * @param arg - Hex string or Buffer containing the raw block data
11
+ */
12
+ export function decodeNakamotoBlock(arg: string | Buffer): DecodedNakamotoBlockResult;
13
+
14
+ /**
15
+ * Decode a Stacks 2.x block.
16
+ * The input should be the raw binary block data as returned by /v2/blocks/{block_id} endpoint.
17
+ * @param arg - Hex string or Buffer containing the raw block data
18
+ */
19
+ export function decodeStacksBlock(arg: string | Buffer): DecodedStacksBlockResult;
20
+
21
+ export function decodeClarityValueToRepr(arg: string | Buffer): string;
22
+
23
+ export function decodeClarityValueToTypeName(arg: string | Buffer): string;
24
+
25
+ export function decodeClarityValue<T extends ClarityValue = ClarityValue>(arg: string | Buffer): T;
26
+
27
+ /**
28
+ *
29
+ * @param arg
30
+ * @param deep - If not true, then the deserialized objects will only contain the
31
+ * properties `hex, repr, type, type_id`. And nested types like Tuple, List, Response, etc will
32
+ * not contain decoded children.
33
+ * TODO: fix the clarity result type definition to be more accurate.
34
+ */
35
+ export function decodeClarityValueList(arg: string | Buffer, deep?: false | undefined): ClarityValueAbstract[];
36
+
37
+ /**
38
+ *
39
+ * @param arg
40
+ * @param deep - If not true, then the deserialized objects will only contain the
41
+ * properties `hex, repr, type, type_id`. And nested types like Tuple, List, Response, etc will
42
+ * not contain decoded children.
43
+ * TODO: fix the clarity result type definition to be more accurate.
44
+ */
45
+ export function decodeClarityValueList(arg: string | Buffer, deep: true): ClarityValue[];
46
+
47
+ export function decodePostConditions(arg: string | Buffer): DecodedPostConditionsResult;
48
+
49
+ export function stacksToBitcoinAddress(stackAddress: string): string;
50
+
51
+ export function bitcoinToStacksAddress(bitcoinAddress: string): string;
52
+
53
+ export function isValidStacksAddress(address: string): boolean;
54
+
55
+ export function decodeStacksAddress(address: string): [version: number, hash160: string];
56
+
57
+ export function decodeClarityValueToPrincipal(clarityValue: string | Buffer) : string;
58
+
59
+ export function stacksAddressFromParts(version: number, hash160: string | Buffer): string;
60
+
61
+ export function memoToString(memo: string | Buffer): string;
62
+
63
+ export function startProfiler(): string;
64
+
65
+ export function stopProfiler(): Buffer;
66
+
67
+ export function createProfiler(): () => Buffer;
68
+
69
+ export function perfTestC32Encode(): Buffer;
package/loader.js ADDED
@@ -0,0 +1,7 @@
1
+ let targetName = `${process.platform}-${process.arch}`;
2
+ if (process.platform === 'linux') {
3
+ const libc = require('detect-libc').familySync() || 'glibc';
4
+ targetName += `-${libc}`;
5
+ }
6
+ module.exports = require(`./native/${targetName}.node`);
7
+
Binary file
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@stacks/codec",
3
+ "version": "1.4.0",
4
+ "description": "Encoding & decoding functions for the Stacks blockchain exposed as a fast native Node.js addon",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build:cargo": "node build.js",
8
+ "build:ts": "tsc",
9
+ "build": "npm run build:ts",
10
+ "build:dev": "npm run build:ts && npm run build:cargo -- dev",
11
+ "test:cargo": "cargo test",
12
+ "test:js": "jest",
13
+ "test": "npm run test:js",
14
+ "lint": "npm run lint:cargo",
15
+ "lint:cargo": "cargo fmt --all -- --check",
16
+ "lint:fix": "cargo fmt --all",
17
+ "perf-test:contract-call-args": "node --expose-gc perf-tests/decode-contract-call-args/test.js",
18
+ "perf-test:decode-post-conditions": "node --expose-gc perf-tests/decode-post-conditions/test.js",
19
+ "perf-test:encode-stx-address": "node perf-tests/encode-stx-address/test.js",
20
+ "perf-test:decode-stx-address": "node perf-tests/decode-stx-address/test.js",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "author": "Stacks Labs",
24
+ "license": "GPL-3.0",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/stx-labs/stacks-codec-js.git"
28
+ },
29
+ "prettier": "@stacks/prettier-config",
30
+ "devDependencies": {
31
+ "@stacks/prettier-config": "^0.0.10",
32
+ "@types/jest": "^27.4.1",
33
+ "cargo-cp-artifact": "^0.1.9",
34
+ "jest": "^27.5.1",
35
+ "ts-jest": "^27.1.4",
36
+ "typescript": "^4.6.3"
37
+ },
38
+ "dependencies": {
39
+ "@types/node": "^16.11.26",
40
+ "detect-libc": "^2.0.1"
41
+ },
42
+ "files": [
43
+ "README.md",
44
+ "native/**/*",
45
+ "index.js",
46
+ "index.d.ts",
47
+ "loader.js",
48
+ "loader.d.ts"
49
+ ],
50
+ "jest": {
51
+ "preset": "ts-jest",
52
+ "testEnvironment": "node",
53
+ "roots": [
54
+ "<rootDir>/tests"
55
+ ]
56
+ }
57
+ }