@arkade-os/swap 0.1.8 → 0.1.10

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 (45) hide show
  1. package/dist/advanced.cjs +1125 -554
  2. package/dist/advanced.d.cts +19 -21
  3. package/dist/advanced.d.ts +19 -21
  4. package/dist/advanced.js +40 -34
  5. package/dist/{chunk-ISWLKKSS.js → chunk-2HQZLIVX.js} +106 -39
  6. package/dist/{chunk-5QPJWOCN.js → chunk-6RJVT65W.js} +2 -0
  7. package/dist/chunk-AUJDREZN.js +445 -0
  8. package/dist/{chunk-ZBJGLLYV.js → chunk-FWRDAL73.js} +157 -477
  9. package/dist/{chunk-PDNVKH5L.js → chunk-LCO6AJOS.js} +453 -70
  10. package/dist/index.cjs +1110 -542
  11. package/dist/index.d.cts +5 -6
  12. package/dist/index.d.ts +5 -6
  13. package/dist/index.js +24 -22
  14. package/dist/{lockupContract-DguuT7NK.d.cts → lockupContract-BTTRVwOt.d.cts} +1 -1
  15. package/dist/{lockupContract-BUOsghl1.d.ts → lockupContract-aFarhlxj.d.ts} +1 -1
  16. package/dist/node/index.d.cts +97 -67
  17. package/dist/node/index.d.ts +97 -67
  18. package/dist/nostr.cjs +925 -516
  19. package/dist/nostr.d.cts +1 -1
  20. package/dist/nostr.d.ts +1 -1
  21. package/dist/nostr.js +3 -2
  22. package/dist/{offerFillPlan-_AOLo6kh.d.ts → offerFillPlan-CJC5tnEd.d.ts} +10 -42
  23. package/dist/{offerFillPlan-Bp6-z6dS.d.cts → offerFillPlan-FmyaQoYT.d.cts} +10 -42
  24. package/dist/protocol.cjs +1082 -188
  25. package/dist/protocol.d.cts +42 -23
  26. package/dist/protocol.d.ts +42 -23
  27. package/dist/protocol.js +20 -7
  28. package/dist/repositories/realm/index.d.cts +3 -2
  29. package/dist/repositories/realm/index.d.ts +3 -2
  30. package/dist/repositories/sqlite/index.d.cts +3 -2
  31. package/dist/repositories/sqlite/index.d.ts +3 -2
  32. package/dist/{repository-CkKK0dGO.d.ts → repository-CwwhIAEd.d.cts} +406 -74
  33. package/dist/{repository--pfikw2R.d.cts → repository-DO6QKxBx.d.ts} +406 -74
  34. package/dist/{rfq-IzhOCG02.d.ts → rfq-CYxOyniL.d.ts} +52 -11
  35. package/dist/rfq-contract.cjs +3 -0
  36. package/dist/rfq-contract.d.cts +3 -1
  37. package/dist/rfq-contract.d.ts +3 -1
  38. package/dist/rfq-contract.js +3 -1
  39. package/dist/{rfq-BRpo5uWU.d.cts → rfq-nMGywkqI.d.cts} +52 -11
  40. package/dist/verify-ENO36VPK.js +33 -0
  41. package/dist/{watch-pvTVFp6-.d.cts → watch-DpDSP2tu.d.cts} +4 -3
  42. package/dist/{watch-DDNycuUa.d.ts → watch-_fZSgx_l.d.ts} +4 -3
  43. package/package.json +2 -2
  44. package/dist/offer-BhonWuKR.d.ts +0 -274
  45. package/dist/offer-D_qHpWo2.d.cts +0 -274
