@secondlayer/stacks 5.1.3 → 6.1.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 +1 -8
- package/dist/shared/{stacks-qt8ehebn.js → stacks-esm-0rdxcta7.js} +2 -2
- package/dist/shared/{stacks-qt8ehebn.js.map → stacks-esm-0rdxcta7.js.map} +1 -1
- package/dist/shared/{stacks-4hs8r4q2.js → stacks-esm-1jae0a3r.js} +2 -2
- package/dist/shared/{stacks-xjkn3en1.cjs.map → stacks-esm-1jae0a3r.js.map} +1 -1
- package/dist/shared/{stacks-4d7c3x27.js → stacks-esm-2mc7ebrm.js} +3 -3
- package/dist/shared/{stacks-4d7c3x27.js.map → stacks-esm-2mc7ebrm.js.map} +1 -1
- package/dist/shared/{stacks-8brgfvqx.js → stacks-esm-3g33bqsm.js} +4 -4
- package/dist/shared/{stacks-8brgfvqx.js.map → stacks-esm-3g33bqsm.js.map} +1 -1
- package/dist/shared/{stacks-gjz26k6b.js → stacks-esm-3pv5avgr.js} +59 -15
- package/dist/shared/stacks-esm-3pv5avgr.js.map +12 -0
- package/dist/shared/{stacks-qex691bh.js → stacks-esm-743jfh10.js} +6 -6
- package/dist/shared/{stacks-qex691bh.js.map → stacks-esm-743jfh10.js.map} +1 -1
- package/dist/shared/{stacks-wxfd6r4b.js → stacks-esm-7fpqgtyx.js} +3 -3
- package/dist/shared/{stacks-wxfd6r4b.js.map → stacks-esm-7fpqgtyx.js.map} +1 -1
- package/dist/shared/{stacks-71k0vmct.js → stacks-esm-9rnx0zxr.js} +5 -5
- package/dist/shared/{stacks-71k0vmct.js.map → stacks-esm-9rnx0zxr.js.map} +1 -1
- package/dist/shared/{stacks-fd4rx56e.js → stacks-esm-aw2mj3sj.js} +7 -7
- package/dist/shared/{stacks-fd4rx56e.js.map → stacks-esm-aw2mj3sj.js.map} +1 -1
- package/dist/shared/{stacks-qs5fzts5.js → stacks-esm-cvhazvhd.js} +2 -2
- package/dist/shared/{stacks-qs5fzts5.js.map → stacks-esm-cvhazvhd.js.map} +1 -1
- package/dist/shared/{stacks-69nq48jp.js → stacks-esm-fcfdn2zx.js} +5 -5
- package/dist/shared/{stacks-69nq48jp.js.map → stacks-esm-fcfdn2zx.js.map} +1 -1
- package/dist/shared/{stacks-x0qqwrev.js → stacks-esm-hr7fpxtj.js} +4 -4
- package/dist/shared/{stacks-x0qqwrev.js.map → stacks-esm-hr7fpxtj.js.map} +1 -1
- package/dist/shared/{stacks-mbt36bzt.js → stacks-esm-kds4n82p.js} +11 -11
- package/dist/shared/{stacks-mbt36bzt.js.map → stacks-esm-kds4n82p.js.map} +1 -1
- package/dist/shared/{stacks-kg1npstf.js → stacks-esm-n53kvwmt.js} +2 -2
- package/dist/shared/{stacks-kg1npstf.js.map → stacks-esm-n53kvwmt.js.map} +1 -1
- package/dist/shared/{stacks-v2rfr46a.js → stacks-esm-pxhfqfff.js} +4 -4
- package/dist/shared/{stacks-v2rfr46a.js.map → stacks-esm-pxhfqfff.js.map} +1 -1
- package/dist/shared/{stacks-yajyej2y.js → stacks-esm-q19a17fd.js} +3 -3
- package/dist/shared/{stacks-yajyej2y.js.map → stacks-esm-q19a17fd.js.map} +1 -1
- package/dist/shared/{stacks-v03zef3g.js → stacks-esm-qk0p9fr6.js} +9 -9
- package/dist/shared/{stacks-v03zef3g.js.map → stacks-esm-qk0p9fr6.js.map} +1 -1
- package/dist/shared/{stacks-qcae5852.js → stacks-esm-qkj67g80.js} +2 -2
- package/dist/shared/{stacks-qcae5852.js.map → stacks-esm-qkj67g80.js.map} +1 -1
- package/dist/shared/{stacks-mftyrc60.js → stacks-esm-r6dejrgr.js} +4 -4
- package/dist/shared/{stacks-mftyrc60.js.map → stacks-esm-r6dejrgr.js.map} +1 -1
- package/dist/shared/{stacks-9c1yam8b.js → stacks-esm-r9q4wnyw.js} +5 -5
- package/dist/shared/{stacks-9c1yam8b.js.map → stacks-esm-r9q4wnyw.js.map} +1 -1
- package/dist/shared/{stacks-51ybf135.js → stacks-esm-skmcf5n2.js} +5 -5
- package/dist/shared/{stacks-51ybf135.js.map → stacks-esm-skmcf5n2.js.map} +1 -1
- package/dist/shared/{stacks-yvb7rvwk.js → stacks-esm-v45990hx.js} +2 -2
- package/dist/shared/{stacks-8vzxax5e.cjs.map → stacks-esm-v45990hx.js.map} +1 -1
- package/dist/shared/{stacks-z90xdr1x.js → stacks-esm-wv67m4g6.js} +3 -3
- package/dist/shared/{stacks-z90xdr1x.js.map → stacks-esm-wv67m4g6.js.map} +1 -1
- package/dist/shared/{stacks-1m3m1agc.js → stacks-esm-x1r01353.js} +5 -5
- package/dist/shared/{stacks-1m3m1agc.js.map → stacks-esm-x1r01353.js.map} +1 -1
- package/dist/src/accounts/index.cjs +558 -4
- package/dist/src/accounts/index.cjs.map +10 -4
- package/dist/src/accounts/index.js +3 -3
- package/dist/src/actions/index.cjs +3815 -1
- package/dist/src/actions/index.cjs.map +65 -3
- package/dist/src/actions/index.js +14 -14
- package/dist/src/bitcoin/index.cjs +2477 -1
- package/dist/src/bitcoin/index.cjs.map +31 -3
- package/dist/src/bitcoin/index.js +11 -11
- package/dist/src/bns/index.cjs +4439 -17
- package/dist/src/bns/index.cjs.map +50 -3
- package/dist/src/bns/index.js +14 -14
- package/dist/src/chains/index.cjs +135 -1
- package/dist/src/chains/index.cjs.map +5 -3
- package/dist/src/chains/index.js +1 -1
- package/dist/src/clarity/index.cjs +2019 -1
- package/dist/src/clarity/index.cjs.map +24 -3
- package/dist/src/clarity/index.js +8 -8
- package/dist/src/filters/index.cjs +725 -1
- package/dist/src/filters/index.cjs.map +11 -3
- package/dist/src/filters/index.d.cts +76 -13
- package/dist/src/filters/index.d.ts +76 -13
- package/dist/src/filters/index.js +8 -4
- package/dist/src/filters/index.js.map +1 -1
- package/dist/src/index.cjs +3935 -62
- package/dist/src/index.cjs.map +69 -4
- package/dist/src/index.d.cts +62 -11
- package/dist/src/index.d.ts +62 -11
- package/dist/src/index.js +16 -16
- package/dist/src/postconditions/index.cjs +1726 -1
- package/dist/src/postconditions/index.cjs.map +20 -3
- package/dist/src/postconditions/index.js +6 -6
- package/dist/src/pox/index.cjs +3855 -1
- package/dist/src/pox/index.cjs.map +48 -3
- package/dist/src/pox/index.js +13 -13
- package/dist/src/pox5/index.cjs +4054 -29
- package/dist/src/pox5/index.cjs.map +54 -3
- package/dist/src/pox5/index.js +15 -15
- package/dist/src/sbtc/index.cjs +3705 -4
- package/dist/src/sbtc/index.cjs.map +47 -3
- package/dist/src/sbtc/index.js +12 -12
- package/dist/src/stackingdao/index.cjs +3751 -8
- package/dist/src/stackingdao/index.cjs.map +45 -3
- package/dist/src/stackingdao/index.js +12 -12
- package/dist/src/subscriptions/index.cjs +153 -1
- package/dist/src/subscriptions/index.cjs.map +6 -3
- package/dist/src/subscriptions/index.js +2 -2
- package/dist/src/transactions/index.cjs +2139 -1
- package/dist/src/transactions/index.cjs.map +23 -3
- package/dist/src/transactions/index.js +6 -6
- package/dist/src/utils/index.cjs +435 -3
- package/dist/src/utils/index.cjs.map +9 -3
- package/dist/src/utils/index.js +3 -3
- package/package.json +3 -23
- package/dist/shared/stacks-02actsrx.cjs +0 -17
- package/dist/shared/stacks-02actsrx.cjs.map +0 -10
- package/dist/shared/stacks-03vfm21t.cjs +0 -222
- package/dist/shared/stacks-03vfm21t.cjs.map +0 -12
- package/dist/shared/stacks-24h5k94q.cjs +0 -74
- package/dist/shared/stacks-24h5k94q.cjs.map +0 -11
- package/dist/shared/stacks-37kz5t9f.cjs +0 -764
- package/dist/shared/stacks-37kz5t9f.cjs.map +0 -23
- package/dist/shared/stacks-3mt2q2na.cjs +0 -65
- package/dist/shared/stacks-3mt2q2na.cjs.map +0 -9
- package/dist/shared/stacks-4hs8r4q2.js.map +0 -10
- package/dist/shared/stacks-8vzxax5e.cjs +0 -45
- package/dist/shared/stacks-agk106zs.cjs +0 -267
- package/dist/shared/stacks-agk106zs.cjs.map +0 -12
- package/dist/shared/stacks-bdgh9n83.cjs +0 -212
- package/dist/shared/stacks-bdgh9n83.cjs.map +0 -11
- package/dist/shared/stacks-dpxnxhqb.cjs +0 -51
- package/dist/shared/stacks-dpxnxhqb.cjs.map +0 -11
- package/dist/shared/stacks-ee53k9j2.cjs +0 -651
- package/dist/shared/stacks-ee53k9j2.cjs.map +0 -13
- package/dist/shared/stacks-enpj9krt.cjs +0 -38
- package/dist/shared/stacks-enpj9krt.cjs.map +0 -10
- package/dist/shared/stacks-f35mnrmg.cjs +0 -164
- package/dist/shared/stacks-f35mnrmg.cjs.map +0 -10
- package/dist/shared/stacks-fhzwsn5d.cjs +0 -261
- package/dist/shared/stacks-fhzwsn5d.cjs.map +0 -12
- package/dist/shared/stacks-gjz26k6b.js.map +0 -12
- package/dist/shared/stacks-gza2n1zs.cjs +0 -73
- package/dist/shared/stacks-gza2n1zs.cjs.map +0 -11
- package/dist/shared/stacks-kem68d80.cjs +0 -156
- package/dist/shared/stacks-kem68d80.cjs.map +0 -10
- package/dist/shared/stacks-n98w7g57.cjs +0 -234
- package/dist/shared/stacks-n98w7g57.cjs.map +0 -10
- package/dist/shared/stacks-p337hp6m.cjs +0 -336
- package/dist/shared/stacks-p337hp6m.cjs.map +0 -25
- package/dist/shared/stacks-q34k4vnc.cjs +0 -286
- package/dist/shared/stacks-q34k4vnc.cjs.map +0 -12
- package/dist/shared/stacks-rryme9bv.cjs +0 -569
- package/dist/shared/stacks-rryme9bv.cjs.map +0 -13
- package/dist/shared/stacks-s5vej5z6.cjs +0 -410
- package/dist/shared/stacks-s5vej5z6.cjs.map +0 -15
- package/dist/shared/stacks-sdqkapct.cjs +0 -67
- package/dist/shared/stacks-sdqkapct.cjs.map +0 -12
- package/dist/shared/stacks-vea7nde7.cjs +0 -731
- package/dist/shared/stacks-vea7nde7.cjs.map +0 -13
- package/dist/shared/stacks-wk8yp13j.cjs +0 -33
- package/dist/shared/stacks-wk8yp13j.cjs.map +0 -11
- package/dist/shared/stacks-xjkn3en1.cjs +0 -42
- package/dist/shared/stacks-xnrywjf1.cjs +0 -131
- package/dist/shared/stacks-xnrywjf1.cjs.map +0 -11
- package/dist/shared/stacks-yvb7rvwk.js.map +0 -10
- package/dist/src/tools/btc/index.cjs +0 -131
- package/dist/src/tools/btc/index.cjs.map +0 -10
- package/dist/src/tools/btc/index.d.cts +0 -10
- package/dist/src/tools/btc/index.d.ts +0 -10
- package/dist/src/tools/btc/index.js +0 -90
- package/dist/src/tools/btc/index.js.map +0 -10
- package/dist/src/tools/index.cjs +0 -3865
- package/dist/src/tools/index.cjs.map +0 -80
- package/dist/src/tools/index.d.cts +0 -206
- package/dist/src/tools/index.d.ts +0 -206
- package/dist/src/tools/index.js +0 -3827
- package/dist/src/tools/index.js.map +0 -80
|
@@ -1,3 +1,1728 @@
|
|
|
1
|
+
var import_node_module = require("node:module");
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
function __accessProp(key) {
|
|
9
|
+
return this[key];
|
|
10
|
+
}
|
|
11
|
+
var __toESMCache_node;
|
|
12
|
+
var __toESMCache_esm;
|
|
13
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
14
|
+
var canCache = mod != null && typeof mod === "object";
|
|
15
|
+
if (canCache) {
|
|
16
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
17
|
+
var cached = cache.get(mod);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
21
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
22
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
if (canCache)
|
|
30
|
+
cache.set(mod, to);
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __toCommonJS = (from) => {
|
|
34
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
35
|
+
if (entry)
|
|
36
|
+
return entry;
|
|
37
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
38
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
39
|
+
for (var key of __getOwnPropNames(from))
|
|
40
|
+
if (!__hasOwnProp.call(entry, key))
|
|
41
|
+
__defProp(entry, key, {
|
|
42
|
+
get: __accessProp.bind(from, key),
|
|
43
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
__moduleCache.set(from, entry);
|
|
47
|
+
return entry;
|
|
48
|
+
};
|
|
49
|
+
var __moduleCache;
|
|
50
|
+
var __returnValue = (v) => v;
|
|
51
|
+
function __exportSetter(name, newValue) {
|
|
52
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
53
|
+
}
|
|
54
|
+
var __export = (target, all) => {
|
|
55
|
+
for (var name in all)
|
|
56
|
+
__defProp(target, name, {
|
|
57
|
+
get: all[name],
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
set: __exportSetter.bind(all, name)
|
|
61
|
+
});
|
|
62
|
+
};
|
|
1
63
|
|
|
2
|
-
|
|
64
|
+
// src/postconditions/index.ts
|
|
65
|
+
var exports_postconditions = {};
|
|
66
|
+
__export(exports_postconditions, {
|
|
67
|
+
wireToPostCondition: () => wireToPostCondition,
|
|
68
|
+
postConditionToHex: () => postConditionToHex,
|
|
69
|
+
parsePostConditionAmount: () => parsePostConditionAmount,
|
|
70
|
+
fromHex: () => fromHex,
|
|
71
|
+
Pc: () => Pc
|
|
72
|
+
});
|
|
73
|
+
module.exports = __toCommonJS(exports_postconditions);
|
|
74
|
+
|
|
75
|
+
// src/utils/c32.ts
|
|
76
|
+
var import_sha2 = require("@noble/hashes/sha2.js");
|
|
77
|
+
|
|
78
|
+
// src/utils/encoding.ts
|
|
79
|
+
var hexes = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
80
|
+
function bytesToHex(bytes) {
|
|
81
|
+
let hex = "";
|
|
82
|
+
for (const b of bytes) {
|
|
83
|
+
hex += hexes[b];
|
|
84
|
+
}
|
|
85
|
+
return hex;
|
|
86
|
+
}
|
|
87
|
+
function hexToBytes(hex) {
|
|
88
|
+
let h = without0x(hex);
|
|
89
|
+
if (h.length % 2)
|
|
90
|
+
h = `0${h}`;
|
|
91
|
+
const array = new Uint8Array(h.length / 2);
|
|
92
|
+
for (let i = 0;i < array.length; i++) {
|
|
93
|
+
const j = i * 2;
|
|
94
|
+
const byte = Number.parseInt(h.slice(j, j + 2), 16);
|
|
95
|
+
if (Number.isNaN(byte) || byte < 0)
|
|
96
|
+
throw new Error("Invalid byte sequence");
|
|
97
|
+
array[i] = byte;
|
|
98
|
+
}
|
|
99
|
+
return array;
|
|
100
|
+
}
|
|
101
|
+
function with0x(value) {
|
|
102
|
+
return /^0x/i.test(value) ? value : `0x${value}`;
|
|
103
|
+
}
|
|
104
|
+
function without0x(value) {
|
|
105
|
+
return /^0x/i.test(value) ? value.slice(2) : value;
|
|
106
|
+
}
|
|
107
|
+
function utf8ToBytes(str) {
|
|
108
|
+
return new TextEncoder().encode(str);
|
|
109
|
+
}
|
|
110
|
+
function bytesToUtf8(bytes) {
|
|
111
|
+
return new TextDecoder().decode(bytes);
|
|
112
|
+
}
|
|
113
|
+
function asciiToBytes(str) {
|
|
114
|
+
const bytes = new Uint8Array(str.length);
|
|
115
|
+
for (let i = 0;i < str.length; i++) {
|
|
116
|
+
bytes[i] = str.charCodeAt(i) & 255;
|
|
117
|
+
}
|
|
118
|
+
return bytes;
|
|
119
|
+
}
|
|
120
|
+
function bytesToAscii(bytes) {
|
|
121
|
+
return String.fromCharCode(...bytes);
|
|
122
|
+
}
|
|
123
|
+
function concatBytes(...arrays) {
|
|
124
|
+
if (arrays.length === 1)
|
|
125
|
+
return arrays[0];
|
|
126
|
+
const length = arrays.reduce((a, arr) => a + arr.length, 0);
|
|
127
|
+
const result = new Uint8Array(length);
|
|
128
|
+
let pad = 0;
|
|
129
|
+
for (const arr of arrays) {
|
|
130
|
+
result.set(arr, pad);
|
|
131
|
+
pad += arr.length;
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function intToBigInt(value) {
|
|
136
|
+
if (typeof value === "bigint")
|
|
137
|
+
return value;
|
|
138
|
+
if (typeof value === "string")
|
|
139
|
+
return BigInt(value);
|
|
140
|
+
if (typeof value === "number") {
|
|
141
|
+
if (!Number.isInteger(value))
|
|
142
|
+
throw new RangeError("Values of type 'number' must be an integer.");
|
|
143
|
+
if (value > Number.MAX_SAFE_INTEGER)
|
|
144
|
+
throw new RangeError(`Values of type 'number' must be <= ${Number.MAX_SAFE_INTEGER}. Use BigInt instead.`);
|
|
145
|
+
return BigInt(value);
|
|
146
|
+
}
|
|
147
|
+
if (value instanceof Uint8Array)
|
|
148
|
+
return BigInt(`0x${bytesToHex(value)}`);
|
|
149
|
+
throw new TypeError("Must be a number, bigint, string, or Uint8Array.");
|
|
150
|
+
}
|
|
151
|
+
function intToBytes(value, byteLength) {
|
|
152
|
+
return bigIntToBytes(intToBigInt(value), byteLength);
|
|
153
|
+
}
|
|
154
|
+
function bigIntToBytes(value, length = 16) {
|
|
155
|
+
const hex = value.toString(16).padStart(length * 2, "0");
|
|
156
|
+
return hexToBytes(hex);
|
|
157
|
+
}
|
|
158
|
+
function intToHex(integer, byteLength = 8) {
|
|
159
|
+
const value = typeof integer === "bigint" ? integer : intToBigInt(integer);
|
|
160
|
+
return value.toString(16).padStart(byteLength * 2, "0");
|
|
161
|
+
}
|
|
162
|
+
function toTwos(value, width) {
|
|
163
|
+
const limit = BigInt(1) << width - BigInt(1);
|
|
164
|
+
if (value < -limit || value > limit - BigInt(1)) {
|
|
165
|
+
throw new Error(`Unable to represent integer in width: ${width}`);
|
|
166
|
+
}
|
|
167
|
+
if (value >= BigInt(0))
|
|
168
|
+
return value;
|
|
169
|
+
return value + (BigInt(1) << width);
|
|
170
|
+
}
|
|
171
|
+
function fromTwos(value, width) {
|
|
172
|
+
if (value & BigInt(1) << width - BigInt(1)) {
|
|
173
|
+
return value - (BigInt(1) << width);
|
|
174
|
+
}
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
function bytesToTwosBigInt(bytes) {
|
|
178
|
+
if (bytes.length === 0)
|
|
179
|
+
return 0n;
|
|
180
|
+
const hex = bytesToHex(bytes);
|
|
181
|
+
if (hex.length === 0)
|
|
182
|
+
return 0n;
|
|
183
|
+
return fromTwos(BigInt(`0x${hex}`), BigInt(bytes.byteLength * 8));
|
|
184
|
+
}
|
|
185
|
+
function writeUInt32BE(value) {
|
|
186
|
+
const buf = new Uint8Array(4);
|
|
187
|
+
buf[0] = value >>> 24 & 255;
|
|
188
|
+
buf[1] = value >>> 16 & 255;
|
|
189
|
+
buf[2] = value >>> 8 & 255;
|
|
190
|
+
buf[3] = value & 255;
|
|
191
|
+
return buf;
|
|
192
|
+
}
|
|
193
|
+
function readUInt32BE(bytes, offset = 0) {
|
|
194
|
+
return (bytes[offset] << 24 | bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
|
|
195
|
+
}
|
|
196
|
+
function writeUInt16BE(value) {
|
|
197
|
+
const buf = new Uint8Array(2);
|
|
198
|
+
buf[0] = value >>> 8 & 255;
|
|
199
|
+
buf[1] = value & 255;
|
|
200
|
+
return buf;
|
|
201
|
+
}
|
|
202
|
+
function readUInt16BE(bytes, offset = 0) {
|
|
203
|
+
return (bytes[offset] << 8 | bytes[offset + 1]) >>> 0;
|
|
204
|
+
}
|
|
205
|
+
function writeUInt8(value) {
|
|
206
|
+
return new Uint8Array([value & 255]);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/utils/c32.ts
|
|
210
|
+
var C32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
211
|
+
var HEX = "0123456789abcdef";
|
|
212
|
+
function c32normalize(input) {
|
|
213
|
+
return input.toUpperCase().replace(/O/g, "0").replace(/L|I/g, "1");
|
|
214
|
+
}
|
|
215
|
+
function c32encode(inputHex) {
|
|
216
|
+
let hex = inputHex;
|
|
217
|
+
if (hex.length % 2 !== 0)
|
|
218
|
+
hex = `0${hex}`;
|
|
219
|
+
hex = hex.toLowerCase();
|
|
220
|
+
const res = [];
|
|
221
|
+
let carry = 0;
|
|
222
|
+
for (let i = hex.length - 1;i >= 0; i--) {
|
|
223
|
+
if (carry < 4) {
|
|
224
|
+
const currentCode = HEX.indexOf(hex[i]) >> carry;
|
|
225
|
+
const nextCode = i !== 0 ? HEX.indexOf(hex[i - 1]) : 0;
|
|
226
|
+
const nextBits = 1 + carry;
|
|
227
|
+
const nextLowBits = nextCode % (1 << nextBits) << 5 - nextBits;
|
|
228
|
+
res.unshift(C32[currentCode + nextLowBits]);
|
|
229
|
+
carry = nextBits;
|
|
230
|
+
} else {
|
|
231
|
+
carry = 0;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
let leadingZeros = 0;
|
|
235
|
+
while (leadingZeros < res.length && res[leadingZeros] === "0")
|
|
236
|
+
leadingZeros++;
|
|
237
|
+
const stripped = res.slice(leadingZeros);
|
|
238
|
+
const bytes = hexToBytes(hex);
|
|
239
|
+
let zeroBytesCount = 0;
|
|
240
|
+
while (zeroBytesCount < bytes.length && bytes[zeroBytesCount] === 0)
|
|
241
|
+
zeroBytesCount++;
|
|
242
|
+
const prefix = Array(zeroBytesCount).fill(C32[0]);
|
|
243
|
+
return [...prefix, ...stripped].join("");
|
|
244
|
+
}
|
|
245
|
+
function c32decode(c32input) {
|
|
246
|
+
const input = c32normalize(c32input);
|
|
247
|
+
if (!input.match(`^[${C32}]*$`))
|
|
248
|
+
throw new Error("Not a c32-encoded string");
|
|
249
|
+
const zeroPrefix = input.match(`^${C32[0]}*`);
|
|
250
|
+
const numLeadingZeroBytes = zeroPrefix ? zeroPrefix[0]?.length : 0;
|
|
251
|
+
const res = [];
|
|
252
|
+
let carry = 0;
|
|
253
|
+
let carryBits = 0;
|
|
254
|
+
for (let i = input.length - 1;i >= 0; i--) {
|
|
255
|
+
if (carryBits === 4) {
|
|
256
|
+
res.unshift(HEX[carry]);
|
|
257
|
+
carryBits = 0;
|
|
258
|
+
carry = 0;
|
|
259
|
+
}
|
|
260
|
+
const currentValue = (C32.indexOf(input[i]) << carryBits) + carry;
|
|
261
|
+
res.unshift(HEX[currentValue % 16]);
|
|
262
|
+
carryBits += 1;
|
|
263
|
+
carry = currentValue >> 4;
|
|
264
|
+
if (carry > 1 << carryBits)
|
|
265
|
+
throw new Error("Panic error in c32 decoding");
|
|
266
|
+
}
|
|
267
|
+
res.unshift(HEX[carry]);
|
|
268
|
+
if (res.length % 2 === 1)
|
|
269
|
+
res.unshift("0");
|
|
270
|
+
let hexLeadingZeros = 0;
|
|
271
|
+
while (hexLeadingZeros < res.length && res[hexLeadingZeros] === "0")
|
|
272
|
+
hexLeadingZeros++;
|
|
273
|
+
let hexStr = res.slice(hexLeadingZeros - hexLeadingZeros % 2).join("");
|
|
274
|
+
for (let i = 0;i < numLeadingZeroBytes; i++)
|
|
275
|
+
hexStr = `00${hexStr}`;
|
|
276
|
+
return hexStr;
|
|
277
|
+
}
|
|
278
|
+
function c32checksum(dataHex) {
|
|
279
|
+
return bytesToHex(import_sha2.sha256(import_sha2.sha256(hexToBytes(dataHex))).slice(0, 4));
|
|
280
|
+
}
|
|
281
|
+
function c32checkEncode(version, data) {
|
|
282
|
+
if (version < 0 || version >= 32)
|
|
283
|
+
throw new Error("Invalid version (must be between 0 and 31)");
|
|
284
|
+
if (!data.match(/^[0-9a-fA-F]*$/))
|
|
285
|
+
throw new Error("Invalid data (not a hex string)");
|
|
286
|
+
let d = data.toLowerCase();
|
|
287
|
+
if (d.length % 2 !== 0)
|
|
288
|
+
d = `0${d}`;
|
|
289
|
+
let versionHex = version.toString(16);
|
|
290
|
+
if (versionHex.length === 1)
|
|
291
|
+
versionHex = `0${versionHex}`;
|
|
292
|
+
const checksumHex = c32checksum(`${versionHex}${d}`);
|
|
293
|
+
return `${C32[version]}${c32encode(`${d}${checksumHex}`)}`;
|
|
294
|
+
}
|
|
295
|
+
function c32checkDecode(c32data) {
|
|
296
|
+
const normalized = c32normalize(c32data);
|
|
297
|
+
const dataHex = c32decode(normalized.slice(1));
|
|
298
|
+
const version = C32.indexOf(normalized[0]);
|
|
299
|
+
const checksum = dataHex.slice(-8);
|
|
300
|
+
let versionHex = version.toString(16);
|
|
301
|
+
if (versionHex.length === 1)
|
|
302
|
+
versionHex = `0${versionHex}`;
|
|
303
|
+
if (c32checksum(`${versionHex}${dataHex.substring(0, dataHex.length - 8)}`) !== checksum) {
|
|
304
|
+
throw new Error("Invalid c32check string: checksum mismatch");
|
|
305
|
+
}
|
|
306
|
+
return [version, dataHex.substring(0, dataHex.length - 8)];
|
|
307
|
+
}
|
|
308
|
+
function c32address(version, hash160hex) {
|
|
309
|
+
if (!hash160hex.match(/^[0-9a-fA-F]{40}$/)) {
|
|
310
|
+
throw new Error("Invalid argument: not a hash160 hex string");
|
|
311
|
+
}
|
|
312
|
+
return `S${c32checkEncode(version, hash160hex)}`;
|
|
313
|
+
}
|
|
314
|
+
function c32addressDecode(c32addr) {
|
|
315
|
+
if (c32addr.length <= 5)
|
|
316
|
+
throw new Error("Invalid c32 address: invalid length");
|
|
317
|
+
if (c32addr[0] !== "S")
|
|
318
|
+
throw new Error('Invalid c32 address: must start with "S"');
|
|
319
|
+
return c32checkDecode(c32addr.slice(1));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/utils/constants.ts
|
|
323
|
+
var MAX_U128 = (1n << 128n) - 1n;
|
|
324
|
+
var MAX_I128 = (1n << 127n) - 1n;
|
|
325
|
+
var MIN_I128 = -(1n << 127n);
|
|
326
|
+
var AddressVersion = {
|
|
327
|
+
MainnetSingleSig: 22,
|
|
328
|
+
MainnetMultiSig: 20,
|
|
329
|
+
TestnetSingleSig: 26,
|
|
330
|
+
TestnetMultiSig: 21
|
|
331
|
+
};
|
|
332
|
+
var ZERO_ADDRESS = "SP000000000000000000002Q6VF78";
|
|
333
|
+
var TESTNET_ZERO_ADDRESS = "ST000000000000000000002AMW42H";
|
|
334
|
+
var MICROSTX_PER_STX = 1000000n;
|
|
335
|
+
|
|
336
|
+
// src/utils/hash.ts
|
|
337
|
+
var import_legacy = require("@noble/hashes/legacy.js");
|
|
338
|
+
var import_sha22 = require("@noble/hashes/sha2.js");
|
|
339
|
+
var import_sha23 = require("@noble/hashes/sha2.js");
|
|
340
|
+
function hash160(input) {
|
|
341
|
+
return import_legacy.ripemd160(import_sha22.sha256(input));
|
|
342
|
+
}
|
|
343
|
+
function txidFromBytes(data) {
|
|
344
|
+
return bytesToHex(import_sha23.sha512_256(data));
|
|
345
|
+
}
|
|
346
|
+
function hashP2PKH(input) {
|
|
347
|
+
return bytesToHex(hash160(input));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/utils/address.ts
|
|
351
|
+
function publicKeyToAddress(publicKey, network = "mainnet") {
|
|
352
|
+
const bytes = typeof publicKey === "string" ? hexToBytes(without0x(publicKey)) : publicKey;
|
|
353
|
+
const version = network === "mainnet" ? AddressVersion.MainnetSingleSig : AddressVersion.TestnetSingleSig;
|
|
354
|
+
return c32address(version, bytesToHex(hash160(bytes)));
|
|
355
|
+
}
|
|
356
|
+
var CONTRACT_NAME_REGEX = /^[a-zA-Z][a-zA-Z0-9_-]{0,127}$/;
|
|
357
|
+
function validateStacksAddress(address) {
|
|
358
|
+
try {
|
|
359
|
+
c32addressDecode(address);
|
|
360
|
+
return true;
|
|
361
|
+
} catch {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
var isValidAddress = validateStacksAddress;
|
|
366
|
+
function parsePrincipal(value) {
|
|
367
|
+
const parts = value.split(".");
|
|
368
|
+
if (parts.length > 2)
|
|
369
|
+
return null;
|
|
370
|
+
const [address, contractName] = parts;
|
|
371
|
+
if (!validateStacksAddress(address))
|
|
372
|
+
return null;
|
|
373
|
+
if (contractName === undefined)
|
|
374
|
+
return { address };
|
|
375
|
+
if (!CONTRACT_NAME_REGEX.test(contractName))
|
|
376
|
+
return null;
|
|
377
|
+
return { address, contractName };
|
|
378
|
+
}
|
|
379
|
+
function parseContractId(contractId) {
|
|
380
|
+
const parsed = parsePrincipal(contractId);
|
|
381
|
+
if (!parsed?.contractName)
|
|
382
|
+
throw new Error(`Invalid contract identifier: ${contractId}`);
|
|
383
|
+
return [parsed.address, parsed.contractName];
|
|
384
|
+
}
|
|
385
|
+
function isClarityName(name) {
|
|
386
|
+
const regex = /^[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*$|^[-+=/*]$|^[<>]=?$/;
|
|
387
|
+
return regex.test(name) && name.length < 128;
|
|
388
|
+
}
|
|
389
|
+
function isAddressEqual(a, b) {
|
|
390
|
+
const [versionA, hashA] = c32addressDecode(a);
|
|
391
|
+
const [versionB, hashB] = c32addressDecode(b);
|
|
392
|
+
return versionA === versionB && hashA.toLowerCase() === hashB.toLowerCase();
|
|
393
|
+
}
|
|
394
|
+
function addressToVersion(address) {
|
|
395
|
+
return c32addressDecode(address)[0];
|
|
396
|
+
}
|
|
397
|
+
function getContractAddress(deployer, contractName) {
|
|
398
|
+
if (!validateStacksAddress(deployer)) {
|
|
399
|
+
throw new Error(`Invalid deployer address: ${deployer}`);
|
|
400
|
+
}
|
|
401
|
+
if (!isClarityName(contractName)) {
|
|
402
|
+
throw new Error(`Invalid contract name: ${contractName}`);
|
|
403
|
+
}
|
|
404
|
+
return `${deployer}.${contractName}`;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// src/transactions/types.ts
|
|
408
|
+
var AuthType = {
|
|
409
|
+
Standard: 4,
|
|
410
|
+
Sponsored: 5
|
|
411
|
+
};
|
|
412
|
+
var PayloadType = {
|
|
413
|
+
TokenTransfer: 0,
|
|
414
|
+
SmartContract: 1,
|
|
415
|
+
ContractCall: 2,
|
|
416
|
+
PoisonMicroblock: 3,
|
|
417
|
+
Coinbase: 4,
|
|
418
|
+
CoinbaseToAltRecipient: 5,
|
|
419
|
+
VersionedSmartContract: 6,
|
|
420
|
+
TenureChange: 7,
|
|
421
|
+
NakamotoCoinbase: 8
|
|
422
|
+
};
|
|
423
|
+
var ClarityVersion = {
|
|
424
|
+
Clarity1: 1,
|
|
425
|
+
Clarity2: 2,
|
|
426
|
+
Clarity3: 3,
|
|
427
|
+
Clarity4: 4,
|
|
428
|
+
Clarity5: 5,
|
|
429
|
+
Clarity6: 6
|
|
430
|
+
};
|
|
431
|
+
var AnchorMode = {
|
|
432
|
+
OnChainOnly: 1,
|
|
433
|
+
OffChainOnly: 2,
|
|
434
|
+
Any: 3
|
|
435
|
+
};
|
|
436
|
+
var PostConditionModeWire = {
|
|
437
|
+
Allow: 1,
|
|
438
|
+
Deny: 2,
|
|
439
|
+
Originator: 3
|
|
440
|
+
};
|
|
441
|
+
var AddressHashMode = {
|
|
442
|
+
P2PKH: 0,
|
|
443
|
+
P2SH: 1,
|
|
444
|
+
P2WPKH: 2,
|
|
445
|
+
P2WSH: 3,
|
|
446
|
+
P2SH_NonSequential: 5,
|
|
447
|
+
P2WSH_P2SH_NonSequential: 7
|
|
448
|
+
};
|
|
449
|
+
var PubKeyEncoding = {
|
|
450
|
+
Compressed: 0,
|
|
451
|
+
Uncompressed: 1
|
|
452
|
+
};
|
|
453
|
+
var FungibleConditionCode = {
|
|
454
|
+
Equal: 1,
|
|
455
|
+
Greater: 2,
|
|
456
|
+
GreaterEqual: 3,
|
|
457
|
+
Less: 4,
|
|
458
|
+
LessEqual: 5
|
|
459
|
+
};
|
|
460
|
+
var NonFungibleConditionCode = {
|
|
461
|
+
Sends: 16,
|
|
462
|
+
DoesNotSend: 17,
|
|
463
|
+
MaybeSent: 18
|
|
464
|
+
};
|
|
465
|
+
var PoxConditionCode = {
|
|
466
|
+
WillNotPerform: 48,
|
|
467
|
+
MayPerform: 49,
|
|
468
|
+
WillPerform: 50
|
|
469
|
+
};
|
|
470
|
+
var PostConditionPrincipalId = {
|
|
471
|
+
Origin: 1,
|
|
472
|
+
Standard: 2,
|
|
473
|
+
Contract: 3
|
|
474
|
+
};
|
|
475
|
+
var AssetType = {
|
|
476
|
+
STX: 0,
|
|
477
|
+
Fungible: 1,
|
|
478
|
+
NonFungible: 2,
|
|
479
|
+
Staking: 3,
|
|
480
|
+
Pox: 4
|
|
481
|
+
};
|
|
482
|
+
var AuthFieldType = {
|
|
483
|
+
PublicKeyCompressed: 0,
|
|
484
|
+
PublicKeyUncompressed: 1,
|
|
485
|
+
SignatureCompressed: 2,
|
|
486
|
+
SignatureUncompressed: 3
|
|
487
|
+
};
|
|
488
|
+
var RECOVERABLE_ECDSA_SIG_LENGTH_BYTES = 65;
|
|
489
|
+
var MEMO_MAX_LENGTH_BYTES = 34;
|
|
490
|
+
var MAX_STRING_LENGTH_BYTES = 128;
|
|
491
|
+
var COINBASE_BYTES_LENGTH = 32;
|
|
492
|
+
var VRF_PROOF_BYTES_LENGTH = 80;
|
|
493
|
+
var MICROBLOCK_HEADER_BYTES_LENGTH = 132;
|
|
494
|
+
var TenureChangeCause = {
|
|
495
|
+
BlockFound: 0,
|
|
496
|
+
Extended: 1,
|
|
497
|
+
ExtendedRuntime: 2,
|
|
498
|
+
ExtendedReadCount: 3,
|
|
499
|
+
ExtendedReadLength: 4,
|
|
500
|
+
ExtendedWriteCount: 5,
|
|
501
|
+
ExtendedWriteLength: 6
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// src/errors/base.ts
|
|
505
|
+
function codeFromName(name) {
|
|
506
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toUpperCase();
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
class BaseError extends Error {
|
|
510
|
+
name = "StacksError";
|
|
511
|
+
#code;
|
|
512
|
+
shortMessage;
|
|
513
|
+
details;
|
|
514
|
+
constructor(shortMessage, options) {
|
|
515
|
+
const message = [
|
|
516
|
+
shortMessage,
|
|
517
|
+
options?.details ? `
|
|
518
|
+
${options.details}` : ""
|
|
519
|
+
].join("");
|
|
520
|
+
super(message, { cause: options?.cause });
|
|
521
|
+
if (options?.code !== undefined)
|
|
522
|
+
this.#code = options.code;
|
|
523
|
+
this.shortMessage = shortMessage;
|
|
524
|
+
this.details = options?.details;
|
|
525
|
+
}
|
|
526
|
+
get code() {
|
|
527
|
+
return this.#code ?? codeFromName(this.name);
|
|
528
|
+
}
|
|
529
|
+
set code(value) {
|
|
530
|
+
this.#code = value;
|
|
531
|
+
}
|
|
532
|
+
toJSON() {
|
|
533
|
+
return {
|
|
534
|
+
name: this.name,
|
|
535
|
+
code: this.code,
|
|
536
|
+
message: this.message,
|
|
537
|
+
shortMessage: this.shortMessage,
|
|
538
|
+
details: this.details,
|
|
539
|
+
cause: this.cause instanceof Error ? this.cause.message : undefined
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// src/errors/transaction.ts
|
|
545
|
+
class TransactionError extends BaseError {
|
|
546
|
+
name = "TransactionError";
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
class BroadcastError extends BaseError {
|
|
550
|
+
name = "BroadcastError";
|
|
551
|
+
txid;
|
|
552
|
+
reason;
|
|
553
|
+
reasonData;
|
|
554
|
+
constructor(message, options) {
|
|
555
|
+
super(message, options);
|
|
556
|
+
this.txid = options?.txid;
|
|
557
|
+
this.reason = options?.reason;
|
|
558
|
+
this.reasonData = options?.reasonData;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
class TransactionAbortedError extends BaseError {
|
|
563
|
+
name = "TransactionAbortedError";
|
|
564
|
+
receipt;
|
|
565
|
+
constructor(message, options) {
|
|
566
|
+
super(message, options);
|
|
567
|
+
this.receipt = options.receipt;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
class TransactionDroppedError extends BaseError {
|
|
572
|
+
name = "TransactionDroppedError";
|
|
573
|
+
txid;
|
|
574
|
+
constructor(message, options) {
|
|
575
|
+
super(message, options);
|
|
576
|
+
this.txid = options.txid;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
class WaitForTransactionTimeoutError extends BaseError {
|
|
581
|
+
name = "WaitForTransactionTimeoutError";
|
|
582
|
+
txid;
|
|
583
|
+
constructor(message, options) {
|
|
584
|
+
super(message, options);
|
|
585
|
+
this.txid = options.txid;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
class SerializationError extends BaseError {
|
|
590
|
+
name = "SerializationError";
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
class SigningError extends BaseError {
|
|
594
|
+
name = "SigningError";
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/utils/bytes-reader.ts
|
|
598
|
+
class BytesReader {
|
|
599
|
+
data;
|
|
600
|
+
offset = 0;
|
|
601
|
+
constructor(data) {
|
|
602
|
+
this.data = data;
|
|
603
|
+
}
|
|
604
|
+
remaining() {
|
|
605
|
+
return this.data.length - this.offset;
|
|
606
|
+
}
|
|
607
|
+
ensure(length) {
|
|
608
|
+
if (this.offset + length > this.data.length) {
|
|
609
|
+
throw new SerializationError(`Buffer underflow: need ${length} bytes at offset ${this.offset}, have ${this.data.length}`);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
readUInt8() {
|
|
613
|
+
this.ensure(1);
|
|
614
|
+
return this.data[this.offset++];
|
|
615
|
+
}
|
|
616
|
+
readUInt16BE() {
|
|
617
|
+
this.ensure(2);
|
|
618
|
+
const val = (this.data[this.offset] << 8 | this.data[this.offset + 1]) >>> 0;
|
|
619
|
+
this.offset += 2;
|
|
620
|
+
return val;
|
|
621
|
+
}
|
|
622
|
+
readUInt32BE() {
|
|
623
|
+
this.ensure(4);
|
|
624
|
+
const val = (this.data[this.offset] << 24 | this.data[this.offset + 1] << 16 | this.data[this.offset + 2] << 8 | this.data[this.offset + 3]) >>> 0;
|
|
625
|
+
this.offset += 4;
|
|
626
|
+
return val;
|
|
627
|
+
}
|
|
628
|
+
readBytes(length) {
|
|
629
|
+
this.ensure(length);
|
|
630
|
+
const slice = this.data.slice(this.offset, this.offset + length);
|
|
631
|
+
this.offset += length;
|
|
632
|
+
return slice;
|
|
633
|
+
}
|
|
634
|
+
readBigUInt64BE() {
|
|
635
|
+
const hex = bytesToHex(this.readBytes(8));
|
|
636
|
+
return hex.length > 0 ? BigInt(`0x${hex}`) : 0n;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// src/clarity/types.ts
|
|
641
|
+
var ClarityWireType = {
|
|
642
|
+
int: 0,
|
|
643
|
+
uint: 1,
|
|
644
|
+
buffer: 2,
|
|
645
|
+
true: 3,
|
|
646
|
+
false: 4,
|
|
647
|
+
address: 5,
|
|
648
|
+
contract: 6,
|
|
649
|
+
ok: 7,
|
|
650
|
+
err: 8,
|
|
651
|
+
none: 9,
|
|
652
|
+
some: 10,
|
|
653
|
+
list: 11,
|
|
654
|
+
tuple: 12,
|
|
655
|
+
ascii: 13,
|
|
656
|
+
utf8: 14
|
|
657
|
+
};
|
|
658
|
+
var wireTypeToClarity = Object.fromEntries(Object.entries(ClarityWireType).map(([k, v]) => [v, k]));
|
|
659
|
+
function clarityTypeFromByte(byte) {
|
|
660
|
+
const type = wireTypeToClarity[byte];
|
|
661
|
+
if (!type)
|
|
662
|
+
throw new Error(`Unknown clarity wire type: ${byte}`);
|
|
663
|
+
return type;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// src/clarity/serialize.ts
|
|
667
|
+
var CLARITY_INT_SIZE = 128n;
|
|
668
|
+
var CLARITY_INT_BYTE_SIZE = 16;
|
|
669
|
+
function typeIdByte(type) {
|
|
670
|
+
return ClarityWireType[type];
|
|
671
|
+
}
|
|
672
|
+
function withTypeId(type, bytes) {
|
|
673
|
+
return concatBytes(new Uint8Array([typeIdByte(type)]), bytes);
|
|
674
|
+
}
|
|
675
|
+
function serializeAddress(c32Address) {
|
|
676
|
+
const [version, hash1602] = c32addressDecode(c32Address);
|
|
677
|
+
return concatBytes(new Uint8Array([version]), hexToBytes(hash1602));
|
|
678
|
+
}
|
|
679
|
+
function serializeLPString(str, prefixBytes = 1) {
|
|
680
|
+
const content = utf8ToBytes(str);
|
|
681
|
+
const lengthPrefix = hexToBytes(intToHex(content.byteLength, prefixBytes));
|
|
682
|
+
return concatBytes(lengthPrefix, content);
|
|
683
|
+
}
|
|
684
|
+
function serializeCVBytes(value) {
|
|
685
|
+
switch (value.type) {
|
|
686
|
+
case "true":
|
|
687
|
+
case "false":
|
|
688
|
+
return new Uint8Array([typeIdByte(value.type)]);
|
|
689
|
+
case "int": {
|
|
690
|
+
const bytes = bigIntToBytes(toTwos(BigInt(value.value), CLARITY_INT_SIZE), CLARITY_INT_BYTE_SIZE);
|
|
691
|
+
return withTypeId(value.type, bytes);
|
|
692
|
+
}
|
|
693
|
+
case "uint": {
|
|
694
|
+
const bytes = bigIntToBytes(BigInt(value.value), CLARITY_INT_BYTE_SIZE);
|
|
695
|
+
return withTypeId(value.type, bytes);
|
|
696
|
+
}
|
|
697
|
+
case "buffer": {
|
|
698
|
+
const bufBytes = hexToBytes(value.value);
|
|
699
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(bufBytes.length), bufBytes));
|
|
700
|
+
}
|
|
701
|
+
case "none":
|
|
702
|
+
return new Uint8Array([typeIdByte(value.type)]);
|
|
703
|
+
case "some":
|
|
704
|
+
return withTypeId(value.type, serializeCVBytes(value.value));
|
|
705
|
+
case "ok":
|
|
706
|
+
case "err":
|
|
707
|
+
return withTypeId(value.type, serializeCVBytes(value.value));
|
|
708
|
+
case "address":
|
|
709
|
+
return withTypeId(value.type, serializeAddress(value.value));
|
|
710
|
+
case "contract": {
|
|
711
|
+
const [addr, name] = value.value.split(".");
|
|
712
|
+
if (!addr || !name)
|
|
713
|
+
throw new Error(`Invalid contract principal: ${value.value}`);
|
|
714
|
+
return withTypeId(value.type, concatBytes(serializeAddress(addr), serializeLPString(name)));
|
|
715
|
+
}
|
|
716
|
+
case "list": {
|
|
717
|
+
const parts = [writeUInt32BE(value.value.length)];
|
|
718
|
+
for (const item of value.value) {
|
|
719
|
+
parts.push(serializeCVBytes(item));
|
|
720
|
+
}
|
|
721
|
+
return withTypeId(value.type, concatBytes(...parts));
|
|
722
|
+
}
|
|
723
|
+
case "tuple": {
|
|
724
|
+
const keys = Object.keys(value.value).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
725
|
+
const parts = [writeUInt32BE(keys.length)];
|
|
726
|
+
for (const key of keys) {
|
|
727
|
+
parts.push(serializeLPString(key));
|
|
728
|
+
parts.push(serializeCVBytes(value.value[key]));
|
|
729
|
+
}
|
|
730
|
+
return withTypeId(value.type, concatBytes(...parts));
|
|
731
|
+
}
|
|
732
|
+
case "ascii": {
|
|
733
|
+
const strBytes = asciiToBytes(value.value);
|
|
734
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(strBytes.length), strBytes));
|
|
735
|
+
}
|
|
736
|
+
case "utf8": {
|
|
737
|
+
const strBytes = utf8ToBytes(value.value);
|
|
738
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(strBytes.length), strBytes));
|
|
739
|
+
}
|
|
740
|
+
default:
|
|
741
|
+
throw new Error("Cannot serialize unknown clarity type");
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
function serializeCV(value) {
|
|
745
|
+
return bytesToHex(serializeCVBytes(value));
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// src/clarity/values.ts
|
|
749
|
+
var MAX_U1282 = BigInt("0xffffffffffffffffffffffffffffffff");
|
|
750
|
+
var MIN_I1282 = BigInt("-170141183460469231731687303715884105728");
|
|
751
|
+
var MAX_I1282 = BigInt("0x7fffffffffffffffffffffffffffffff");
|
|
752
|
+
function intCV(value) {
|
|
753
|
+
let v = value;
|
|
754
|
+
if (typeof v === "string" && v.toLowerCase().startsWith("0x")) {
|
|
755
|
+
v = bytesToTwosBigInt(hexToBytes(v));
|
|
756
|
+
}
|
|
757
|
+
if (v instanceof Uint8Array)
|
|
758
|
+
v = bytesToTwosBigInt(v);
|
|
759
|
+
const n = intToBigInt(v);
|
|
760
|
+
if (n > MAX_I1282)
|
|
761
|
+
throw new RangeError(`Int exceeds max i128: ${MAX_I1282}`);
|
|
762
|
+
if (n < MIN_I1282)
|
|
763
|
+
throw new RangeError(`Int below min i128: ${MIN_I1282}`);
|
|
764
|
+
return { type: "int", value: n };
|
|
765
|
+
}
|
|
766
|
+
function uintCV(value) {
|
|
767
|
+
const n = intToBigInt(value);
|
|
768
|
+
if (n < 0n)
|
|
769
|
+
throw new RangeError("Cannot construct unsigned int from negative value");
|
|
770
|
+
if (n > MAX_U1282)
|
|
771
|
+
throw new RangeError(`UInt exceeds max u128: ${MAX_U1282}`);
|
|
772
|
+
return { type: "uint", value: n };
|
|
773
|
+
}
|
|
774
|
+
var trueCV = () => ({ type: "true" });
|
|
775
|
+
var falseCV = () => ({ type: "false" });
|
|
776
|
+
var boolCV = (v) => v ? trueCV() : falseCV();
|
|
777
|
+
function bufferCV(buffer) {
|
|
778
|
+
if (buffer.byteLength > 1048576) {
|
|
779
|
+
throw new Error("Buffer exceeds max size of 1MB");
|
|
780
|
+
}
|
|
781
|
+
return { type: "buffer", value: bytesToHex(buffer) };
|
|
782
|
+
}
|
|
783
|
+
function standardPrincipalCV(address) {
|
|
784
|
+
const [version, hash1602] = c32addressDecode(address);
|
|
785
|
+
const normalized = c32address(version, hash1602);
|
|
786
|
+
return { type: "address", value: normalized };
|
|
787
|
+
}
|
|
788
|
+
function contractPrincipalCV(address, contractName) {
|
|
789
|
+
const [version, hash1602] = c32addressDecode(address);
|
|
790
|
+
const normalized = c32address(version, hash1602);
|
|
791
|
+
if (utf8ToBytes(contractName).byteLength >= 128) {
|
|
792
|
+
throw new Error("Contract name must be less than 128 bytes");
|
|
793
|
+
}
|
|
794
|
+
return { type: "contract", value: `${normalized}.${contractName}` };
|
|
795
|
+
}
|
|
796
|
+
function noneCV() {
|
|
797
|
+
return { type: "none" };
|
|
798
|
+
}
|
|
799
|
+
function someCV(value) {
|
|
800
|
+
return { type: "some", value };
|
|
801
|
+
}
|
|
802
|
+
function responseOkCV(value) {
|
|
803
|
+
return { type: "ok", value };
|
|
804
|
+
}
|
|
805
|
+
function responseErrorCV(value) {
|
|
806
|
+
return { type: "err", value };
|
|
807
|
+
}
|
|
808
|
+
function listCV(values) {
|
|
809
|
+
return { type: "list", value: values };
|
|
810
|
+
}
|
|
811
|
+
function tupleCV(data) {
|
|
812
|
+
for (const key in data) {
|
|
813
|
+
if (!isClarityName(key)) {
|
|
814
|
+
throw new Error(`"${key}" is not a valid Clarity name`);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
return { type: "tuple", value: data };
|
|
818
|
+
}
|
|
819
|
+
function stringAsciiCV(value) {
|
|
820
|
+
return { type: "ascii", value };
|
|
821
|
+
}
|
|
822
|
+
function stringUtf8CV(value) {
|
|
823
|
+
return { type: "utf8", value };
|
|
824
|
+
}
|
|
825
|
+
var Cl = {
|
|
826
|
+
int: intCV,
|
|
827
|
+
uint: uintCV,
|
|
828
|
+
bool: boolCV,
|
|
829
|
+
principal(address) {
|
|
830
|
+
const parsed = parsePrincipal(address);
|
|
831
|
+
if (!parsed)
|
|
832
|
+
throw new Error(`Invalid principal: ${address}`);
|
|
833
|
+
return parsed.contractName ? contractPrincipalCV(parsed.address, parsed.contractName) : standardPrincipalCV(parsed.address);
|
|
834
|
+
},
|
|
835
|
+
address(address) {
|
|
836
|
+
return Cl.principal(address);
|
|
837
|
+
},
|
|
838
|
+
contractPrincipal: contractPrincipalCV,
|
|
839
|
+
standardPrincipal: standardPrincipalCV,
|
|
840
|
+
buffer: bufferCV,
|
|
841
|
+
bufferFromHex: (hex) => bufferCV(hexToBytes(hex)),
|
|
842
|
+
bufferFromAscii: (ascii) => bufferCV(asciiToBytes(ascii)),
|
|
843
|
+
bufferFromUtf8: (utf8) => bufferCV(utf8ToBytes(utf8)),
|
|
844
|
+
none: noneCV,
|
|
845
|
+
some: someCV,
|
|
846
|
+
ok: responseOkCV,
|
|
847
|
+
error: responseErrorCV,
|
|
848
|
+
list: listCV,
|
|
849
|
+
tuple: tupleCV,
|
|
850
|
+
stringAscii: stringAsciiCV,
|
|
851
|
+
stringUtf8: stringUtf8CV,
|
|
852
|
+
serialize(value) {
|
|
853
|
+
return bytesToHex(serializeCVBytes(value));
|
|
854
|
+
},
|
|
855
|
+
deserialize: deserializeCVBytes
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
// src/clarity/deserialize.ts
|
|
859
|
+
function readAddress(reader) {
|
|
860
|
+
const version = reader.readUInt8();
|
|
861
|
+
const hash1602 = bytesToHex(reader.readBytes(20));
|
|
862
|
+
return c32address(version, hash1602);
|
|
863
|
+
}
|
|
864
|
+
function readLPString(reader, prefixBytes = 1) {
|
|
865
|
+
let length = 0;
|
|
866
|
+
for (let i = 0;i < prefixBytes; i++) {
|
|
867
|
+
length = length << 8 | reader.readUInt8();
|
|
868
|
+
}
|
|
869
|
+
return bytesToUtf8(reader.readBytes(length));
|
|
870
|
+
}
|
|
871
|
+
var MAX_CV_DEPTH = 64;
|
|
872
|
+
var MIN_LIST_ITEM_BYTES = 1;
|
|
873
|
+
var MIN_TUPLE_ENTRY_BYTES = 2;
|
|
874
|
+
function checkCount(reader, count, minBytes) {
|
|
875
|
+
if (count * minBytes > reader.remaining()) {
|
|
876
|
+
throw new SerializationError(`Clarity value declares ${count} elements but only ${reader.remaining()} bytes remain`);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
function readCV(reader, depth = 0) {
|
|
880
|
+
if (depth > MAX_CV_DEPTH) {
|
|
881
|
+
throw new SerializationError(`Clarity value nests deeper than ${MAX_CV_DEPTH} levels`);
|
|
882
|
+
}
|
|
883
|
+
const typeByte = reader.readUInt8();
|
|
884
|
+
const type = clarityTypeFromByte(typeByte);
|
|
885
|
+
switch (type) {
|
|
886
|
+
case "int":
|
|
887
|
+
return intCV(bytesToTwosBigInt(reader.readBytes(16)));
|
|
888
|
+
case "uint":
|
|
889
|
+
return uintCV(reader.readBytes(16));
|
|
890
|
+
case "true":
|
|
891
|
+
return trueCV();
|
|
892
|
+
case "false":
|
|
893
|
+
return falseCV();
|
|
894
|
+
case "buffer": {
|
|
895
|
+
const len = reader.readUInt32BE();
|
|
896
|
+
return bufferCV(reader.readBytes(len));
|
|
897
|
+
}
|
|
898
|
+
case "none":
|
|
899
|
+
return noneCV();
|
|
900
|
+
case "some":
|
|
901
|
+
return someCV(readCV(reader, depth + 1));
|
|
902
|
+
case "ok":
|
|
903
|
+
return responseOkCV(readCV(reader, depth + 1));
|
|
904
|
+
case "err":
|
|
905
|
+
return responseErrorCV(readCV(reader, depth + 1));
|
|
906
|
+
case "address":
|
|
907
|
+
return standardPrincipalCV(readAddress(reader));
|
|
908
|
+
case "contract": {
|
|
909
|
+
const addr = readAddress(reader);
|
|
910
|
+
const name = readLPString(reader);
|
|
911
|
+
return contractPrincipalCV(addr, name);
|
|
912
|
+
}
|
|
913
|
+
case "list": {
|
|
914
|
+
const len = reader.readUInt32BE();
|
|
915
|
+
checkCount(reader, len, MIN_LIST_ITEM_BYTES);
|
|
916
|
+
const items = [];
|
|
917
|
+
for (let i = 0;i < len; i++) {
|
|
918
|
+
items.push(readCV(reader, depth + 1));
|
|
919
|
+
}
|
|
920
|
+
return listCV(items);
|
|
921
|
+
}
|
|
922
|
+
case "tuple": {
|
|
923
|
+
const len = reader.readUInt32BE();
|
|
924
|
+
checkCount(reader, len, MIN_TUPLE_ENTRY_BYTES);
|
|
925
|
+
const data = {};
|
|
926
|
+
for (let i = 0;i < len; i++) {
|
|
927
|
+
const key = readLPString(reader);
|
|
928
|
+
data[key] = readCV(reader, depth + 1);
|
|
929
|
+
}
|
|
930
|
+
return tupleCV(data);
|
|
931
|
+
}
|
|
932
|
+
case "ascii": {
|
|
933
|
+
const len = reader.readUInt32BE();
|
|
934
|
+
return stringAsciiCV(bytesToAscii(reader.readBytes(len)));
|
|
935
|
+
}
|
|
936
|
+
case "utf8": {
|
|
937
|
+
const len = reader.readUInt32BE();
|
|
938
|
+
return stringUtf8CV(bytesToUtf8(reader.readBytes(len)));
|
|
939
|
+
}
|
|
940
|
+
default:
|
|
941
|
+
throw new SerializationError(`Cannot deserialize unknown clarity type: ${type}`);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function deserializeCVBytes(input) {
|
|
945
|
+
const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
|
|
946
|
+
return readCV(new BytesReader(bytes));
|
|
947
|
+
}
|
|
948
|
+
function deserializeCV(input) {
|
|
949
|
+
return deserializeCVBytes(input);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// src/transactions/wire/deserialize.ts
|
|
953
|
+
function readSpendingCondition(r) {
|
|
954
|
+
const hashMode = r.readUInt8();
|
|
955
|
+
const signer = bytesToHex(r.readBytes(20));
|
|
956
|
+
const nonce = r.readBigUInt64BE();
|
|
957
|
+
const fee = r.readBigUInt64BE();
|
|
958
|
+
if (hashMode === AddressHashMode.P2PKH || hashMode === AddressHashMode.P2WPKH) {
|
|
959
|
+
const keyEncoding = r.readUInt8();
|
|
960
|
+
const signature = bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES));
|
|
961
|
+
return {
|
|
962
|
+
hashMode,
|
|
963
|
+
signer,
|
|
964
|
+
nonce,
|
|
965
|
+
fee,
|
|
966
|
+
keyEncoding,
|
|
967
|
+
signature
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
const fieldCount = r.readUInt32BE();
|
|
971
|
+
const fields = [];
|
|
972
|
+
for (let i = 0;i < fieldCount; i++) {
|
|
973
|
+
const fieldType = r.readUInt8();
|
|
974
|
+
if (fieldType <= 1) {
|
|
975
|
+
const keyLen = fieldType === 0 ? 33 : 65;
|
|
976
|
+
fields.push({
|
|
977
|
+
type: "publicKey",
|
|
978
|
+
pubKeyEncoding: fieldType,
|
|
979
|
+
data: bytesToHex(r.readBytes(keyLen))
|
|
980
|
+
});
|
|
981
|
+
} else {
|
|
982
|
+
fields.push({
|
|
983
|
+
type: "signature",
|
|
984
|
+
pubKeyEncoding: fieldType === 2 ? 0 : 1,
|
|
985
|
+
data: bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES))
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
const signaturesRequired = r.readUInt16BE();
|
|
990
|
+
return {
|
|
991
|
+
hashMode,
|
|
992
|
+
signer,
|
|
993
|
+
nonce,
|
|
994
|
+
fee,
|
|
995
|
+
fields,
|
|
996
|
+
signaturesRequired
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
function readAuthorization(r) {
|
|
1000
|
+
const authType = r.readUInt8();
|
|
1001
|
+
const spendingCondition = readSpendingCondition(r);
|
|
1002
|
+
if (authType === AuthType.Standard) {
|
|
1003
|
+
return { authType: AuthType.Standard, spendingCondition };
|
|
1004
|
+
}
|
|
1005
|
+
const sponsorSpendingCondition = readSpendingCondition(r);
|
|
1006
|
+
return {
|
|
1007
|
+
authType: AuthType.Sponsored,
|
|
1008
|
+
spendingCondition,
|
|
1009
|
+
sponsorSpendingCondition
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
function readAssetInfo(r) {
|
|
1013
|
+
return {
|
|
1014
|
+
address: readAddress(r),
|
|
1015
|
+
contractName: readLPString(r),
|
|
1016
|
+
assetName: readLPString(r)
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
function readPostConditionPrincipal(r) {
|
|
1020
|
+
const type = r.readUInt8();
|
|
1021
|
+
switch (type) {
|
|
1022
|
+
case PostConditionPrincipalId.Origin:
|
|
1023
|
+
return { type: "origin" };
|
|
1024
|
+
case PostConditionPrincipalId.Standard:
|
|
1025
|
+
return { type: "standard", address: readAddress(r) };
|
|
1026
|
+
case PostConditionPrincipalId.Contract:
|
|
1027
|
+
return {
|
|
1028
|
+
type: "contract",
|
|
1029
|
+
address: readAddress(r),
|
|
1030
|
+
contractName: readLPString(r)
|
|
1031
|
+
};
|
|
1032
|
+
default:
|
|
1033
|
+
return { type: "origin" };
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
function readPostCondition(r) {
|
|
1037
|
+
const assetType = r.readUInt8();
|
|
1038
|
+
const principal = readPostConditionPrincipal(r);
|
|
1039
|
+
switch (assetType) {
|
|
1040
|
+
case AssetType.STX:
|
|
1041
|
+
return {
|
|
1042
|
+
type: "stx",
|
|
1043
|
+
principal,
|
|
1044
|
+
conditionCode: r.readUInt8(),
|
|
1045
|
+
amount: r.readBigUInt64BE()
|
|
1046
|
+
};
|
|
1047
|
+
case AssetType.Fungible:
|
|
1048
|
+
return {
|
|
1049
|
+
type: "ft",
|
|
1050
|
+
principal,
|
|
1051
|
+
asset: readAssetInfo(r),
|
|
1052
|
+
conditionCode: r.readUInt8(),
|
|
1053
|
+
amount: r.readBigUInt64BE()
|
|
1054
|
+
};
|
|
1055
|
+
case AssetType.NonFungible:
|
|
1056
|
+
return {
|
|
1057
|
+
type: "nft",
|
|
1058
|
+
principal,
|
|
1059
|
+
asset: readAssetInfo(r),
|
|
1060
|
+
assetId: readCV(r),
|
|
1061
|
+
conditionCode: r.readUInt8()
|
|
1062
|
+
};
|
|
1063
|
+
case AssetType.Staking:
|
|
1064
|
+
return {
|
|
1065
|
+
type: "staking",
|
|
1066
|
+
principal,
|
|
1067
|
+
conditionCode: r.readUInt8(),
|
|
1068
|
+
amount: r.readBigUInt64BE()
|
|
1069
|
+
};
|
|
1070
|
+
case AssetType.Pox:
|
|
1071
|
+
return {
|
|
1072
|
+
type: "pox",
|
|
1073
|
+
principal,
|
|
1074
|
+
conditionCode: r.readUInt8()
|
|
1075
|
+
};
|
|
1076
|
+
default:
|
|
1077
|
+
throw new Error(`Unknown post-condition asset type: 0x${assetType.toString(16).padStart(2, "0")}`);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
function readPostConditions(r) {
|
|
1081
|
+
const count = r.readUInt32BE();
|
|
1082
|
+
const pcs = [];
|
|
1083
|
+
for (let i = 0;i < count; i++) {
|
|
1084
|
+
try {
|
|
1085
|
+
pcs.push(readPostCondition(r));
|
|
1086
|
+
} catch (e) {
|
|
1087
|
+
if (e instanceof Error && e.message.startsWith("Unknown post-condition asset type:")) {
|
|
1088
|
+
throw new Error(`${e.message} (post-condition ${i + 1}/${count})`);
|
|
1089
|
+
}
|
|
1090
|
+
throw e;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
return pcs;
|
|
1094
|
+
}
|
|
1095
|
+
function readPayload(r) {
|
|
1096
|
+
const payloadType = r.readUInt8();
|
|
1097
|
+
switch (payloadType) {
|
|
1098
|
+
case PayloadType.TokenTransfer: {
|
|
1099
|
+
const recipient = readCV(r);
|
|
1100
|
+
const amount = r.readBigUInt64BE();
|
|
1101
|
+
const memoBytes = r.readBytes(MEMO_MAX_LENGTH_BYTES);
|
|
1102
|
+
const memo = bytesToAscii(memoBytes).replace(/\0+$/, "");
|
|
1103
|
+
return {
|
|
1104
|
+
payloadType: PayloadType.TokenTransfer,
|
|
1105
|
+
recipient,
|
|
1106
|
+
amount,
|
|
1107
|
+
memo
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
case PayloadType.ContractCall: {
|
|
1111
|
+
const contractAddress = readAddress(r);
|
|
1112
|
+
const contractName = readLPString(r);
|
|
1113
|
+
const functionName = readLPString(r);
|
|
1114
|
+
const numArgs = r.readUInt32BE();
|
|
1115
|
+
const functionArgs = [];
|
|
1116
|
+
for (let i = 0;i < numArgs; i++)
|
|
1117
|
+
functionArgs.push(readCV(r));
|
|
1118
|
+
return {
|
|
1119
|
+
payloadType: PayloadType.ContractCall,
|
|
1120
|
+
contractAddress,
|
|
1121
|
+
contractName,
|
|
1122
|
+
functionName,
|
|
1123
|
+
functionArgs
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
case PayloadType.SmartContract: {
|
|
1127
|
+
const contractName = readLPString(r);
|
|
1128
|
+
const codeBody = readLPString(r, 4);
|
|
1129
|
+
return { payloadType: PayloadType.SmartContract, contractName, codeBody };
|
|
1130
|
+
}
|
|
1131
|
+
case PayloadType.VersionedSmartContract: {
|
|
1132
|
+
const clarityVersion = r.readUInt8();
|
|
1133
|
+
const contractName = readLPString(r);
|
|
1134
|
+
const codeBody = readLPString(r, 4);
|
|
1135
|
+
return {
|
|
1136
|
+
payloadType: PayloadType.VersionedSmartContract,
|
|
1137
|
+
clarityVersion,
|
|
1138
|
+
contractName,
|
|
1139
|
+
codeBody
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
case PayloadType.Coinbase: {
|
|
1143
|
+
const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
|
|
1144
|
+
return { payloadType: PayloadType.Coinbase, coinbaseBuffer };
|
|
1145
|
+
}
|
|
1146
|
+
case PayloadType.CoinbaseToAltRecipient: {
|
|
1147
|
+
const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
|
|
1148
|
+
const recipient = readCV(r);
|
|
1149
|
+
return {
|
|
1150
|
+
payloadType: PayloadType.CoinbaseToAltRecipient,
|
|
1151
|
+
coinbaseBuffer,
|
|
1152
|
+
recipient
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
case PayloadType.PoisonMicroblock: {
|
|
1156
|
+
const header1 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
|
|
1157
|
+
const header2 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
|
|
1158
|
+
return { payloadType: PayloadType.PoisonMicroblock, header1, header2 };
|
|
1159
|
+
}
|
|
1160
|
+
case PayloadType.TenureChange: {
|
|
1161
|
+
const tenureConsensusHash = bytesToHex(r.readBytes(20));
|
|
1162
|
+
const prevTenureConsensusHash = bytesToHex(r.readBytes(20));
|
|
1163
|
+
const burnViewConsensusHash = bytesToHex(r.readBytes(20));
|
|
1164
|
+
const previousTenureEnd = bytesToHex(r.readBytes(32));
|
|
1165
|
+
const previousTenureBlocks = r.readUInt32BE();
|
|
1166
|
+
const cause = r.readUInt8();
|
|
1167
|
+
const pubkeyHash = bytesToHex(r.readBytes(20));
|
|
1168
|
+
return {
|
|
1169
|
+
payloadType: PayloadType.TenureChange,
|
|
1170
|
+
tenureConsensusHash,
|
|
1171
|
+
prevTenureConsensusHash,
|
|
1172
|
+
burnViewConsensusHash,
|
|
1173
|
+
previousTenureEnd,
|
|
1174
|
+
previousTenureBlocks,
|
|
1175
|
+
cause,
|
|
1176
|
+
pubkeyHash
|
|
1177
|
+
};
|
|
1178
|
+
}
|
|
1179
|
+
case PayloadType.NakamotoCoinbase: {
|
|
1180
|
+
const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
|
|
1181
|
+
const optionalCV = readCV(r);
|
|
1182
|
+
const recipient = optionalCV.type === "none" ? null : optionalCV.type === "some" ? optionalCV.value : optionalCV;
|
|
1183
|
+
const vrfProof = bytesToHex(r.readBytes(VRF_PROOF_BYTES_LENGTH));
|
|
1184
|
+
return {
|
|
1185
|
+
payloadType: PayloadType.NakamotoCoinbase,
|
|
1186
|
+
coinbaseBuffer,
|
|
1187
|
+
recipient,
|
|
1188
|
+
vrfProof
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
default:
|
|
1192
|
+
throw new Error(`Unknown payload type: ${payloadType}`);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
function deserializePostConditionWire(input) {
|
|
1196
|
+
const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
|
|
1197
|
+
return readPostCondition(new BytesReader(bytes));
|
|
1198
|
+
}
|
|
1199
|
+
function deserializeTransaction(input, meta) {
|
|
1200
|
+
const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
|
|
1201
|
+
const r = new BytesReader(bytes);
|
|
1202
|
+
const tx = {
|
|
1203
|
+
version: r.readUInt8(),
|
|
1204
|
+
chainId: r.readUInt32BE(),
|
|
1205
|
+
auth: readAuthorization(r),
|
|
1206
|
+
anchorMode: r.readUInt8(),
|
|
1207
|
+
postConditionMode: r.readUInt8(),
|
|
1208
|
+
postConditions: readPostConditions(r),
|
|
1209
|
+
payload: readPayload(r)
|
|
1210
|
+
};
|
|
1211
|
+
if (meta?._multisig) {
|
|
1212
|
+
tx._multisig = meta._multisig;
|
|
1213
|
+
}
|
|
1214
|
+
return tx;
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
// src/transactions/wire/serialize.ts
|
|
1218
|
+
function serializeAddress2(c32Address) {
|
|
1219
|
+
const [version, hash1602] = c32addressDecode(c32Address);
|
|
1220
|
+
return concatBytes(new Uint8Array([version]), hexToBytes(hash1602));
|
|
1221
|
+
}
|
|
1222
|
+
function serializeLPString2(str, prefixBytes = 1) {
|
|
1223
|
+
const content = utf8ToBytes(str);
|
|
1224
|
+
const prefix = hexToBytes(intToHex(content.byteLength, prefixBytes));
|
|
1225
|
+
return concatBytes(prefix, content);
|
|
1226
|
+
}
|
|
1227
|
+
function serializeLPStringLong(str) {
|
|
1228
|
+
return serializeLPString2(str, 4);
|
|
1229
|
+
}
|
|
1230
|
+
function serializeMemo(memo) {
|
|
1231
|
+
const content = asciiToBytes(memo);
|
|
1232
|
+
const padded = new Uint8Array(MEMO_MAX_LENGTH_BYTES);
|
|
1233
|
+
padded.set(content.slice(0, MEMO_MAX_LENGTH_BYTES));
|
|
1234
|
+
return padded;
|
|
1235
|
+
}
|
|
1236
|
+
function serializeSpendingCondition(condition) {
|
|
1237
|
+
const parts = [
|
|
1238
|
+
writeUInt8(condition.hashMode),
|
|
1239
|
+
hexToBytes(condition.signer),
|
|
1240
|
+
intToBytes(condition.nonce, 8),
|
|
1241
|
+
intToBytes(condition.fee, 8)
|
|
1242
|
+
];
|
|
1243
|
+
if ("signature" in condition) {
|
|
1244
|
+
const sc = condition;
|
|
1245
|
+
parts.push(writeUInt8(sc.keyEncoding));
|
|
1246
|
+
parts.push(hexToBytes(sc.signature));
|
|
1247
|
+
} else {
|
|
1248
|
+
const mc = condition;
|
|
1249
|
+
parts.push(writeUInt32BE(mc.fields.length));
|
|
1250
|
+
for (const field of mc.fields) {
|
|
1251
|
+
parts.push(serializeAuthField(field));
|
|
1252
|
+
}
|
|
1253
|
+
parts.push(writeUInt16BE(mc.signaturesRequired));
|
|
1254
|
+
}
|
|
1255
|
+
return concatBytes(...parts);
|
|
1256
|
+
}
|
|
1257
|
+
function serializeAuthField(field) {
|
|
1258
|
+
if (field.type === "publicKey") {
|
|
1259
|
+
const typeId2 = field.pubKeyEncoding === 0 ? 0 : 1;
|
|
1260
|
+
return concatBytes(writeUInt8(typeId2), hexToBytes(field.data));
|
|
1261
|
+
}
|
|
1262
|
+
const typeId = field.pubKeyEncoding === 0 ? 2 : 3;
|
|
1263
|
+
return concatBytes(writeUInt8(typeId), hexToBytes(field.data));
|
|
1264
|
+
}
|
|
1265
|
+
function serializeAuthorization(auth) {
|
|
1266
|
+
const parts = [writeUInt8(auth.authType)];
|
|
1267
|
+
parts.push(serializeSpendingCondition(auth.spendingCondition));
|
|
1268
|
+
if (auth.authType === AuthType.Sponsored) {
|
|
1269
|
+
parts.push(serializeSpendingCondition(auth.sponsorSpendingCondition));
|
|
1270
|
+
}
|
|
1271
|
+
return concatBytes(...parts);
|
|
1272
|
+
}
|
|
1273
|
+
function serializePrincipal(principal) {
|
|
1274
|
+
switch (principal.type) {
|
|
1275
|
+
case "origin":
|
|
1276
|
+
return writeUInt8(PostConditionPrincipalId.Origin);
|
|
1277
|
+
case "standard":
|
|
1278
|
+
return concatBytes(writeUInt8(PostConditionPrincipalId.Standard), serializeAddress2(principal.address));
|
|
1279
|
+
case "contract":
|
|
1280
|
+
return concatBytes(writeUInt8(PostConditionPrincipalId.Contract), serializeAddress2(principal.address), serializeLPString2(principal.contractName));
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
function serializeAssetInfo(asset) {
|
|
1284
|
+
return concatBytes(serializeAddress2(asset.address), serializeLPString2(asset.contractName), serializeLPString2(asset.assetName));
|
|
1285
|
+
}
|
|
1286
|
+
function serializePostConditionWire(pc) {
|
|
1287
|
+
const parts = [];
|
|
1288
|
+
switch (pc.type) {
|
|
1289
|
+
case "stx":
|
|
1290
|
+
parts.push(writeUInt8(AssetType.STX));
|
|
1291
|
+
parts.push(serializePrincipal(pc.principal));
|
|
1292
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
1293
|
+
parts.push(intToBytes(pc.amount, 8));
|
|
1294
|
+
break;
|
|
1295
|
+
case "ft":
|
|
1296
|
+
parts.push(writeUInt8(AssetType.Fungible));
|
|
1297
|
+
parts.push(serializePrincipal(pc.principal));
|
|
1298
|
+
parts.push(serializeAssetInfo(pc.asset));
|
|
1299
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
1300
|
+
parts.push(intToBytes(pc.amount, 8));
|
|
1301
|
+
break;
|
|
1302
|
+
case "nft":
|
|
1303
|
+
parts.push(writeUInt8(AssetType.NonFungible));
|
|
1304
|
+
parts.push(serializePrincipal(pc.principal));
|
|
1305
|
+
parts.push(serializeAssetInfo(pc.asset));
|
|
1306
|
+
parts.push(serializeCVBytes(pc.assetId));
|
|
1307
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
1308
|
+
break;
|
|
1309
|
+
case "staking":
|
|
1310
|
+
parts.push(writeUInt8(AssetType.Staking));
|
|
1311
|
+
parts.push(serializePrincipal(pc.principal));
|
|
1312
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
1313
|
+
parts.push(intToBytes(pc.amount, 8));
|
|
1314
|
+
break;
|
|
1315
|
+
case "pox":
|
|
1316
|
+
parts.push(writeUInt8(AssetType.Pox));
|
|
1317
|
+
parts.push(serializePrincipal(pc.principal));
|
|
1318
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
1319
|
+
break;
|
|
1320
|
+
}
|
|
1321
|
+
return concatBytes(...parts);
|
|
1322
|
+
}
|
|
1323
|
+
function serializePostConditions(pcs) {
|
|
1324
|
+
const parts = [writeUInt32BE(pcs.length)];
|
|
1325
|
+
for (const pc of pcs)
|
|
1326
|
+
parts.push(serializePostConditionWire(pc));
|
|
1327
|
+
return concatBytes(...parts);
|
|
1328
|
+
}
|
|
1329
|
+
function serializePayload(payload) {
|
|
1330
|
+
const parts = [writeUInt8(payload.payloadType)];
|
|
1331
|
+
switch (payload.payloadType) {
|
|
1332
|
+
case PayloadType.TokenTransfer:
|
|
1333
|
+
parts.push(serializeCVBytes(payload.recipient));
|
|
1334
|
+
parts.push(intToBytes(payload.amount, 8));
|
|
1335
|
+
parts.push(serializeMemo(payload.memo));
|
|
1336
|
+
break;
|
|
1337
|
+
case PayloadType.ContractCall:
|
|
1338
|
+
parts.push(serializeAddress2(payload.contractAddress));
|
|
1339
|
+
parts.push(serializeLPString2(payload.contractName));
|
|
1340
|
+
parts.push(serializeLPString2(payload.functionName));
|
|
1341
|
+
parts.push(writeUInt32BE(payload.functionArgs.length));
|
|
1342
|
+
for (const arg of payload.functionArgs) {
|
|
1343
|
+
parts.push(serializeCVBytes(arg));
|
|
1344
|
+
}
|
|
1345
|
+
break;
|
|
1346
|
+
case PayloadType.SmartContract:
|
|
1347
|
+
parts.push(serializeLPString2(payload.contractName));
|
|
1348
|
+
parts.push(serializeLPStringLong(payload.codeBody));
|
|
1349
|
+
break;
|
|
1350
|
+
case PayloadType.VersionedSmartContract:
|
|
1351
|
+
parts.push(writeUInt8(payload.clarityVersion ?? 2));
|
|
1352
|
+
parts.push(serializeLPString2(payload.contractName));
|
|
1353
|
+
parts.push(serializeLPStringLong(payload.codeBody));
|
|
1354
|
+
break;
|
|
1355
|
+
case PayloadType.Coinbase:
|
|
1356
|
+
parts.push(hexToBytes(payload.coinbaseBuffer));
|
|
1357
|
+
break;
|
|
1358
|
+
case PayloadType.CoinbaseToAltRecipient: {
|
|
1359
|
+
const cbAlt = payload;
|
|
1360
|
+
parts.push(hexToBytes(cbAlt.coinbaseBuffer));
|
|
1361
|
+
parts.push(serializeCVBytes(cbAlt.recipient));
|
|
1362
|
+
break;
|
|
1363
|
+
}
|
|
1364
|
+
case PayloadType.PoisonMicroblock: {
|
|
1365
|
+
const pm = payload;
|
|
1366
|
+
parts.push(hexToBytes(pm.header1));
|
|
1367
|
+
parts.push(hexToBytes(pm.header2));
|
|
1368
|
+
break;
|
|
1369
|
+
}
|
|
1370
|
+
case PayloadType.TenureChange: {
|
|
1371
|
+
const tc = payload;
|
|
1372
|
+
parts.push(hexToBytes(tc.tenureConsensusHash));
|
|
1373
|
+
parts.push(hexToBytes(tc.prevTenureConsensusHash));
|
|
1374
|
+
parts.push(hexToBytes(tc.burnViewConsensusHash));
|
|
1375
|
+
parts.push(hexToBytes(tc.previousTenureEnd));
|
|
1376
|
+
parts.push(writeUInt32BE(tc.previousTenureBlocks));
|
|
1377
|
+
parts.push(writeUInt8(tc.cause));
|
|
1378
|
+
parts.push(hexToBytes(tc.pubkeyHash));
|
|
1379
|
+
break;
|
|
1380
|
+
}
|
|
1381
|
+
case PayloadType.NakamotoCoinbase: {
|
|
1382
|
+
const nc = payload;
|
|
1383
|
+
parts.push(hexToBytes(nc.coinbaseBuffer));
|
|
1384
|
+
if (nc.recipient) {
|
|
1385
|
+
parts.push(serializeCVBytes({ type: "some", value: nc.recipient }));
|
|
1386
|
+
} else {
|
|
1387
|
+
parts.push(serializeCVBytes({ type: "none" }));
|
|
1388
|
+
}
|
|
1389
|
+
parts.push(hexToBytes(nc.vrfProof));
|
|
1390
|
+
break;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
return concatBytes(...parts);
|
|
1394
|
+
}
|
|
1395
|
+
var _txCache = new WeakMap;
|
|
1396
|
+
function clearTxCache(tx) {
|
|
1397
|
+
_txCache.delete(tx);
|
|
1398
|
+
}
|
|
1399
|
+
function serializeTransaction(tx) {
|
|
1400
|
+
const cached = _txCache.get(tx);
|
|
1401
|
+
if (cached)
|
|
1402
|
+
return cached;
|
|
1403
|
+
const bytes = concatBytes(writeUInt8(tx.version), writeUInt32BE(tx.chainId), serializeAuthorization(tx.auth), writeUInt8(tx.anchorMode), writeUInt8(tx.postConditionMode), serializePostConditions(tx.postConditions), serializePayload(tx.payload));
|
|
1404
|
+
_txCache.set(tx, bytes);
|
|
1405
|
+
return bytes;
|
|
1406
|
+
}
|
|
1407
|
+
function serializeTransactionHex(tx) {
|
|
1408
|
+
return bytesToHex(serializeTransaction(tx));
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
// src/postconditions/convert.ts
|
|
1412
|
+
var MAX_U64 = 18446744073709551615n;
|
|
1413
|
+
function parsePostConditionAmount(amount) {
|
|
1414
|
+
const value = intToBigInt(amount);
|
|
1415
|
+
if (value < 0n || value > MAX_U64) {
|
|
1416
|
+
throw new RangeError(`Post-condition amount must be between 0 and ${MAX_U64} (u64 max), received: ${value}`);
|
|
1417
|
+
}
|
|
1418
|
+
return value;
|
|
1419
|
+
}
|
|
1420
|
+
var FUNGIBLE_CODE_MAP = {
|
|
1421
|
+
eq: FungibleConditionCode.Equal,
|
|
1422
|
+
gt: FungibleConditionCode.Greater,
|
|
1423
|
+
gte: FungibleConditionCode.GreaterEqual,
|
|
1424
|
+
lt: FungibleConditionCode.Less,
|
|
1425
|
+
lte: FungibleConditionCode.LessEqual
|
|
1426
|
+
};
|
|
1427
|
+
var FUNGIBLE_CODE_REVERSE = {
|
|
1428
|
+
[FungibleConditionCode.Equal]: "eq",
|
|
1429
|
+
[FungibleConditionCode.Greater]: "gt",
|
|
1430
|
+
[FungibleConditionCode.GreaterEqual]: "gte",
|
|
1431
|
+
[FungibleConditionCode.Less]: "lt",
|
|
1432
|
+
[FungibleConditionCode.LessEqual]: "lte"
|
|
1433
|
+
};
|
|
1434
|
+
var NFT_CODE_MAP = {
|
|
1435
|
+
sent: NonFungibleConditionCode.Sends,
|
|
1436
|
+
"not-sent": NonFungibleConditionCode.DoesNotSend,
|
|
1437
|
+
"maybe-sent": NonFungibleConditionCode.MaybeSent
|
|
1438
|
+
};
|
|
1439
|
+
var NFT_CODE_REVERSE = {
|
|
1440
|
+
[NonFungibleConditionCode.Sends]: "sent",
|
|
1441
|
+
[NonFungibleConditionCode.DoesNotSend]: "not-sent",
|
|
1442
|
+
[NonFungibleConditionCode.MaybeSent]: "maybe-sent"
|
|
1443
|
+
};
|
|
1444
|
+
var POX_CODE_MAP = {
|
|
1445
|
+
"will-not-perform": PoxConditionCode.WillNotPerform,
|
|
1446
|
+
"may-perform": PoxConditionCode.MayPerform,
|
|
1447
|
+
"will-perform": PoxConditionCode.WillPerform
|
|
1448
|
+
};
|
|
1449
|
+
var POX_CODE_REVERSE = {
|
|
1450
|
+
[PoxConditionCode.WillNotPerform]: "will-not-perform",
|
|
1451
|
+
[PoxConditionCode.MayPerform]: "may-perform",
|
|
1452
|
+
[PoxConditionCode.WillPerform]: "will-perform"
|
|
1453
|
+
};
|
|
1454
|
+
function resolvePrincipal(address) {
|
|
1455
|
+
if (address === "origin")
|
|
1456
|
+
return { type: "origin" };
|
|
1457
|
+
const [addr, name] = address.split(".");
|
|
1458
|
+
if (name) {
|
|
1459
|
+
return { type: "contract", address: addr, contractName: name };
|
|
1460
|
+
}
|
|
1461
|
+
return { type: "standard", address: addr };
|
|
1462
|
+
}
|
|
1463
|
+
function principalToString(principal) {
|
|
1464
|
+
if (principal.type === "origin")
|
|
1465
|
+
return "origin";
|
|
1466
|
+
if (principal.type === "contract") {
|
|
1467
|
+
return `${principal.address}.${principal.contractName}`;
|
|
1468
|
+
}
|
|
1469
|
+
return principal.address;
|
|
1470
|
+
}
|
|
1471
|
+
function assetToString(asset) {
|
|
1472
|
+
return `${asset.address}.${asset.contractName}::${asset.assetName}`;
|
|
1473
|
+
}
|
|
1474
|
+
function parseAsset(asset) {
|
|
1475
|
+
const [contractId, tokenName] = asset.split("::");
|
|
1476
|
+
const [addr, name] = parseContractId(contractId);
|
|
1477
|
+
return { address: addr, contractName: name, assetName: tokenName };
|
|
1478
|
+
}
|
|
1479
|
+
function convertPostCondition(pc) {
|
|
1480
|
+
switch (pc.type) {
|
|
1481
|
+
case "stx-postcondition":
|
|
1482
|
+
return {
|
|
1483
|
+
type: "stx",
|
|
1484
|
+
principal: resolvePrincipal(pc.address),
|
|
1485
|
+
conditionCode: FUNGIBLE_CODE_MAP[pc.condition],
|
|
1486
|
+
amount: parsePostConditionAmount(pc.amount)
|
|
1487
|
+
};
|
|
1488
|
+
case "ft-postcondition":
|
|
1489
|
+
return {
|
|
1490
|
+
type: "ft",
|
|
1491
|
+
principal: resolvePrincipal(pc.address),
|
|
1492
|
+
asset: parseAsset(pc.asset),
|
|
1493
|
+
conditionCode: FUNGIBLE_CODE_MAP[pc.condition],
|
|
1494
|
+
amount: parsePostConditionAmount(pc.amount)
|
|
1495
|
+
};
|
|
1496
|
+
case "nft-postcondition":
|
|
1497
|
+
return {
|
|
1498
|
+
type: "nft",
|
|
1499
|
+
principal: resolvePrincipal(pc.address),
|
|
1500
|
+
asset: parseAsset(pc.asset),
|
|
1501
|
+
conditionCode: NFT_CODE_MAP[pc.condition],
|
|
1502
|
+
assetId: pc.assetId
|
|
1503
|
+
};
|
|
1504
|
+
case "staking-postcondition":
|
|
1505
|
+
return {
|
|
1506
|
+
type: "staking",
|
|
1507
|
+
principal: resolvePrincipal(pc.address),
|
|
1508
|
+
conditionCode: FUNGIBLE_CODE_MAP[pc.condition],
|
|
1509
|
+
amount: parsePostConditionAmount(pc.amount)
|
|
1510
|
+
};
|
|
1511
|
+
case "pox-postcondition":
|
|
1512
|
+
return {
|
|
1513
|
+
type: "pox",
|
|
1514
|
+
principal: resolvePrincipal(pc.address),
|
|
1515
|
+
conditionCode: POX_CODE_MAP[pc.condition]
|
|
1516
|
+
};
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
function wireToPostCondition(wire) {
|
|
1520
|
+
switch (wire.type) {
|
|
1521
|
+
case "stx":
|
|
1522
|
+
return {
|
|
1523
|
+
type: "stx-postcondition",
|
|
1524
|
+
address: principalToString(wire.principal),
|
|
1525
|
+
condition: fungibleName(wire.conditionCode),
|
|
1526
|
+
amount: wire.amount.toString()
|
|
1527
|
+
};
|
|
1528
|
+
case "ft":
|
|
1529
|
+
return {
|
|
1530
|
+
type: "ft-postcondition",
|
|
1531
|
+
address: principalToString(wire.principal),
|
|
1532
|
+
condition: fungibleName(wire.conditionCode),
|
|
1533
|
+
amount: wire.amount.toString(),
|
|
1534
|
+
asset: assetToString(wire.asset)
|
|
1535
|
+
};
|
|
1536
|
+
case "nft":
|
|
1537
|
+
return {
|
|
1538
|
+
type: "nft-postcondition",
|
|
1539
|
+
address: principalToString(wire.principal),
|
|
1540
|
+
condition: nftName(wire.conditionCode),
|
|
1541
|
+
asset: assetToString(wire.asset),
|
|
1542
|
+
assetId: wire.assetId
|
|
1543
|
+
};
|
|
1544
|
+
case "staking":
|
|
1545
|
+
return {
|
|
1546
|
+
type: "staking-postcondition",
|
|
1547
|
+
address: principalToString(wire.principal),
|
|
1548
|
+
condition: fungibleName(wire.conditionCode),
|
|
1549
|
+
amount: wire.amount.toString()
|
|
1550
|
+
};
|
|
1551
|
+
case "pox":
|
|
1552
|
+
return {
|
|
1553
|
+
type: "pox-postcondition",
|
|
1554
|
+
address: principalToString(wire.principal),
|
|
1555
|
+
condition: poxName(wire.conditionCode)
|
|
1556
|
+
};
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
function fungibleName(code) {
|
|
1560
|
+
const name = FUNGIBLE_CODE_REVERSE[code];
|
|
1561
|
+
if (!name)
|
|
1562
|
+
throw new Error(`Unknown fungible post-condition code: ${code}`);
|
|
1563
|
+
return name;
|
|
1564
|
+
}
|
|
1565
|
+
function nftName(code) {
|
|
1566
|
+
const name = NFT_CODE_REVERSE[code];
|
|
1567
|
+
if (!name)
|
|
1568
|
+
throw new Error(`Unknown nft post-condition code: ${code}`);
|
|
1569
|
+
return name;
|
|
1570
|
+
}
|
|
1571
|
+
function poxName(code) {
|
|
1572
|
+
const name = POX_CODE_REVERSE[code];
|
|
1573
|
+
if (!name)
|
|
1574
|
+
throw new Error(`Unknown pox post-condition code: ${code}`);
|
|
1575
|
+
return name;
|
|
1576
|
+
}
|
|
1577
|
+
function fromHex(hex) {
|
|
1578
|
+
return wireToPostCondition(deserializePostConditionWire(hex));
|
|
1579
|
+
}
|
|
1580
|
+
function postConditionToHex(pc) {
|
|
1581
|
+
return bytesToHex(serializePostConditionWire(convertPostCondition(pc)));
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
// src/postconditions/builder.ts
|
|
1585
|
+
class PartialPcWithPrincipal {
|
|
1586
|
+
address;
|
|
1587
|
+
constructor(address) {
|
|
1588
|
+
this.address = address;
|
|
1589
|
+
}
|
|
1590
|
+
willSendEq(amount) {
|
|
1591
|
+
return new PartialPcFtWithCode(this.address, amount, "eq");
|
|
1592
|
+
}
|
|
1593
|
+
willSendGte(amount) {
|
|
1594
|
+
return new PartialPcFtWithCode(this.address, amount, "gte");
|
|
1595
|
+
}
|
|
1596
|
+
willSendGt(amount) {
|
|
1597
|
+
return new PartialPcFtWithCode(this.address, amount, "gt");
|
|
1598
|
+
}
|
|
1599
|
+
willSendLte(amount) {
|
|
1600
|
+
return new PartialPcFtWithCode(this.address, amount, "lte");
|
|
1601
|
+
}
|
|
1602
|
+
willSendLt(amount) {
|
|
1603
|
+
return new PartialPcFtWithCode(this.address, amount, "lt");
|
|
1604
|
+
}
|
|
1605
|
+
willSendAsset() {
|
|
1606
|
+
return new PartialPcNftWithCode(this.address, "sent");
|
|
1607
|
+
}
|
|
1608
|
+
willNotSendAsset() {
|
|
1609
|
+
return new PartialPcNftWithCode(this.address, "not-sent");
|
|
1610
|
+
}
|
|
1611
|
+
willMaybeSendAsset() {
|
|
1612
|
+
return new PartialPcNftWithCode(this.address, "maybe-sent");
|
|
1613
|
+
}
|
|
1614
|
+
willPerformPox() {
|
|
1615
|
+
return {
|
|
1616
|
+
type: "pox-postcondition",
|
|
1617
|
+
address: this.address,
|
|
1618
|
+
condition: "will-perform"
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
willNotPerformPox() {
|
|
1622
|
+
return {
|
|
1623
|
+
type: "pox-postcondition",
|
|
1624
|
+
address: this.address,
|
|
1625
|
+
condition: "will-not-perform"
|
|
1626
|
+
};
|
|
1627
|
+
}
|
|
1628
|
+
mayPerformPox() {
|
|
1629
|
+
return {
|
|
1630
|
+
type: "pox-postcondition",
|
|
1631
|
+
address: this.address,
|
|
1632
|
+
condition: "may-perform"
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
class PartialPcFtWithCode {
|
|
1638
|
+
address;
|
|
1639
|
+
amount;
|
|
1640
|
+
code;
|
|
1641
|
+
constructor(address, amount, code) {
|
|
1642
|
+
this.address = address;
|
|
1643
|
+
this.amount = amount;
|
|
1644
|
+
this.code = code;
|
|
1645
|
+
}
|
|
1646
|
+
ustx() {
|
|
1647
|
+
return {
|
|
1648
|
+
type: "stx-postcondition",
|
|
1649
|
+
address: this.address,
|
|
1650
|
+
condition: this.code,
|
|
1651
|
+
amount: parsePostConditionAmount(this.amount).toString()
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
ustxToLock() {
|
|
1655
|
+
return {
|
|
1656
|
+
type: "staking-postcondition",
|
|
1657
|
+
address: this.address,
|
|
1658
|
+
condition: this.code,
|
|
1659
|
+
amount: parsePostConditionAmount(this.amount).toString()
|
|
1660
|
+
};
|
|
1661
|
+
}
|
|
1662
|
+
ft(contractId, tokenName) {
|
|
1663
|
+
const [address, name] = contractId.split(".");
|
|
1664
|
+
if (!address || !validateStacksAddress(address) || !name) {
|
|
1665
|
+
throw new Error(`Invalid contract id: ${contractId}`);
|
|
1666
|
+
}
|
|
1667
|
+
return {
|
|
1668
|
+
type: "ft-postcondition",
|
|
1669
|
+
address: this.address,
|
|
1670
|
+
condition: this.code,
|
|
1671
|
+
amount: parsePostConditionAmount(this.amount).toString(),
|
|
1672
|
+
asset: `${contractId}::${tokenName}`
|
|
1673
|
+
};
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
class PartialPcNftWithCode {
|
|
1678
|
+
address;
|
|
1679
|
+
code;
|
|
1680
|
+
constructor(address, code) {
|
|
1681
|
+
this.address = address;
|
|
1682
|
+
this.code = code;
|
|
1683
|
+
}
|
|
1684
|
+
nft(assetOrContractId, tokenNameOrAssetId, assetId) {
|
|
1685
|
+
let contractAddress;
|
|
1686
|
+
let contractName;
|
|
1687
|
+
let tokenName;
|
|
1688
|
+
let id;
|
|
1689
|
+
if (assetId !== undefined) {
|
|
1690
|
+
[contractAddress, contractName] = parseContractId(assetOrContractId);
|
|
1691
|
+
tokenName = tokenNameOrAssetId;
|
|
1692
|
+
id = assetId;
|
|
1693
|
+
} else {
|
|
1694
|
+
const [principal, tName] = assetOrContractId.split("::");
|
|
1695
|
+
if (!principal || !tName)
|
|
1696
|
+
throw new Error(`Invalid asset name: ${assetOrContractId}`);
|
|
1697
|
+
[contractAddress, contractName] = parseContractId(principal);
|
|
1698
|
+
tokenName = tName;
|
|
1699
|
+
id = tokenNameOrAssetId;
|
|
1700
|
+
}
|
|
1701
|
+
if (!validateStacksAddress(contractAddress)) {
|
|
1702
|
+
throw new Error(`Invalid contract address: ${contractAddress}`);
|
|
1703
|
+
}
|
|
1704
|
+
return {
|
|
1705
|
+
type: "nft-postcondition",
|
|
1706
|
+
address: this.address,
|
|
1707
|
+
condition: this.code,
|
|
1708
|
+
asset: `${contractAddress}.${contractName}::${tokenName}`,
|
|
1709
|
+
assetId: id
|
|
1710
|
+
};
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
var Pc = {
|
|
1714
|
+
principal(address) {
|
|
1715
|
+
const [addr, name] = address.split(".");
|
|
1716
|
+
if (!addr || !validateStacksAddress(addr) || typeof name === "string" && !name) {
|
|
1717
|
+
throw new Error(`Invalid principal: ${address}`);
|
|
1718
|
+
}
|
|
1719
|
+
return new PartialPcWithPrincipal(address);
|
|
1720
|
+
},
|
|
1721
|
+
origin() {
|
|
1722
|
+
return new PartialPcWithPrincipal("origin");
|
|
1723
|
+
},
|
|
1724
|
+
fromHex: (hex) => fromHex(hex)
|
|
1725
|
+
};
|
|
1726
|
+
|
|
1727
|
+
//# debugId=C4D696AAD8A01C4A64756E2164756E21
|
|
3
1728
|
//# sourceMappingURL=index.cjs.map
|