@arkade-os/swap 0.1.5 → 0.1.6

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 (29) hide show
  1. package/dist/advanced.cjs +228 -63
  2. package/dist/advanced.d.cts +6 -6
  3. package/dist/advanced.d.ts +6 -6
  4. package/dist/advanced.js +2 -2
  5. package/dist/{chunk-GJKGPE3S.js → chunk-QFM55KP2.js} +175 -29
  6. package/dist/{chunk-HDCC3JZR.js → chunk-SDPNWPGY.js} +30 -2
  7. package/dist/index.cjs +219 -54
  8. package/dist/index.d.cts +5 -5
  9. package/dist/index.d.ts +5 -5
  10. package/dist/index.js +2 -2
  11. package/dist/{lockupContract-CdXh9yIr.d.ts → lockupContract-CKPFpIt9.d.ts} +1 -1
  12. package/dist/{lockupContract-QfbsjyXG.d.cts → lockupContract-CpJXTjX_.d.cts} +1 -1
  13. package/dist/{offer-BxzPQ6zh.d.cts → offer-Boxive2u.d.cts} +1 -1
  14. package/dist/{offer-Q1lVhrVX.d.ts → offer-Drw1k_jt.d.ts} +1 -1
  15. package/dist/{offerFillPlan-BfcxbVvw.d.cts → offerFillPlan-BxNkWLtw.d.cts} +2 -2
  16. package/dist/{offerFillPlan-C11P2ek2.d.ts → offerFillPlan-D2N4s7Xe.d.ts} +2 -2
  17. package/dist/protocol.cjs +83 -33
  18. package/dist/protocol.d.cts +11 -27
  19. package/dist/protocol.d.ts +11 -27
  20. package/dist/protocol.js +1 -1
  21. package/dist/repositories/realm/index.d.cts +1 -1
  22. package/dist/repositories/realm/index.d.ts +1 -1
  23. package/dist/repositories/sqlite/index.d.cts +1 -1
  24. package/dist/repositories/sqlite/index.d.ts +1 -1
  25. package/dist/{repository-Cut4eEkm.d.cts → repository-DFDNvcZ7.d.cts} +26 -4
  26. package/dist/{repository-BOQ1qYoj.d.ts → repository-Dn1V17T2.d.ts} +26 -4
  27. package/dist/{watch-D15U0QpD.d.ts → watch-D-mc_wfD.d.ts} +1 -1
  28. package/dist/{watch-D4PeBZ3H.d.cts → watch-DFNMY2K-.d.cts} +1 -1
  29. package/package.json +2 -2
package/dist/advanced.cjs CHANGED
@@ -4770,7 +4770,7 @@ var scopedToRail = (table, rail) => ({
4770
4770
  });
4771
4771
 
4772
4772
  // src/client/client.ts
4773
- var import_base22 = require("@scure/base");
4773
+ var import_base23 = require("@scure/base");
4774
4774
  var import_sdk37 = require("@arkade-os/sdk");
4775
4775
  init_repository();
4776
4776
 
@@ -4861,10 +4861,11 @@ async function readLockupFate(indexer, input) {
4861
4861
  const spentBy = /* @__PURE__ */ new Map();
4862
4862
  let everySpendNamed = true;
4863
4863
  let sweptLeft = false;
4864
+ let live = false;
4864
4865
  for (const vtxo of all) {
4865
4866
  if (!(0, import_sdk14.isVtxoSpent)(vtxo)) {
4866
- if (!(input.receiver && vtxo.isSwept)) return { fate: "open" };
4867
- sweptLeft = true;
4867
+ if (input.receiver && vtxo.isSwept) sweptLeft = true;
4868
+ else live = true;
4868
4869
  continue;
4869
4870
  }
4870
4871
  if (vtxo.spentBy)
@@ -4874,8 +4875,16 @@ async function readLockupFate(indexer, input) {
4874
4875
  });
4875
4876
  else everySpendNamed = false;
4876
4877
  }
4878
+ if (live && (!input.receiver || spentBy.size === 0)) return { fate: "open" };
4877
4879
  const spends = [...spentBy.values()];
4878
- const { txs } = await indexer.getVirtualTxs([...spentBy.keys()]);
4880
+ const arkTxids = new Set(spends.flatMap((spend) => spend.txid ? [spend.txid] : []));
4881
+ let txs;
4882
+ try {
4883
+ ({ txs } = await indexer.getVirtualTxs([.../* @__PURE__ */ new Set([...spentBy.keys(), ...arkTxids])]));
4884
+ } catch (error) {
4885
+ if (live) return { fate: "open" };
4886
+ throw error;
4887
+ }
4879
4888
  const refunded = /* @__PURE__ */ new Set();
4880
4889
  const leafScripts = /* @__PURE__ */ new Set();
4881
4890
  for (const raw of txs) {
@@ -4885,18 +4894,29 @@ async function readLockupFate(indexer, input) {
4885
4894
  } catch {
4886
4895
  continue;
4887
4896
  }
4888
- if (!spentBy.has(tx.id)) continue;
4897
+ const checkpoint = spentBy.has(tx.id);
4898
+ if (!checkpoint && !arkTxids.has(tx.id)) continue;
4889
4899
  for (let i = 0; i < tx.inputsLength; i++) {
4890
4900
  const spent = tx.getInput(i);
4891
4901
  if (!spent.txid) continue;
4892
4902
  const txid = import_base10.hex.encode(spent.txid);
4903
+ if (!checkpoint) {
4904
+ if (spent.index !== 0 || !spentBy.has(txid)) continue;
4905
+ const preimage = candidateWitnessItems(tx, i).find(
4906
+ (candidate) => hashesTo(candidate, input.paymentHash)
4907
+ );
4908
+ if (preimage) {
4909
+ return live ? { fate: "open", preimage } : { fate: "claimed", preimage, spends };
4910
+ }
4911
+ continue;
4912
+ }
4893
4913
  const vtxo = all.find(
4894
4914
  (vtxo2) => vtxo2.txid === txid && vtxo2.vout === spent.index && vtxo2.spentBy === tx.id
4895
4915
  );
4896
4916
  if (!vtxo) continue;
4897
4917
  for (const candidate of candidateWitnessItems(tx, i)) {
4898
4918
  if (hashesTo(candidate, input.paymentHash)) {
4899
- return { fate: "claimed", preimage: candidate, spends };
4919
+ return live ? { fate: "open", preimage: candidate } : { fate: "claimed", preimage: candidate, spends };
4900
4920
  }
4901
4921
  }
4902
4922
  const leafScript = refundLeafScript(tx, i);
@@ -4906,7 +4926,7 @@ async function readLockupFate(indexer, input) {
4906
4926
  }
4907
4927
  }
4908
4928
  }