@@ -9,22 +9,28 @@ import {
9
9
  RETIRABLE,
10
10
  RFQ_TERMINAL_STATES,
11
11
  assertPreimageMatches,
12
+ bindingFromOffer,
13
+ bindingProgram,
12
14
  classifyOnchainHtlc,
13
15
  decodeOffer,
16
+ encodeOffer,
14
17
  gateError,
15
18
  getAssetSwaps,
16
19
  hydrateHashlock,
17
20
  l1NetworkFromArk,
18
21
  lockupContractParams,
19
22
  offerContract,
23
+ offerFromBinding,
20
24
  onchainHtlcScript,
21
25
  pollUntil,
26
+ recordOfferBinding,
22
27
  registerLockupContract,
23
28
  retireOfferContract,
24
29
  rfqCorridorHandlers,
25
30
  rfqSignerOf,
31
+ storedOffer,
26
32
  updateAssetSwapBestEffort
27
- } from "./chunk-PDNVKH5L.js";
33
+ } from "./chunk-LCO6AJOS.js";
28
34
  import {
29
35
  collectRfqSwaps
30
36
  } from "./chunk-A3SHHRX4.js";
@@ -1540,6 +1546,7 @@ import { base64 as base642, hex as hex4 } from "@scure/base";
1540
1546
  import {
1541
1547
  Extension as Extension2,
1542
1548
  Transaction as Transaction2,
1549
+ arkade,
1543
1550
  scriptFromTapLeafScript as scriptFromTapLeafScript2
1544
1551
  } from "@arkade-os/sdk";
1545
1552
  var TXS_PER_REQUEST = 50;
