@secondlayer/stacks 0.0.1
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 +92 -0
- package/dist/shared/stacks-02actsrx.cjs +17 -0
- package/dist/shared/stacks-02actsrx.cjs.map +10 -0
- package/dist/shared/stacks-1aqm7c2h.cjs +110 -0
- package/dist/shared/stacks-1aqm7c2h.cjs.map +11 -0
- package/dist/shared/stacks-24h5k94q.cjs +74 -0
- package/dist/shared/stacks-24h5k94q.cjs.map +11 -0
- package/dist/shared/stacks-2ar4prfz.cjs +328 -0
- package/dist/shared/stacks-2ar4prfz.cjs.map +11 -0
- package/dist/shared/stacks-4py5pm0r.cjs +237 -0
- package/dist/shared/stacks-4py5pm0r.cjs.map +10 -0
- package/dist/shared/stacks-5tprhns9.cjs +54 -0
- package/dist/shared/stacks-5tprhns9.cjs.map +11 -0
- package/dist/shared/stacks-61rvzkz1.cjs +132 -0
- package/dist/shared/stacks-61rvzkz1.cjs.map +17 -0
- package/dist/shared/stacks-709tra19.js +30 -0
- package/dist/shared/stacks-709tra19.js.map +10 -0
- package/dist/shared/stacks-74k8cxgb.cjs +567 -0
- package/dist/shared/stacks-74k8cxgb.cjs.map +13 -0
- package/dist/shared/stacks-8m9byk2j.js +7 -0
- package/dist/shared/stacks-8m9byk2j.js.map +9 -0
- package/dist/shared/stacks-8yykgdww.js +125 -0
- package/dist/shared/stacks-8yykgdww.js.map +11 -0
- package/dist/shared/stacks-9g0jhab3.cjs +40 -0
- package/dist/shared/stacks-9g0jhab3.cjs.map +10 -0
- package/dist/shared/stacks-an5k8n4t.cjs +291 -0
- package/dist/shared/stacks-an5k8n4t.cjs.map +23 -0
- package/dist/shared/stacks-b008c3w8.cjs +214 -0
- package/dist/shared/stacks-b008c3w8.cjs.map +11 -0
- package/dist/shared/stacks-bnvfmt7y.cjs +31 -0
- package/dist/shared/stacks-bnvfmt7y.cjs.map +9 -0
- package/dist/shared/stacks-bycy6t9k.js +22 -0
- package/dist/shared/stacks-bycy6t9k.js.map +10 -0
- package/dist/shared/stacks-d1fmkv3p.js +258 -0
- package/dist/shared/stacks-d1fmkv3p.js.map +13 -0
- package/dist/shared/stacks-dr5en54r.cjs +119 -0
- package/dist/shared/stacks-dr5en54r.cjs.map +10 -0
- package/dist/shared/stacks-dxhntr76.js +278 -0
- package/dist/shared/stacks-dxhntr76.js.map +10 -0
- package/dist/shared/stacks-evnzth1q.cjs +35 -0
- package/dist/shared/stacks-evnzth1q.cjs.map +10 -0
- package/dist/shared/stacks-f3tk2k5s.js +27 -0
- package/dist/shared/stacks-f3tk2k5s.js.map +9 -0
- package/dist/shared/stacks-f7wdrzs3.js +285 -0
- package/dist/shared/stacks-f7wdrzs3.js.map +11 -0
- package/dist/shared/stacks-fcaefc5b.cjs +16 -0
- package/dist/shared/stacks-fcaefc5b.cjs.map +10 -0
- package/dist/shared/stacks-g9mcjcqg.cjs +3 -0
- package/dist/shared/stacks-g9mcjcqg.cjs.map +9 -0
- package/dist/shared/stacks-gck1sq7h.cjs +28 -0
- package/dist/shared/stacks-gck1sq7h.cjs.map +10 -0
- package/dist/shared/stacks-gjvbq6w7.js +46 -0
- package/dist/shared/stacks-gjvbq6w7.js.map +10 -0
- package/dist/shared/stacks-j2cy7qtt.js +237 -0
- package/dist/shared/stacks-j2cy7qtt.js.map +11 -0
- package/dist/shared/stacks-k08tgzyp.cjs +232 -0
- package/dist/shared/stacks-k08tgzyp.cjs.map +13 -0
- package/dist/shared/stacks-k2p5tv6x.js +37 -0
- package/dist/shared/stacks-k2p5tv6x.js.map +10 -0
- package/dist/shared/stacks-k58k970n.js +234 -0
- package/dist/shared/stacks-k58k970n.js.map +10 -0
- package/dist/shared/stacks-n2g56wgg.js +338 -0
- package/dist/shared/stacks-n2g56wgg.js.map +23 -0
- package/dist/shared/stacks-nqytwfcc.js +243 -0
- package/dist/shared/stacks-nqytwfcc.js.map +11 -0
- package/dist/shared/stacks-qcae5852.js +19 -0
- package/dist/shared/stacks-qcae5852.js.map +10 -0
- package/dist/shared/stacks-qt8ehebn.js +76 -0
- package/dist/shared/stacks-qt8ehebn.js.map +11 -0
- package/dist/shared/stacks-qvg7c24g.cjs +232 -0
- package/dist/shared/stacks-qvg7c24g.cjs.map +10 -0
- package/dist/shared/stacks-r1t6g3c3.cjs +241 -0
- package/dist/shared/stacks-r1t6g3c3.cjs.map +11 -0
- package/dist/shared/stacks-tjxmrssa.js +282 -0
- package/dist/shared/stacks-tjxmrssa.js.map +11 -0
- package/dist/shared/stacks-v9jqcpd4.js +18 -0
- package/dist/shared/stacks-v9jqcpd4.js.map +10 -0
- package/dist/shared/stacks-vdfag81a.cjs +121 -0
- package/dist/shared/stacks-vdfag81a.cjs.map +10 -0
- package/dist/shared/stacks-vdfs3e5v.js +134 -0
- package/dist/shared/stacks-vdfs3e5v.js.map +10 -0
- package/dist/shared/stacks-y3dh7hv1.js +60 -0
- package/dist/shared/stacks-y3dh7hv1.js.map +11 -0
- package/dist/shared/stacks-ya3x01yd.cjs +16 -0
- package/dist/shared/stacks-ya3x01yd.cjs.map +10 -0
- package/dist/shared/stacks-yfgsm6ap.js +599 -0
- package/dist/shared/stacks-yfgsm6ap.js.map +13 -0
- package/dist/shared/stacks-yfveb68e.js +121 -0
- package/dist/shared/stacks-yfveb68e.js.map +10 -0
- package/dist/shared/stacks-zm0b3tgy.js +160 -0
- package/dist/shared/stacks-zm0b3tgy.js.map +17 -0
- package/dist/shared/stacks-zyv9qqme.cjs +249 -0
- package/dist/shared/stacks-zyv9qqme.cjs.map +11 -0
- package/dist/src/accounts/index.cjs +90 -0
- package/dist/src/accounts/index.cjs.map +12 -0
- package/dist/src/accounts/index.d.cts +47 -0
- package/dist/src/accounts/index.d.ts +47 -0
- package/dist/src/accounts/index.js +104 -0
- package/dist/src/accounts/index.js.map +12 -0
- package/dist/src/actions/index.cjs +36 -0
- package/dist/src/actions/index.cjs.map +9 -0
- package/dist/src/actions/index.d.cts +886 -0
- package/dist/src/actions/index.d.ts +886 -0
- package/dist/src/actions/index.js +81 -0
- package/dist/src/actions/index.js.map +9 -0
- package/dist/src/bns/index.cjs +590 -0
- package/dist/src/bns/index.cjs.map +15 -0
- package/dist/src/bns/index.d.cts +241 -0
- package/dist/src/bns/index.d.ts +241 -0
- package/dist/src/bns/index.js +611 -0
- package/dist/src/bns/index.js.map +15 -0
- package/dist/src/chains/index.cjs +13 -0
- package/dist/src/chains/index.cjs.map +9 -0
- package/dist/src/chains/index.d.cts +53 -0
- package/dist/src/chains/index.d.ts +53 -0
- package/dist/src/chains/index.js +18 -0
- package/dist/src/chains/index.js.map +9 -0
- package/dist/src/clarity/index.cjs +3 -0
- package/dist/src/clarity/index.cjs.map +9 -0
- package/dist/src/clarity/index.d.cts +821 -0
- package/dist/src/clarity/index.d.ts +821 -0
- package/dist/src/clarity/index.js +145 -0
- package/dist/src/clarity/index.js.map +9 -0
- package/dist/src/connect/index.cjs +480 -0
- package/dist/src/connect/index.cjs.map +19 -0
- package/dist/src/connect/index.d.cts +110 -0
- package/dist/src/connect/index.d.ts +110 -0
- package/dist/src/connect/index.js +448 -0
- package/dist/src/connect/index.js.map +19 -0
- package/dist/src/connect/walletconnect/index.cjs +1322 -0
- package/dist/src/connect/walletconnect/index.cjs.map +18 -0
- package/dist/src/connect/walletconnect/index.d.cts +132 -0
- package/dist/src/connect/walletconnect/index.d.ts +132 -0
- package/dist/src/connect/walletconnect/index.js +1290 -0
- package/dist/src/connect/walletconnect/index.js.map +18 -0
- package/dist/src/index.cjs +557 -0
- package/dist/src/index.cjs.map +21 -0
- package/dist/src/index.d.cts +847 -0
- package/dist/src/index.d.ts +847 -0
- package/dist/src/index.js +629 -0
- package/dist/src/index.js.map +21 -0
- package/dist/src/postconditions/index.cjs +3 -0
- package/dist/src/postconditions/index.cjs.map +9 -0
- package/dist/src/postconditions/index.d.cts +91 -0
- package/dist/src/postconditions/index.d.ts +91 -0
- package/dist/src/postconditions/index.js +12 -0
- package/dist/src/postconditions/index.js.map +9 -0
- package/dist/src/pox/index.cjs +860 -0
- package/dist/src/pox/index.cjs.map +15 -0
- package/dist/src/pox/index.d.cts +244 -0
- package/dist/src/pox/index.d.ts +244 -0
- package/dist/src/pox/index.js +877 -0
- package/dist/src/pox/index.js.map +15 -0
- package/dist/src/stackingdao/index.cjs +300 -0
- package/dist/src/stackingdao/index.cjs.map +13 -0
- package/dist/src/stackingdao/index.d.cts +179 -0
- package/dist/src/stackingdao/index.d.ts +179 -0
- package/dist/src/stackingdao/index.js +318 -0
- package/dist/src/stackingdao/index.js.map +13 -0
- package/dist/src/subscriptions/index.cjs +14 -0
- package/dist/src/subscriptions/index.cjs.map +9 -0
- package/dist/src/subscriptions/index.d.cts +250 -0
- package/dist/src/subscriptions/index.d.ts +250 -0
- package/dist/src/subscriptions/index.js +21 -0
- package/dist/src/subscriptions/index.js.map +9 -0
- package/dist/src/transactions/index.cjs +50 -0
- package/dist/src/transactions/index.cjs.map +9 -0
- package/dist/src/transactions/index.d.cts +412 -0
- package/dist/src/transactions/index.d.ts +412 -0
- package/dist/src/transactions/index.js +101 -0
- package/dist/src/transactions/index.js.map +9 -0
- package/dist/src/utils/index.cjs +168 -0
- package/dist/src/utils/index.cjs.map +11 -0
- package/dist/src/utils/index.d.cts +140 -0
- package/dist/src/utils/index.d.ts +140 -0
- package/dist/src/utils/index.js +222 -0
- package/dist/src/utils/index.js.map +11 -0
- package/package.json +105 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// src/clarity/deserialize.ts
|
|
2
|
+
class BytesReader {
|
|
3
|
+
data;
|
|
4
|
+
offset = 0;
|
|
5
|
+
constructor(data) {
|
|
6
|
+
this.data = data;
|
|
7
|
+
}
|
|
8
|
+
readUInt8() {
|
|
9
|
+
const value = this.data[this.offset];
|
|
10
|
+
this.offset += 1;
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
readUInt32BE() {
|
|
14
|
+
const value = (this.data[this.offset] << 24 | this.data[this.offset + 1] << 16 | this.data[this.offset + 2] << 8 | this.data[this.offset + 3]) >>> 0;
|
|
15
|
+
this.offset += 4;
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
readBytes(length) {
|
|
19
|
+
const slice = this.data.slice(this.offset, this.offset + length);
|
|
20
|
+
this.offset += length;
|
|
21
|
+
return slice;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function readAddress(reader) {
|
|
25
|
+
const version = reader.readUInt8();
|
|
26
|
+
const hash160 = bytesToHex(reader.readBytes(20));
|
|
27
|
+
return c32address(version, hash160);
|
|
28
|
+
}
|
|
29
|
+
function readLPString(reader, prefixBytes = 1) {
|
|
30
|
+
let length = 0;
|
|
31
|
+
for (let i = 0;i < prefixBytes; i++) {
|
|
32
|
+
length = length << 8 | reader.readUInt8();
|
|
33
|
+
}
|
|
34
|
+
return bytesToUtf8(reader.readBytes(length));
|
|
35
|
+
}
|
|
36
|
+
function readCV(reader) {
|
|
37
|
+
const typeByte = reader.readUInt8();
|
|
38
|
+
const type = clarityTypeFromByte(typeByte);
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "int":
|
|
41
|
+
return intCV(bytesToTwosBigInt(reader.readBytes(16)));
|
|
42
|
+
case "uint":
|
|
43
|
+
return uintCV(reader.readBytes(16));
|
|
44
|
+
case "true":
|
|
45
|
+
return trueCV();
|
|
46
|
+
case "false":
|
|
47
|
+
return falseCV();
|
|
48
|
+
case "buffer": {
|
|
49
|
+
const len = reader.readUInt32BE();
|
|
50
|
+
return bufferCV(reader.readBytes(len));
|
|
51
|
+
}
|
|
52
|
+
case "none":
|
|
53
|
+
return noneCV();
|
|
54
|
+
case "some":
|
|
55
|
+
return someCV(readCV(reader));
|
|
56
|
+
case "ok":
|
|
57
|
+
return responseOkCV(readCV(reader));
|
|
58
|
+
case "err":
|
|
59
|
+
return responseErrorCV(readCV(reader));
|
|
60
|
+
case "address":
|
|
61
|
+
return standardPrincipalCV(readAddress(reader));
|
|
62
|
+
case "contract": {
|
|
63
|
+
const addr = readAddress(reader);
|
|
64
|
+
const name = readLPString(reader);
|
|
65
|
+
return contractPrincipalCV(addr, name);
|
|
66
|
+
}
|
|
67
|
+
case "list": {
|
|
68
|
+
const len = reader.readUInt32BE();
|
|
69
|
+
const items = [];
|
|
70
|
+
for (let i = 0;i < len; i++) {
|
|
71
|
+
items.push(readCV(reader));
|
|
72
|
+
}
|
|
73
|
+
return listCV(items);
|
|
74
|
+
}
|
|
75
|
+
case "tuple": {
|
|
76
|
+
const len = reader.readUInt32BE();
|
|
77
|
+
const data = {};
|
|
78
|
+
for (let i = 0;i < len; i++) {
|
|
79
|
+
const key = readLPString(reader);
|
|
80
|
+
data[key] = readCV(reader);
|
|
81
|
+
}
|
|
82
|
+
return tupleCV(data);
|
|
83
|
+
}
|
|
84
|
+
case "ascii": {
|
|
85
|
+
const len = reader.readUInt32BE();
|
|
86
|
+
return stringAsciiCV(bytesToAscii(reader.readBytes(len)));
|
|
87
|
+
}
|
|
88
|
+
case "utf8": {
|
|
89
|
+
const len = reader.readUInt32BE();
|
|
90
|
+
return stringUtf8CV(bytesToUtf8(reader.readBytes(len)));
|
|
91
|
+
}
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(`Cannot deserialize unknown clarity type: ${type}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function deserializeCVBytes(input) {
|
|
97
|
+
const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
|
|
98
|
+
return readCV(new BytesReader(bytes));
|
|
99
|
+
}
|
|
100
|
+
function deserializeCV(input) {
|
|
101
|
+
return deserializeCVBytes(input);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/clarity/values.ts
|
|
105
|
+
var MAX_U128 = BigInt("0xffffffffffffffffffffffffffffffff");
|
|
106
|
+
var MIN_I128 = BigInt("-170141183460469231731687303715884105728");
|
|
107
|
+
var MAX_I128 = BigInt("0x7fffffffffffffffffffffffffffffff");
|
|
108
|
+
function intCV(value) {
|
|
109
|
+
if (typeof value === "string" && value.toLowerCase().startsWith("0x")) {
|
|
110
|
+
value = bytesToTwosBigInt(hexToBytes(value));
|
|
111
|
+
}
|
|
112
|
+
if (value instanceof Uint8Array)
|
|
113
|
+
value = bytesToTwosBigInt(value);
|
|
114
|
+
const n = intToBigInt(value);
|
|
115
|
+
if (n > MAX_I128)
|
|
116
|
+
throw new RangeError(`Int exceeds max i128: ${MAX_I128}`);
|
|
117
|
+
if (n < MIN_I128)
|
|
118
|
+
throw new RangeError(`Int below min i128: ${MIN_I128}`);
|
|
119
|
+
return { type: "int", value: n };
|
|
120
|
+
}
|
|
121
|
+
function uintCV(value) {
|
|
122
|
+
const n = intToBigInt(value);
|
|
123
|
+
if (n < 0n)
|
|
124
|
+
throw new RangeError("Cannot construct unsigned int from negative value");
|
|
125
|
+
if (n > MAX_U128)
|
|
126
|
+
throw new RangeError(`UInt exceeds max u128: ${MAX_U128}`);
|
|
127
|
+
return { type: "uint", value: n };
|
|
128
|
+
}
|
|
129
|
+
var trueCV = () => ({ type: "true" });
|
|
130
|
+
var falseCV = () => ({ type: "false" });
|
|
131
|
+
var boolCV = (v) => v ? trueCV() : falseCV();
|
|
132
|
+
function bufferCV(buffer) {
|
|
133
|
+
if (buffer.byteLength > 1048576) {
|
|
134
|
+
throw new Error("Buffer exceeds max size of 1MB");
|
|
135
|
+
}
|
|
136
|
+
return { type: "buffer", value: bytesToHex(buffer) };
|
|
137
|
+
}
|
|
138
|
+
function standardPrincipalCV(address) {
|
|
139
|
+
const [version, hash160] = c32addressDecode(address);
|
|
140
|
+
const normalized = c32address(version, hash160);
|
|
141
|
+
return { type: "address", value: normalized };
|
|
142
|
+
}
|
|
143
|
+
function contractPrincipalCV(address, contractName) {
|
|
144
|
+
const [version, hash160] = c32addressDecode(address);
|
|
145
|
+
const normalized = c32address(version, hash160);
|
|
146
|
+
if (utf8ToBytes(contractName).byteLength >= 128) {
|
|
147
|
+
throw new Error("Contract name must be less than 128 bytes");
|
|
148
|
+
}
|
|
149
|
+
return { type: "contract", value: `${normalized}.${contractName}` };
|
|
150
|
+
}
|
|
151
|
+
function noneCV() {
|
|
152
|
+
return { type: "none" };
|
|
153
|
+
}
|
|
154
|
+
function someCV(value) {
|
|
155
|
+
return { type: "some", value };
|
|
156
|
+
}
|
|
157
|
+
function responseOkCV(value) {
|
|
158
|
+
return { type: "ok", value };
|
|
159
|
+
}
|
|
160
|
+
function responseErrorCV(value) {
|
|
161
|
+
return { type: "err", value };
|
|
162
|
+
}
|
|
163
|
+
function listCV(values) {
|
|
164
|
+
return { type: "list", value: values };
|
|
165
|
+
}
|
|
166
|
+
function tupleCV(data) {
|
|
167
|
+
for (const key in data) {
|
|
168
|
+
if (!isClarityName(key)) {
|
|
169
|
+
throw new Error(`"${key}" is not a valid Clarity name`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return { type: "tuple", value: data };
|
|
173
|
+
}
|
|
174
|
+
function stringAsciiCV(value) {
|
|
175
|
+
return { type: "ascii", value };
|
|
176
|
+
}
|
|
177
|
+
function stringUtf8CV(value) {
|
|
178
|
+
return { type: "utf8", value };
|
|
179
|
+
}
|
|
180
|
+
var Cl = {
|
|
181
|
+
int: intCV,
|
|
182
|
+
uint: uintCV,
|
|
183
|
+
bool: boolCV,
|
|
184
|
+
principal(address) {
|
|
185
|
+
const [addr, name] = address.split(".");
|
|
186
|
+
if (!addr)
|
|
187
|
+
throw new Error("Invalid principal address");
|
|
188
|
+
return name ? contractPrincipalCV(addr, name) : standardPrincipalCV(addr);
|
|
189
|
+
},
|
|
190
|
+
address(address) {
|
|
191
|
+
return Cl.principal(address);
|
|
192
|
+
},
|
|
193
|
+
contractPrincipal: contractPrincipalCV,
|
|
194
|
+
standardPrincipal: standardPrincipalCV,
|
|
195
|
+
buffer: bufferCV,
|
|
196
|
+
bufferFromHex: (hex) => bufferCV(hexToBytes(hex)),
|
|
197
|
+
bufferFromAscii: (ascii) => bufferCV(asciiToBytes(ascii)),
|
|
198
|
+
bufferFromUtf8: (utf8) => bufferCV(utf8ToBytes(utf8)),
|
|
199
|
+
none: noneCV,
|
|
200
|
+
some: someCV,
|
|
201
|
+
ok: responseOkCV,
|
|
202
|
+
error: responseErrorCV,
|
|
203
|
+
list: listCV,
|
|
204
|
+
tuple: tupleCV,
|
|
205
|
+
stringAscii: stringAsciiCV,
|
|
206
|
+
stringUtf8: stringUtf8CV,
|
|
207
|
+
serialize(value) {
|
|
208
|
+
return bytesToHex(serializeCVBytes(value));
|
|
209
|
+
},
|
|
210
|
+
deserialize: deserializeCVBytes
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
//# debugId=9E0A409BD71E7ABA64756E2164756E21
|
|
214
|
+
//# sourceMappingURL=stacks-b008c3w8.cjs.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/clarity/deserialize.ts", "../src/clarity/values.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n hexToBytes,\n bytesToHex,\n bytesToAscii,\n bytesToUtf8,\n bytesToTwosBigInt,\n without0x,\n} from \"../utils/encoding.ts\";\nimport { c32address } from \"../utils/c32.ts\";\nimport {\n clarityTypeFromByte,\n type ClarityValue,\n} from \"./types.ts\";\nimport {\n intCV,\n uintCV,\n trueCV,\n falseCV,\n bufferCV,\n noneCV,\n someCV,\n responseOkCV,\n responseErrorCV,\n standardPrincipalCV,\n contractPrincipalCV,\n listCV,\n tupleCV,\n stringAsciiCV,\n stringUtf8CV,\n} from \"./values.ts\";\n\nclass BytesReader {\n private data: Uint8Array;\n private offset = 0;\n\n constructor(data: Uint8Array) {\n this.data = data;\n }\n\n readUInt8(): number {\n const value = this.data[this.offset]!;\n this.offset += 1;\n return value;\n }\n\n readUInt32BE(): number {\n const value =\n ((this.data[this.offset]! << 24) |\n (this.data[this.offset + 1]! << 16) |\n (this.data[this.offset + 2]! << 8) |\n this.data[this.offset + 3]!) >>>\n 0;\n this.offset += 4;\n return value;\n }\n\n readBytes(length: number): Uint8Array {\n const slice = this.data.slice(this.offset, this.offset + length);\n this.offset += length;\n return slice;\n }\n}\n\nfunction readAddress(reader: BytesReader): string {\n const version = reader.readUInt8();\n const hash160 = bytesToHex(reader.readBytes(20));\n return c32address(version, hash160);\n}\n\nfunction readLPString(reader: BytesReader, prefixBytes = 1): string {\n let length = 0;\n for (let i = 0; i < prefixBytes; i++) {\n length = (length << 8) | reader.readUInt8();\n }\n return bytesToUtf8(reader.readBytes(length));\n}\n\nfunction readCV(reader: BytesReader): ClarityValue {\n const typeByte = reader.readUInt8();\n const type = clarityTypeFromByte(typeByte);\n\n switch (type) {\n case \"int\":\n return intCV(bytesToTwosBigInt(reader.readBytes(16)));\n\n case \"uint\":\n return uintCV(reader.readBytes(16));\n\n case \"true\":\n return trueCV();\n\n case \"false\":\n return falseCV();\n\n case \"buffer\": {\n const len = reader.readUInt32BE();\n return bufferCV(reader.readBytes(len));\n }\n\n case \"none\":\n return noneCV();\n\n case \"some\":\n return someCV(readCV(reader));\n\n case \"ok\":\n return responseOkCV(readCV(reader));\n\n case \"err\":\n return responseErrorCV(readCV(reader));\n\n case \"address\":\n return standardPrincipalCV(readAddress(reader));\n\n case \"contract\": {\n const addr = readAddress(reader);\n const name = readLPString(reader);\n return contractPrincipalCV(addr, name);\n }\n\n case \"list\": {\n const len = reader.readUInt32BE();\n const items: ClarityValue[] = [];\n for (let i = 0; i < len; i++) {\n items.push(readCV(reader));\n }\n return listCV(items);\n }\n\n case \"tuple\": {\n const len = reader.readUInt32BE();\n const data: Record<string, ClarityValue> = {};\n for (let i = 0; i < len; i++) {\n const key = readLPString(reader);\n data[key] = readCV(reader);\n }\n return tupleCV(data);\n }\n\n case \"ascii\": {\n const len = reader.readUInt32BE();\n return stringAsciiCV(bytesToAscii(reader.readBytes(len)));\n }\n\n case \"utf8\": {\n const len = reader.readUInt32BE();\n return stringUtf8CV(bytesToUtf8(reader.readBytes(len)));\n }\n\n default:\n throw new Error(`Cannot deserialize unknown clarity type: ${type}`);\n }\n}\n\nexport function deserializeCVBytes<T extends ClarityValue = ClarityValue>(\n input: Uint8Array | string\n): T {\n const bytes =\n typeof input === \"string\" ? hexToBytes(without0x(input)) : input;\n return readCV(new BytesReader(bytes)) as T;\n}\n\nexport function deserializeCV<T extends ClarityValue = ClarityValue>(\n input: Uint8Array | string\n): T {\n return deserializeCVBytes(input);\n}\n",
|
|
6
|
+
"import { c32address, c32addressDecode } from \"../utils/c32.ts\";\nimport {\n bytesToHex,\n hexToBytes,\n asciiToBytes,\n utf8ToBytes,\n intToBigInt,\n bytesToTwosBigInt,\n type IntegerType,\n} from \"../utils/encoding.ts\";\nimport { isClarityName } from \"../utils/address.ts\";\nimport type {\n IntCV,\n UIntCV,\n BooleanCV,\n TrueCV,\n FalseCV,\n BufferCV,\n NoneCV,\n SomeCV,\n OptionalCV,\n ResponseOkCV,\n ResponseErrorCV,\n StandardPrincipalCV,\n ContractPrincipalCV,\n ListCV,\n TupleCV,\n TupleData,\n StringAsciiCV,\n StringUtf8CV,\n ClarityValue,\n} from \"./types.ts\";\nimport { serializeCVBytes } from \"./serialize.ts\";\nimport { deserializeCVBytes } from \"./deserialize.ts\";\n\nconst MAX_U128 = BigInt(\"0xffffffffffffffffffffffffffffffff\");\nconst MIN_I128 = BigInt(\"-170141183460469231731687303715884105728\");\nconst MAX_I128 = BigInt(\"0x7fffffffffffffffffffffffffffffff\");\n\n// Primitives\n\nexport function intCV(value: IntegerType): IntCV {\n if (typeof value === \"string\" && value.toLowerCase().startsWith(\"0x\")) {\n value = bytesToTwosBigInt(hexToBytes(value));\n }\n if (value instanceof Uint8Array) value = bytesToTwosBigInt(value);\n const n = intToBigInt(value);\n if (n > MAX_I128) throw new RangeError(`Int exceeds max i128: ${MAX_I128}`);\n if (n < MIN_I128) throw new RangeError(`Int below min i128: ${MIN_I128}`);\n return { type: \"int\", value: n };\n}\n\nexport function uintCV(value: IntegerType): UIntCV {\n const n = intToBigInt(value);\n if (n < 0n) throw new RangeError(\"Cannot construct unsigned int from negative value\");\n if (n > MAX_U128) throw new RangeError(`UInt exceeds max u128: ${MAX_U128}`);\n return { type: \"uint\", value: n };\n}\n\nexport const trueCV = (): TrueCV => ({ type: \"true\" });\nexport const falseCV = (): FalseCV => ({ type: \"false\" });\nexport const boolCV = (v: boolean): BooleanCV => (v ? trueCV() : falseCV());\n\nexport function bufferCV(buffer: Uint8Array): BufferCV {\n if (buffer.byteLength > 1_048_576) {\n throw new Error(\"Buffer exceeds max size of 1MB\");\n }\n return { type: \"buffer\", value: bytesToHex(buffer) };\n}\n\nexport function standardPrincipalCV(address: string): StandardPrincipalCV {\n // Validate by decoding\n const [version, hash160] = c32addressDecode(address);\n const normalized = c32address(version, hash160);\n return { type: \"address\", value: normalized };\n}\n\nexport function contractPrincipalCV(\n address: string,\n contractName: string\n): ContractPrincipalCV {\n const [version, hash160] = c32addressDecode(address);\n const normalized = c32address(version, hash160);\n if (utf8ToBytes(contractName).byteLength >= 128) {\n throw new Error(\"Contract name must be less than 128 bytes\");\n }\n return { type: \"contract\", value: `${normalized}.${contractName}` };\n}\n\nexport function noneCV(): NoneCV {\n return { type: \"none\" };\n}\n\nexport function someCV(value: ClarityValue): SomeCV {\n return { type: \"some\", value };\n}\n\nexport function responseOkCV(value: ClarityValue): ResponseOkCV {\n return { type: \"ok\", value };\n}\n\nexport function responseErrorCV(value: ClarityValue): ResponseErrorCV {\n return { type: \"err\", value };\n}\n\nexport function listCV(values: ClarityValue[]): ListCV {\n return { type: \"list\", value: values };\n}\n\nexport function tupleCV(data: TupleData): TupleCV {\n for (const key in data) {\n if (!isClarityName(key)) {\n throw new Error(`\"${key}\" is not a valid Clarity name`);\n }\n }\n return { type: \"tuple\", value: data };\n}\n\nexport function stringAsciiCV(value: string): StringAsciiCV {\n return { type: \"ascii\", value };\n}\n\nexport function stringUtf8CV(value: string): StringUtf8CV {\n return { type: \"utf8\", value };\n}\n\n// Cl namespace — clean API\n\nexport const Cl = {\n int: intCV,\n uint: uintCV,\n bool: boolCV,\n principal(address: string): StandardPrincipalCV | ContractPrincipalCV {\n const [addr, name] = address.split(\".\");\n if (!addr) throw new Error(\"Invalid principal address\");\n return name ? contractPrincipalCV(addr, name) : standardPrincipalCV(addr);\n },\n address(address: string) {\n return Cl.principal(address);\n },\n contractPrincipal: contractPrincipalCV,\n standardPrincipal: standardPrincipalCV,\n buffer: bufferCV,\n bufferFromHex: (hex: string) => bufferCV(hexToBytes(hex)),\n bufferFromAscii: (ascii: string) => bufferCV(asciiToBytes(ascii)),\n bufferFromUtf8: (utf8: string) => bufferCV(utf8ToBytes(utf8)),\n none: noneCV,\n some: someCV,\n ok: responseOkCV,\n error: responseErrorCV,\n list: listCV,\n tuple: tupleCV,\n stringAscii: stringAsciiCV,\n stringUtf8: stringUtf8CV,\n serialize(value: ClarityValue): string {\n return bytesToHex(serializeCVBytes(value));\n },\n deserialize: deserializeCVBytes,\n} as const;\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AA+BA,MAAM,YAAY;AAAA,EACR;AAAA,EACA,SAAS;AAAA,EAEjB,WAAW,CAAC,MAAkB;AAAA,IAC5B,KAAK,OAAO;AAAA;AAAA,EAGd,SAAS,GAAW;AAAA,IAClB,MAAM,QAAQ,KAAK,KAAK,KAAK;AAAA,IAC7B,KAAK,UAAU;AAAA,IACf,OAAO;AAAA;AAAA,EAGT,YAAY,GAAW;AAAA,IACrB,MAAM,SACF,KAAK,KAAK,KAAK,WAAY,KAC1B,KAAK,KAAK,KAAK,SAAS,MAAO,KAC/B,KAAK,KAAK,KAAK,SAAS,MAAO,IAChC,KAAK,KAAK,KAAK,SAAS,QAC1B;AAAA,IACF,KAAK,UAAU;AAAA,IACf,OAAO;AAAA;AAAA,EAGT,SAAS,CAAC,QAA4B;AAAA,IACpC,MAAM,QAAQ,KAAK,KAAK,MAAM,KAAK,QAAQ,KAAK,SAAS,MAAM;AAAA,IAC/D,KAAK,UAAU;AAAA,IACf,OAAO;AAAA;AAEX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IAAI,IAAI,oBAAoB,IAAI;AAAA;AAAA,EAE1E,OAAO,CAAC,SAAiB;AAAA,IACvB,OAAO,GAAG,UAAU,OAAO;AAAA;AAAA,EAE7B,mBAAmB;AAAA,EACnB,mBAAmB;AAAA,EACnB,QAAQ;AAAA,EACR,eAAe,CAAC,QAAgB,SAAS,WAAW,GAAG,CAAC;AAAA,EACxD,iBAAiB,CAAC,UAAkB,SAAS,aAAa,KAAK,CAAC;AAAA,EAChE,gBAAgB,CAAC,SAAiB,SAAS,YAAY,IAAI,CAAC;AAAA,EAC5D,MAAM;AAAA,EACN,MAAM;AAAA,EACN,IAAI;AAAA,EACJ,OAAO;AAAA,EACP,MAAM;AAAA,EACN,OAAO;AAAA,EACP,aAAa;AAAA,EACb,YAAY;AAAA,EACZ,SAAS,CAAC,OAA6B;AAAA,IACrC,OAAO,WAAW,iBAAiB,KAAK,CAAC;AAAA;AAAA,EAE3C,aAAa;AACf;",
|
|
9
|
+
"debugId": "9E0A409BD71E7ABA64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
var import_node_module = require("node:module");
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//# debugId=5555BAAAF1009B9864756E2164756E21
|
|
31
|
+
//# sourceMappingURL=stacks-bnvfmt7y.cjs.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bytesToHex
|
|
3
|
+
} from "./stacks-nqytwfcc.js";
|
|
4
|
+
|
|
5
|
+
// src/utils/hash.ts
|
|
6
|
+
import { ripemd160 } from "@noble/hashes/legacy.js";
|
|
7
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
8
|
+
import { sha512_256 } from "@noble/hashes/sha2.js";
|
|
9
|
+
function hash160(input) {
|
|
10
|
+
return ripemd160(sha256(input));
|
|
11
|
+
}
|
|
12
|
+
function txidFromBytes(data) {
|
|
13
|
+
return bytesToHex(sha512_256(data));
|
|
14
|
+
}
|
|
15
|
+
function hashP2PKH(input) {
|
|
16
|
+
return bytesToHex(hash160(input));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { ripemd160, sha256, sha512_256, hash160, txidFromBytes, hashP2PKH };
|
|
20
|
+
|
|
21
|
+
//# debugId=E53549EA02A4B7DB64756E2164756E21
|
|
22
|
+
//# sourceMappingURL=stacks-bycy6t9k.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/utils/hash.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { ripemd160 } from \"@noble/hashes/legacy.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { sha512_256 } from \"@noble/hashes/sha2.js\";\nimport { bytesToHex } from \"./encoding.ts\";\n\nexport { sha256, sha512_256, ripemd160 };\n\n/** RIPEMD160(SHA256(input)) — standard Bitcoin/Stacks address hash */\nexport function hash160(input: Uint8Array): Uint8Array {\n return ripemd160(sha256(input));\n}\n\n/** Hash used for transaction IDs */\nexport function txidFromBytes(data: Uint8Array): string {\n return bytesToHex(sha512_256(data));\n}\n\n/** hash160 as hex — used for P2PKH address derivation */\nexport function hashP2PKH(input: Uint8Array): string {\n return bytesToHex(hash160(input));\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;AAAA;AACA;AACA;AAMO,SAAS,OAAO,CAAC,OAA+B;AAAA,EACrD,OAAO,UAAU,OAAO,KAAK,CAAC;AAAA;AAIzB,SAAS,aAAa,CAAC,MAA0B;AAAA,EACtD,OAAO,WAAW,WAAW,IAAI,CAAC;AAAA;AAI7B,SAAS,SAAS,CAAC,OAA2B;AAAA,EACnD,OAAO,WAAW,QAAQ,KAAK,CAAC;AAAA;",
|
|
8
|
+
"debugId": "E53549EA02A4B7DB64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Cl,
|
|
3
|
+
boolCV,
|
|
4
|
+
bufferCV,
|
|
5
|
+
intCV,
|
|
6
|
+
listCV,
|
|
7
|
+
noneCV,
|
|
8
|
+
responseErrorCV,
|
|
9
|
+
responseOkCV,
|
|
10
|
+
someCV,
|
|
11
|
+
stringAsciiCV,
|
|
12
|
+
stringUtf8CV,
|
|
13
|
+
tupleCV,
|
|
14
|
+
uintCV
|
|
15
|
+
} from "./stacks-j2cy7qtt.js";
|
|
16
|
+
import {
|
|
17
|
+
MAX_I128,
|
|
18
|
+
MAX_U128,
|
|
19
|
+
MIN_I128
|
|
20
|
+
} from "./stacks-v9jqcpd4.js";
|
|
21
|
+
import {
|
|
22
|
+
validateStacksAddress
|
|
23
|
+
} from "./stacks-gjvbq6w7.js";
|
|
24
|
+
|
|
25
|
+
// src/clarity/abi/types.ts
|
|
26
|
+
var CONTRACT_NAME_REGEX = /^[a-zA-Z][a-zA-Z0-9\-]{0,127}$/;
|
|
27
|
+
|
|
28
|
+
// src/clarity/abi/guards.ts
|
|
29
|
+
function isUint128(value) {
|
|
30
|
+
return typeof value === "bigint" && value >= 0n && value <= MAX_U128;
|
|
31
|
+
}
|
|
32
|
+
function isInt128(value) {
|
|
33
|
+
return typeof value === "bigint" && value >= MIN_I128 && value <= MAX_I128;
|
|
34
|
+
}
|
|
35
|
+
function isBool(value) {
|
|
36
|
+
return typeof value === "boolean";
|
|
37
|
+
}
|
|
38
|
+
function isPrincipal(value) {
|
|
39
|
+
if (typeof value !== "string")
|
|
40
|
+
return false;
|
|
41
|
+
const parts = value.split(".");
|
|
42
|
+
const address = parts[0];
|
|
43
|
+
const contractName = parts[1];
|
|
44
|
+
if (!validateStacksAddress(address))
|
|
45
|
+
return false;
|
|
46
|
+
if (contractName !== undefined) {
|
|
47
|
+
return CONTRACT_NAME_REGEX.test(contractName);
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
function isStandardPrincipal(value) {
|
|
52
|
+
if (typeof value !== "string")
|
|
53
|
+
return false;
|
|
54
|
+
if (value.includes("."))
|
|
55
|
+
return false;
|
|
56
|
+
return validateStacksAddress(value);
|
|
57
|
+
}
|
|
58
|
+
function isContractPrincipal(value) {
|
|
59
|
+
if (typeof value !== "string")
|
|
60
|
+
return false;
|
|
61
|
+
if (!value.includes("."))
|
|
62
|
+
return false;
|
|
63
|
+
const parts = value.split(".");
|
|
64
|
+
const address = parts[0];
|
|
65
|
+
const contractName = parts[1];
|
|
66
|
+
if (!validateStacksAddress(address))
|
|
67
|
+
return false;
|
|
68
|
+
return CONTRACT_NAME_REGEX.test(contractName);
|
|
69
|
+
}
|
|
70
|
+
function isTraitReference(value) {
|
|
71
|
+
return isContractPrincipal(value);
|
|
72
|
+
}
|
|
73
|
+
function isString(value) {
|
|
74
|
+
return typeof value === "string";
|
|
75
|
+
}
|
|
76
|
+
function isUint8Array(value) {
|
|
77
|
+
return value instanceof Uint8Array;
|
|
78
|
+
}
|
|
79
|
+
function isArray(value, itemGuard) {
|
|
80
|
+
return Array.isArray(value) && value.every(itemGuard);
|
|
81
|
+
}
|
|
82
|
+
function isOptional(value, guard) {
|
|
83
|
+
return value === null || guard(value);
|
|
84
|
+
}
|
|
85
|
+
function isOkResponse(response) {
|
|
86
|
+
return "ok" in response && !("err" in response);
|
|
87
|
+
}
|
|
88
|
+
function isErrResponse(response) {
|
|
89
|
+
return "err" in response && !("ok" in response);
|
|
90
|
+
}
|
|
91
|
+
function isResponse(value, okGuard, errGuard) {
|
|
92
|
+
if (typeof value !== "object" || value === null)
|
|
93
|
+
return false;
|
|
94
|
+
const obj = value;
|
|
95
|
+
if ("ok" in value && !("err" in value))
|
|
96
|
+
return okGuard(obj.ok);
|
|
97
|
+
if ("err" in value && !("ok" in value))
|
|
98
|
+
return errGuard(obj.err);
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
function isAbiList(type) {
|
|
102
|
+
return typeof type === "object" && type !== null && "list" in type;
|
|
103
|
+
}
|
|
104
|
+
function isAbiTuple(type) {
|
|
105
|
+
return typeof type === "object" && type !== null && "tuple" in type;
|
|
106
|
+
}
|
|
107
|
+
function isAbiOptional(type) {
|
|
108
|
+
return typeof type === "object" && type !== null && "optional" in type;
|
|
109
|
+
}
|
|
110
|
+
function isAbiResponse(type) {
|
|
111
|
+
return typeof type === "object" && type !== null && "response" in type;
|
|
112
|
+
}
|
|
113
|
+
function isAbiBuffer(type) {
|
|
114
|
+
return typeof type === "object" && type !== null && "buff" in type;
|
|
115
|
+
}
|
|
116
|
+
function isAbiStringAscii(type) {
|
|
117
|
+
return typeof type === "object" && type !== null && "string-ascii" in type;
|
|
118
|
+
}
|
|
119
|
+
function isAbiStringUtf8(type) {
|
|
120
|
+
return typeof type === "object" && type !== null && "string-utf8" in type;
|
|
121
|
+
}
|
|
122
|
+
function isAbiTraitReference(type) {
|
|
123
|
+
return type === "trait_reference";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/clarity/abi/utils.ts
|
|
127
|
+
function toCamelCase(str) {
|
|
128
|
+
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/-([A-Z])/g, (_, letter) => letter).replace(/-(\d)/g, (_, digit) => digit).replace(/-/g, "").replace(/^\d/, "_$&");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/clarity/bridge.ts
|
|
132
|
+
function jsToClarityValue(abiType, value) {
|
|
133
|
+
if (abiType === "uint128")
|
|
134
|
+
return uintCV(value);
|
|
135
|
+
if (abiType === "int128")
|
|
136
|
+
return intCV(value);
|
|
137
|
+
if (abiType === "bool")
|
|
138
|
+
return boolCV(value);
|
|
139
|
+
if (abiType === "principal" || abiType === "trait_reference")
|
|
140
|
+
return Cl.principal(value);
|
|
141
|
+
if (isAbiStringAscii(abiType))
|
|
142
|
+
return stringAsciiCV(value);
|
|
143
|
+
if (isAbiStringUtf8(abiType))
|
|
144
|
+
return stringUtf8CV(value);
|
|
145
|
+
if (isAbiBuffer(abiType))
|
|
146
|
+
return bufferCV(value);
|
|
147
|
+
if (isAbiList(abiType)) {
|
|
148
|
+
const arr = value;
|
|
149
|
+
return listCV(arr.map((item) => jsToClarityValue(abiType.list.type, item)));
|
|
150
|
+
}
|
|
151
|
+
if (isAbiTuple(abiType)) {
|
|
152
|
+
const obj = value;
|
|
153
|
+
const data = {};
|
|
154
|
+
for (const field of abiType.tuple) {
|
|
155
|
+
const camelKey = toCamelCase(field.name);
|
|
156
|
+
const hasOriginal = field.name in obj;
|
|
157
|
+
const hasCamel = camelKey in obj;
|
|
158
|
+
const fieldValue = hasOriginal ? obj[field.name] : hasCamel ? obj[camelKey] : undefined;
|
|
159
|
+
if (fieldValue === undefined && !isAbiOptional(field.type)) {
|
|
160
|
+
throw new Error(`Missing tuple field: ${field.name}`);
|
|
161
|
+
}
|
|
162
|
+
data[field.name] = jsToClarityValue(field.type, fieldValue);
|
|
163
|
+
}
|
|
164
|
+
return tupleCV(data);
|
|
165
|
+
}
|
|
166
|
+
if (isAbiOptional(abiType)) {
|
|
167
|
+
if (value === null || value === undefined)
|
|
168
|
+
return noneCV();
|
|
169
|
+
return someCV(jsToClarityValue(abiType.optional, value));
|
|
170
|
+
}
|
|
171
|
+
if (isAbiResponse(abiType)) {
|
|
172
|
+
const obj = value;
|
|
173
|
+
if ("ok" in obj && !("err" in obj))
|
|
174
|
+
return responseOkCV(jsToClarityValue(abiType.response.ok, obj.ok));
|
|
175
|
+
if ("err" in obj && !("ok" in obj))
|
|
176
|
+
return responseErrorCV(jsToClarityValue(abiType.response.error, obj.err));
|
|
177
|
+
throw new Error("Response must have exactly 'ok' or 'err' property");
|
|
178
|
+
}
|
|
179
|
+
throw new Error(`Unknown ABI type: ${JSON.stringify(abiType)}`);
|
|
180
|
+
}
|
|
181
|
+
function clarityValueToJS(abiType, cv) {
|
|
182
|
+
return clarityValueToJSInner(abiType, cv);
|
|
183
|
+
}
|
|
184
|
+
function clarityValueToJSUntyped(abiType, cv) {
|
|
185
|
+
return clarityValueToJSInner(abiType, cv);
|
|
186
|
+
}
|
|
187
|
+
function clarityValueToJSInner(abiType, cv) {
|
|
188
|
+
if (abiType === "uint128" || abiType === "int128") {
|
|
189
|
+
if (cv.type !== "int" && cv.type !== "uint")
|
|
190
|
+
throw new Error(`Expected int/uint CV, got ${cv.type}`);
|
|
191
|
+
return cv.value;
|
|
192
|
+
}
|
|
193
|
+
if (abiType === "bool") {
|
|
194
|
+
if (cv.type === "true")
|
|
195
|
+
return true;
|
|
196
|
+
if (cv.type === "false")
|
|
197
|
+
return false;
|
|
198
|
+
throw new Error(`Expected bool CV, got ${cv.type}`);
|
|
199
|
+
}
|
|
200
|
+
if (abiType === "principal" || abiType === "trait_reference") {
|
|
201
|
+
if (cv.type !== "address" && cv.type !== "contract")
|
|
202
|
+
throw new Error(`Expected principal CV, got ${cv.type}`);
|
|
203
|
+
return cv.value;
|
|
204
|
+
}
|
|
205
|
+
if (isAbiStringAscii(abiType) || isAbiStringUtf8(abiType)) {
|
|
206
|
+
if (cv.type !== "ascii" && cv.type !== "utf8")
|
|
207
|
+
throw new Error(`Expected string CV, got ${cv.type}`);
|
|
208
|
+
return cv.value;
|
|
209
|
+
}
|
|
210
|
+
if (isAbiBuffer(abiType)) {
|
|
211
|
+
if (cv.type !== "buffer")
|
|
212
|
+
throw new Error(`Expected buffer CV, got ${cv.type}`);
|
|
213
|
+
const hex = cv.value;
|
|
214
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
215
|
+
for (let i = 0;i < bytes.length; i++) {
|
|
216
|
+
bytes[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
|
|
217
|
+
}
|
|
218
|
+
return bytes;
|
|
219
|
+
}
|
|
220
|
+
if (isAbiList(abiType)) {
|
|
221
|
+
if (cv.type !== "list")
|
|
222
|
+
throw new Error(`Expected list CV, got ${cv.type}`);
|
|
223
|
+
return cv.value.map((item) => clarityValueToJSInner(abiType.list.type, item));
|
|
224
|
+
}
|
|
225
|
+
if (isAbiTuple(abiType)) {
|
|
226
|
+
if (cv.type !== "tuple")
|
|
227
|
+
throw new Error(`Expected tuple CV, got ${cv.type}`);
|
|
228
|
+
const result = {};
|
|
229
|
+
for (const field of abiType.tuple) {
|
|
230
|
+
const fieldCV = cv.value[field.name];
|
|
231
|
+
if (!fieldCV)
|
|
232
|
+
throw new Error(`Missing tuple field in CV: ${field.name}`);
|
|
233
|
+
const camelKey = toCamelCase(field.name);
|
|
234
|
+
result[camelKey] = clarityValueToJSInner(field.type, fieldCV);
|
|
235
|
+
}
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
if (isAbiOptional(abiType)) {
|
|
239
|
+
if (cv.type === "none")
|
|
240
|
+
return null;
|
|
241
|
+
if (cv.type === "some")
|
|
242
|
+
return clarityValueToJSInner(abiType.optional, cv.value);
|
|
243
|
+
throw new Error(`Expected optional CV, got ${cv.type}`);
|
|
244
|
+
}
|
|
245
|
+
if (isAbiResponse(abiType)) {
|
|
246
|
+
if (cv.type === "ok")
|
|
247
|
+
return { ok: clarityValueToJSInner(abiType.response.ok, cv.value) };
|
|
248
|
+
if (cv.type === "err")
|
|
249
|
+
return { err: clarityValueToJSInner(abiType.response.error, cv.value) };
|
|
250
|
+
throw new Error(`Expected response CV, got ${cv.type}`);
|
|
251
|
+
}
|
|
252
|
+
throw new Error(`Unknown ABI type: ${JSON.stringify(abiType)}`);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export { CONTRACT_NAME_REGEX, isUint128, isInt128, isBool, isPrincipal, isStandardPrincipal, isContractPrincipal, isTraitReference, isString, isUint8Array, isArray, isOptional, isOkResponse, isErrResponse, isResponse, isAbiList, isAbiTuple, isAbiOptional, isAbiResponse, isAbiBuffer, isAbiStringAscii, isAbiStringUtf8, isAbiTraitReference, toCamelCase, jsToClarityValue, clarityValueToJS, clarityValueToJSUntyped };
|
|
256
|
+
|
|
257
|
+
//# debugId=F8671962BA8FE8C064756E2164756E21
|
|
258
|
+
//# sourceMappingURL=stacks-d1fmkv3p.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/clarity/abi/types.ts", "../src/clarity/abi/guards.ts", "../src/clarity/abi/utils.ts", "../src/clarity/bridge.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { MAX_U128, MAX_I128, MIN_I128 } from \"../../utils/constants.ts\";\n\nexport { MAX_U128, MAX_I128, MIN_I128 };\n\nexport const CONTRACT_NAME_REGEX: RegExp = /^[a-zA-Z][a-zA-Z0-9\\-]{0,127}$/;\n\n// Primitive types\n\nexport type AbiUInt128 = \"uint128\";\nexport type AbiInt128 = \"int128\";\nexport type AbiBool = \"bool\";\nexport type AbiPrincipal = \"principal\";\nexport type AbiTraitReference = \"trait_reference\";\nexport type AbiNone = \"none\";\n\nexport type AbiStringAscii<L extends number = number> = {\n \"string-ascii\": { length: L };\n};\n\nexport type AbiStringUtf8<L extends number = number> = {\n \"string-utf8\": { length: L };\n};\n\nexport type AbiBuffer<L extends number = number> = {\n buff: { length: L };\n};\n\nexport type AbiPrimitiveType =\n | AbiUInt128\n | AbiInt128\n | AbiBool\n | AbiPrincipal\n | AbiTraitReference\n | AbiNone\n | AbiStringAscii\n | AbiStringUtf8\n | AbiBuffer;\n\n// Composite types (interfaces for recursion)\n\nexport interface AbiListType {\n list: { type: AbiType; length: number };\n}\n\nexport interface AbiTupleType {\n tuple: ReadonlyArray<{ name: string; type: AbiType }>;\n}\n\nexport interface AbiOptionalType {\n optional: AbiType;\n}\n\nexport interface AbiResponseType {\n response: { ok: AbiType; error: AbiType };\n}\n\n/** Discriminated union of all Clarity value types (primitives, buffers, lists, tuples, optionals, responses). */\nexport type AbiType =\n | AbiPrimitiveType\n | AbiListType\n | AbiTupleType\n | AbiOptionalType\n | AbiResponseType;\n\n// Generic versions for better type inference\n\nexport type AbiList<\n T extends AbiType = AbiType,\n L extends number = number,\n> = {\n list: { type: T; length: L };\n};\n\nexport type AbiTuple<\n T extends ReadonlyArray<{ name: string; type: AbiType }>,\n> = {\n tuple: T;\n};\n\nexport type AbiOptional<T extends AbiType = AbiType> = {\n optional: T;\n};\n\nexport type AbiResponse<\n O extends AbiType = AbiType,\n E extends AbiType = AbiType,\n> = {\n response: { ok: O; error: E };\n};\n",
|
|
6
|
+
"import { validateStacksAddress } from \"../../utils/address.ts\";\nimport { MAX_U128, MIN_I128, MAX_I128, CONTRACT_NAME_REGEX } from \"./types.ts\";\nimport type { ResponseOk, ResponseErr } from \"./mappings.ts\";\nimport type {\n AbiType,\n AbiListType,\n AbiTupleType,\n AbiOptionalType,\n AbiResponseType,\n AbiBuffer,\n AbiStringAscii,\n AbiStringUtf8,\n AbiTraitReference,\n} from \"./types.ts\";\n\n// Value guards\n\nexport function isUint128(value: unknown): value is bigint {\n return typeof value === \"bigint\" && value >= 0n && value <= MAX_U128;\n}\n\nexport function isInt128(value: unknown): value is bigint {\n return typeof value === \"bigint\" && value >= MIN_I128 && value <= MAX_I128;\n}\n\nexport function isBool(value: unknown): value is boolean {\n return typeof value === \"boolean\";\n}\n\nexport function isPrincipal(value: unknown): value is string {\n if (typeof value !== \"string\") return false;\n const parts = value.split(\".\");\n const address = parts[0]!;\n const contractName = parts[1];\n if (!validateStacksAddress(address)) return false;\n if (contractName !== undefined) {\n return CONTRACT_NAME_REGEX.test(contractName);\n }\n return true;\n}\n\nexport function isStandardPrincipal(value: unknown): value is string {\n if (typeof value !== \"string\") return false;\n if (value.includes(\".\")) return false;\n return validateStacksAddress(value);\n}\n\nexport function isContractPrincipal(value: unknown): value is string {\n if (typeof value !== \"string\") return false;\n if (!value.includes(\".\")) return false;\n const parts = value.split(\".\");\n const address = parts[0]!;\n const contractName = parts[1]!;\n if (!validateStacksAddress(address)) return false;\n return CONTRACT_NAME_REGEX.test(contractName);\n}\n\nexport function isTraitReference(value: unknown): value is string {\n return isContractPrincipal(value);\n}\n\nexport function isString(value: unknown): value is string {\n return typeof value === \"string\";\n}\n\nexport function isUint8Array(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\nexport function isArray<T>(\n value: unknown,\n itemGuard: (item: unknown) => item is T,\n): value is T[] {\n return Array.isArray(value) && value.every(itemGuard);\n}\n\nexport function isOptional<T>(\n value: unknown,\n guard: (val: unknown) => val is T,\n): value is T | null {\n return value === null || guard(value);\n}\n\nexport function isOkResponse<T>(\n response: { ok: T } | { err: any },\n): response is ResponseOk<T> {\n return \"ok\" in response && !(\"err\" in response);\n}\n\nexport function isErrResponse<E>(\n response: { ok: any } | { err: E },\n): response is ResponseErr<E> {\n return \"err\" in response && !(\"ok\" in response);\n}\n\nexport function isResponse<T, E>(\n value: unknown,\n okGuard: (val: unknown) => val is T,\n errGuard: (val: unknown) => val is E,\n): value is ResponseOk<T> | ResponseErr<E> {\n if (typeof value !== \"object\" || value === null) return false;\n const obj = value as Record<string, unknown>;\n if (\"ok\" in value && !(\"err\" in value)) return okGuard(obj.ok);\n if (\"err\" in value && !(\"ok\" in value)) return errGuard(obj.err);\n return false;\n}\n\n// ABI type definition guards\n\nexport function isAbiList(type: AbiType): type is AbiListType {\n return typeof type === \"object\" && type !== null && \"list\" in type;\n}\n\nexport function isAbiTuple(type: AbiType): type is AbiTupleType {\n return typeof type === \"object\" && type !== null && \"tuple\" in type;\n}\n\nexport function isAbiOptional(type: AbiType): type is AbiOptionalType {\n return typeof type === \"object\" && type !== null && \"optional\" in type;\n}\n\nexport function isAbiResponse(type: AbiType): type is AbiResponseType {\n return typeof type === \"object\" && type !== null && \"response\" in type;\n}\n\nexport function isAbiBuffer(type: AbiType): type is AbiBuffer {\n return typeof type === \"object\" && type !== null && \"buff\" in type;\n}\n\nexport function isAbiStringAscii(type: AbiType): type is AbiStringAscii {\n return typeof type === \"object\" && type !== null && \"string-ascii\" in type;\n}\n\nexport function isAbiStringUtf8(type: AbiType): type is AbiStringUtf8 {\n return typeof type === \"object\" && type !== null && \"string-utf8\" in type;\n}\n\nexport function isAbiTraitReference(type: AbiType): type is AbiTraitReference {\n return type === \"trait_reference\";\n}\n",
|
|
7
|
+
"type CamelCaseInner<S extends string> =\n S extends `${infer P1}-${infer P2}${infer P3}`\n ? `${P1}${Capitalize<CamelCaseInner<`${P2}${P3}`>>}`\n : S;\n\nexport type ToCamelCase<S extends string> =\n CamelCaseInner<S> extends `${number}${string}`\n ? `_${CamelCaseInner<S>}`\n : CamelCaseInner<S>;\n\nexport function toCamelCase(str: string): string {\n return str\n .replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())\n .replace(/-([A-Z])/g, (_, letter) => letter)\n .replace(/-(\\d)/g, (_, digit) => digit)\n .replace(/-/g, \"\")\n .replace(/^\\d/, \"_$&\");\n}\n",
|
|
8
|
+
"import type { AbiType } from \"./abi/types.ts\";\nimport type { AbiToTS } from \"./abi/mappings.ts\";\nimport {\n isAbiList,\n isAbiTuple,\n isAbiOptional,\n isAbiResponse,\n isAbiBuffer,\n isAbiStringAscii,\n isAbiStringUtf8,\n} from \"./abi/guards.ts\";\nimport { toCamelCase } from \"./abi/utils.ts\";\nimport type { ClarityValue } from \"./types.ts\";\nimport {\n intCV,\n uintCV,\n boolCV,\n bufferCV,\n noneCV,\n someCV,\n responseOkCV,\n responseErrorCV,\n listCV,\n tupleCV,\n stringAsciiCV,\n stringUtf8CV,\n} from \"./values.ts\";\nimport { Cl } from \"./values.ts\";\n\n/**\n * Convert a JS value to a ClarityValue using ABI type information.\n */\nexport function jsToClarityValue(abiType: AbiType, value: unknown): ClarityValue {\n if (abiType === \"uint128\") return uintCV(value as bigint | number);\n if (abiType === \"int128\") return intCV(value as bigint | number);\n if (abiType === \"bool\") return boolCV(value as boolean);\n if (abiType === \"principal\" || abiType === \"trait_reference\")\n return Cl.principal(value as string);\n\n if (isAbiStringAscii(abiType)) return stringAsciiCV(value as string);\n if (isAbiStringUtf8(abiType)) return stringUtf8CV(value as string);\n if (isAbiBuffer(abiType)) return bufferCV(value as Uint8Array);\n\n if (isAbiList(abiType)) {\n const arr = value as unknown[];\n return listCV(arr.map((item) => jsToClarityValue(abiType.list.type, item)));\n }\n\n if (isAbiTuple(abiType)) {\n const obj = value as Record<string, unknown>;\n const data: Record<string, ClarityValue> = {};\n for (const field of abiType.tuple) {\n const camelKey = toCamelCase(field.name);\n const hasOriginal = field.name in obj;\n const hasCamel = camelKey in obj;\n const fieldValue = hasOriginal ? obj[field.name] : hasCamel ? obj[camelKey] : undefined;\n if (fieldValue === undefined && !isAbiOptional(field.type)) {\n throw new Error(`Missing tuple field: ${field.name}`);\n }\n data[field.name] = jsToClarityValue(field.type, fieldValue);\n }\n return tupleCV(data);\n }\n\n if (isAbiOptional(abiType)) {\n if (value === null || value === undefined) return noneCV();\n return someCV(jsToClarityValue(abiType.optional, value));\n }\n\n if (isAbiResponse(abiType)) {\n const obj = value as Record<string, unknown>;\n if (\"ok\" in obj && !(\"err\" in obj))\n return responseOkCV(jsToClarityValue(abiType.response.ok, obj.ok));\n if (\"err\" in obj && !(\"ok\" in obj))\n return responseErrorCV(jsToClarityValue(abiType.response.error, obj.err));\n throw new Error(\"Response must have exactly 'ok' or 'err' property\");\n }\n\n throw new Error(`Unknown ABI type: ${JSON.stringify(abiType)}`);\n}\n\n/**\n * Convert a ClarityValue back to a typed JS value using ABI type information.\n */\nexport function clarityValueToJS<T extends AbiType>(\n abiType: T,\n cv: ClarityValue,\n): AbiToTS<T> {\n return clarityValueToJSInner(abiType, cv) as AbiToTS<T>;\n}\n\n/** Non-generic version for internal use — avoids deep type instantiation. */\nexport function clarityValueToJSUntyped(abiType: AbiType, cv: ClarityValue): unknown {\n return clarityValueToJSInner(abiType, cv);\n}\n\nfunction clarityValueToJSInner(abiType: AbiType, cv: ClarityValue): unknown {\n if (abiType === \"uint128\" || abiType === \"int128\") {\n if (cv.type !== \"int\" && cv.type !== \"uint\")\n throw new Error(`Expected int/uint CV, got ${cv.type}`);\n return cv.value;\n }\n\n if (abiType === \"bool\") {\n if (cv.type === \"true\") return true;\n if (cv.type === \"false\") return false;\n throw new Error(`Expected bool CV, got ${cv.type}`);\n }\n\n if (abiType === \"principal\" || abiType === \"trait_reference\") {\n if (cv.type !== \"address\" && cv.type !== \"contract\")\n throw new Error(`Expected principal CV, got ${cv.type}`);\n return cv.value;\n }\n\n if (isAbiStringAscii(abiType) || isAbiStringUtf8(abiType)) {\n if (cv.type !== \"ascii\" && cv.type !== \"utf8\")\n throw new Error(`Expected string CV, got ${cv.type}`);\n return cv.value;\n }\n\n if (isAbiBuffer(abiType)) {\n if (cv.type !== \"buffer\") throw new Error(`Expected buffer CV, got ${cv.type}`);\n // BufferCV stores hex string; convert back to Uint8Array\n const hex = cv.value;\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);\n }\n return bytes;\n }\n\n if (isAbiList(abiType)) {\n if (cv.type !== \"list\") throw new Error(`Expected list CV, got ${cv.type}`);\n return cv.value.map((item) => clarityValueToJSInner(abiType.list.type, item));\n }\n\n if (isAbiTuple(abiType)) {\n if (cv.type !== \"tuple\") throw new Error(`Expected tuple CV, got ${cv.type}`);\n const result: Record<string, unknown> = {};\n for (const field of abiType.tuple) {\n const fieldCV = cv.value[field.name];\n if (!fieldCV) throw new Error(`Missing tuple field in CV: ${field.name}`);\n const camelKey = toCamelCase(field.name);\n result[camelKey] = clarityValueToJSInner(field.type, fieldCV);\n }\n return result;\n }\n\n if (isAbiOptional(abiType)) {\n if (cv.type === \"none\") return null;\n if (cv.type === \"some\") return clarityValueToJSInner(abiType.optional, cv.value);\n throw new Error(`Expected optional CV, got ${cv.type}`);\n }\n\n if (isAbiResponse(abiType)) {\n if (cv.type === \"ok\") return { ok: clarityValueToJSInner(abiType.response.ok, cv.value) };\n if (cv.type === \"err\") return { err: clarityValueToJSInner(abiType.response.error, cv.value) };\n throw new Error(`Expected response CV, got ${cv.type}`);\n }\n\n throw new Error(`Unknown ABI type: ${JSON.stringify(abiType)}`);\n}\n"
|
|
9
|
+
],
|
|
10
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAIO,IAAM,sBAA8B;;;ACapC,SAAS,SAAS,CAAC,OAAiC;AAAA,EACzD,OAAO,OAAO,UAAU,YAAY,SAAS,MAAM,SAAS;AAAA;AAGvD,SAAS,QAAQ,CAAC,OAAiC;AAAA,EACxD,OAAO,OAAO,UAAU,YAAY,SAAS,YAAY,SAAS;AAAA;AAG7D,SAAS,MAAM,CAAC,OAAkC;AAAA,EACvD,OAAO,OAAO,UAAU;AAAA;AAGnB,SAAS,WAAW,CAAC,OAAiC;AAAA,EAC3D,IAAI,OAAO,UAAU;AAAA,IAAU,OAAO;AAAA,EACtC,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