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