@arkade-os/sdk 0.5.3 → 0.5.5

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.
Files changed (114) hide show
  1. package/README.md +138 -50
  2. package/dist/adapters/expo.cjs +10 -5
  3. package/dist/adapters/expo.d.cts +13 -8
  4. package/dist/adapters/expo.d.ts +13 -8
  5. package/dist/adapters/expo.js +4 -3
  6. package/dist/{asyncStorageTaskQueue-C9PkPcW_.d.cts → asyncStorageTaskQueue-BbH-THYJ.d.cts} +2 -2
  7. package/dist/{asyncStorageTaskQueue-B3U5gQ-i.d.ts → asyncStorageTaskQueue-BlvkJyTa.d.ts} +2 -2
  8. package/dist/base-C-Y5t_0F.d.cts +359 -0
  9. package/dist/base-C-Y5t_0F.d.ts +359 -0
  10. package/dist/{chunk-OPX2HRWM.js → chunk-4I53THL6.js} +210 -937
  11. package/dist/chunk-4I53THL6.js.map +1 -0
  12. package/dist/{chunk-5WRA66RV.cjs → chunk-5SYUEVJH.cjs} +10 -10
  13. package/dist/chunk-5SYUEVJH.cjs.map +1 -0
  14. package/dist/{chunk-6FL4N4S2.js → chunk-7GIKLFHC.js} +83 -20
  15. package/dist/chunk-7GIKLFHC.js.map +1 -0
  16. package/dist/{chunk-BYPBVHVA.cjs → chunk-AMH7Q3DF.cjs} +61 -935
  17. package/dist/chunk-AMH7Q3DF.cjs.map +1 -0
  18. package/dist/{chunk-P7B4LQ3H.js → chunk-BCA6D255.js} +18 -842
  19. package/dist/chunk-BCA6D255.js.map +1 -0
  20. package/dist/{chunk-T7C3E2HL.js → chunk-CE46RS6L.js} +20 -39
  21. package/dist/chunk-CE46RS6L.js.map +1 -0
  22. package/dist/chunk-DNFTHG73.js +829 -0
  23. package/dist/chunk-DNFTHG73.js.map +1 -0
  24. package/dist/chunk-FDJ734CH.js +882 -0
  25. package/dist/chunk-FDJ734CH.js.map +1 -0
  26. package/dist/chunk-FLPWNMX3.cjs +930 -0
  27. package/dist/chunk-FLPWNMX3.cjs.map +1 -0
  28. package/dist/{chunk-Y4BL3YYX.js → chunk-FVVXCDNE.js} +4 -4
  29. package/dist/chunk-FVVXCDNE.js.map +1 -0
  30. package/dist/{chunk-ZPF4I3K2.cjs → chunk-HQQ4NRXI.cjs} +29 -47
  31. package/dist/chunk-HQQ4NRXI.cjs.map +1 -0
  32. package/dist/chunk-JFJNB7ZH.cjs +884 -0
  33. package/dist/chunk-JFJNB7ZH.cjs.map +1 -0
  34. package/dist/{chunk-KW7ZEWYS.cjs → chunk-KCL5AVML.cjs} +223 -985
  35. package/dist/chunk-KCL5AVML.cjs.map +1 -0
  36. package/dist/{chunk-HAQAWBYG.js → chunk-OO2KEK64.js} +2860 -1366
  37. package/dist/chunk-OO2KEK64.js.map +1 -0
  38. package/dist/{chunk-OUTWUYJ4.cjs → chunk-T254PXSN.cjs} +3337 -1824
  39. package/dist/chunk-T254PXSN.cjs.map +1 -0
  40. package/dist/chunk-V3ZLL5VY.js +380 -0
  41. package/dist/chunk-V3ZLL5VY.js.map +1 -0
  42. package/dist/chunk-X6ZI53DZ.cjs +383 -0
  43. package/dist/chunk-X6ZI53DZ.cjs.map +1 -0
  44. package/dist/{chunk-ZMSM3NOH.cjs → chunk-XWUCNMPN.cjs} +98 -34
  45. package/dist/chunk-XWUCNMPN.cjs.map +1 -0
  46. package/dist/compiler.cjs +50 -0
  47. package/dist/compiler.cjs.map +1 -0
  48. package/dist/compiler.d.cts +38 -0
  49. package/dist/compiler.d.ts +38 -0
  50. package/dist/compiler.js +47 -0
  51. package/dist/compiler.js.map +1 -0
  52. package/dist/contracts/handlers/index.cjs +11 -9
  53. package/dist/contracts/handlers/index.d.cts +6 -4
  54. package/dist/contracts/handlers/index.d.ts +6 -4
  55. package/dist/contracts/handlers/index.js +4 -2
  56. package/dist/{delegate-Cd28gxpM.d.ts → delegate-gGkUnYpi.d.cts} +1 -1
  57. package/dist/{delegate-ByfKszb6.d.cts → delegate-iyDum4fw.d.ts} +1 -1
  58. package/dist/{index-B6X2E7W_.d.ts → index-BWwVnJhU.d.cts} +7 -272
  59. package/dist/{index-DgH_0j14.d.cts → index-Bx-iArDs.d.ts} +7 -272
  60. package/dist/{index-BURzbRup.d.cts → index-DCml4QJn.d.ts} +1090 -1140
  61. package/dist/{index-BURzbRup.d.ts → index-DkyzIzKL.d.cts} +1090 -1140
  62. package/dist/index.cjs +470 -371
  63. package/dist/index.d.cts +687 -410
  64. package/dist/index.d.ts +687 -410
  65. package/dist/index.js +7 -4
  66. package/dist/{wallet-BnmhohgZ.d.ts → keyringDescriptorProvider-CS4w4PWd.d.ts} +427 -372
  67. package/dist/{wallet-BjQugVUt.d.cts → keyringDescriptorProvider-CmHXrIXd.d.cts} +427 -372
  68. package/dist/program-BARSNg3X.d.ts +286 -0
  69. package/dist/program-znrWkxE4.d.cts +286 -0
  70. package/dist/repositories/realm/index.cjs +100 -51
  71. package/dist/repositories/realm/index.cjs.map +1 -1
  72. package/dist/repositories/realm/index.d.cts +25 -10
  73. package/dist/repositories/realm/index.d.ts +25 -10
  74. package/dist/repositories/realm/index.js +72 -24
  75. package/dist/repositories/realm/index.js.map +1 -1
  76. package/dist/repositories/sqlite/index.cjs +222 -131
  77. package/dist/repositories/sqlite/index.cjs.map +1 -1
  78. package/dist/repositories/sqlite/index.d.cts +36 -17
  79. package/dist/repositories/sqlite/index.d.ts +36 -17
  80. package/dist/repositories/sqlite/index.js +196 -107
  81. package/dist/repositories/sqlite/index.js.map +1 -1
  82. package/dist/{taskRunner-BaNCSxqK.d.ts → taskRunner-DgMLSk_O.d.ts} +4 -4
  83. package/dist/{taskRunner-i56-drQV.d.cts → taskRunner-buh6aFpG.d.cts} +4 -4
  84. package/dist/wallet/expo/background.cjs +17 -14
  85. package/dist/wallet/expo/background.cjs.map +1 -1
  86. package/dist/wallet/expo/background.d.cts +5 -4
  87. package/dist/wallet/expo/background.d.ts +5 -4
  88. package/dist/wallet/expo/background.js +10 -7
  89. package/dist/wallet/expo/background.js.map +1 -1
  90. package/dist/wallet/expo/index.cjs +29 -16
  91. package/dist/wallet/expo/index.cjs.map +1 -1
  92. package/dist/wallet/expo/index.d.cts +12 -7
  93. package/dist/wallet/expo/index.d.ts +12 -7
  94. package/dist/wallet/expo/index.js +20 -7
  95. package/dist/wallet/expo/index.js.map +1 -1
  96. package/dist/worker/expo/index.cjs +12 -9
  97. package/dist/worker/expo/index.cjs.map +1 -1
  98. package/dist/worker/expo/index.d.cts +6 -5
  99. package/dist/worker/expo/index.d.ts +6 -5
  100. package/dist/worker/expo/index.js +8 -5
  101. package/dist/worker/expo/index.js.map +1 -1
  102. package/package.json +16 -1
  103. package/dist/chunk-5WRA66RV.cjs.map +0 -1
  104. package/dist/chunk-6FL4N4S2.js.map +0 -1
  105. package/dist/chunk-BYPBVHVA.cjs.map +0 -1
  106. package/dist/chunk-HAQAWBYG.js.map +0 -1
  107. package/dist/chunk-KW7ZEWYS.cjs.map +0 -1
  108. package/dist/chunk-OPX2HRWM.js.map +0 -1
  109. package/dist/chunk-OUTWUYJ4.cjs.map +0 -1
  110. package/dist/chunk-P7B4LQ3H.js.map +0 -1
  111. package/dist/chunk-T7C3E2HL.js.map +0 -1
  112. package/dist/chunk-Y4BL3YYX.js.map +0 -1
  113. package/dist/chunk-ZMSM3NOH.cjs.map +0 -1
  114. package/dist/chunk-ZPF4I3K2.cjs.map +0 -1
