@arkade-os/sdk 0.4.45 → 0.4.47

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 (91) hide show
  1. package/dist/adapters/expo.cjs +5 -5
  2. package/dist/adapters/expo.d.cts +2 -2
  3. package/dist/adapters/expo.d.ts +2 -2
  4. package/dist/adapters/expo.js +3 -3
  5. package/dist/adapters/indexedDB.cjs +5 -5
  6. package/dist/adapters/indexedDB.js +4 -4
  7. package/dist/{ark-C-PjOngm.d.cts → ark-CoJgwTi_.d.cts} +31 -3
  8. package/dist/{ark-C-PjOngm.d.ts → ark-CoJgwTi_.d.ts} +31 -3
  9. package/dist/{asyncStorageTaskQueue-BmBPvTyh.d.cts → asyncStorageTaskQueue-Dowyo59a.d.cts} +2 -2
  10. package/dist/{asyncStorageTaskQueue-CHpI3moJ.d.ts → asyncStorageTaskQueue-Z3aFoXCl.d.ts} +2 -2
  11. package/dist/{chunk-QZ6HZZZK.js → chunk-3N5I6GT6.js} +3 -3
  12. package/dist/{chunk-QZ6HZZZK.js.map → chunk-3N5I6GT6.js.map} +1 -1
  13. package/dist/{chunk-BOA4SHCD.js → chunk-AJQ4AOT3.js} +4 -4
  14. package/dist/{chunk-BOA4SHCD.js.map → chunk-AJQ4AOT3.js.map} +1 -1
  15. package/dist/{chunk-BE5MUB7T.js → chunk-C7TXXTMS.js} +9 -5
  16. package/dist/chunk-C7TXXTMS.js.map +1 -0
  17. package/dist/{chunk-YBQV7XBC.js → chunk-CDDWXBEM.js} +139 -21
  18. package/dist/chunk-CDDWXBEM.js.map +1 -0
  19. package/dist/{chunk-UHOHLN3C.cjs → chunk-HHV5FPC6.cjs} +148 -25
  20. package/dist/chunk-HHV5FPC6.cjs.map +1 -0
  21. package/dist/{chunk-HZ7VIN7P.cjs → chunk-JAYOXOLT.cjs} +14 -14
  22. package/dist/{chunk-HZ7VIN7P.cjs.map → chunk-JAYOXOLT.cjs.map} +1 -1
  23. package/dist/{chunk-HXDZ2QTW.cjs → chunk-L5ND2FJJ.cjs} +9 -5
  24. package/dist/chunk-L5ND2FJJ.cjs.map +1 -0
  25. package/dist/{chunk-TGRJ5WAX.js → chunk-OCS5S3L3.js} +1085 -103
  26. package/dist/chunk-OCS5S3L3.js.map +1 -0
  27. package/dist/chunk-RQSA7ADZ.cjs +2286 -0
  28. package/dist/chunk-RQSA7ADZ.cjs.map +1 -0
  29. package/dist/{chunk-OFQGANH2.cjs → chunk-VE67GMGM.cjs} +7 -7
  30. package/dist/{chunk-OFQGANH2.cjs.map → chunk-VE67GMGM.cjs.map} +1 -1
  31. package/dist/{chunk-SRCWBFSZ.cjs → chunk-WTTLIFS7.cjs} +1281 -289
  32. package/dist/chunk-WTTLIFS7.cjs.map +1 -0
  33. package/dist/chunk-XN3554FT.js +2244 -0
  34. package/dist/chunk-XN3554FT.js.map +1 -0
  35. package/dist/contracts/handlers/index.cjs +11 -7
  36. package/dist/contracts/handlers/index.d.cts +3 -3
  37. package/dist/contracts/handlers/index.d.ts +3 -3
  38. package/dist/contracts/handlers/index.js +2 -2
  39. package/dist/{delegate-BKNqva1j.d.ts → delegate-CuaDqqQi.d.ts} +1 -1
  40. package/dist/{delegate-DOEobYwH.d.cts → delegate-Dl87_zMA.d.cts} +1 -1
  41. package/dist/index-BC_G0VTh.d.cts +512 -0
  42. package/dist/index-CKwpZEpw.d.ts +512 -0
  43. package/dist/index.cjs +219 -159
  44. package/dist/index.d.cts +3097 -2090
  45. package/dist/index.d.ts +3097 -2090
  46. package/dist/index.js +4 -4
  47. package/dist/repositories/realm/index.cjs +23 -19
  48. package/dist/repositories/realm/index.cjs.map +1 -1
  49. package/dist/repositories/realm/index.d.cts +2 -2
  50. package/dist/repositories/realm/index.d.ts +2 -2
  51. package/dist/repositories/realm/index.js +9 -5
  52. package/dist/repositories/realm/index.js.map +1 -1
  53. package/dist/repositories/sqlite/index.cjs +22 -18
  54. package/dist/repositories/sqlite/index.cjs.map +1 -1
  55. package/dist/repositories/sqlite/index.d.cts +2 -2
  56. package/dist/repositories/sqlite/index.d.ts +2 -2
  57. package/dist/repositories/sqlite/index.js +9 -5
  58. package/dist/repositories/sqlite/index.js.map +1 -1
  59. package/dist/{taskRunner-BngWSFMi.d.ts → taskRunner-Dz-TI4Rb.d.ts} +2 -2
  60. package/dist/{taskRunner-DOiyRuJa.d.cts → taskRunner-Dz47k0lp.d.cts} +2 -2
  61. package/dist/wallet/expo/background.cjs +14 -14
  62. package/dist/wallet/expo/background.d.cts +3 -3
  63. package/dist/wallet/expo/background.d.ts +3 -3
  64. package/dist/wallet/expo/background.js +6 -6
  65. package/dist/wallet/expo/index.cjs +18 -14
  66. package/dist/wallet/expo/index.cjs.map +1 -1
  67. package/dist/wallet/expo/index.d.cts +4 -4
  68. package/dist/wallet/expo/index.d.ts +4 -4
  69. package/dist/wallet/expo/index.js +9 -5
  70. package/dist/wallet/expo/index.js.map +1 -1
  71. package/dist/{wallet-DBjumS42.d.cts → wallet-Oxz_wLxo.d.cts} +47 -3
  72. package/dist/{wallet-B3awP1Nj.d.ts → wallet-x7egmXq6.d.ts} +47 -3
  73. package/dist/worker/expo/index.cjs +10 -9
  74. package/dist/worker/expo/index.cjs.map +1 -1
  75. package/dist/worker/expo/index.d.cts +4 -4
  76. package/dist/worker/expo/index.d.ts +4 -4
  77. package/dist/worker/expo/index.js +6 -5
  78. package/dist/worker/expo/index.js.map +1 -1
  79. package/package.json +2 -2
  80. package/dist/chunk-32YKLQPS.cjs +0 -970
  81. package/dist/chunk-32YKLQPS.cjs.map +0 -1
  82. package/dist/chunk-BE5MUB7T.js.map +0 -1
  83. package/dist/chunk-HXDZ2QTW.cjs.map +0 -1
  84. package/dist/chunk-SRCWBFSZ.cjs.map +0 -1
  85. package/dist/chunk-TGRJ5WAX.js.map +0 -1
  86. package/dist/chunk-UHOHLN3C.cjs.map +0 -1
  87. package/dist/chunk-YBQV7XBC.js.map +0 -1
  88. package/dist/chunk-YNEDJTX2.js +0 -957
  89. package/dist/chunk-YNEDJTX2.js.map +0 -1
  90. package/dist/index-BlW4I30l.d.cts +0 -259
  91. package/dist/index-Cm2fGeox.d.ts +0 -259
