@arkade-os/sdk 0.4.23 → 0.4.24

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 (40) hide show
  1. package/README.md +21 -1
  2. package/dist/cjs/contracts/contractManager.js +29 -4
  3. package/dist/cjs/contracts/contractWatcher.js +9 -3
  4. package/dist/cjs/contracts/handlers/default.js +3 -2
  5. package/dist/cjs/contracts/handlers/delegate.js +3 -2
  6. package/dist/cjs/contracts/handlers/helpers.js +2 -58
  7. package/dist/cjs/contracts/handlers/vhtlc.js +7 -6
  8. package/dist/cjs/contracts/vtxoOwnership.js +60 -0
  9. package/dist/cjs/index.js +3 -3
  10. package/dist/cjs/script/base.js +12 -47
  11. package/dist/cjs/script/tapscript.js +97 -73
  12. package/dist/cjs/utils/timelock.js +59 -0
  13. package/dist/cjs/utils/unknownFields.js +2 -39
  14. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +59 -9
  15. package/dist/cjs/wallet/unroll.js +79 -67
  16. package/dist/cjs/wallet/wallet.js +78 -8
  17. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +7 -2
  18. package/dist/esm/contracts/contractManager.js +29 -4
  19. package/dist/esm/contracts/contractWatcher.js +9 -3
  20. package/dist/esm/contracts/handlers/default.js +2 -1
  21. package/dist/esm/contracts/handlers/delegate.js +2 -1
  22. package/dist/esm/contracts/handlers/helpers.js +1 -22
  23. package/dist/esm/contracts/handlers/vhtlc.js +2 -1
  24. package/dist/esm/contracts/vtxoOwnership.js +53 -0
  25. package/dist/esm/index.js +1 -1
  26. package/dist/esm/script/base.js +12 -14
  27. package/dist/esm/script/tapscript.js +97 -40
  28. package/dist/esm/utils/timelock.js +22 -0
  29. package/dist/esm/utils/unknownFields.js +2 -6
  30. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +59 -9
  31. package/dist/esm/wallet/unroll.js +78 -67
  32. package/dist/esm/wallet/wallet.js +76 -6
  33. package/dist/esm/worker/expo/processors/contractPollProcessor.js +7 -2
  34. package/dist/types/contracts/handlers/helpers.d.ts +0 -9
  35. package/dist/types/contracts/vtxoOwnership.d.ts +25 -0
  36. package/dist/types/index.d.ts +1 -1
  37. package/dist/types/script/tapscript.d.ts +4 -0
  38. package/dist/types/utils/timelock.d.ts +9 -0
  39. package/dist/types/wallet/unroll.d.ts +10 -0
  40. package/package.json +1 -1