@@ -0,0 +1,882 @@
1
+ import * as bip68 from 'bip68';
2
+ import { ScriptNum, p2tr_ms, Script, p2tr, taprootNumsKey, Address } from '@scure/btc-signer';
3
+ import { hex, bech32m } from '@scure/base';
4
+ import { TEST_NETWORK, NETWORK } from '@scure/btc-signer/utils.js';
5
+ import { VarBytes, Script as Script$1 } from '@scure/btc-signer/script.js';
6
+ import { TAP_LEAF_VERSION } from '@scure/btc-signer/payment.js';
7
+ import * as P from 'micro-packed';
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all)
12
+ __defProp(target, name, { get: all[name], enumerable: true });
13
+ };
14
+ function timelockToSequence(timelock) {
15
+ return bip68.encode(
16
+ timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
17
+ );
18
+ }
19
+ function sequenceToTimelock(sequence) {
20
+ const decoded = bip68.decode(sequence);
21
+ if ("blocks" in decoded && decoded.blocks !== void 0) {
22
+ return { type: "blocks", value: BigInt(decoded.blocks) };
23
+ }
24
+ if ("seconds" in decoded && decoded.seconds !== void 0) {
25
+ return { type: "seconds", value: BigInt(decoded.seconds) };
26
+ }
27
+ throw new Error(`Invalid BIP68 sequence: ${sequence}`);
28
+ }
29
+ function sequenceToCanonicalTimelock(sequence) {
30
+ if (sequence < 0n || sequence >= 0x80000000n) return void 0;
31
+ const timelock = sequenceToTimelock(Number(sequence));
32
+ return BigInt(timelockToSequence(timelock)) === sequence ? timelock : void 0;
33
+ }
34
+ var MinimalScriptNum = ScriptNum(void 0, true);
35
+ function decodeTapscript(script) {
36
+ const types = [
37
+ MultisigTapscript,
38
+ CSVMultisigTapscript,
39
+ ConditionCSVMultisigTapscript,
40
+ ConditionMultisigTapscript,
41
+ CLTVMultisigTapscript
42
+ ];
43
+ for (const type of types) {
44
+ try {
45
+ return type.decode(script);
46
+ } catch (error) {
47
+ continue;
48
+ }
49
+ }
50
+ throw new Error(`Failed to decode: script ${hex.encode(script)} is not a valid tapscript`);
51
+ }
52
+ var MultisigTapscript;
53
+ ((MultisigTapscript2) => {
54
+ ((MultisigType2) => {
55
+ MultisigType2[MultisigType2["CHECKSIG"] = 0] = "CHECKSIG";
56
+ MultisigType2[MultisigType2["CHECKSIGADD"] = 1] = "CHECKSIGADD";
57
+ })(MultisigTapscript2.MultisigType || (MultisigTapscript2.MultisigType = {}));
58
+ function encode2(params) {
59
+ if (params.pubkeys.length === 0) {
60
+ throw new Error("At least 1 pubkey is required");
61
+ }
62
+ for (const pubkey of params.pubkeys) {
63
+ if (pubkey.length !== 32) {
64
+ throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
65
+ }
66
+ }
67
+ if (!params.type) {
68
+ params.type = 0 /* CHECKSIG */;
69
+ }
70
+ if (params.type === 1 /* CHECKSIGADD */) {
71
+ return {
72
+ type: "multisig" /* Multisig */,
73
+ params,
74
+ script: p2tr_ms(params.pubkeys.length, params.pubkeys).script
75
+ };
76
+ }
77
+ const asm = [];
78
+ for (let i = 0; i < params.pubkeys.length; i++) {
79
+ asm.push(params.pubkeys[i]);
80
+ if (i < params.pubkeys.length - 1) {
81
+ asm.push("CHECKSIGVERIFY");
82
+ } else {
83
+ asm.push("CHECKSIG");
84
+ }
85
+ }
86
+ return {
87
+ type: "multisig" /* Multisig */,
88
+ params,
89
+ script: Script.encode(asm)
90
+ };
91
+ }
92
+ MultisigTapscript2.encode = encode2;
93
+ function decode2(script) {
94
+ if (script.length === 0) {
95
+ throw new Error("Failed to decode: script is empty");
96
+ }
97
+ try {
98
+ return decodeChecksigAdd(script);
99
+ } catch (error) {
100
+ try {
101
+ return decodeChecksig(script);
102
+ } catch (error2) {
103
+ throw new Error(
104
+ `Failed to decode script: ${error2 instanceof Error ? error2.message : String(error2)}`
105
+ );
106
+ }
107
+ }
108
+ }
109
+ MultisigTapscript2.decode = decode2;
110
+ function decodeChecksigAdd(script) {
111
+ const asm = Script.decode(script);
112
+ const pubkeys = [];
113
+ let foundNumEqual = false;
114
+ for (let i = 0; i < asm.length; i++) {
115
+ const op = asm[i];
116
+ if (typeof op !== "string" && typeof op !== "number") {
117
+ if (op.length !== 32) {
118
+ throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
119
+ }
120
+ pubkeys.push(op);
121
+ if (i + 1 >= asm.length || asm[i + 1] !== "CHECKSIGADD" && asm[i + 1] !== "CHECKSIG") {
122
+ throw new Error("Expected CHECKSIGADD or CHECKSIG after pubkey");
123
+ }
124
+ i++;
125
+ continue;
126
+ }
127
+ if (i === asm.length - 1) {
128
+ if (op !== "NUMEQUAL") {
129
+ throw new Error("Expected NUMEQUAL at end of script");
130
+ }
131
+ foundNumEqual = true;
132
+ }
133
+ }
134
+ if (!foundNumEqual) {
135
+ throw new Error("Missing NUMEQUAL operation");
136
+ }
137
+ if (pubkeys.length === 0) {
138
+ throw new Error("Invalid script: must have at least 1 pubkey");
139
+ }
140
+ const reconstructed = encode2({
141
+ pubkeys,
142
+ type: 1 /* CHECKSIGADD */
143
+ });
144
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
145
+ throw new Error("Invalid script format: script reconstruction mismatch");
146
+ }
147
+ return {
148
+ type: "multisig" /* Multisig */,
149
+ params: { pubkeys, type: 1 /* CHECKSIGADD */ },
150
+ script
151
+ };
152
+ }
153
+ function decodeChecksig(script) {
154
+ const asm = Script.decode(script);
155
+ const pubkeys = [];
156
+ for (let i = 0; i < asm.length; i++) {
157
+ const op = asm[i];
158
+ if (typeof op !== "string" && typeof op !== "number") {
159
+ if (op.length !== 32) {
160
+ throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
161
+ }
162
+ pubkeys.push(op);
163
+ if (i + 1 >= asm.length) {
164
+ throw new Error("Unexpected end of script");
165
+ }
166
+ const nextOp = asm[i + 1];
167
+ if (nextOp !== "CHECKSIGVERIFY" && nextOp !== "CHECKSIG") {
168
+ throw new Error("Expected CHECKSIGVERIFY or CHECKSIG after pubkey");
169
+ }
170
+ if (i === asm.length - 2 && nextOp !== "CHECKSIG") {
171
+ throw new Error("Last operation must be CHECKSIG");
172
+ }
173
+ i++;
174
+ continue;
175
+ }
176
+ }
177
+ if (pubkeys.length === 0) {
178
+ throw new Error("Invalid script: must have at least 1 pubkey");
179
+ }
180
+ const reconstructed = encode2({ pubkeys, type: 0 /* CHECKSIG */ });
181
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
182
+ throw new Error("Invalid script format: script reconstruction mismatch");
183
+ }
184
+ return {
185
+ type: "multisig" /* Multisig */,
186
+ params: { pubkeys, type: 0 /* CHECKSIG */ },
187
+ script
188
+ };
189
+ }
190
+ function is(tapscript) {
191
+ return tapscript.type === "multisig" /* Multisig */;
192
+ }
193
+ MultisigTapscript2.is = is;
194
+ })(MultisigTapscript || (MultisigTapscript = {}));
195
+ var CSVMultisigTapscript;
196
+ ((CSVMultisigTapscript2) => {
197
+ function encode2(params) {
198
+ for (const pubkey of params.pubkeys) {
199
+ if (pubkey.length !== 32) {
200
+ throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
201
+ }
202
+ }
203
+ const sequence = MinimalScriptNum.encode(BigInt(timelockToSequence(params.timelock)));
204
+ const asm = [
205
+ sequence.length === 1 ? sequence[0] : sequence,
206
+ "CHECKSEQUENCEVERIFY",
207
+ "DROP"
208
+ ];
209
+ const multisigScript = MultisigTapscript.encode(params);
210
+ const script = new Uint8Array([...Script.encode(asm), ...multisigScript.script]);
211
+ return {
212
+ type: "csv-multisig" /* CSVMultisig */,
213
+ params,
214
+ script
215
+ };
216
+ }
217
+ CSVMultisigTapscript2.encode = encode2;
218
+ function decode2(script) {
219
+ if (script.length === 0) {
220
+ throw new Error("Failed to decode: script is empty");
221
+ }
222
+ const isValid = isScriptValid(script);
223
+ if (isValid instanceof Error) {
224
+ throw isValid;
225
+ }
226
+ const asm = Script.decode(script);
227
+ const sequence = asm[0];
228
+ const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
229
+ let multisig;
230
+ try {
231
+ multisig = MultisigTapscript.decode(multisigScript);
232
+ } catch (error) {
233
+ throw new Error(
234
+ `Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
235
+ );
236
+ }
237
+ let sequenceNum;
238
+ if (typeof sequence === "number") {
239
+ sequenceNum = sequence;
240
+ } else {
241
+ sequenceNum = Number(MinimalScriptNum.decode(sequence));
242
+ }
243
+ const timelock = sequenceToTimelock(sequenceNum);
244
+ const reconstructed = encode2({
245
+ timelock,
246
+ ...multisig.params
247
+ });
248
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
249
+ throw new Error("Invalid script format: script reconstruction mismatch");
250
+ }
251
+ return {
252
+ type: "csv-multisig" /* CSVMultisig */,
253
+ params: {
254
+ timelock,
255
+ ...multisig.params
256
+ },
257
+ script
258
+ };
259
+ }
260
+ CSVMultisigTapscript2.decode = decode2;
261
+ function is(tapscript) {
262
+ return tapscript.type === "csv-multisig" /* CSVMultisig */;
263
+ }
264
+ CSVMultisigTapscript2.is = is;
265
+ function isScriptValid(script) {
266
+ const asm = Script.decode(script);
267
+ if (asm.length < 3) {
268
+ return new Error(`Invalid script: too short (expected at least 3)`);
269
+ }
270
+ const sequence = asm[0];
271
+ if (typeof sequence === "string") {
272
+ return new Error("Invalid script: expected sequence number");
273
+ }
274
+ if (asm[1] !== "CHECKSEQUENCEVERIFY" || asm[2] !== "DROP") {
275
+ return new Error("Invalid script: expected CHECKSEQUENCEVERIFY DROP");
276
+ }
277
+ return true;
278
+ }
279
+ CSVMultisigTapscript2.isScriptValid = isScriptValid;
280
+ })(CSVMultisigTapscript || (CSVMultisigTapscript = {}));
281
+ function isConditionScriptValid(script) {
282
+ const asm = Script.decode(script);
283
+ if (asm.length < 1) {
284
+ return new Error(`Invalid script: too short (expected at least 1)`);
285
+ }
286
+ if (asm.lastIndexOf("VERIFY") === -1) {
287
+ return new Error("Invalid script: missing VERIFY operation");
288
+ }
289
+ return true;
290
+ }
291
+ var ConditionCSVMultisigTapscript;
292
+ ((ConditionCSVMultisigTapscript2) => {
293
+ function encode2(params) {
294
+ const script = new Uint8Array([
295
+ ...params.conditionScript,
296
+ ...Script.encode(["VERIFY"]),
297
+ ...CSVMultisigTapscript.encode(params).script
298
+ ]);
299
+ return {
300
+ type: "condition-csv-multisig" /* ConditionCSVMultisig */,
301
+ params,
302
+ script
303
+ };
304
+ }
305
+ ConditionCSVMultisigTapscript2.encode = encode2;
306
+ function decode2(script) {
307
+ if (script.length === 0) {
308
+ throw new Error("Failed to decode: script is empty");
309
+ }
310
+ const isValid = isScriptValid(script);
311
+ if (isValid instanceof Error) {
312
+ throw isValid;
313
+ }
314
+ const asm = Script.decode(script);
315
+ const verifyIndex = asm.lastIndexOf("VERIFY");
316
+ if (verifyIndex === -1) {
317
+ throw Error("Invalid script: missing VERIFY operation");
318
+ }
319
+ const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
320
+ const csvMultisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
321
+ let csvMultisig;
322
+ try {
323
+ csvMultisig = CSVMultisigTapscript.decode(csvMultisigScript);
324
+ } catch (error) {
325
+ throw new Error(
326
+ `Invalid CSV multisig script: ${error instanceof Error ? error.message : String(error)}`
327
+ );
328
+ }
329
+ const reconstructed = encode2({
330
+ conditionScript,
331
+ ...csvMultisig.params
332
+ });
333
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
334
+ throw new Error("Invalid script format: script reconstruction mismatch");
335
+ }
336
+ return {
337
+ type: "condition-csv-multisig" /* ConditionCSVMultisig */,
338
+ params: {
339
+ conditionScript,
340
+ ...csvMultisig.params
341
+ },
342
+ script
343
+ };
344
+ }
345
+ ConditionCSVMultisigTapscript2.decode = decode2;
346
+ function is(tapscript) {
347
+ return tapscript.type === "condition-csv-multisig" /* ConditionCSVMultisig */;
348
+ }
349
+ ConditionCSVMultisigTapscript2.is = is;
350
+ function isScriptValid(script) {
351
+ return isConditionScriptValid(script);
352
+ }
353
+ ConditionCSVMultisigTapscript2.isScriptValid = isScriptValid;
354
+ })(ConditionCSVMultisigTapscript || (ConditionCSVMultisigTapscript = {}));
355
+ var ConditionMultisigTapscript;
356
+ ((ConditionMultisigTapscript2) => {
357
+ function encode2(params) {
358
+ const script = new Uint8Array([
359
+ ...params.conditionScript,
360
+ ...Script.encode(["VERIFY"]),
361
+ ...MultisigTapscript.encode(params).script
362
+ ]);
363
+ return {
364
+ type: "condition-multisig" /* ConditionMultisig */,
365
+ params,
366
+ script
367
+ };
368
+ }
369
+ ConditionMultisigTapscript2.encode = encode2;
370
+ function decode2(script) {
371
+ if (script.length === 0) {
372
+ throw new Error("Failed to decode: script is empty");
373
+ }
374
+ const isValid = isScriptValid(script);
375
+ if (isValid instanceof Error) {
376
+ throw isValid;
377
+ }
378
+ const asm = Script.decode(script);
379
+ const verifyIndex = asm.lastIndexOf("VERIFY");
380
+ if (verifyIndex === -1) {
381
+ throw Error("Invalid script: missing VERIFY operation");
382
+ }
383
+ const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
384
+ const multisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
385
+ let multisig;
386
+ try {
387
+ multisig = MultisigTapscript.decode(multisigScript);
388
+ } catch (error) {
389
+ throw new Error(
390
+ `Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
391
+ );
392
+ }
393
+ const reconstructed = encode2({
394
+ conditionScript,
395
+ ...multisig.params
396
+ });
397
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
398
+ throw new Error("Invalid script format: script reconstruction mismatch");
399
+ }
400
+ return {
401
+ type: "condition-multisig" /* ConditionMultisig */,
402
+ params: {
403
+ conditionScript,
404
+ ...multisig.params
405
+ },
406
+ script
407
+ };
408
+ }
409
+ ConditionMultisigTapscript2.decode = decode2;
410
+ function is(tapscript) {
411
+ return tapscript.type === "condition-multisig" /* ConditionMultisig */;
412
+ }
413
+ ConditionMultisigTapscript2.is = is;
414
+ function isScriptValid(script) {
415
+ return isConditionScriptValid(script);
416
+ }
417
+ ConditionMultisigTapscript2.isScriptValid = isScriptValid;
418
+ })(ConditionMultisigTapscript || (ConditionMultisigTapscript = {}));
419
+ var CLTVMultisigTapscript;
420
+ ((CLTVMultisigTapscript2) => {
421
+ function encode2(params) {
422
+ const locktime = MinimalScriptNum.encode(params.absoluteTimelock);
423
+ const asm = [
424
+ locktime.length === 1 ? locktime[0] : locktime,
425
+ "CHECKLOCKTIMEVERIFY",
426
+ "DROP"
427
+ ];
428
+ const timelockedScript = Script.encode(asm);
429
+ const script = new Uint8Array([
430
+ ...timelockedScript,
431
+ ...MultisigTapscript.encode(params).script
432
+ ]);
433
+ return {
434
+ type: "cltv-multisig" /* CLTVMultisig */,
435
+ params,
436
+ script
437
+ };
438
+ }
439
+ CLTVMultisigTapscript2.encode = encode2;
440
+ function decode2(script) {
441
+ if (script.length === 0) {
442
+ throw new Error("Failed to decode: script is empty");
443
+ }
444
+ const isValid = isScriptValid(script);
445
+ if (isValid instanceof Error) {
446
+ throw isValid;
447
+ }
448
+ const asm = Script.decode(script);
449
+ const locktime = asm[0];
450
+ if (typeof locktime === "string") {
451
+ throw new Error("Invalid script: expected locktime number");
452
+ }
453
+ if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
454
+ throw new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
455
+ }
456
+ const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
457
+ let multisig;
458
+ try {
459
+ multisig = MultisigTapscript.decode(multisigScript);
460
+ } catch (error) {
461
+ throw new Error(
462
+ `Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
463
+ );
464
+ }
465
+ let absoluteTimelock;
466
+ if (typeof locktime === "number") {
467
+ absoluteTimelock = BigInt(locktime);
468
+ } else {
469
+ absoluteTimelock = MinimalScriptNum.decode(locktime);
470
+ }
471
+ const reconstructed = encode2({
472
+ absoluteTimelock,
473
+ ...multisig.params
474
+ });
475
+ if (hex.encode(reconstructed.script) !== hex.encode(script)) {
476
+ throw new Error("Invalid script format: script reconstruction mismatch");
477
+ }
478
+ return {
479
+ type: "cltv-multisig" /* CLTVMultisig */,
480
+ params: {
481
+ absoluteTimelock,
482
+ ...multisig.params
483
+ },
484
+ script
485
+ };
486
+ }
487
+ CLTVMultisigTapscript2.decode = decode2;
488
+ function is(tapscript) {
489
+ return tapscript.type === "cltv-multisig" /* CLTVMultisig */;
490
+ }
491
+ CLTVMultisigTapscript2.is = is;
492
+ function isScriptValid(script) {
493
+ const asm = Script.decode(script);
494
+ if (asm.length < 3) {
495
+ return new Error(`Invalid script: too short (expected at least 3)`);
496
+ }
497
+ const locktime = asm[0];
498
+ if (typeof locktime === "string") {
499
+ return new Error("Invalid script: expected locktime as number or bytes");
500
+ }
501
+ if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
502
+ return new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
503
+ }
504
+ return true;
505
+ }
506
+ CLTVMultisigTapscript2.isScriptValid = isScriptValid;
507
+ })(CLTVMultisigTapscript || (CLTVMultisigTapscript = {}));
508
+ var getNetwork = (network) => {
509
+ const found = networks[network];
510
+ if (!found) throw new Error(`Unsupported network: ${network}`);
511
+ return found;
512
+ };
513
+ var networkFromOperatorInfo = (info) => getNetwork(info.network);
514
+ var networkFromArkadeInfo = networkFromOperatorInfo;
515
+ var networks = {
516
+ bitcoin: withArkPrefix(NETWORK, "ark", "bitcoin"),
517
+ testnet: withArkPrefix(TEST_NETWORK, "tark", "testnet"),
518
+ signet: withArkPrefix(TEST_NETWORK, "tark", "signet"),
519
+ mutinynet: withArkPrefix(TEST_NETWORK, "tark", "mutinynet"),
520
+ regtest: withArkPrefix(
521
+ {
522
+ ...TEST_NETWORK,
523
+ bech32: "bcrt",
524
+ pubKeyHash: 111,
525
+ scriptHash: 196
526
+ },
527
+ "tark",
528
+ "regtest"
529
+ )
530
+ };
531
+ function withArkPrefix(network, prefix, name) {
532
+ return {
533
+ ...network,
534
+ hrp: prefix,
535
+ name
536
+ };
537
+ }
538
+ var DEFAULT_ARKADE_SERVER_URL = "https://arkade.computer";
539
+ var DEFAULT_ARKADE_EMULATOR_URL = "https://emulator.arkade.computer";
540
+ var DEFAULT_NETWORK = networks.bitcoin;
541
+ var DEFAULT_NETWORK_NAME = "bitcoin";
542
+ var COMPRESSED_PUBKEY = /^0[23][0-9a-f]{64}$/;
543
+ var BITCOIN_EMULATOR_PUBKEY = "0239c196415da47b26456a101daaa12ba9e445bfe153197f1e2b750bf40e52092e";
544
+ var MUTINYNET_EMULATOR_PUBKEY = "03f823b9b2febc81f4af967e77aed2f541cbd3397c6d8f5a72e32eb7b471af889a";
545
+ var REGTEST_EMULATOR_PUBKEY = "02999413c46fa10ada5cbc4bcc79a1d09160c2ba3cfc812705d7a13e5e545fb2a9";
546
+ var EMULATOR_PUBKEYS = {
547
+ bitcoin: BITCOIN_EMULATOR_PUBKEY,
548
+ mutinynet: MUTINYNET_EMULATOR_PUBKEY,
549
+ regtest: REGTEST_EMULATOR_PUBKEY
550
+ };
551
+ function defaultEmulatorPubkey(network) {
552
+ const pinned = network.name ? EMULATOR_PUBKEYS[network.name] : void 0;
553
+ if (!pinned) {
554
+ const cause = network.name ? `no emulator is deployed for ${network.name}` : `this Network carries no name, so it cannot be matched (build it with getNetwork(...) rather than by hand)`;
555
+ throw new Error(
556
+ `No emulator co-signer key is pinned for this network: ${cause}; pass emulatorPubkey: "<33-byte compressed hex>" to Arkade.connect to co-sign with your own emulator instead. Pinned networks: ${Object.keys(EMULATOR_PUBKEYS).join(", ")}`
557
+ );
558
+ }
559
+ return pinned;
560
+ }
561
+ function resolveEmulatorPubkey(network, override) {
562
+ if (override === void 0) return defaultEmulatorPubkey(network);
563
+ if (!COMPRESSED_PUBKEY.test(override)) {
564
+ throw new Error(
565
+ `Emulator pubkey override must be 33-byte compressed secp256k1 hex (66 lowercase chars, 02/03 prefix), got ${JSON.stringify(override)}.`
566
+ );
567
+ }
568
+ return override;
569
+ }
570
+ var ArkAddress = class _ArkAddress {
571
+ /**
572
+ * Create an Arkade address from its server public key, Taproot output key, and prefix.
573
+ *
574
+ * @param serverPubKey - 32-byte Arkade server public key
575
+ * @param vtxoTaprootKey - 32-byte Taproot output key (a.k.a. tweaked public key)
576
+ * @param hrp - Bech32 human-readable prefix
577
+ * @param version - Address version byte
578
+ * @defaultValue `version = 0`
579
+ * @throws Error if either public key is not 32 bytes long
580
+ */
581
+ constructor(serverPubKey, vtxoTaprootKey, hrp = DEFAULT_NETWORK.hrp, version = 0) {
582
+ this.serverPubKey = serverPubKey;
583
+ this.vtxoTaprootKey = vtxoTaprootKey;
584
+ this.hrp = hrp;
585
+ this.version = version;
586
+ if (serverPubKey.length !== 32) {
587
+ throw new Error(
588
+ "Invalid server public key length, expected 32 bytes, got " + serverPubKey.length
589
+ );
590
+ }
591
+ if (vtxoTaprootKey.length !== 32) {
592
+ throw new Error(
593
+ "Invalid vtxo taproot public key length, expected 32 bytes, got " + vtxoTaprootKey.length
594
+ );
595
+ }
596
+ }
597
+ serverPubKey;
598
+ vtxoTaprootKey;
599
+ hrp;
600
+ version;
601
+ /**
602
+ * Decode an Arkade address from its bech32m string form.
603
+ *
604
+ * @param address - Bech32m-encoded Arkade address
605
+ * @returns Decoded Arkade address
606
+ * @throws Error if the address is malformed or has an invalid payload length
607
+ * @see encode
608
+ */
609
+ static decode(address) {
610
+ const decoded = bech32m.decodeUnsafe(address, 1023);
611
+ if (!decoded) {
612
+ throw new Error("Invalid address");
613
+ }
614
+ const data = new Uint8Array(bech32m.fromWords(decoded.words));
615
+ if (data.length !== 1 + 32 + 32) {
616
+ throw new Error("Invalid data length, expected 65 bytes, got " + data.length);
617
+ }
618
+ const version = data[0];
619
+ const serverPubKey = data.slice(1, 33);
620
+ const vtxoTaprootPubKey = data.slice(33, 65);
621
+ return new _ArkAddress(serverPubKey, vtxoTaprootPubKey, decoded.prefix, version);
622
+ }
623
+ /**
624
+ * Encode the address to its bech32m string form.
625
+ *
626
+ * @returns Bech32m-encoded Arkade address
627
+ * @see decode
628
+ */
629
+ encode() {
630
+ const data = new Uint8Array(1 + 32 + 32);
631
+ data[0] = this.version;
632
+ data.set(this.serverPubKey, 1);
633
+ data.set(this.vtxoTaprootKey, 33);
634
+ const words = bech32m.toWords(data);
635
+ return bech32m.encode(this.hrp, words, 1023);
636
+ }
637
+ /** ScriptPubKey used to send non-dust funds to the address. */
638
+ get pkScript() {
639
+ return Script$1.encode(["OP_1", this.vtxoTaprootKey]);
640
+ }
641
+ /** ScriptPubKey used to send sub-dust funds to the address. */
642
+ get subdustPkScript() {
643
+ return Script$1.encode(["RETURN", this.vtxoTaprootKey]);
644
+ }
645
+ };
646
+ function assembleBtcdTaprootTree(scripts) {
647
+ if (scripts.length === 0) {
648
+ throw new Error("assembleBtcdTaprootTree: empty scripts list");
649
+ }
650
+ const leaves = scripts.map((script) => ({
651
+ script,
652
+ leafVersion: TAP_LEAF_VERSION
653
+ }));
654
+ if (leaves.length === 1) {
655
+ return leaves[0];
656
+ }
657
+ const branches = [];
658
+ for (let i = 0; i < leaves.length; i += 2) {
659
+ if (i === leaves.length - 1) {
660
+ const last = branches.pop();
661
+ if (last === void 0) {
662
+ throw new Error(
663
+ `assembleBtcdTaprootTree: unexpected odd leaf at i=${i} with no prior branch`
664
+ );
665
+ }
666
+ branches.push([last, leaves[i]]);
667
+ } else {
668
+ branches.push([leaves[i], leaves[i + 1]]);
669
+ }
670
+ }
671
+ while (branches.length >= 2) {
672
+ const left = branches.shift();
673
+ const right = branches.shift();
674
+ branches.push([left, right]);
675
+ }
676
+ return branches[0];
677
+ }
678
+ function btcdLeafLayout(count) {
679
+ const placeholders = Array.from({ length: count }, () => new Uint8Array());
680
+ const indexOf = new Map(placeholders.map((script, index) => [script, index]));
681
+ const layout = [];
682
+ const walk = (node, depth) => {
683
+ if (Array.isArray(node)) {
684
+ walk(node[0], depth + 1);
685
+ walk(node[1], depth + 1);
686
+ } else {
687
+ layout.push({ index: indexOf.get(node.script), depth });
688
+ }
689
+ };
690
+ walk(assembleBtcdTaprootTree(placeholders), 0);
691
+ return layout;
692
+ }
693
+ var proofTapLeafScriptOf = (coin) => coin.intentTapLeafScript ?? coin.proofTapLeafScript;
694
+ var TapTreeCoder = P.array(
695
+ null,
696
+ P.struct({ depth: P.U8, version: P.U8, script: VarBytes })
697
+ );
698
+ function scriptFromTapLeafScript(leaf) {
699
+ return leaf[1].subarray(0, leaf[1].length - 1);
700
+ }
701
+ var VtxoScript = class _VtxoScript {
702
+ /**
703
+ * Create a virtual output script from its tapleaf scripts.
704
+ *
705
+ * The Taproot script tree is assembled using btcd's algorithm
706
+ * (`txscript.AssembleTaprootScriptTree`) so the derived taproot output
707
+ * key agrees with arkd for any leaf count. `@scure/btc-signer`'s
708
+ * default `taprootListToTree` is a Huffman builder that only agrees
709
+ * with arkd for power-of-2 leaf counts.
710
+ *
711
+ * @param scripts - Raw tapscript bytes for each leaf
712
+ * @throws Error if the provided leaves cannot produce a valid Taproot tree
713
+ */
714
+ constructor(scripts) {
715
+ this.scripts = scripts;
716
+ const tapTree = assembleBtcdTaprootTree(scripts);
717
+ const payment = p2tr(taprootNumsKey(), tapTree, void 0, true);
718
+ if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
719
+ throw new Error("invalid scripts");
720
+ }
721
+ this.leaves = payment.tapLeafScript;
722
+ this.tweakedPublicKey = payment.tweakedPubkey;
723
+ this.pkScript = payment.script;
724
+ }
725
+ scripts;
726
+ leaves;
727
+ tweakedPublicKey;
728
+ pkScript;
729
+ /**
730
+ * Decode a virtual output script from an encoded TapTree.
731
+ *
732
+ * Leaves are normally read in construction order and their depths
733
+ * ignored: that covers the {@link encode} form (every depth 1) and the
734
+ * placeholder depths other writers use, such as arkd's. The exception is
735
+ * the BIP-371 form {@link toBIP371TapTree} writes, whose leaves are in the
736
+ * tree's DFS order: for some leaf counts (6, 7, 10, ...) that differs from
737
+ * construction order. A tree whose depths are exactly the shape
738
+ * `assembleBtcdTaprootTree` builds is that form, so its leaves are mapped
739
+ * back to construction order first. No construction-order writer produces
740
+ * that depth sequence when the two orders differ, so this never reorders
741
+ * a tree that decoded correctly before.
742
+ *
743
+ * Every leaf must carry the base tapscript version: the tree is rebuilt
744
+ * with {@link TAP_LEAF_VERSION} leaves, so any other version would
745
+ * silently derive a different taproot key.
746
+ *
747
+ * @param tapTree - Encoded TapTree bytes
748
+ * @returns Decoded virtual output script
749
+ * @throws Error if the TapTree cannot be decoded into a valid script set
750
+ * or a leaf carries a version other than {@link TAP_LEAF_VERSION}
751
+ * @see encode
752
+ */
753
+ static decode(tapTree) {
754
+ const leaves = TapTreeCoder.decode(tapTree);
755
+ const foreign = leaves.find((leaf) => leaf.version !== TAP_LEAF_VERSION);
756
+ if (foreign) {
757
+ throw new Error(`unsupported tap leaf version 0x${foreign.version.toString(16)}`);
758
+ }
759
+ const scripts = leaves.map((leaf) => leaf.script);
760
+ if (leaves.every((leaf) => leaf.depth === 1)) {
761
+ return new _VtxoScript(scripts);
762
+ }
763
+ const layout = btcdLeafLayout(leaves.length);
764
+ if (layout.some((slot, i) => slot.depth !== leaves[i].depth)) {
765
+ return new _VtxoScript(scripts);
766
+ }
767
+ const ordered = new Array(leaves.length);
768
+ layout.forEach((slot, i) => {
769
+ ordered[slot.index] = scripts[i];
770
+ });
771
+ return new _VtxoScript(ordered);
772
+ }
773
+ /**
774
+ * Encode the virtual output script to a TapTree byte representation.
775
+ *
776
+ * @returns Encoded TapTree bytes
777
+ * @see decode
778
+ */
779
+ encode() {
780
+ const tapTree = TapTreeCoder.encode(
781
+ this.scripts.map((script) => ({
782
+ depth: 1,
783
+ version: TAP_LEAF_VERSION,
784
+ script
785
+ }))
786
+ );
787
+ return tapTree;
788
+ }
789
+ /**
790
+ * Build the Arkade address corresponding to this virtual output script.
791
+ *
792
+ * @param prefix - Bech32 human-readable prefix
793
+ * @param serverPubKey - 32-byte Arkade server public key
794
+ * @returns Arkade address for this script
795
+ * @see ArkAddress
796
+ */
797
+ address(prefix = DEFAULT_NETWORK.hrp, serverPubKey) {
798
+ return new ArkAddress(serverPubKey, this.tweakedPublicKey, prefix);
799
+ }
800
+ /**
801
+ * Build the Taproot onchain address corresponding to this virtual output script.
802
+ *
803
+ * @param network - Bitcoin network descriptor
804
+ * @returns Taproot onchain address
805
+ * @see address
806
+ */
807
+ onchainAddress(network = DEFAULT_NETWORK) {
808
+ return Address(network).encode({
809
+ type: "tr",
810
+ pubkey: this.tweakedPublicKey
811
+ });
812
+ }
813
+ /**
814
+ * Look up a tapleaf script by its hex-encoded tapscript body.
815
+ *
816
+ * @param scriptHex - Hex-encoded tapscript body without the leaf version byte
817
+ * @returns Matching tapleaf script
818
+ * @throws Error if no matching leaf exists
819
+ */
820
+ findLeaf(scriptHex) {
821
+ const leaf = this.leaves.find(
822
+ (leaf2) => hex.encode(scriptFromTapLeafScript(leaf2)) === scriptHex
823
+ );
824
+ if (!leaf) {
825
+ throw new Error(`leaf '${scriptHex}' not found`);
826
+ }
827
+ return leaf;
828
+ }
829
+ /**
830
+ * Return all unilateral exit paths embedded in the virtual output script.
831
+ *
832
+ * @returns CSV-based exit paths found in the leaves
833
+ * @see getSequence
834
+ */
835
+ exitPaths() {
836
+ const paths = [];
837
+ for (const leaf of this.leaves) {
838
+ try {
839
+ const script = scriptFromTapLeafScript(leaf);
840
+ if (CSVMultisigTapscript.isScriptValid(script) === true) {
841
+ const tapScript = CSVMultisigTapscript.decode(script);
842
+ paths.push(tapScript);
843
+ } else if (ConditionCSVMultisigTapscript.isScriptValid(script) === true) {
844
+ const tapScript = ConditionCSVMultisigTapscript.decode(script);
845
+ paths.push(tapScript);
846
+ }
847
+ } catch (e) {
848
+ console.debug("Failed to decode script", e);
849
+ }
850
+ }
851
+ return paths;
852
+ }
853
+ };
854
+ function toBIP371TapTree(tapTree) {
855
+ const scripts = TapTreeCoder.decode(tapTree).map((leaf) => leaf.script);
856
+ const vtxoScript = new VtxoScript(scripts);
857
+ return vtxoScript.leaves.map((leaf) => ({
858
+ depth: leaf[0].merklePath.length,
859
+ version: TAP_LEAF_VERSION,
860
+ script: scriptFromTapLeafScript(leaf)
861
+ }));
862
+ }
863
+ function getSequence(tapLeafScript) {
864
+ let sequence = void 0;
865
+ try {
866
+ const scriptWithLeafVersion = tapLeafScript[1];
867
+ const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
868
+ try {
869
+ const params = CSVMultisigTapscript.decode(script).params;
870
+ sequence = timelockToSequence(params.timelock);
871
+ } catch {
872
+ const params = CLTVMultisigTapscript.decode(script).params;
873
+ sequence = Number(params.absoluteTimelock);
874
+ }
875
+ } catch {
876
+ }
877
+ return sequence;
878
+ }
879
+
880
+ export { ArkAddress, BITCOIN_EMULATOR_PUBKEY, CLTVMultisigTapscript, CSVMultisigTapscript, ConditionCSVMultisigTapscript, ConditionMultisigTapscript, DEFAULT_ARKADE_EMULATOR_URL, DEFAULT_ARKADE_SERVER_URL, DEFAULT_NETWORK, DEFAULT_NETWORK_NAME, MUTINYNET_EMULATOR_PUBKEY, MultisigTapscript, REGTEST_EMULATOR_PUBKEY, TapTreeCoder, VtxoScript, __export, assembleBtcdTaprootTree, decodeTapscript, defaultEmulatorPubkey, getNetwork, getSequence, networkFromArkadeInfo, networkFromOperatorInfo, networks, proofTapLeafScriptOf, resolveEmulatorPubkey, scriptFromTapLeafScript, sequenceToCanonicalTimelock, sequenceToTimelock, timelockToSequence, toBIP371TapTree };
881
+ //# sourceMappingURL=chunk-FDJ734CH.js.map
882
+ //# sourceMappingURL=chunk-FDJ734CH.js.map