@@ -1578,8 +1585,16 @@ var unscannedSwapCandidates = (txs, existingIds, scanned) => txs.filter(
1578
1585
  function classifySpend(offer, operatorPubkey, spendTx, deposit) {
1579
1586
  let leaves;
1580
1587
  try {
1581
- const script = offerContract(offer, operatorPubkey);
1582
- if (hex4.encode(script.pkScript) !== hex4.encode(offer.swapPkScript)) return "indeterminate";
1588
+ let script;
1589
+ if ("version" in offer) {
1590
+ const { program, args, keys } = bindingProgram(offer);
1591
+ script = new arkade.ArkadeProgramScript(program, args, keys);
1592
+ } else {
1593
+ script = offerContract(offer, operatorPubkey);
1594
+ if (hex4.encode(script.pkScript) !== hex4.encode(offer.swapPkScript)) {
1595
+ return "indeterminate";
1596
+ }
1597
+ }
1583
1598
  leaves = {
1584
1599
  // `exit` is absent on an offer with no exit closure, and drops out here
1585
1600
  returned: ["cancel", "exit"].map((name) => script.functionByName(name)?.leafScript).filter((leaf) => leaf !== void 0),
@@ -1608,21 +1623,56 @@ function classifyDepositSpend(offer, operatorPubkey, spendTxs, deposit) {
1608
1623
  }
1609
1624
  return "indeterminate";
1610
1625
  }
1626
+ var encodesOffer = (offerHex, offer) => {
1627
+ try {
1628
+ const { ratioNum: _num, ratioDen: _den, ...terms } = decodeOffer(hex4.decode(offerHex));
1629
+ return hex4.encode(encodeOffer(terms)) === hex4.encode(encodeOffer(offer));
1630
+ } catch {
1631
+ return false;
1632
+ }
1633
+ };
1634
+ var packetRecoverySource = (indexer) => ({
1635
+ name: "packet",
1636
+ async discover({ candidateTxids, operatorPubkey }) {
1637
+ const found = [];
1638
+ const declined = [];
1639
+ for (const [txid, parsed] of await fetchParsedTxs(indexer, [...candidateTxids])) {
1640
+ let packet;
1641
+ try {
1642
+ packet = Extension2.fromTx(parsed).getPacketByType(OFFER_PACKET_TYPE);
1643
+ } catch {
1644
+ }
1645
+ if (!packet) {
1646
+ declined.push(txid);
1647
+ continue;
1648
+ }
1649
+ const payload = packet.serialize();
1650
+ try {
1651
+ const binding = bindingFromOffer(decodeOffer(payload), operatorPubkey);
1652
+ found.push({ fundingTxid: txid, binding, offerHex: hex4.encode(payload) });
1653
+ } catch {
1654
+ }
1655
+ }
1656
+ return { found, declined };
1657
+ }
1658
+ });
1611
1659
  async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1612
1660
  const {
1613
1661
  operatorPubkey,
1614
1662
  scanned = /* @__PURE__ */ new Set(),
1615
1663
  reopen = [],
1616
1664
  hrp,
1617
- recoveredIn = /* @__PURE__ */ new Set()
1665
+ recoveredIn = /* @__PURE__ */ new Set(),
1666
+ sources = [packetRecoverySource(indexer)]
1618
1667
  } = opts;
1619
1668
  const reopened = [];
1620
1669
  for (const swap of reopen) {
1621
1670
  try {
1671
+ const source = storedOffer(swap);
1622
1672
  reopened.push({
1623
1673
  fundingTx: { type: "sent", redeemTxid: swap.fundingTxid },
1624
- offer: decodeOffer(hex4.decode(swap.offerHex)),
1625
- offerHex: swap.offerHex,
1674
+ offer: "version" in source ? offerFromBinding(source) : source,
1675
+ source,
1626
1676
  existing: swap
1627
1677
  });
1628
1678
  } catch {
@@ -1636,29 +1686,40 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1636
1686
  return { restored: [], scannedTxids: [] };
1637
1687
  }
1638
1688
  const byTxid = new Map(candidates.map((tx) => [tx.redeemTxid, tx]));
1639
- const parsedByTxid = await fetchParsedTxs(
1640
- indexer,
1641
- candidates.map((tx) => tx.redeemTxid)
1642
- );
1643
- const fetchedTxids = [];
1689
+ const candidateTxids = candidates.map((tx) => tx.redeemTxid);
1644
1690
  const found = [...reopened];
1645
- for (const [txid, parsed] of parsedByTxid) {
1646
- const fundingTx = byTxid.get(txid);
1647
- if (!fundingTx) continue;
1648
- fetchedTxids.push(txid);
1691
+ const claimed = /* @__PURE__ */ new Set();
1692
+ const declined = /* @__PURE__ */ new Set();
1693
+ for (const recovery of candidateTxids.length === 0 ? [] : sources) {
1694
+ let answer;
1649
1695
  try {
1650
- const packet = Extension2.fromTx(parsed).getPacketByType(OFFER_PACKET_TYPE);
1651
- if (!packet) continue;
1652
- const payload = packet.serialize();
1653
- found.push({
1654
- fundingTx,
1655
- offer: decodeOffer(payload),
1656
- offerHex: hex4.encode(payload)
1657
- });
1658
- } catch {
1696
+ answer = await recovery.discover({ candidateTxids, operatorPubkey });
1697
+ } catch (error) {
1698
+ console.warn(`[swap] recovery source ${recovery.name} failed`, error);
1699
+ continue;
1700
+ }
1701
+ for (const txid of answer.declined) declined.add(txid);
1702
+ for (const { fundingTxid, binding, offerHex } of answer.found) {
1703
+ const fundingTx = byTxid.get(fundingTxid);
1704
+ if (!fundingTx || claimed.has(fundingTxid)) continue;
1705
+ let offer;
1706
+ try {
1707
+ offer = offerFromBinding(binding);
1708
+ } catch (error) {
1709
+ console.warn(`[swap] ${recovery.name} sent a binding that does not compile`, error);
1710
+ continue;
1711
+ }
1712
+ if (offerHex !== void 0 && !encodesOffer(offerHex, offer)) {
1713
+ console.warn(`[swap] ${recovery.name} sent a packet its binding does not match`);
1714
+ continue;
1715
+ }
1716
+ claimed.add(fundingTxid);
1717
+ const packet = offerHex ?? hex4.encode(encodeOffer(offer));
1718
+ found.push({ fundingTx, offer, source: binding, offerHex: packet });
1659
1719
  }
1660
1720
  }
1661
- if (found.length === 0) return { restored: [], scannedTxids: fetchedTxids };
1721
+ const answered = [...claimed, ...[...declined].filter((txid) => !claimed.has(txid))];
1722
+ if (found.length === 0) return { restored: [], scannedTxids: answered };
1662
1723
  const scripts = [...new Set(found.map((f) => hex4.encode(f.offer.swapPkScript)))];
1663
1724
  const { vtxos } = await indexer.getVtxos({ scripts });
1664
1725
  const vtxoByScriptAndTxid = new Map(vtxos.map((v) => [`${v.script}:${v.txid}`, v]));
@@ -1679,7 +1740,7 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1679
1740
  const spendTxByTxid = await fetchParsedTxs(indexer, [...spendTxids]);
1680
1741
  const restored = [];
1681
1742
  const unresolved = /* @__PURE__ */ new Set();
1682
- for (const { fundingTx, offer, offerHex, existing } of found) {
1743
+ for (const { fundingTx, offer, source, offerHex, existing } of found) {
1683
1744
  const swapPkScript = hex4.encode(offer.swapPkScript);
1684
1745
  const vtxo = vtxoByScriptAndTxid.get(`${swapPkScript}:${fundingTx.redeemTxid}`);
1685
1746
  if (!vtxo) {
@@ -1708,7 +1769,7 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1708
1769
  } else if (vtxo.isSwept && !vtxo.isSpent) status = "recoverable";
1709
1770
  else if (vtxo.isSpent) {
1710
1771
  const spendTxs = spendTxidsOf(vtxo).map((id) => spendTxByTxid.get(id)).filter((tx) => tx !== void 0);
1711
- const kind = classifyDepositSpend(offer, operatorPubkey, spendTxs, {
1772
+ const kind = classifyDepositSpend(source, operatorPubkey, spendTxs, {
1712
1773
  txid: vtxo.txid,
1713
1774
  vout: vtxo.vout
1714
1775
  });
@@ -1724,20 +1785,19 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1724
1785
  restored.push({ ...existing, status, spentTxid, ...completion });
1725
1786
  continue;
1726
1787
  }
1788
+ if (!("version" in source)) continue;
1727
1789
  let swapAddress = "";
1728
1790
  try {
1729
- const compiled = offerContract(offer, operatorPubkey);
1730
- if (hex4.encode(compiled.pkScript) !== swapPkScript) {
1731
- unresolved.add(fundingTx.redeemTxid);
1732
- continue;
1733
- }
1734
1791
  if (hrp !== void 0) {
1735
- swapAddress = compiled.address(hrp, operatorPubkey).encode();
1792
+ const { program, args, keys } = bindingProgram(source);
1793
+ const compiled = new arkade.ArkadeProgramScript(program, args, keys);
1794
+ swapAddress = compiled.address(hrp, source.serverPublicKey).encode();
1736
1795
  }
1737
1796
  } catch {
1738
1797
  unresolved.add(fundingTx.redeemTxid);
1739
1798
  continue;
1740
1799
  }
1800
+ const covenant = source.taker === void 0 ? { offerHex: offerHex ?? hex4.encode(encodeOffer(offer)) } : { binding: recordOfferBinding(source) };
1741
1801
  restored.push({
1742
1802
  id: fundingTx.redeemTxid,
1743
1803
  fromAsset,
@@ -1746,7 +1806,7 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1746
1806
  toAmount: offer.wantAmount.toString(),
1747
1807
  swapAddress,
1748
1808
  swapPkScript,
1749
- offerHex,
1809
+ ...covenant,
1750
1810
  fundingTxid: fundingTx.redeemTxid,
1751
1811
  spentTxid,
1752
1812
  status,
@@ -1754,7 +1814,7 @@ async function restoreAssetSwaps(indexer, txs, existingIds, opts) {
1754
1814
  ...completion
1755
1815
  });
1756
1816
  }
1757
- return { restored, scannedTxids: fetchedTxids.filter((id) => !unresolved.has(id)) };
1817
+ return { restored, scannedTxids: answered.filter((id) => !unresolved.has(id)) };
1758
1818
  }
1759
1819
 
1760
1820
  // src/rfqSwapState.ts
@@ -1932,6 +1992,9 @@ async function pushClaim(operator, input) {
1932
1992
  assertFiniteAmount(funded, "lockup_malformed", "the lockup's summed value");
1933
1993
  if (!input.partiallyClaimed) {
1934
1994
  assertFiniteAmount(input.expectedAmount, "invalid_gate_input", "expectedAmount");
1995
+ if (input.expectedAmount <= 0) {
1996
+ throw gateError("invalid_gate_input", "expectedAmount is not positive");
1997
+ }
1935
1998
  if (funded < input.expectedAmount) {
1936
1999
  throw new LockupAmountMismatchError(input.expectedAmount, funded);
1937
2000
  }
@@ -2882,10 +2945,10 @@ var RfqSwapManager = class _RfqSwapManager {
2882
2945
  return;
2883
2946
  }
2884
2947
  if (!partiallyClaimed) {
2885
- if (!Number.isFinite(swap.expectedAmount)) {
2948
+ if (!Number.isSafeInteger(swap.expectedAmount) || swap.expectedAmount <= 0) {
2886
2949
  return this.block(
2887
2950
  swap,
2888
- `expectedAmount is not a finite number (${String(swap.expectedAmount)}), so the funded value cannot be checked \u2014 refusing to publish the preimage`
2951
+ `expectedAmount is not a positive number of sats (${String(swap.expectedAmount)}), so the funded value cannot be checked \u2014 refusing to publish the preimage`
2889
2952
  );
2890
2953
  }
2891
2954
  const locked = vtxos.reduce((sum, vtxo) => sum + vtxo.value, 0);
@@ -3146,6 +3209,9 @@ var RfqSwapManager = class _RfqSwapManager {
3146
3209
  utxo,
3147
3210
  bump ? { bump } : void 0
3148
3211
  );
3212
+ if (swap.funding?.txid !== utxo.txid || swap.funding.vout !== utxo.vout || swap.funding.amount === void 0) {
3213
+ swap.funding = { txid: utxo.txid, vout: utxo.vout, amount: Number(utxo.amount) };
3214
+ }
3149
3215
  swap.claimTxid = txid;
3150
3216
  swap.claimBroadcastAt = this.config.now();
3151
3217
  delete swap.claimConfirmed;
@@ -3400,7 +3466,7 @@ var errorMessage = (error) => error instanceof Error ? error.message : String(er
3400
3466
  var outpointKey = (vtxo) => `${vtxo.txid}:${vtxo.vout}`;
3401
3467
 
3402
3468
  // src/watch.ts
3403
- import { base64 as base644, hex as hex8 } from "@scure/base";
3469
+ import { base64 as base644 } from "@scure/base";
3404
3470
  import {
3405
3471
  ArkAddress as ArkAddress2,
3406
3472
  isContractVtxoEvent as isContractVtxoEvent2,
@@ -3450,7 +3516,7 @@ async function watchOfferSwaps({
3450
3516
  if (candidates.length === 0) return "indeterminate";
3451
3517
  const { txs } = await indexer.getVirtualTxs(candidates);
3452
3518
  return classifyDepositSpend(
3453
- decodeOffer(hex8.decode(swap.offerHex)),
3519
+ storedOffer(swap),
3454
3520
  operatorPubkey,
3455
3521
  txs.map((psbt) => Transaction4.fromPSBT(base644.decode(psbt))),
3456
3522
  { txid: vtxo.txid, vout: vtxo.vout }
@@ -3563,6 +3629,7 @@ export {
3563
3629
  classifySpend,
3564
3630
  spendTxidsOf,
3565
3631
  classifyDepositSpend,
3632
+ packetRecoverySource,
3566
3633
  restoreAssetSwaps,
3567
3634
  isRfqSwapTerminal,
3568
3635
  isRecheckableRefundedSend,
@@ -4,6 +4,7 @@ var RFQ_CONTRACT_VERSION_KEY = "contract_version";
4
4
  var RFQ_CONTRACT_VERSION_UNSUPPORTED = "contract_version_unsupported";
5
5
  var ASSET_SWAP_V2 = "asset-swap-v2";
6
6
  var ASSET_SWAP_V3 = "asset-swap-v3";
7
+ var ASSET_SWAP_V4 = "asset-swap-v4";
7
8
  var ASSET_SWAP_BASELINE = ASSET_SWAP_V2;
8
9
 
9
10
  export {
@@ -12,5 +13,6 @@ export {
12
13
  RFQ_CONTRACT_VERSION_UNSUPPORTED,
13
14
  ASSET_SWAP_V2,
14
15
  ASSET_SWAP_V3,
16
+ ASSET_SWAP_V4,
15
17
  ASSET_SWAP_BASELINE
16
18
  };