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