package/README.md CHANGED
@@ -733,7 +733,7 @@ for await (const step of session) {
733
733
  console.log(`Waiting for transaction ${step.txid} to be confirmed`);
734
734
  break;
735
735
  case Unroll.StepType.UNROLL:
736
- console.log(`Broadcasting transaction ${step.tx.id}`);
736
+ console.log(`Transaction ${step.tx.id} unrolled`);
737
737
  break;
738
738
  case Unroll.StepType.DONE:
739
739
  console.log(`Unrolling complete for virtual output ${step.vtxoTxid}`);
@@ -749,6 +749,26 @@ The unrolling process works by:
749
749
  - Waiting for confirmations between steps
750
750
  - Using P2A (Pay-to-Anchor) transactions to pay for fees
751
751
 
752
+ Optionally, you can use `session.next()` to control the broadcasting process manually.
753
+
754
+ ```typescript
755
+ const step = await session.next();
756
+ switch (step.type) {
757
+ case Unroll.StepType.WAIT:
758
+ await step.do(); // wait for the transaction to be confirmed
759
+ break;
760
+ case Unroll.StepType.UNROLL:
761
+ const [parent, child] = step.pkg;
762
+ console.log(`Parent: ${parent}`)
763
+ console.log(`Child: ${child}`)
764
+ await step.do(); // broadcast the 1C1P package
765
+ break;
766
+ case Unroll.StepType.DONE:
767
+ console.log(`Unrolling complete for VTXO ${step.vtxoTxid}`);
768
+ break;
769
+ }
770
+ ```
771
+
752
772
  #### Step 2: Completing the Exit
753
773
 
754
774
  Once virtual outputs are fully unrolled and the unilateral exit timelock has expired, you can complete the exit:
@@ -6,6 +6,7 @@ const contractWatcher_1 = require("./contractWatcher");
6
6
  const handlers_1 = require("./handlers");
7
7
  const utils_1 = require("../wallet/utils");
8
8
  const syncCursors_1 = require("../utils/syncCursors");
9
+ const vtxoOwnership_1 = require("./vtxoOwnership");
9
10
  const DEFAULT_PAGE_SIZE = 500;
10
11
  /**
11
12
  * Central manager for contract lifecycle and operations.
@@ -357,9 +358,19 @@ class ContractManager {
357
358
  const contracts = opts?.scripts
358
359
  ? await this.getContracts({ script: opts.scripts })
359
360
  : undefined;
361
+ // Only forward an explicit window when the caller supplied one. An
362
+ // empty `{ after: undefined, before: undefined }` would short-circuit
363
+ // both the cursor-derived `?after=` query in `syncContracts` (because
364
+ // `??` doesn't fire on a non-nullish object) AND the cursor-advance
365
+ // gate (which requires `options.window === undefined`), turning every
366
+ // `refreshVtxos()` call into an unbounded full re-scan whose cursor
367
+ // never moves forward.
368
+ const hasExplicitWindow = opts?.after !== undefined || opts?.before !== undefined;
360
369
  await this.syncContracts({
361
370
  contracts,
362
- window: { after: opts?.after, before: opts?.before },
371
+ window: hasExplicitWindow
372
+ ? { after: opts?.after, before: opts?.before }
373
+ : undefined,
363
374
  });
364
375
  }
365
376
  /**
@@ -402,7 +413,11 @@ class ContractManager {
402
413
  this.emitEvent(event);
403
414
  }
404
415
  async getVtxosForContracts(contracts) {
405
- const res = await Promise.all(contracts.map(({ script, address }) => this.config.walletRepository.getVtxos(address).then((vtxos) => vtxos.map((vtxo) => ({
416
+ const res = await Promise.all(contracts.map(({ script, address }) => this.config.walletRepository.getVtxos(address).then((vtxos) =>
417
+ // Address buckets may carry legacy duplicate rows from
418
+ // other contracts that once shared the same address —
419
+ // gate by script so the wrong-script row never wins.
420
+ (0, vtxoOwnership_1.filterVtxosForScript)(vtxos, script).map((vtxo) => ({
406
421
  ...vtxo,
407
422
  contractScript: script,
408
423
  })))));
@@ -470,7 +485,14 @@ class ContractManager {
470
485
  });
471
486
  }
472
487
  for (const [addr, contractVtxos] of byContract) {
473
- await this.config.walletRepository.saveVtxos(addr, contractVtxos);
488
+ // The bucket is keyed by contract address, so the script filter
489
+ // here is the same as the contract's. Skip wrong-script rows
490
+ // rather than crash the reconcile loop.
491
+ const contract = contracts.find((c) => c.address === addr);
492
+ const filtered = (0, vtxoOwnership_1.warnAndFilterVtxosForScript)(contractVtxos, contract.script, "ContractManager.reconcilePendingFrontier");
493
+ if (filtered.length === 0)
494
+ continue;
495
+ await this.config.walletRepository.saveVtxos(addr, filtered);
474
496
  }
475
497
  }
476
498
  async fetchContractVxosFromIndexer(contracts, pageSize, syncWindow) {
@@ -480,7 +502,10 @@ class ContractManager {
480
502
  result.set(contractScript, vtxos);
481
503
  const contract = contracts.find((c) => c.script === contractScript);
482
504
  if (contract) {
483
- await this.config.walletRepository.saveVtxos(contract.address, vtxos);
505
+ const filtered = (0, vtxoOwnership_1.warnAndFilterVtxosForScript)(vtxos, contract.script, "ContractManager.fetchContractVxosFromIndexer");
506
+ if (filtered.length === 0)
507
+ continue;
508
+ await this.config.walletRepository.saveVtxos(contract.address, filtered);
484
509
  }
485
510
  }
486
511
  return result;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ContractWatcher = void 0;
4
4
  const utils_1 = require("../wallet/utils");
5
5
  const utils_2 = require("../providers/utils");
6
+ const vtxoOwnership_1 = require("./vtxoOwnership");
6
7
  /**
7
8
  * Watches multiple contracts for virtual output state changes with resilient connection handling.
8
9
  *
@@ -90,7 +91,10 @@ class ContractWatcher {
90
91
  */
91
92
  async seedLastKnownVtxos(state) {
92
93
  try {
93
- const cached = await this.config.walletRepository.getVtxos(state.contract.address);
94
+ // Apply the same script gate used by getContractVtxos so a legacy
95
+ // wrong-script row in the address bucket can't seed the baseline
96
+ // and then look "spent" on the first poll.
97
+ const cached = (0, vtxoOwnership_1.filterVtxosForScript)(await this.config.walletRepository.getVtxos(state.contract.address), state.contract.script);
94
98
  for (const vtxo of cached) {
95
99
  if (vtxo.isSpent)
96
100
  continue;
@@ -169,8 +173,10 @@ class ContractWatcher {
169
173
  return true;
170
174
  })
171
175
  .map(async (state) => {
172
- // Use contract address as cache key
173
- const cached = await repo.getVtxos(state.contract.address);
176
+ // Use contract address as cache key. Legacy address buckets
177
+ // can contain rows from other contracts; gate by script before
178
+ // converting so a wrong-script row never reaches the watcher.
179
+ const cached = (0, vtxoOwnership_1.filterVtxosForScript)(await repo.getVtxos(state.contract.address), state.contract.script);
174
180
  if (cached.length > 0) {
175
181
  // Convert to ContractVtxo with contractScript
176
182
  const contractVtxos = cached.map((v) => ({
@@ -4,6 +4,7 @@ exports.DefaultContractHandler = void 0;
4
4
  const base_1 = require("@scure/base");
5
5
  const default_1 = require("../../script/default");
6
6
  const helpers_1 = require("./helpers");
7
+ const timelock_1 = require("../../utils/timelock");
7
8
  const descriptor_1 = require("../../identity/descriptor");
8
9
  /**
9
10
  * Extract pubkey bytes from a descriptor or hex string.
@@ -28,12 +29,12 @@ exports.DefaultContractHandler = {
28
29
  return {
29
30
  pubKey: base_1.hex.encode(params.pubKey),
30
31
  serverPubKey: base_1.hex.encode(params.serverPubKey),
31
- csvTimelock: (0, helpers_1.timelockToSequence)(params.csvTimelock).toString(),
32
+ csvTimelock: (0, timelock_1.timelockToSequence)(params.csvTimelock).toString(),
32
33
  };
33
34
  },
34
35
  deserializeParams(params) {
35
36
  const csvTimelock = params.csvTimelock
36
- ? (0, helpers_1.sequenceToTimelock)(Number(params.csvTimelock))
37
+ ? (0, timelock_1.sequenceToTimelock)(Number(params.csvTimelock))
37
38
  : default_1.DefaultVtxo.Script.DEFAULT_TIMELOCK;
38
39
  return {
39
40
  pubKey: extractPubKeyBytes(params.pubKey),
@@ -5,6 +5,7 @@ const base_1 = require("@scure/base");
5
5
  const delegate_1 = require("../../script/delegate");
6
6
  const default_1 = require("../../script/default");
7
7
  const helpers_1 = require("./helpers");
8
+ const timelock_1 = require("../../utils/timelock");
8
9
  /**
9
10
  * Handler for delegate wallet virtual outputs.
10
11
  *
@@ -24,12 +25,12 @@ exports.DelegateContractHandler = {
24
25
  pubKey: base_1.hex.encode(params.pubKey),
25
26
  serverPubKey: base_1.hex.encode(params.serverPubKey),
26
27
  delegatePubKey: base_1.hex.encode(params.delegatePubKey),
27
- csvTimelock: (0, helpers_1.timelockToSequence)(params.csvTimelock).toString(),
28
+ csvTimelock: (0, timelock_1.timelockToSequence)(params.csvTimelock).toString(),
28
29
  };
29
30
  },
30
31
  deserializeParams(params) {
31
32
  const csvTimelock = params.csvTimelock
32
- ? (0, helpers_1.sequenceToTimelock)(Number(params.csvTimelock))
33
+ ? (0, timelock_1.sequenceToTimelock)(Number(params.csvTimelock))
33
34
  : default_1.DefaultVtxo.Script.DEFAULT_TIMELOCK;
34
35
  return {
35
36
  pubKey: base_1.hex.decode(params.pubKey),
@@ -1,44 +1,9 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.timelockToSequence = timelockToSequence;
37
- exports.sequenceToTimelock = sequenceToTimelock;
38
3
  exports.resolveRole = resolveRole;
39
4
  exports.isCltvSatisfied = isCltvSatisfied;
40
5
  exports.isCsvSpendable = isCsvSpendable;
41
- const bip68 = __importStar(require("bip68"));
6
+ const timelock_1 = require("../../utils/timelock");
42
7
  const descriptor_1 = require("../../identity/descriptor");
43
8
  /**
44
9
  * Extract raw hex pubkey from a value that may be a descriptor or raw hex.
@@ -56,27 +21,6 @@ function extractRawPubKey(value) {
56
21
  return undefined;
57
22
  }
58
23
  }
59
- /**
60
- * Convert RelativeTimelock to BIP68 sequence number.
61
- */
62
- function timelockToSequence(timelock) {
63
- return bip68.encode(timelock.type === "blocks"
64
- ? { blocks: Number(timelock.value) }
65
- : { seconds: Number(timelock.value) });
66
- }
67
- /**
68
- * Convert BIP68 sequence number back to RelativeTimelock.
69
- */
70
- function sequenceToTimelock(sequence) {
71
- const decoded = bip68.decode(sequence);
72
- if ("blocks" in decoded && decoded.blocks !== undefined) {
73
- return { type: "blocks", value: BigInt(decoded.blocks) };
74
- }
75
- if ("seconds" in decoded && decoded.seconds !== undefined) {
76
- return { type: "seconds", value: BigInt(decoded.seconds) };
77
- }
78
- throw new Error(`Invalid BIP68 sequence: ${sequence}`);
79
- }
80
24
  /**
81
25
  * Resolve wallet's role from explicit role or by matching descriptor/pubkey.
82
26
  */
@@ -152,7 +96,7 @@ function isCsvSpendable(context, sequence) {
152
96
  return true;
153
97
  if (!context.vtxo)
154
98
  return false;
155
- const timelock = sequenceToTimelock(sequence);
99
+ const timelock = (0, timelock_1.sequenceToTimelock)(sequence);
156
100
  if (timelock.type === "blocks") {
157
101
  if (context.blockHeight === undefined ||
158
102
  context.vtxo.status.block_height === undefined) {
@@ -4,6 +4,7 @@ exports.VHTLCContractHandler = void 0;
4
4
  const base_1 = require("@scure/base");
5
5
  const vhtlc_1 = require("../../script/vhtlc");
6
6
  const helpers_1 = require("./helpers");
7
+ const timelock_1 = require("../../utils/timelock");
7
8
  /**
8
9
  * Handler for Virtual Hash Time Lock Contract (VHTLC).
9
10
  *
@@ -32,9 +33,9 @@ exports.VHTLCContractHandler = {
32
33
  server: base_1.hex.encode(params.server),
33
34
  hash: base_1.hex.encode(params.preimageHash),
34
35
  refundLocktime: params.refundLocktime.toString(),
35
- claimDelay: (0, helpers_1.timelockToSequence)(params.unilateralClaimDelay).toString(),
36
- refundDelay: (0, helpers_1.timelockToSequence)(params.unilateralRefundDelay).toString(),
37
- refundNoReceiverDelay: (0, helpers_1.timelockToSequence)(params.unilateralRefundWithoutReceiverDelay).toString(),
36
+ claimDelay: (0, timelock_1.timelockToSequence)(params.unilateralClaimDelay).toString(),
37
+ refundDelay: (0, timelock_1.timelockToSequence)(params.unilateralRefundDelay).toString(),
38
+ refundNoReceiverDelay: (0, timelock_1.timelockToSequence)(params.unilateralRefundWithoutReceiverDelay).toString(),
38
39
  };
39
40
  },
40
41
  deserializeParams(params) {
@@ -44,9 +45,9 @@ exports.VHTLCContractHandler = {
44
45
  server: base_1.hex.decode(params.server),
45
46
  preimageHash: base_1.hex.decode(params.hash),
46
47
  refundLocktime: BigInt(params.refundLocktime),
47
- unilateralClaimDelay: (0, helpers_1.sequenceToTimelock)(Number(params.claimDelay)),
48
- unilateralRefundDelay: (0, helpers_1.sequenceToTimelock)(Number(params.refundDelay)),
49
- unilateralRefundWithoutReceiverDelay: (0, helpers_1.sequenceToTimelock)(Number(params.refundNoReceiverDelay)),
48
+ unilateralClaimDelay: (0, timelock_1.sequenceToTimelock)(Number(params.claimDelay)),
49
+ unilateralRefundDelay: (0, timelock_1.sequenceToTimelock)(Number(params.refundDelay)),
50
+ unilateralRefundWithoutReceiverDelay: (0, timelock_1.sequenceToTimelock)(Number(params.refundNoReceiverDelay)),
50
51
  };
51
52
  },
52
53
  /**
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vtxoOutpoint = vtxoOutpoint;
4
+ exports.isVtxoForScript = isVtxoForScript;
5
+ exports.filterVtxosForScript = filterVtxosForScript;
6
+ exports.warnAndFilterVtxosForScript = warnAndFilterVtxosForScript;
7
+ exports.validateVtxosForScript = validateVtxosForScript;
8
+ /**
9
+ * Tier 1 helpers that enforce VTXO ownership at call sites that already know
10
+ * the intended contract script. Address-keyed repositories may still hand back
11
+ * legacy duplicate rows under the wrong bucket; these helpers gate reads and
12
+ * writes so a wrong-script row never wins.
13
+ *
14
+ * `script` is the authoritative ownership key. Equality is strict: a missing
15
+ * or empty `vtxo.script` never matches.
16
+ */
17
+ function vtxoOutpoint(vtxo) {
18
+ return `${vtxo.txid}:${vtxo.vout}`;
19
+ }
20
+ function isVtxoForScript(vtxo, script) {
21
+ return !!vtxo.script && vtxo.script === script;
22
+ }
23
+ function filterVtxosForScript(vtxos, script) {
24
+ return vtxos.filter((v) => isVtxoForScript(v, script));
25
+ }
26
+ /**
27
+ * Background/indexer sync flavour: drop wrong-script rows and log enough
28
+ * context to identify each rejection. Returns only matching rows so the
29
+ * caller can keep going.
30
+ */
31
+ function warnAndFilterVtxosForScript(vtxos, script, context) {
32
+ const matches = [];
33
+ const rejected = [];
34
+ for (const v of vtxos) {
35
+ if (isVtxoForScript(v, script)) {
36
+ matches.push(v);
37
+ }
38
+ else {
39
+ rejected.push(`${vtxoOutpoint(v)}(script=${v.script ?? ""})`);
40
+ }
41
+ }
42
+ if (rejected.length > 0) {
43
+ console.warn(`${context}: dropped ${rejected.length} wrong-script VTXO(s) for script ${script}: ${rejected.join(", ")}`);
44
+ }
45
+ return matches;
46
+ }
47
+ /**
48
+ * User-initiated transaction/signing flavour: throw before persisting or
49
+ * signing inconsistent ownership state. Silently skipping here would hide a
50
+ * serious bug in the wallet's spend path.
51
+ */
52
+ function validateVtxosForScript(vtxos, script, context) {
53
+ const mismatches = vtxos.filter((v) => !isVtxoForScript(v, script));
54
+ if (mismatches.length === 0)
55
+ return;
56
+ const detail = mismatches
57
+ .map((v) => `${vtxoOutpoint(v)}(script=${v.script ?? ""})`)
58
+ .join(", ");
59
+ throw new Error(`${context}: refusing to persist ${mismatches.length} VTXO(s) whose script does not match ${script}: ${detail}`);
60
+ }
package/dist/cjs/index.js CHANGED
@@ -183,9 +183,9 @@ Object.defineProperty(exports, "decodeArkContract", { enumerable: true, get: fun
183
183
  Object.defineProperty(exports, "contractFromArkContract", { enumerable: true, get: function () { return contracts_1.contractFromArkContract; } });
184
184
  Object.defineProperty(exports, "contractFromArkContractWithAddress", { enumerable: true, get: function () { return contracts_1.contractFromArkContractWithAddress; } });
185
185
  Object.defineProperty(exports, "isArkContract", { enumerable: true, get: function () { return contracts_1.isArkContract; } });
186
- const helpers_1 = require("./contracts/handlers/helpers");
187
- Object.defineProperty(exports, "timelockToSequence", { enumerable: true, get: function () { return helpers_1.timelockToSequence; } });
188
- Object.defineProperty(exports, "sequenceToTimelock", { enumerable: true, get: function () { return helpers_1.sequenceToTimelock; } });
186
+ const timelock_1 = require("./utils/timelock");
187
+ Object.defineProperty(exports, "timelockToSequence", { enumerable: true, get: function () { return timelock_1.timelockToSequence; } });
188
+ Object.defineProperty(exports, "sequenceToTimelock", { enumerable: true, get: function () { return timelock_1.sequenceToTimelock; } });
189
189
  const manager_1 = require("./repositories/indexedDB/manager");
190
190
  Object.defineProperty(exports, "closeDatabase", { enumerable: true, get: function () { return manager_1.closeDatabase; } });
191
191
  Object.defineProperty(exports, "openDatabase", { enumerable: true, get: function () { return manager_1.openDatabase; } });
@@ -1,47 +1,14 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  Object.defineProperty(exports, "__esModule", { value: true });
36
3
  exports.VtxoScript = exports.TapTreeCoder = void 0;
37
4
  exports.scriptFromTapLeafScript = scriptFromTapLeafScript;
38
5
  exports.getSequence = getSequence;
39
6
  const btc_signer_1 = require("@scure/btc-signer");
40
- const bip68 = __importStar(require("bip68"));
41
7
  const payment_js_1 = require("@scure/btc-signer/payment.js");
42
8
  const psbt_js_1 = require("@scure/btc-signer/psbt.js");
43
9
  const base_1 = require("@scure/base");
44
10
  const address_1 = require("./address");
11
+ const timelock_1 = require("../utils/timelock");
45
12
  const tapscript_1 = require("./tapscript");
46
13
  exports.TapTreeCoder = psbt_js_1.PSBTOutput.tapTree[2];
47
14
  function scriptFromTapLeafScript(leaf) {
@@ -163,19 +130,19 @@ class VtxoScript {
163
130
  const paths = [];
164
131
  for (const leaf of this.leaves) {
165
132
  try {
166
- const tapscript = tapscript_1.CSVMultisigTapscript.decode(scriptFromTapLeafScript(leaf));
167
- paths.push(tapscript);
168
- continue;
169
- }
170
- catch (e) {
171
- try {
172
- const tapscript = tapscript_1.ConditionCSVMultisigTapscript.decode(scriptFromTapLeafScript(leaf));
173
- paths.push(tapscript);
133
+ const script = scriptFromTapLeafScript(leaf);
134
+ if (tapscript_1.CSVMultisigTapscript.isScriptValid(script) === true) {
135
+ const tapScript = tapscript_1.CSVMultisigTapscript.decode(script);
136
+ paths.push(tapScript);
174
137
  }
175
- catch (e) {
176
- continue;
138
+ else if (tapscript_1.ConditionCSVMultisigTapscript.isScriptValid(script) === true) {
139
+ const tapScript = tapscript_1.ConditionCSVMultisigTapscript.decode(script);
140
+ paths.push(tapScript);
177
141
  }
178
142
  }
143
+ catch (e) {
144
+ console.debug("Failed to decode script", e);
145
+ }
179
146
  }
180
147
  return paths;
181
148
  }
@@ -206,9 +173,7 @@ function getSequence(tapLeafScript) {
206
173
  const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
207
174
  try {
208
175
  const params = tapscript_1.CSVMultisigTapscript.decode(script).params;
209
- sequence = bip68.encode(params.timelock.type === "blocks"
210
- ? { blocks: Number(params.timelock.value) }
211
- : { seconds: Number(params.timelock.value) });
176
+ sequence = (0, timelock_1.timelockToSequence)(params.timelock);
212
177
  }
213
178
  catch {
214
179
  const params = tapscript_1.CLTVMultisigTapscript.decode(script).params;