@@ -1,957 +0,0 @@
1
- import { VtxoScript, MultisigTapscript, CSVMultisigTapscript, ConditionMultisigTapscript, CLTVMultisigTapscript, ConditionCSVMultisigTapscript, timelockToSequence, sequenceToTimelock } from './chunk-BE5MUB7T.js';
2
- import { hex } from '@scure/base';
3
- import { networks, expand } from '@bitcoinerlab/descriptors-scure';
4
- import { Script } from '@scure/btc-signer';
5
-
6
- // src/contracts/handlers/registry.ts
7
- var ContractHandlerRegistry = class {
8
- handlers = /* @__PURE__ */ new Map();
9
- /**
10
- * Register a contract handler.
11
- *
12
- * @param handler - The handler to register
13
- * @throws If a handler for this type is already registered
14
- */
15
- register(handler) {
16
- if (this.handlers.has(handler.type)) {
17
- throw new Error(`Contract handler for type '${handler.type}' is already registered`);
18
- }
19
- this.handlers.set(handler.type, handler);
20
- }
21
- /**
22
- * Get a handler by type.
23
- *
24
- * @param type - The contract type
25
- * @returns The handler, or undefined if not found
26
- */
27
- get(type) {
28
- return this.handlers.get(type);
29
- }
30
- /**
31
- * Get a handler by type, throwing if not found.
32
- *
33
- * @param type - The contract type
34
- * @returns The handler
35
- * @throws If no handler is registered for this type
36
- */
37
- getOrThrow(type) {
38
- const handler = this.get(type);
39
- if (!handler) {
40
- throw new Error(`No contract handler registered for type '${type}'`);
41
- }
42
- return handler;
43
- }
44
- /**
45
- * Check if a handler is registered.
46
- *
47
- * @param type - The contract type
48
- */
49
- has(type) {
50
- return this.handlers.has(type);
51
- }
52
- /**
53
- * Get all registered types.
54
- */
55
- getRegisteredTypes() {
56
- return Array.from(this.handlers.keys());
57
- }
58
- /**
59
- * Unregister a handler (mainly for testing).
60
- */
61
- unregister(type) {
62
- return this.handlers.delete(type);
63
- }
64
- /**
65
- * Clear all handlers (mainly for testing).
66
- */
67
- clear() {
68
- this.handlers.clear();
69
- }
70
- };
71
- var contractHandlers = new ContractHandlerRegistry();
72
- var DefaultVtxo;
73
- ((DefaultVtxo2) => {
74
- class Script2 extends VtxoScript {
75
- /** Create the default virtual output script with one forfeit path and one exit path. */
76
- constructor(options) {
77
- const { pubKey, serverPubKey, csvTimelock } = options;
78
- const forfeitScript = MultisigTapscript.encode({
79
- pubkeys: [pubKey, serverPubKey]
80
- }).script;
81
- const exitScript = CSVMultisigTapscript.encode({
82
- timelock: csvTimelock,
83
- pubkeys: [pubKey]
84
- }).script;
85
- super([forfeitScript, exitScript]);
86
- this.options = options;
87
- this.forfeitScript = hex.encode(forfeitScript);
88
- this.exitScript = hex.encode(exitScript);
89
- }
90
- static DEFAULT_TIMELOCK = {
91
- value: 144n,
92
- type: "blocks"
93
- };
94
- // 1 day in blocks
95
- forfeitScript;
96
- exitScript;
97
- /** Return the forfeit tapleaf script. */
98
- forfeit() {
99
- return this.findLeaf(this.forfeitScript);
100
- }
101
- /** Return the unilateral exit tapleaf script. */
102
- exit() {
103
- return this.findLeaf(this.exitScript);
104
- }
105
- }
106
- DefaultVtxo2.Script = Script2;
107
- })(DefaultVtxo || (DefaultVtxo = {}));
108
- function isMainnetDescriptor(descriptor) {
109
- return !descriptor.includes("tpub");
110
- }
111
- function descriptorIsOurs(candidate, ourDescriptor, ourXOnlyPubkey) {
112
- if (!isDescriptor(candidate)) return false;
113
- try {
114
- const candidateInfo = expand({
115
- descriptor: candidate,
116
- network: isMainnetDescriptor(candidate) ? networks.bitcoin : networks.testnet
117
- }).expansionMap?.["@0"];
118
- if (!candidateInfo) return false;
119
- if (candidateInfo.bip32) {
120
- const ourBip32 = expand({
121
- descriptor: ourDescriptor,
122
- network: isMainnetDescriptor(ourDescriptor) ? networks.bitcoin : networks.testnet
123
- }).expansionMap?.["@0"]?.bip32;
124
- if (!ourBip32) return false;
125
- return ourBip32.toBase58() === candidateInfo.bip32.toBase58();
126
- }
127
- if (candidateInfo.pubkey) {
128
- const candidatePub = candidateInfo.pubkey.length === 33 ? candidateInfo.pubkey.subarray(1) : candidateInfo.pubkey;
129
- if (candidatePub.length !== ourXOnlyPubkey.length) return false;
130
- return hex.encode(candidatePub) === hex.encode(ourXOnlyPubkey);
131
- }
132
- return false;
133
- } catch {
134
- return false;
135
- }
136
- }
137
- function isDescriptor(value) {
138
- if (typeof value !== "string") return false;
139
- if (!value.startsWith("tr(") || !value.endsWith(")")) return false;
140
- return value.length > "tr()".length;
141
- }
142
- function normalizeToDescriptor(value) {
143
- if (typeof value !== "string" || value.length === 0) {
144
- throw new Error("normalizeToDescriptor: expected a non-empty string value");
145
- }
146
- if (isDescriptor(value)) {
147
- return value;
148
- }
149
- return `tr(${value})`;
150
- }
151
- function extractPubKey(descriptor) {
152
- if (!isDescriptor(descriptor)) {
153
- return descriptor;
154
- }
155
- const network = isMainnetDescriptor(descriptor) ? networks.bitcoin : networks.testnet;
156
- const expansion = expand({ descriptor, network });
157
- if (!expansion.expansionMap) {
158
- throw new Error("Cannot extract pubkey from descriptor: expansion failed.");
159
- }
160
- const key = expansion.expansionMap["@0"];
161
- if (key?.bip32) {
162
- throw new Error(
163
- "Cannot extract pubkey from HD descriptor without derivation. Use DescriptorProvider to derive the key from the xpub."
164
- );
165
- }
166
- if (!key?.pubkey) {
167
- throw new Error("Cannot extract pubkey from descriptor: no key found.");
168
- }
169
- return hex.encode(key.pubkey);
170
- }
171
- function deriveDescriptorLeafPubKey(descriptor) {
172
- const network = isMainnetDescriptor(descriptor) ? networks.bitcoin : networks.testnet;
173
- let expansion;
174
- try {
175
- expansion = expand({ descriptor, network });
176
- } catch (e) {
177
- throw new Error(
178
- `Cannot derive leaf pubkey from descriptor (length=${descriptor.length}): ensure it is materialized (no wildcard) and parsable.`,
179
- { cause: e }
180
- );
181
- }
182
- const key = expansion.expansionMap?.["@0"];
183
- if (!key?.pubkey) {
184
- throw new Error(
185
- `Cannot derive leaf pubkey from descriptor (length=${descriptor.length}): parsed but no '@0' pubkey in the expansion map.`
186
- );
187
- }
188
- return key.pubkey;
189
- }
190
-
191
- // src/contracts/constants.ts
192
- var DEFAULT_PAGE_SIZE = 500;
193
-
194
- // src/contracts/handlers/helpers.ts
195
- function extractRawPubKey(value) {
196
- if (!isDescriptor(value)) {
197
- return value.toLowerCase();
198
- }
199
- try {
200
- return extractPubKey(value).toLowerCase();
201
- } catch {
202
- return void 0;
203
- }
204
- }
205
- function resolveRole(contract, context) {
206
- if (context.role === "sender" || context.role === "receiver") {
207
- return context.role;
208
- }
209
- const senderKey = contract.params.sender ? extractRawPubKey(contract.params.sender) : void 0;
210
- const receiverKey = contract.params.receiver ? extractRawPubKey(contract.params.receiver) : void 0;
211
- const matchRole = (rawWalletKey) => {
212
- if (!rawWalletKey) return void 0;
213
- if (senderKey && rawWalletKey === senderKey) {
214
- return "sender";
215
- }
216
- if (receiverKey && rawWalletKey === receiverKey) {
217
- return "receiver";
218
- }
219
- return void 0;
220
- };
221
- if (context.walletDescriptor) {
222
- const walletDescriptorKey = extractRawPubKey(context.walletDescriptor);
223
- const matchedRole = matchRole(walletDescriptorKey);
224
- if (matchedRole) {
225
- return matchedRole;
226
- }
227
- if (!walletDescriptorKey && context.walletPubKey) {
228
- return matchRole(extractRawPubKey(context.walletPubKey));
229
- }
230
- return void 0;
231
- }
232
- if (context.walletPubKey) {
233
- return matchRole(extractRawPubKey(context.walletPubKey));
234
- }
235
- return void 0;
236
- }
237
- var CLTV_HEIGHT_THRESHOLD = 500000000n;
238
- function isCltvSatisfied(context, locktime) {
239
- if (locktime < CLTV_HEIGHT_THRESHOLD) {
240
- if (context.blockHeight === void 0) return false;
241
- return BigInt(context.blockHeight) >= locktime;
242
- }
243
- const currentTimeSec = BigInt(Math.floor(context.currentTime / 1e3));
244
- return currentTimeSec >= locktime;
245
- }
246
- function isCsvSpendable(context, sequence) {
247
- if (sequence === void 0) return true;
248
- if (!context.vtxo) return false;
249
- const timelock = sequenceToTimelock(sequence);
250
- if (timelock.type === "blocks") {
251
- if (context.blockHeight === void 0 || context.vtxo.status.block_height === void 0) {
252
- return false;
253
- }
254
- return context.blockHeight - context.vtxo.status.block_height >= Number(timelock.value);
255
- }
256
- if (timelock.type === "seconds") {
257
- const blockTime = context.vtxo.status.block_time;
258
- if (blockTime === void 0) return false;
259
- return context.currentTime / 1e3 - blockTime >= Number(timelock.value);
260
- }
261
- return false;
262
- }
263
- async function detectUsedScripts(indexerProvider, scriptHexes) {
264
- const used = /* @__PURE__ */ new Set();
265
- if (scriptHexes.length === 0) return used;
266
- const remaining = new Set(scriptHexes);
267
- let pageIndex = 0;
268
- let hasMore = true;
269
- while (hasMore && remaining.size > 0) {
270
- const { vtxos, page } = await indexerProvider.getVtxos({
271
- scripts: scriptHexes,
272
- pageIndex,
273
- pageSize: DEFAULT_PAGE_SIZE
274
- });
275
- for (const vtxo of vtxos) {
276
- if (remaining.delete(vtxo.script)) used.add(vtxo.script);
277
- }
278
- hasMore = page ? vtxos.length === DEFAULT_PAGE_SIZE : false;
279
- pageIndex++;
280
- }
281
- return used;
282
- }
283
-
284
- // src/contracts/metadata.ts
285
- var WALLET_RECEIVE_SOURCE = "wallet-receive";
286
-
287
- // src/contracts/handlers/default.ts
288
- function extractPubKeyBytes(value) {
289
- return hex.decode(extractPubKey(normalizeToDescriptor(value)));
290
- }
291
- var DefaultContractHandler = {
292
- type: "default",
293
- createScript(params) {
294
- const typed = this.deserializeParams(params);
295
- return new DefaultVtxo.Script(typed);
296
- },
297
- serializeParams(params) {
298
- return {
299
- pubKey: hex.encode(params.pubKey),
300
- serverPubKey: hex.encode(params.serverPubKey),
301
- csvTimelock: timelockToSequence(params.csvTimelock).toString()
302
- };
303
- },
304
- deserializeParams(params) {
305
- const csvTimelock = params.csvTimelock !== void 0 && params.csvTimelock !== "" ? sequenceToTimelock(Number(params.csvTimelock)) : DefaultVtxo.Script.DEFAULT_TIMELOCK;
306
- return {
307
- pubKey: extractPubKeyBytes(params.pubKey),
308
- serverPubKey: extractPubKeyBytes(params.serverPubKey),
309
- csvTimelock
310
- };
311
- },
312
- selectPath(script, contract, context) {
313
- if (context.collaborative) {
314
- return { leaf: script.forfeit() };
315
- }
316
- const sequence = contract.params.csvTimelock ? Number(contract.params.csvTimelock) : void 0;
317
- if (!isCsvSpendable(context, sequence)) {
318
- return null;
319
- }
320
- return {
321
- leaf: script.exit(),
322
- sequence
323
- };
324
- },
325
- getAllSpendingPaths(script, contract, context) {
326
- const paths = [];
327
- if (context.collaborative) {
328
- paths.push({ leaf: script.forfeit() });
329
- }
330
- const exitPath = { leaf: script.exit() };
331
- if (contract.params.csvTimelock) {
332
- exitPath.sequence = Number(contract.params.csvTimelock);
333
- }
334
- paths.push(exitPath);
335
- return paths;
336
- },
337
- getSpendablePaths(script, contract, context) {
338
- const paths = [];
339
- if (context.collaborative) {
340
- paths.push({ leaf: script.forfeit() });
341
- }
342
- const exitSequence = contract.params.csvTimelock ? Number(contract.params.csvTimelock) : void 0;
343
- if (isCsvSpendable(context, exitSequence)) {
344
- const exitPath = { leaf: script.exit() };
345
- if (exitSequence !== void 0) {
346
- exitPath.sequence = exitSequence;
347
- }
348
- paths.push(exitPath);
349
- }
350
- return paths;
351
- },
352
- async discoverAt(index, descriptor, deps) {
353
- const pubKey = deriveDescriptorLeafPubKey(descriptor);
354
- const signers = [deps.serverPubKey, ...deps.deprecatedSignerPubKeys ?? []];
355
- const seen = /* @__PURE__ */ new Set();
356
- const candidates = [];
357
- for (const serverPubKey of signers) {
358
- for (const csvTimelock of deps.csvTimelocks) {
359
- const script = new DefaultVtxo.Script({
360
- pubKey,
361
- serverPubKey,
362
- csvTimelock
363
- });
364
- const scriptHex = hex.encode(script.pkScript);
365
- if (seen.has(scriptHex)) continue;
366
- seen.add(scriptHex);
367
- candidates.push({ serverPubKey, csvTimelock, script, scriptHex });
368
- }
369
- }
370
- const used = await detectUsedScripts(
371
- deps.indexerProvider,
372
- candidates.map((c) => c.scriptHex)
373
- );
374
- const out = [];
375
- for (const c of candidates) {
376
- if (!used.has(c.scriptHex)) continue;
377
- out.push({
378
- type: "default",
379
- params: {
380
- pubKey: hex.encode(pubKey),
381
- serverPubKey: hex.encode(c.serverPubKey),
382
- csvTimelock: timelockToSequence(c.csvTimelock).toString()
383
- },
384
- script: c.scriptHex,
385
- address: c.script.address(deps.network.hrp, c.serverPubKey).encode(),
386
- ...index > 0 ? {
387
- metadata: {
388
- source: WALLET_RECEIVE_SOURCE,
389
- signingDescriptor: descriptor
390
- }
391
- } : {}
392
- });
393
- }
394
- return out;
395
- }
396
- };
397
- var DelegateVtxo;
398
- ((DelegateVtxo2) => {
399
- class Script2 extends VtxoScript {
400
- /** Create a delegated virtual output script with forfeit, exit, and delegate paths. */
401
- constructor(options) {
402
- const defaultVtxo = new DefaultVtxo.Script(options);
403
- const { delegatePubKey, pubKey, serverPubKey } = options;
404
- const delegateScript = MultisigTapscript.encode({
405
- pubkeys: [pubKey, delegatePubKey, serverPubKey]
406
- }).script;
407
- super([...defaultVtxo.scripts, delegateScript]);
408
- this.options = options;
409
- this.defaultVtxo = defaultVtxo;
410
- this.delegateScript = hex.encode(delegateScript);
411
- }
412
- defaultVtxo;
413
- delegateScript;
414
- /** Return the forfeit tapleaf script. */
415
- forfeit() {
416
- return this.findLeaf(this.defaultVtxo.forfeitScript);
417
- }
418
- /** Return the unilateral exit tapleaf script. */
419
- exit() {
420
- return this.findLeaf(this.defaultVtxo.exitScript);
421
- }
422
- /** Return the delegate tapleaf script. */
423
- delegate() {
424
- return this.findLeaf(this.delegateScript);
425
- }
426
- }
427
- DelegateVtxo2.Script = Script2;
428
- })(DelegateVtxo || (DelegateVtxo = {}));
429
-
430
- // src/contracts/handlers/delegate.ts
431
- var DelegateContractHandler = {
432
- type: "delegate",
433
- createScript(params) {
434
- const typed = this.deserializeParams(params);
435
- return new DelegateVtxo.Script(typed);
436
- },
437
- serializeParams(params) {
438
- return {
439
- pubKey: hex.encode(params.pubKey),
440
- serverPubKey: hex.encode(params.serverPubKey),
441
- delegatePubKey: hex.encode(params.delegatePubKey),
442
- csvTimelock: timelockToSequence(params.csvTimelock).toString()
443
- };
444
- },
445
- deserializeParams(params) {
446
- const csvTimelock = sequenceToTimelock(Number(params.csvTimelock));
447
- return {
448
- pubKey: hex.decode(params.pubKey),
449
- serverPubKey: hex.decode(params.serverPubKey),
450
- delegatePubKey: hex.decode(params.delegatePubKey),
451
- csvTimelock
452
- };
453
- },
454
- selectPath(script, contract, context) {
455
- if (context.collaborative) {
456
- return { leaf: script.forfeit() };
457
- }
458
- const sequence = contract.params.csvTimelock ? Number(contract.params.csvTimelock) : void 0;
459
- if (!isCsvSpendable(context, sequence)) {
460
- return null;
461
- }
462
- return {
463
- leaf: script.exit(),
464
- sequence
465
- };
466
- },
467
- getAllSpendingPaths(script, contract, context) {
468
- const paths = [];
469
- if (context.collaborative) {
470
- paths.push({ leaf: script.forfeit() });
471
- }
472
- const exitPath = { leaf: script.exit() };
473
- if (contract.params.csvTimelock) {
474
- exitPath.sequence = Number(contract.params.csvTimelock);
475
- }
476
- paths.push(exitPath);
477
- if (context.collaborative) {
478
- paths.push({ leaf: script.delegate() });
479
- }
480
- return paths;
481
- },
482
- getSpendablePaths(script, contract, context) {
483
- const paths = [];
484
- if (context.collaborative) {
485
- paths.push({ leaf: script.forfeit() });
486
- }
487
- const exitSequence = contract.params.csvTimelock ? Number(contract.params.csvTimelock) : void 0;
488
- if (isCsvSpendable(context, exitSequence)) {
489
- const exitPath = { leaf: script.exit() };
490
- if (exitSequence !== void 0) {
491
- exitPath.sequence = exitSequence;
492
- }
493
- paths.push(exitPath);
494
- }
495
- return paths;
496
- },
497
- async discoverAt(index, descriptor, deps) {
498
- if (!deps.delegatePubKey) return [];
499
- const pubKey = deriveDescriptorLeafPubKey(descriptor);
500
- const signers = [deps.serverPubKey, ...deps.deprecatedSignerPubKeys ?? []];
501
- const seen = /* @__PURE__ */ new Set();
502
- const candidates = [];
503
- for (const serverPubKey of signers) {
504
- for (const csvTimelock of deps.csvTimelocks) {
505
- const script = new DelegateVtxo.Script({
506
- pubKey,
507
- serverPubKey,
508
- delegatePubKey: deps.delegatePubKey,
509
- csvTimelock
510
- });
511
- const scriptHex = hex.encode(script.pkScript);
512
- if (seen.has(scriptHex)) continue;
513
- seen.add(scriptHex);
514
- candidates.push({ serverPubKey, csvTimelock, script, scriptHex });
515
- }
516
- }
517
- const used = await detectUsedScripts(
518
- deps.indexerProvider,
519
- candidates.map((c) => c.scriptHex)
520
- );
521
- const out = [];
522
- for (const c of candidates) {
523
- if (!used.has(c.scriptHex)) continue;
524
- out.push({
525
- type: "delegate",
526
- params: {
527
- pubKey: hex.encode(pubKey),
528
- serverPubKey: hex.encode(c.serverPubKey),
529
- delegatePubKey: hex.encode(deps.delegatePubKey),
530
- csvTimelock: timelockToSequence(c.csvTimelock).toString()
531
- },
532
- script: c.scriptHex,
533
- address: c.script.address(deps.network.hrp, c.serverPubKey).encode(),
534
- ...index > 0 ? {
535
- metadata: {
536
- source: WALLET_RECEIVE_SOURCE,
537
- signingDescriptor: descriptor
538
- }
539
- } : {}
540
- });
541
- }
542
- return out;
543
- }
544
- };
545
- var VHTLC;
546
- ((VHTLC2) => {
547
- class Script2 extends VtxoScript {
548
- /** Create a VHTLC script from the supplied participant keys, hash, and timelocks. */
549
- constructor(options) {
550
- validateOptions(options);
551
- const {
552
- sender,
553
- receiver,
554
- server,
555
- preimageHash,
556
- refundLocktime,
557
- unilateralClaimDelay,
558
- unilateralRefundDelay,
559
- unilateralRefundWithoutReceiverDelay
560
- } = options;
561
- const conditionScript = preimageConditionScript(preimageHash);
562
- const claimScript = ConditionMultisigTapscript.encode({
563
- conditionScript,
564
- pubkeys: [receiver, server]
565
- }).script;
566
- const refundScript = MultisigTapscript.encode({
567
- pubkeys: [sender, receiver, server]
568
- }).script;
569
- const refundWithoutReceiverScript = CLTVMultisigTapscript.encode({
570
- absoluteTimelock: refundLocktime,
571
- pubkeys: [sender, server]
572
- }).script;
573
- const unilateralClaimScript = ConditionCSVMultisigTapscript.encode({
574
- conditionScript,
575
- timelock: unilateralClaimDelay,
576
- pubkeys: [receiver]
577
- }).script;
578
- const unilateralRefundScript = CSVMultisigTapscript.encode({
579
- timelock: unilateralRefundDelay,
580
- pubkeys: [sender, receiver]
581
- }).script;
582
- const unilateralRefundWithoutReceiverScript = CSVMultisigTapscript.encode({
583
- timelock: unilateralRefundWithoutReceiverDelay,
584
- pubkeys: [sender]
585
- }).script;
586
- super([
587
- claimScript,
588
- refundScript,
589
- refundWithoutReceiverScript,
590
- unilateralClaimScript,
591
- unilateralRefundScript,
592
- unilateralRefundWithoutReceiverScript
593
- ]);
594
- this.options = options;
595
- this.claimScript = hex.encode(claimScript);
596
- this.refundScript = hex.encode(refundScript);
597
- this.refundWithoutReceiverScript = hex.encode(refundWithoutReceiverScript);
598
- this.unilateralClaimScript = hex.encode(unilateralClaimScript);
599
- this.unilateralRefundScript = hex.encode(unilateralRefundScript);
600
- this.unilateralRefundWithoutReceiverScript = hex.encode(
601
- unilateralRefundWithoutReceiverScript
602
- );
603
- }
604
- claimScript;
605
- refundScript;
606
- refundWithoutReceiverScript;
607
- unilateralClaimScript;
608
- unilateralRefundScript;
609
- unilateralRefundWithoutReceiverScript;
610
- /** Return the collaborative claim tapleaf script. */
611
- claim() {
612
- return this.findLeaf(this.claimScript);
613
- }
614
- /** Return the collaborative refund tapleaf script. */
615
- refund() {
616
- return this.findLeaf(this.refundScript);
617
- }
618
- /** Return the refund-without-receiver tapleaf script. */
619
- refundWithoutReceiver() {
620
- return this.findLeaf(this.refundWithoutReceiverScript);
621
- }
622
- /** Return the unilateral claim tapleaf script. */
623
- unilateralClaim() {
624
- return this.findLeaf(this.unilateralClaimScript);
625
- }
626
- /** Return the unilateral refund tapleaf script. */
627
- unilateralRefund() {
628
- return this.findLeaf(this.unilateralRefundScript);
629
- }
630
- /** Return the unilateral refund-without-receiver tapleaf script. */
631
- unilateralRefundWithoutReceiver() {
632
- return this.findLeaf(this.unilateralRefundWithoutReceiverScript);
633
- }
634
- }
635
- VHTLC2.Script = Script2;
636
- function validateOptions(options) {
637
- const {
638
- sender,
639
- receiver,
640
- server,
641
- preimageHash,
642
- refundLocktime,
643
- unilateralClaimDelay,
644
- unilateralRefundDelay,
645
- unilateralRefundWithoutReceiverDelay
646
- } = options;
647
- if (!preimageHash || preimageHash.length !== 20) {
648
- throw new Error("preimage hash must be 20 bytes");
649
- }
650
- if (!receiver || receiver.length !== 32) {
651
- throw new Error("Invalid public key length (receiver)");
652
- }
653
- if (!sender || sender.length !== 32) {
654
- throw new Error("Invalid public key length (sender)");
655
- }
656
- if (!server || server.length !== 32) {
657
- throw new Error("Invalid public key length (server)");
658
- }
659
- if (typeof refundLocktime !== "bigint" || refundLocktime <= 0n) {
660
- throw new Error("refund locktime must be greater than 0");
661
- }
662
- if (!unilateralClaimDelay || typeof unilateralClaimDelay.value !== "bigint" || unilateralClaimDelay.value <= 0n) {
663
- throw new Error("unilateral claim delay must greater than 0");
664
- }
665
- if (unilateralClaimDelay.type === "seconds" && unilateralClaimDelay.value % 512n !== 0n) {
666
- throw new Error("seconds timelock must be multiple of 512");
667
- }
668
- if (unilateralClaimDelay.type === "seconds" && unilateralClaimDelay.value < 512n) {
669
- throw new Error("seconds timelock must be greater or equal to 512");
670
- }
671
- if (!unilateralRefundDelay || typeof unilateralRefundDelay.value !== "bigint" || unilateralRefundDelay.value <= 0n) {
672
- throw new Error("unilateral refund delay must greater than 0");
673
- }
674
- if (unilateralRefundDelay.type === "seconds" && unilateralRefundDelay.value % 512n !== 0n) {
675
- throw new Error("seconds timelock must be multiple of 512");
676
- }
677
- if (unilateralRefundDelay.type === "seconds" && unilateralRefundDelay.value < 512n) {
678
- throw new Error("seconds timelock must be greater or equal to 512");
679
- }
680
- if (!unilateralRefundWithoutReceiverDelay || typeof unilateralRefundWithoutReceiverDelay.value !== "bigint" || unilateralRefundWithoutReceiverDelay.value <= 0n) {
681
- throw new Error("unilateral refund without receiver delay must greater than 0");
682
- }
683
- if (unilateralRefundWithoutReceiverDelay.type === "seconds" && unilateralRefundWithoutReceiverDelay.value % 512n !== 0n) {
684
- throw new Error("seconds timelock must be multiple of 512");
685
- }
686
- if (unilateralRefundWithoutReceiverDelay.type === "seconds" && unilateralRefundWithoutReceiverDelay.value < 512n) {
687
- throw new Error("seconds timelock must be greater or equal to 512");
688
- }
689
- }
690
- })(VHTLC || (VHTLC = {}));
691
- function preimageConditionScript(preimageHash) {
692
- return Script.encode(["HASH160", preimageHash, "EQUAL"]);
693
- }
694
-
695
- // src/contracts/handlers/vhtlc.ts
696
- var VHTLCContractHandler = {
697
- type: "vhtlc",
698
- createScript(params) {
699
- const typed = this.deserializeParams(params);
700
- return new VHTLC.Script(typed);
701
- },
702
- serializeParams(params) {
703
- return {
704
- sender: hex.encode(params.sender),
705
- receiver: hex.encode(params.receiver),
706
- server: hex.encode(params.server),
707
- hash: hex.encode(params.preimageHash),
708
- refundLocktime: params.refundLocktime.toString(),
709
- claimDelay: timelockToSequence(params.unilateralClaimDelay).toString(),
710
- refundDelay: timelockToSequence(params.unilateralRefundDelay).toString(),
711
- refundNoReceiverDelay: timelockToSequence(
712
- params.unilateralRefundWithoutReceiverDelay
713
- ).toString()
714
- };
715
- },
716
- deserializeParams(params) {
717
- return {
718
- sender: hex.decode(params.sender),
719
- receiver: hex.decode(params.receiver),
720
- server: hex.decode(params.server),
721
- preimageHash: hex.decode(params.hash),
722
- refundLocktime: BigInt(params.refundLocktime),
723
- unilateralClaimDelay: sequenceToTimelock(Number(params.claimDelay)),
724
- unilateralRefundDelay: sequenceToTimelock(Number(params.refundDelay)),
725
- unilateralRefundWithoutReceiverDelay: sequenceToTimelock(
726
- Number(params.refundNoReceiverDelay)
727
- )
728
- };
729
- },
730
- /**
731
- * Select spending path based on context.
732
- *
733
- * Role is determined from `context.role` or by matching
734
- * `context.walletDescriptor` (preferred) / `context.walletPubKey`
735
- * against sender/receiver in contract params.
736
- */
737
- selectPath(script, contract, context) {
738
- const role = resolveRole(contract, context);
739
- const preimage = contract.params?.preimage;
740
- const refundLocktime = BigInt(contract.params.refundLocktime);
741
- if (!role) {
742
- return null;
743
- }
744
- if (context.collaborative) {
745
- if (role === "receiver" && preimage) {
746
- return {
747
- leaf: script.claim(),
748
- extraWitness: [hex.decode(preimage)]
749
- };
750
- }
751
- if (role === "sender" && isCltvSatisfied(context, refundLocktime)) {
752
- return {
753
- leaf: script.refundWithoutReceiver()
754
- };
755
- }
756
- return null;
757
- }
758
- if (role === "receiver" && preimage) {
759
- const sequence = Number(contract.params.claimDelay);
760
- if (!isCsvSpendable(context, sequence)) return null;
761
- return {
762
- leaf: script.unilateralClaim(),
763
- extraWitness: [hex.decode(preimage)],
764
- sequence
765
- };
766
- }
767
- if (role === "sender") {
768
- const sequence = Number(contract.params.refundNoReceiverDelay);
769
- if (!isCsvSpendable(context, sequence)) return null;
770
- return {
771
- leaf: script.unilateralRefundWithoutReceiver(),
772
- sequence
773
- };
774
- }
775
- return null;
776
- },
777
- /**
778
- * Get all possible spending paths (no timelock checks).
779
- *
780
- * Role is determined from `context.role` or by matching
781
- * `context.walletDescriptor` (preferred) / `context.walletPubKey`
782
- * against sender/receiver in contract params.
783
- */
784
- getAllSpendingPaths(script, contract, context) {
785
- const role = resolveRole(contract, context);
786
- const paths = [];
787
- if (!role) {
788
- return paths;
789
- }
790
- const preimage = contract.params?.preimage;
791
- if (context.collaborative) {
792
- if (role === "receiver" && preimage) {
793
- paths.push({
794
- leaf: script.claim(),
795
- extraWitness: [hex.decode(preimage)]
796
- });
797
- }
798
- if (role === "sender") {
799
- paths.push({
800
- leaf: script.refundWithoutReceiver()
801
- });
802
- }
803
- } else {
804
- if (role === "receiver" && preimage) {
805
- const sequence = Number(contract.params.claimDelay);
806
- paths.push({
807
- leaf: script.unilateralClaim(),
808
- extraWitness: [hex.decode(preimage)],
809
- sequence
810
- });
811
- }
812
- if (role === "sender") {
813
- const sequence = Number(contract.params.refundNoReceiverDelay);
814
- paths.push({
815
- leaf: script.unilateralRefundWithoutReceiver(),
816
- sequence
817
- });
818
- }
819
- }
820
- return paths;
821
- },
822
- getSpendablePaths(script, contract, context) {
823
- const role = resolveRole(contract, context);
824
- const paths = [];
825
- if (!role) {
826
- return paths;
827
- }
828
- const preimage = contract.params?.preimage;
829
- const refundLocktime = BigInt(contract.params.refundLocktime);
830
- if (context.collaborative) {
831
- if (role === "receiver" && preimage) {
832
- paths.push({
833
- leaf: script.claim(),
834
- extraWitness: [hex.decode(preimage)]
835
- });
836
- }
837
- if (role === "sender" && isCltvSatisfied(context, refundLocktime)) {
838
- paths.push({
839
- leaf: script.refundWithoutReceiver()
840
- });
841
- }
842
- return paths;
843
- }
844
- if (role === "receiver" && preimage) {
845
- const sequence = Number(contract.params.claimDelay);
846
- if (isCsvSpendable(context, sequence)) {
847
- paths.push({
848
- leaf: script.unilateralClaim(),
849
- extraWitness: [hex.decode(preimage)],
850
- sequence
851
- });
852
- }
853
- }
854
- if (role === "sender") {
855
- const sequence = Number(contract.params.refundNoReceiverDelay);
856
- if (isCsvSpendable(context, sequence)) {
857
- paths.push({
858
- leaf: script.unilateralRefundWithoutReceiver(),
859
- sequence
860
- });
861
- }
862
- }
863
- return paths;
864
- }
865
- };
866
- var BoardingContractHandler = {
867
- type: "boarding",
868
- createScript(params) {
869
- return DefaultContractHandler.createScript(params);
870
- },
871
- serializeParams(params) {
872
- return DefaultContractHandler.serializeParams(params);
873
- },
874
- deserializeParams(params) {
875
- return DefaultContractHandler.deserializeParams(params);
876
- },
877
- selectPath(script, contract, context) {
878
- return DefaultContractHandler.selectPath(script, contract, context);
879
- },
880
- getAllSpendingPaths(script, contract, context) {
881
- return DefaultContractHandler.getAllSpendingPaths(script, contract, context);
882
- },
883
- getSpendablePaths(script, contract, context) {
884
- return DefaultContractHandler.getSpendablePaths(script, contract, context);
885
- },
886
- /**
887
- * Probe the on-chain UTXO set for a boarding output at this HD index.
888
- *
889
- * Boarding's source of truth is the **current** on-chain coin set
890
- * (`OnchainProvider.getCoins`), not the Ark indexer: a boarded (spent)
891
- * boarding output becomes an L2 VTXO at the receive index, so the
892
- * indexer probe already keeps the gap window open for it; only an
893
- * *unspent* boarding output needs the on-chain probe (see plan §2).
894
- *
895
- * No-ops (returns `[]`) when boarding discovery is not plumbed — i.e.
896
- * `deps.boardingTimelock` or `deps.onchainNetwork` is absent — so the
897
- * scanner unit harness (which sets neither) is unaffected.
898
- *
899
- * Always emits `type: "boarding"`, even in the degenerate equal-delay case
900
- * where the boarding script is byte-identical to a `default` script at this
901
- * index (`boardingExitDelay === unilateralExitDelay`). The collision is no
902
- * longer pre-judged in discovery; it is tolerated FIRST-WINS at the
903
- * persistence layer ({@link ContractManager.upsertContract}). `deps.csvTimelocks`
904
- * is intentionally not read here.
905
- */
906
- async discoverAt(index, descriptor, deps) {
907
- if (!deps.boardingTimelock || !deps.onchainNetwork) return [];
908
- const pubKey = deriveDescriptorLeafPubKey(descriptor);
909
- const script = new DefaultVtxo.Script({
910
- pubKey,
911
- serverPubKey: deps.serverPubKey,
912
- csvTimelock: deps.boardingTimelock
913
- });
914
- const onchainAddress = script.onchainAddress(deps.onchainNetwork);
915
- const coins = await deps.onchainProvider.getCoins(onchainAddress);
916
- if (coins.length === 0) return [];
917
- const scriptHex = hex.encode(script.pkScript);
918
- const boardingSeq = timelockToSequence(deps.boardingTimelock);
919
- return [
920
- {
921
- type: "boarding",
922
- params: {
923
- pubKey: hex.encode(pubKey),
924
- serverPubKey: hex.encode(deps.serverPubKey),
925
- csvTimelock: boardingSeq.toString()
926
- },
927
- script: scriptHex,
928
- // The persisted row's `address` is the *Ark* address (not the
929
- // on-chain P2TR), matching the row registered at init so the
930
- // ContractWatcher keeps monitoring the same L2 script and the
931
- // VTXO repository bucket lines up (plan §6-I.4). The P2TR is
932
- // recomputed from params only for the `getCoins` probe.
933
- address: script.address(deps.network.hrp, deps.serverPubKey).encode(),
934
- // Tag rotated rows (index > 0) so boot resolution can find the
935
- // newest boarding address and descriptor-aware signing can
936
- // recover the per-index key (plan §6-II/§6-III.3). The index-0
937
- // baseline stays untagged, matching `default`/`delegate`.
938
- ...index > 0 ? {
939
- metadata: {
940
- source: WALLET_RECEIVE_SOURCE,
941
- signingDescriptor: descriptor
942
- }
943
- } : {}
944
- }
945
- ];
946
- }
947
- };
948
-
949
- // src/contracts/handlers/index.ts
950
- contractHandlers.register(DefaultContractHandler);
951
- contractHandlers.register(DelegateContractHandler);
952
- contractHandlers.register(VHTLCContractHandler);
953
- contractHandlers.register(BoardingContractHandler);
954
-
955
- export { BoardingContractHandler, DEFAULT_PAGE_SIZE, DefaultContractHandler, DefaultVtxo, DelegateContractHandler, DelegateVtxo, VHTLC, VHTLCContractHandler, WALLET_RECEIVE_SOURCE, contractHandlers, deriveDescriptorLeafPubKey, descriptorIsOurs, isCltvSatisfied, isCsvSpendable, isMainnetDescriptor };
956
- //# sourceMappingURL=chunk-YNEDJTX2.js.map
957
- //# sourceMappingURL=chunk-YNEDJTX2.js.map