4909
- if (sweptLeft) return { fate: "open" };
4929
+ if (live || sweptLeft) return { fate: "open" };
4910
4930
  return everySpendNamed && all.every((vtxo) => refunded.has(`${vtxo.txid}:${vtxo.vout}`)) ? { fate: "returned", spends, leafScripts: [...leafScripts] } : { fate: "unknown" };
4911
4931
  }
4912
4932
  var assertNoneSwept = (vtxos, contract) => {
@@ -6346,9 +6366,10 @@ var retireOfferScripts = async (wallet, repository, record) => {
6346
6366
 
6347
6367
  // src/client/drive.ts
6348
6368
  var import_sdk31 = require("@arkade-os/sdk");
6349
- var import_base19 = require("@scure/base");
6369
+ var import_base20 = require("@scure/base");
6350
6370
 
6351
6371
  // src/claim.ts
6372
+ var import_base17 = require("@scure/base");
6352
6373
  var import_sdk25 = require("@arkade-os/sdk");
6353
6374
  init_onchainHtlc();
6354
6375
  var LockupAmountMismatchError = class extends Error {
@@ -6400,6 +6421,95 @@ async function pushClaim(operator, input) {
6400
6421
  });
6401
6422
  return { txid, amount: locked, left: live.length - vtxos.length, spent: vtxos };
6402
6423
  }
6424
+ async function findPendingClaims(operator, indexer, input) {
6425
+ const { vtxos } = await indexer.getVtxos({
6426
+ scripts: [import_base17.hex.encode(input.contract.pkScript)],
6427
+ pendingOnly: true
6428
+ });
6429
+ const byArkTx = /* @__PURE__ */ new Map();
6430
+ for (const vtxo of vtxos ?? []) {
6431
+ if (!(0, import_sdk25.isVtxoSpent)(vtxo) || !vtxo.spentBy || !vtxo.arkTxId) continue;
6432
+ byArkTx.set(vtxo.arkTxId, [...byArkTx.get(vtxo.arkTxId) ?? [], vtxo]);
6433
+ }
6434
+ if (byArkTx.size === 0) return [];
6435
+ const current = await operatorUnrollScript(operator, input.network);
6436
+ const { deprecatedSigners } = await operator.getInfo();
6437
+ const unrollScripts = [
6438
+ current,
6439
+ ...(deprecatedSigners ?? []).flatMap(
6440
+ ({ pubkey }) => pubkey ? [
6441
+ import_sdk25.CSVMultisigTapscript.encode({
6442
+ ...current.params,
6443
+ pubkeys: [(0, import_sdk25.toXOnly)(import_base17.hex.decode(pubkey), "deprecated signer")]
6444
+ })
6445
+ ] : []
6446
+ )
6447
+ ];
6448
+ const claims = [];
6449
+ for (const [arkTxid, spent] of byArkTx) {
6450
+ const live = spent.map(({ txid, vout, value }) => ({
6451
+ txid,
6452
+ vout,
6453
+ value: Number(value),
6454
+ recoverable: false
6455
+ }));
6456
+ const total = live.reduce((sum, vtxo) => sum + vtxo.value, 0);
6457
+ const checkpoints = unrollScripts.map(
6458
+ (unroll) => (0, import_sdk25.buildOffchainTx)(
6459
+ lockupInputs(live, input.contract, input.contract.claim()),
6460
+ [{ script: input.contract.pkScript, amount: BigInt(total) }],
6461
+ unroll
6462
+ ).checkpoints
6463
+ ).find(
6464
+ (built) => built.length === spent.length && built.every((checkpoint, i) => checkpoint.id === spent[i].spentBy)
6465
+ );
6466
+ if (checkpoints) claims.push({ arkTxid, checkpoints });
6467
+ }
6468
+ return claims;
6469
+ }
6470
+ async function finalizePendingClaim(operator, indexer, input) {
6471
+ const claims = await findPendingClaims(operator, indexer, input);
6472
+ if (claims.length === 0) return [];
6473
+ assertPreimageMatches(input.preimage, input.contract.options.preimageHash);
6474
+ const signer = (0, import_sdk25.claimWithPreimageIdentity)(input.receiver, input.preimage);
6475
+ const ours = import_base17.hex.encode(await input.receiver.xOnlyPublicKey());
6476
+ const finalized = [];
6477
+ for (const { arkTxid, checkpoints } of claims) {
6478
+ const { txs } = await indexer.getVirtualTxs(checkpoints.map((checkpoint) => checkpoint.id));
6479
+ const stored = new Map(
6480
+ txs.map((raw) => {
6481
+ const tx = import_sdk25.Transaction.fromPSBT(import_base17.base64.decode(raw));
6482
+ return [tx.id, tx];
6483
+ })
6484
+ );
6485
+ const signed = await Promise.all(
6486
+ checkpoints.map(async (checkpoint) => {
6487
+ const theirs = stored.get(checkpoint.id);
6488
+ if (!theirs) {
6489
+ throw new Error(
6490
+ `pending claim ${arkTxid}: no stored checkpoint ${checkpoint.id}`
6491
+ );
6492
+ }
6493
+ const server = (theirs.getInput(0).tapScriptSig ?? []).filter(
6494
+ ([{ pubKey }]) => import_base17.hex.encode(pubKey) !== ours
6495
+ );
6496
+ if (server.length === 0) {
6497
+ throw new Error(
6498
+ `pending claim ${arkTxid}: the indexer served checkpoint ${checkpoint.id} without the operator's signature, so it cannot be finalized from here`
6499
+ );
6500
+ }
6501
+ const tx = await signer.sign(checkpoint, [0]);
6502
+ tx.updateInput(0, {
6503
+ tapScriptSig: [...tx.getInput(0).tapScriptSig ?? [], ...server]
6504
+ });
6505
+ return import_base17.base64.encode(tx.toPSBT());
6506
+ })
6507
+ );
6508
+ await operator.finalizeTx(arkTxid, signed);
6509
+ finalized.push(arkTxid);
6510
+ }
6511
+ return finalized;
6512
+ }
6403
6513
 
6404
6514
  // src/client/drive.ts
6405
6515
  init_onchainHtlc();
@@ -6626,7 +6736,7 @@ init_store();
6626
6736
  init_repository();
6627
6737
 
6628
6738
  // src/swapManager.ts
6629
- var import_base17 = require("@scure/base");
6739
+ var import_base18 = require("@scure/base");
6630
6740
  var import_sdk28 = require("@arkade-os/sdk");
6631
6741
  init_onchainHtlc();
6632
6742
  init_lockupContract();
@@ -7113,13 +7223,13 @@ var RfqSwapManager = class _RfqSwapManager {
7113
7223
  // ── internals ────────────────────────────────────────────────────────────
7114
7224
  track(swap) {
7115
7225
  this.monitored.set(swap.rfqId, swap);
7116
- this.byLockupScript.set(import_base17.hex.encode(swap.lockupPkScript), swap);
7226
+ this.byLockupScript.set(import_base18.hex.encode(swap.lockupPkScript), swap);
7117
7227
  }
7118
7228
  /** Drops the swap from BOTH indexes. `pollSwap`'s `monitored` check also stops a late
7119
7229
  * event, deliberately redundant so neither change alone re-drives a removed swap. */
7120
7230
  untrack(rfqId) {
7121
7231
  const swap = this.monitored.get(rfqId);
7122
- if (swap) this.byLockupScript.delete(import_base17.hex.encode(swap.lockupPkScript));
7232
+ if (swap) this.byLockupScript.delete(import_base18.hex.encode(swap.lockupPkScript));
7123
7233
  this.monitored.delete(rfqId);
7124
7234
  this.refundRefused.delete(rfqId);
7125
7235
  this.claimedOutpoints.delete(rfqId);
@@ -7165,7 +7275,7 @@ var RfqSwapManager = class _RfqSwapManager {
7165
7275
  if (!lockup) {
7166
7276
  try {
7167
7277
  const [existing] = await contracts.getContracts({
7168
- script: import_base17.hex.encode(swap.lockupPkScript)
7278
+ script: import_base18.hex.encode(swap.lockupPkScript)
7169
7279
  });
7170
7280
  if (existing) {
7171
7281
  this.registered.set(swap.rfqId, true);
@@ -7184,13 +7294,13 @@ var RfqSwapManager = class _RfqSwapManager {
7184
7294
  );
7185
7295
  return;
7186
7296
  }
7187
- const script = import_base17.hex.encode(lockup.script.pkScript);
7188
- if (script !== import_base17.hex.encode(swap.lockupPkScript)) {
7297
+ const script = import_base18.hex.encode(lockup.script.pkScript);
7298
+ if (script !== import_base18.hex.encode(swap.lockupPkScript)) {
7189
7299
  this.registered.set(swap.rfqId, false);
7190
7300
  this.emitFailed(
7191
7301
  swap,
7192
7302
  new Error(
7193
- `swap ${swap.rfqId} lockup script ${script} does not match its lockupPkScript ${import_base17.hex.encode(swap.lockupPkScript)}`
7303
+ `swap ${swap.rfqId} lockup script ${script} does not match its lockupPkScript ${import_base18.hex.encode(swap.lockupPkScript)}`
7194
7304
  )
7195
7305
  );
7196
7306
  return;
@@ -7207,7 +7317,7 @@ var RfqSwapManager = class _RfqSwapManager {
7207
7317
  retireContract(swap) {
7208
7318
  if (!this.deps.contracts || !this.registered.get(swap.rfqId)) return;
7209
7319
  if (!LOCKUP_RETIRABLE.includes(swap.state)) return;
7210
- void this.deps.contracts.setContractWatchState(import_base17.hex.encode(swap.lockupPkScript), "retained").catch((error) => this.emitFailed(swap, error));
7320
+ void this.deps.contracts.setContractWatchState(import_base18.hex.encode(swap.lockupPkScript), "retained").catch((error) => this.emitFailed(swap, error));
7211
7321
  }
7212
7322
  arm() {
7213
7323
  if (!this.running) return;
@@ -7267,6 +7377,11 @@ var RfqSwapManager = class _RfqSwapManager {
7267
7377
  )) {
7268
7378
  fate = { fate: "unknown" };
7269
7379
  }
7380
+ if (swap.kind === "lightning_receive" && fate.fate !== "exited" && fate.fate !== "returned") {
7381
+ const pending = await this.finalizeOwnClaim(swap);
7382
+ const windowOpen = fate.fate === "open" && this.config.now() < swap.refundLocktime;
7383
+ if (pending !== "none" && !windowOpen) return;
7384
+ }
7270
7385
  if (swap.kind === "onchain_receive") return this.driveOnchainReceive(swap, fate);
7271
7386
  if (fate.fate === "claimed" || fate.fate === "returned") {
7272
7387
  this.stampLockupSpends(swap, fate.spends);
@@ -7279,8 +7394,13 @@ var RfqSwapManager = class _RfqSwapManager {
7279
7394
  }
7280
7395
  if (swap.kind === "lightning_receive") {
7281
7396
  if (fate.fate === "exited") return this.blockExitedLockup(swap, fate);
7282
- if (fate.fate === "open") return this.driveReceiveClaim(swap);
7283
- if (this.config.now() >= swap.refundLocktime) return this.driveReceiveClaim(swap);
7397
+ if (fate.fate === "open") {
7398
+ return this.driveReceiveClaim(swap, fate.preimage !== void 0);
7399
+ }
7400
+ if (fate.fate === "unknown" && swap.claimTxid !== void 0) return;
7401
+ if (this.config.now() >= swap.refundLocktime) {
7402
+ return this.driveReceiveClaim(swap, false);
7403
+ }
7284
7404
  return;
7285
7405
  }
7286
7406
  if (swap.kind === "onchain_send") {
@@ -7293,6 +7413,25 @@ var RfqSwapManager = class _RfqSwapManager {
7293
7413
  }
7294
7414
  await this.driveArkadeRefund(swap);
7295
7415
  }
7416
+ /** `none` when no claim of ours is pending; `failed` covers a pending read that failed too. */
7417
+ async finalizeOwnClaim(swap) {
7418
+ const pendingClaims = this.callbacks?.pendingClaims;
7419
+ const finalize = this.callbacks?.finalizeClaim;
7420
+ if (!pendingClaims || !finalize || !this.config.enableAutoActions) return "none";
7421
+ try {
7422
+ const pending = await pendingClaims(swap);
7423
+ if (pending.length === 0) return "none";
7424
+ if (swap.claimTxid === void 0) {
7425
+ swap.claimTxid = pending[0];
7426
+ this.touch(swap);
7427
+ }
7428
+ await finalize(swap);
7429
+ return "finalized";
7430
+ } catch (error) {
7431
+ this.emitFailed(swap, error);
7432
+ return "failed";
7433
+ }
7434
+ }
7296
7435
  /**
7297
7436
  * The lockup was unilaterally exited: its outputs sit onchain, beyond any offchain
7298
7437
  * spend. `needs_counterparty`, not terminal: it can still end either way onchain. Set
@@ -7315,8 +7454,9 @@ var RfqSwapManager = class _RfqSwapManager {
7315
7454
  *
7316
7455
  * After the window the trader has no move; the manager only watches.
7317
7456
  */
7318
- async driveReceiveClaim(swap) {
7457
+ async driveReceiveClaim(swap, revealed) {
7319
7458
  const now = this.config.now();
7459
+ const claimOut = swap.claimTxid !== void 0 || revealed;
7320
7460
  if (now < swap.refundLocktime) {
7321
7461
  let vtxos;
7322
7462
  try {
@@ -7325,26 +7465,25 @@ var RfqSwapManager = class _RfqSwapManager {
7325
7465
  this.emitFailed(swap, error);
7326
7466
  return;
7327
7467
  }
7328
- return this.claimIfFunded(swap, vtxos);
7468
+ return this.claimIfFunded(swap, vtxos, claimOut);
7329
7469
  }
7330
7470
  if (now < swap.refundLocktime + REFUND_MTP_LAG_SECONDS) {
7331
- if (swap.claimTxid) return;
7471
+ if (claimOut) return;
7332
7472
  return this.block(
7333
7473
  swap,
7334
7474
  "the claim window closed with the lockup unclaimed \u2014 only the solver can act now"
7335
7475
  );
7336
7476
  }
7337
7477
  const failure = swap.claimFailure;
7338
- if (failure && !swap.claimTxid) {
7478
+ if (failure && !claimOut) {
7339
7479
  return this.fail(swap, new Error(failure));
7340
7480
  }
7341
7481
  this.setState(swap, "refunded");
7342
7482
  }
7343
7483
  /** Claim what the solver funded, once it is enough (the inner gate that matters is
7344
7484
  * `pushClaim`'s; see {@link RfqSwapManagerCallbacks.claimLockup}). */
7345
- async claimIfFunded(swap, vtxos) {
7485
+ async claimIfFunded(swap, vtxos, partiallyClaimed) {
7346
7486
  if (vtxos.length === 0) return this.unblock(swap);
7347
- const partiallyClaimed = swap.claimTxid !== void 0;
7348
7487
  const toClaim = partiallyClaimed ? this.unclaimed(swap.rfqId, vtxos) : vtxos;
7349
7488
  if (toClaim.length === 0) {
7350
7489
  return;
@@ -7424,7 +7563,7 @@ var RfqSwapManager = class _RfqSwapManager {
7424
7563
  "the L1 funding was refunded, so claiming the lockup too would take both sides of the trade \u2014 the solver's reclaim ends this swap"
7425
7564
  );
7426
7565
  }
7427
- await this.driveReceiveClaim(swap);
7566
+ await this.driveReceiveClaim(swap, false);
7428
7567
  if (l1.blocked && !isRfqSwapTerminal(swap.state)) this.block(swap, l1.blocked);
7429
7568
  }
7430
7569
  /** `resolved`: the HTLC can hold nothing more. `blocked` is a refusal the
@@ -7745,7 +7884,7 @@ var RfqSwapManager = class _RfqSwapManager {
7745
7884
  */
7746
7885
  stampSettlementPreimage(swap, preimage) {
7747
7886
  if (swap.kind !== "lightning_send") return;
7748
- swap.settlementPreimageHex = import_base17.hex.encode(preimage);
7887
+ swap.settlementPreimageHex = import_base18.hex.encode(preimage);
7749
7888
  this.touch(swap);
7750
7889
  }
7751
7890
  touch(swap) {
@@ -7868,7 +8007,7 @@ var errorMessage = (error) => error instanceof Error ? error.message : String(er
7868
8007
  var outpointKey = (vtxo) => `${vtxo.txid}:${vtxo.vout}`;
7869
8008
 
7870
8009
  // src/watch.ts
7871
- var import_base18 = require("@scure/base");
8010
+ var import_base19 = require("@scure/base");
7872
8011
  var import_sdk29 = require("@arkade-os/sdk");
7873
8012
  init_coverage();
7874
8013
  init_offer();
@@ -7917,9 +8056,9 @@ async function watchOfferSwaps({
7917
8056
  if (candidates.length === 0) return "indeterminate";
7918
8057
  const { txs } = await indexer.getVirtualTxs(candidates);
7919
8058
  return classifyDepositSpend(
7920
- decodeOffer(import_base18.hex.decode(swap.offerHex)),
8059
+ decodeOffer(import_base19.hex.decode(swap.offerHex)),
7921
8060
  operatorPubkey,
7922
- txs.map((psbt) => import_sdk29.Transaction.fromPSBT(import_base18.base64.decode(psbt))),
8061
+ txs.map((psbt) => import_sdk29.Transaction.fromPSBT(import_base19.base64.decode(psbt))),
7923
8062
  { txid: vtxo.txid, vout: vtxo.vout }
7924
8063
  );
7925
8064
  } catch {
@@ -8233,7 +8372,7 @@ var createSwapDrive = (config) => {
8233
8372
  const signer = await (0, import_sdk31.contractSigner)(wallet, await (0, import_sdk31.identityDescriptor)(wallet.identity));
8234
8373
  const claimKey = record.profile.claimKey;
8235
8374
  if (typeof claimKey === "string") {
8236
- const held = import_base19.hex.encode(await signer.xOnlyPublicKey());
8375
+ const held = import_base20.hex.encode(await signer.xOnlyPublicKey());
8237
8376
  if (held !== claimKey) {
8238
8377
  throw new Error(
8239
8378
  `this wallet's identity key ${held} is not the payout key ${claimKey} rfq swap ${swap.rfqId} was quoted against \u2014 a wrong seed, or key provisioning has drifted from the identity key`
@@ -8277,7 +8416,7 @@ var createSwapDrive = (config) => {
8277
8416
  );
8278
8417
  }
8279
8418
  const signer = await (0, import_sdk31.contractSigner)(wallet, descriptor);
8280
- const held = import_base19.hex.encode(await signer.xOnlyPublicKey());
8419
+ const held = import_base20.hex.encode(await signer.xOnlyPublicKey());
8281
8420
  if (held !== record.profile.refundKey) {
8282
8421
  throw new Error(
8283
8422
  `the key behind ${descriptor} is ${held}, not the refund key ${String(record.profile.refundKey)} rfq swap ${swap.rfqId}'s HTLC was derived with`
@@ -8317,13 +8456,37 @@ var createSwapDrive = (config) => {
8317
8456
  };
8318
8457
  const drivable = async (record) => !readsChain(record.kind) || await resolveOnchain();
8319
8458
  const pinnedNetwork = async () => (0, import_sdk31.getNetwork)(await config.network());
8320
- const claimLockup = async (swap, vtxos, { partiallyClaimed }) => {
8459
+ const claimSecretOf = async (swap) => {
8321
8460
  const record = await corridorStore().getRfqSwap(swap.rfqId);
8322
8461
  if (!record) throw new Error(`rfq swap ${swap.rfqId} has no stored record to claim from`);
8323
8462
  const secret = rfqClaimSecretOf(record);
8324
8463
  if (!secret) throw new Error(`rfq swap ${swap.rfqId} carries no claim secret`);
8325
8464
  const script = swap.lockup?.script;
8326
8465
  if (!script) throw new Error(`rfq swap ${swap.rfqId} carries no lockup covenant`);
8466
+ return { record, secret, script };
8467
+ };
8468
+ const pendingClaims = async (swap) => {
8469
+ const script = swap.lockup?.script;
8470
+ if (!script) throw new Error(`rfq swap ${swap.rfqId} carries no lockup covenant`);
8471
+ const claims = await findPendingClaims(operator, indexer, {
8472
+ contract: script,
8473
+ network: await pinnedNetwork()
8474
+ });
8475
+ return claims.map(({ arkTxid }) => arkTxid);
8476
+ };
8477
+ const finalizeClaim = async (swap) => {
8478
+ const { secret, script } = await claimSecretOf(swap);
8479
+ await track(
8480
+ finalizePendingClaim(operator, indexer, {
8481
+ contract: script,
8482
+ receiver: await (0, import_sdk31.contractSigner)(wallet, secret.signingDescriptor),
8483
+ preimage: await preimageForSwapRecord(wallet, secret),
8484
+ network: await pinnedNetwork()
8485
+ })
8486
+ );
8487
+ };
8488
+ const claimLockup = async (swap, vtxos, { partiallyClaimed }) => {
8489
+ const { record, secret, script } = await claimSecretOf(swap);
8327
8490
  const payoutAddress = rfqClaimDestinationOf(record);
8328
8491
  if (!payoutAddress) {
8329
8492
  throw new Error(`rfq swap ${swap.rfqId} carries no claim destination`);
@@ -8388,6 +8551,8 @@ var createSwapDrive = (config) => {
8388
8551
  },
8389
8552
  canRefundArkade,
8390
8553
  claimLockup,
8554
+ pendingClaims,
8555
+ finalizeClaim,
8391
8556
  ...onchainSeams?.claim ? { claimOnchain: onchainSeams.claim } : {},
8392
8557
  ...onchainSeams?.refund ? { refundOnchain: onchainSeams.refund } : {}
8393
8558
  });
@@ -8680,7 +8845,7 @@ var createSwapDrive = (config) => {
8680
8845
  id
8681
8846
  );
8682
8847
  }
8683
- const script = import_base19.hex.decode(record.lockupPkScript);
8848
+ const script = import_base20.hex.decode(record.lockupPkScript);
8684
8849
  const before = await recoverableAt(script);
8685
8850
  if (before.length === 0) {
8686
8851
  throw new SwapDriveRefusedError(
@@ -9336,12 +9501,12 @@ var verifyPlanLegs = (plan, legs, give) => {
9336
9501
  };
9337
9502
 
9338
9503
  // src/client/quoteRfq.ts
9339
- var import_base21 = require("@scure/base");
9504
+ var import_base22 = require("@scure/base");
9340
9505
  var import_sdk35 = require("@arkade-os/sdk");
9341
9506
  var import_solver_discovery12 = require("@arkade-os/solver-discovery");
9342
9507
 
9343
9508
  // src/jointGraph.ts
9344
- var import_base20 = require("@scure/base");
9509
+ var import_base21 = require("@scure/base");
9345
9510
  var import_sha26 = require("@noble/hashes/sha2.js");
9346
9511
  var import_sdk32 = require("@arkade-os/sdk");
9347
9512
  function digestJointGraph(plan, template) {
@@ -9377,7 +9542,7 @@ function digestJointGraph(plan, template) {
9377
9542
  preimage.set(part, at2);
9378
9543
  at2 += part.length;
9379
9544
  }
9380
- return import_base20.hex.encode((0, import_sha26.sha256)(preimage));
9545
+ return import_base21.hex.encode((0, import_sha26.sha256)(preimage));
9381
9546
  }
9382
9547
  function verifyJointGraph(plan, template) {
9383
9548
  try {
@@ -9405,7 +9570,7 @@ function verifyJointGraph(plan, template) {
9405
9570
  }
9406
9571
  }
9407
9572
  function parseGraphTx(psbt) {
9408
- return import_sdk32.Transaction.fromPSBT(import_base20.base64.decode(psbt));
9573
+ return import_sdk32.Transaction.fromPSBT(import_base21.base64.decode(psbt));
9409
9574
  }
9410
9575
  function deepFreeze(value) {
9411
9576
  if (value !== null && typeof value === "object") {
@@ -9495,10 +9660,10 @@ var covenantInputs = (input) => {
9495
9660
  const network = (0, import_sdk35.networkFromOperatorInfo)(input.info);
9496
9661
  const arkade3 = input.corridors.get("arkade").deps;
9497
9662
  return {
9498
- operatorPubkey: (0, import_sdk35.toXOnly)(import_base21.hex.decode(input.info.signerPubkey), "ark signer key"),
9663
+ operatorPubkey: (0, import_sdk35.toXOnly)(import_base22.hex.decode(input.info.signerPubkey), "ark signer key"),
9499
9664
  // The resolved per-network pin, never operator-reported: it lands in a covenant leaf
9500
9665
  // that decides who can move the funds.
9501
- emulatorPubkey: (0, import_sdk35.toXOnly)(import_base21.hex.decode(arkade3.emulatorPubkey), "emulator signer key"),
9666
+ emulatorPubkey: (0, import_sdk35.toXOnly)(import_base22.hex.decode(arkade3.emulatorPubkey), "emulator signer key"),
9502
9667
  claimDelay: unilateralClaimDelay(Number(input.info.unilateralExitDelay)),
9503
9668
  hrp: network.hrp
9504
9669
  };
@@ -9506,7 +9671,7 @@ var covenantInputs = (input) => {
9506
9671
  var sealingKey = (deps) => {
9507
9672
  const configured = deps.covclaimd?.pubkey;
9508
9673
  if (configured === void 0) return void 0;
9509
- const key = import_base21.hex.decode(configured);
9674
+ const key = import_base22.hex.decode(configured);
9510
9675
  if (key.length !== 33) {
9511
9676
  throw new Error(
9512
9677
  `the covclaimd deployment key must be 33-byte compressed hex, got ${key.length} bytes`
@@ -9563,7 +9728,7 @@ var quoteArkadeAsset = async (input) => {
9563
9728
  receiveAddress,
9564
9729
  maker.pkScript,
9565
9730
  (0, import_sdk35.networkFromOperatorInfo)(input.info).hrp,
9566
- (0, import_sdk35.toXOnly)(import_base21.hex.decode(input.info.signerPubkey), "ark signer key")
9731
+ (0, import_sdk35.toXOnly)(import_base22.hex.decode(input.info.signerPubkey), "ark signer key")
9567
9732
  );
9568
9733
  const choice = payment?.carrier;
9569
9734
  const carrierRequest = choice === void 0 ? void 0 : choice.mode === "purchase" ? { mode: "purchase" } : {
@@ -9575,7 +9740,7 @@ var quoteArkadeAsset = async (input) => {
9575
9740
  if (choice?.mode === "recycleReceiver") {
9576
9741
  if (choice.quote.assetId !== sides.wantAsset?.toString())
9577
9742
  throw new Error("carrier quote assetId differs from requested asset");
9578
- if (choice.quote.senderKey !== import_base21.hex.encode(maker.publicKey))
9743
+ if (choice.quote.senderKey !== import_base22.hex.encode(maker.publicKey))
9579
9744
  throw new Error("carrier quote senderKey differs from wallet identity");
9580
9745
  if (input.now >= choice.quote.expiresAt - 30)
9581
9746
  throw new Error("carrier quote expired before RFQ");
@@ -9628,7 +9793,7 @@ var quoteArkadeAsset = async (input) => {
9628
9793
  if (input.now >= validUntil) throw new Error("asset payment terms lapsed before funding");
9629
9794
  assetPayment = {
9630
9795
  receiveAddress: receiveAddress ?? new import_sdk35.ArkAddress(
9631
- (0, import_sdk35.toXOnly)(import_base21.hex.decode(input.info.signerPubkey), "ark signer key"),
9796
+ (0, import_sdk35.toXOnly)(import_base22.hex.decode(input.info.signerPubkey), "ark signer key"),
9632
9797
  maker.pkScript.slice(2),
9633
9798
  (0, import_sdk35.networkFromOperatorInfo)(input.info).hrp
9634
9799
  ).encode(),
@@ -9805,7 +9970,7 @@ var quoteLightningReceive = async (input) => {
9805
9970
  (0, import_sdk35.provisionClaimSecret)(input.wallet),
9806
9971
  input.wallet.getAddress()
9807
9972
  ]);
9808
- const paymentHash = import_base21.hex.encode(secrets.paymentHash);
9973
+ const paymentHash = import_base22.hex.encode(secrets.paymentHash);
9809
9974
  const lightning = input.corridors.get("lightning").deps;
9810
9975
  const sealTo = sealingKey(lightning);
9811
9976
  const sealsAmount = pinned.on === "take" && input.legs.take.assetId === BTC_ASSET_ID;
@@ -9925,7 +10090,7 @@ var quoteOnchainSend = async (input) => {
9925
10090
  (0, import_sdk35.provisionClaimSecret)(input.wallet),
9926
10091
  (0, import_sdk35.provisionRefundKey)(input.wallet)
9927
10092
  ]);
9928
- const paymentHash = import_base21.hex.encode(secrets.paymentHash);
10093
+ const paymentHash = import_base22.hex.encode(secrets.paymentHash);
9929
10094
  const wire = await input.transport.requestQuote(
9930
10095
  withCanonicalAmount(
9931
10096
  onchainSendRequest({
@@ -10036,7 +10201,7 @@ var quoteOnchainReceive = async (input) => {
10036
10201
  input.wallet.getAddress(),
10037
10202
  input.wallet.getBoardingAddress()
10038
10203
  ]);
10039
- const paymentHash = import_base21.hex.encode(secrets.paymentHash);
10204
+ const paymentHash = import_base22.hex.encode(secrets.paymentHash);
10040
10205
  const refundPkScript = l1ScriptForAddress(boardingAddress, l1Network);
10041
10206
  const wire = await input.transport.requestQuote(
10042
10207
  withCanonicalAmount(
@@ -10236,7 +10401,7 @@ var nostrTransportFactory = createNostrTransportFactory();
10236
10401
 
10237
10402
  // src/client/client.ts
10238
10403
  var PREPARATIONS_HELD = 64;
10239
- var mintQuoteId = () => import_base22.hex.encode(crypto.getRandomValues(new Uint8Array(16)));
10404
+ var mintQuoteId = () => import_base23.hex.encode(crypto.getRandomValues(new Uint8Array(16)));
10240
10405
  var createSwapClient = (config) => {
10241
10406
  const { wallet } = config;
10242
10407
  const operator = config.operator ?? walletOperator(wallet);
@@ -10491,7 +10656,7 @@ var createSwapClient = (config) => {
10491
10656
  };
10492
10657
 
10493
10658
  // src/reveal.ts
10494
- var import_base23 = require("@scure/base");
10659
+ var import_base24 = require("@scure/base");
10495
10660
  var import_sdk38 = require("@arkade-os/sdk");
10496
10661
  init_claimAmountGate();
10497
10662
  init_claimPacket();
@@ -10519,7 +10684,7 @@ var decodeKey = (value, field) => {
10519
10684
  if (typeof value !== "string") throw new Error(`covclaimd info: ${field} is not a string`);
10520
10685
  let bytes;
10521
10686
  try {
10522
- bytes = import_base23.hex.decode(value);
10687
+ bytes = import_base24.hex.decode(value);
10523
10688
  } catch {
10524
10689
  throw new Error(`covclaimd info: ${field} is not hex`);
10525
10690
  }
@@ -10580,7 +10745,7 @@ var covclaimdClient = (baseUrl, options = {}) => {
10580
10745
  const body = await readJson("info", response);
10581
10746
  return {
10582
10747
  covclaimdPubkey: decodeKey(body?.covclaimd_pub_key, "covclaimd_pub_key"),
10583
- emulatorPubkey: import_base23.hex.encode(decodeKey(body?.emulator_pub_key, "emulator_pub_key"))
10748
+ emulatorPubkey: import_base24.hex.encode(decodeKey(body?.emulator_pub_key, "emulator_pub_key"))
10584
10749
  };
10585
10750
  },
10586
10751
  async reveal(params) {
@@ -10608,13 +10773,13 @@ function revealFieldsFromScript(script) {
10608
10773
  throw new Error("this covenant has no non-interactive claim leaf to reveal against");
10609
10774
  }
10610
10775
  return {
10611
- arkadeScript: import_base23.base64.encode(script.nonInteractiveClaimArkadeScript),
10612
- taptree: import_base23.hex.encode(script.encode())
10776
+ arkadeScript: import_base24.base64.encode(script.nonInteractiveClaimArkadeScript),
10777
+ taptree: import_base24.hex.encode(script.encode())
10613
10778
  };
10614
10779
  }
10615
10780
  async function revealClaimPacket(client, input) {
10616
10781
  const fields = revealFieldsFromScript(input.script);
10617
- if (import_base23.hex.encode(import_sdk38.ArkAddress.decode(input.address).pkScript) !== import_base23.hex.encode(input.script.pkScript)) {
10782
+ if (import_base24.hex.encode(import_sdk38.ArkAddress.decode(input.address).pkScript) !== import_base24.hex.encode(input.script.pkScript)) {
10618
10783
  throw new Error("address does not belong to this covenant");
10619
10784
  }
10620
10785
  assertPreimageMatches(input.preimage, input.script.options.preimageHash);
@@ -10630,7 +10795,7 @@ async function revealClaimPacket(client, input) {
10630
10795
  }
10631
10796
 
10632
10797
  // src/offerFillPlan.ts
10633
- var import_base24 = require("@scure/base");
10798
+ var import_base25 = require("@scure/base");
10634
10799
  var import_sdk39 = require("@arkade-os/sdk");
10635
10800
  init_offer();
10636
10801
  var OFFER_FILL_TEMPLATE = "offer-fill/1";
@@ -10645,7 +10810,7 @@ async function buildOfferFillPlan(wallet, arkServerUrl, offerHex, opts) {
10645
10810
  assetCarrierSats = ASSET_CARRIER_SATS,
10646
10811
  sponsor
10647
10812
  } = opts;
10648
- const offer = decodeOffer(import_base24.hex.decode(offerHex));
10813
+ const offer = decodeOffer(import_base25.hex.decode(offerHex));
10649
10814
  const contract = await connectFillContract(wallet, arkServerUrl, offer, { swapAddress });
10650
10815
  const vtxos = await contract.getUtxos();
10651
10816
  const vtxo = resolveDeposit(vtxos, { fundingTxid, fundingOutpoint });
@@ -10694,7 +10859,7 @@ function toJointGraph(arkTx, checkpoints, layout, wantedAssetId) {
10694
10859
  }
10695
10860
  layout.inputs.forEach((input, i) => {
10696
10861
  const spent = checkpoints[i].getInput(0);
10697
- if (!(spent.txid instanceof Uint8Array) || import_base24.hex.encode(spent.txid) !== input.txid || spent.index !== input.vout) {
10862
+ if (!(spent.txid instanceof Uint8Array) || import_base25.hex.encode(spent.txid) !== input.txid || spent.index !== input.vout) {
10698
10863
  throw new Error(`fill plan checkpoint ${i} does not spend ${input.txid}:${input.vout}`);
10699
10864
  }
10700
10865
  });
@@ -10706,7 +10871,7 @@ function toJointGraph(arkTx, checkpoints, layout, wantedAssetId) {
10706
10871
  }
10707
10872
  layout.outputs.forEach((expected, i) => {
10708
10873
  const out = arkTx.getOutput(i);
10709
- if (!out?.script || import_base24.hex.encode(out.script) !== import_base24.hex.encode(expected.script)) {
10874
+ if (!out?.script || import_base25.hex.encode(out.script) !== import_base25.hex.encode(expected.script)) {
10710
10875
  throw new Error(`fill plan output ${i} is not the ${expected.role} script`);
10711
10876
  }
10712
10877
  if (out.amount !== expected.sats) {
@@ -10720,7 +10885,7 @@ function toJointGraph(arkTx, checkpoints, layout, wantedAssetId) {
10720
10885
  throw new Error(`fill plan output ${payments} is not the extension packet`);
10721
10886
  }
10722
10887
  const anchor = arkTx.getOutput(payments + 1);
10723
- if (!anchor?.script || import_base24.hex.encode(anchor.script) !== import_base24.hex.encode(import_sdk39.P2A.script) || anchor.amount !== import_sdk39.P2A.amount) {
10888
+ if (!anchor?.script || import_base25.hex.encode(anchor.script) !== import_base25.hex.encode(import_sdk39.P2A.script) || anchor.amount !== import_sdk39.P2A.amount) {
10724
10889
  throw new Error(`fill plan output ${payments + 1} is not the anchor`);
10725
10890
  }
10726
10891
  const packet = import_sdk39.Extension.fromTx(arkTx).getAssetPacket();
@@ -10752,8 +10917,8 @@ function toJointGraph(arkTx, checkpoints, layout, wantedAssetId) {
10752
10917
  );
10753
10918
  }
10754
10919
  }
10755
- const arkTxPsbt = import_base24.base64.encode(arkTx.toPSBT());
10756
- const checkpointPsbts = checkpoints.map((c) => import_base24.base64.encode(c.toPSBT()));
10920
+ const arkTxPsbt = import_base25.base64.encode(arkTx.toPSBT());
10921
+ const checkpointPsbts = checkpoints.map((c) => import_base25.base64.encode(c.toPSBT()));
10757
10922
  const inputOwners = layout.inputs.map((input) => input.owner);
10758
10923
  const graph = {
10759
10924
  arkTx: arkTxPsbt,
@@ -10774,7 +10939,7 @@ function verifyOfferFillPlan(plan) {
10774
10939
  }
10775
10940
 
10776
10941
  // src/evmRfq.ts
10777
- var import_base25 = require("@scure/base");
10942
+ var import_base26 = require("@scure/base");
10778
10943
  init_rfq();
10779
10944
  var EVM_CHAIN = "ethereum";
10780
10945
  var EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;
@@ -10839,7 +11004,7 @@ var evmSendRequest = (input) => {
10839
11004
  payment_hash: input.paymentHash,
10840
11005
  evm_claim_address: assertEvmAddress(input.evmClaimAddress, "evmClaimAddress"),
10841
11006
  refund_address: input.refundAddress,
10842
- client_refund_pubkey: import_base25.hex.encode(input.senderPubkey)
11007
+ client_refund_pubkey: import_base26.hex.encode(input.senderPubkey)
10843
11008
  }
10844
11009
  };
10845
11010
  };
@@ -10863,7 +11028,7 @@ var evmReceiveRequest = (input) => {
10863
11028
  evm_timeout_block: input.evmTimeoutBlock,
10864
11029
  evm_refund_address: assertEvmAddress(input.evmRefundAddress, "evmRefundAddress"),
10865
11030
  payout_address: input.payoutAddress,
10866
- payout_pubkey: import_base25.hex.encode(input.payoutPubkey)
11031
+ payout_pubkey: import_base26.hex.encode(input.payoutPubkey)
10867
11032
  }
10868
11033
  };
10869
11034
  };