aboutsummaryrefslogtreecommitdiffstats
path: root/tools/swap-e2e-submarine.test.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-06-27 08:41:08 -0700
committerAdam Soltys <[email protected]>2026-06-27 08:41:08 -0700
commit10f9ebc0d047f1599f4665fda0b073c449bb8f64 (patch)
treeb5e68e8044cabd9d878b8ed8e3715d46e5cc9d0c /tools/swap-e2e-submarine.test.js
parenta9a903e7a1f80ed4a6f4163419b25d4e6de5db01 (diff)
downloadcoinosv3-10f9ebc0d047f1599f4665fda0b073c449bb8f64.tar.gz
coinosv3-10f9ebc0d047f1599f4665fda0b073c449bb8f64.zip
Add Boltz submarine + reverse swaps (Lightning <-> on-chain)
Non-custodial swaps via Boltz, fully client-side: hal holds the swap key + preimage and claims (reverse) / refunds (submarine) on-chain via the taproot script path, so Boltz can never strand funds. - src/swap.js: tapleaf/tree construction (byte-exact vs boltz-core vectors), swap P2TR address derivation (MuSig2 internal key + taptweak), script-path claim/refund tx builder, bolt11 decode, Boltz api-v2 client, and a polling SwapManager (reverse auto-claim; submarine fund + status + refund). - wallet.js: deterministic swap-key derivation (dedicated chain) + persistence. - api.js: configurable Boltz REST/WS endpoints. - app.js: ⚡ Swap tab — Receive over LN (reverse) + Pay an invoice (submarine). - tools/swap-*.test.js: vectors, live-address, and end-to-end regtest tests. Verified end-to-end on regtest against a local Boltz wired to coinos's bc + a dedicated CLN/hold node. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Diffstat (limited to 'tools/swap-e2e-submarine.test.js')
-rw-r--r--tools/swap-e2e-submarine.test.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/tools/swap-e2e-submarine.test.js b/tools/swap-e2e-submarine.test.js
new file mode 100644
index 0000000..265721d
--- /dev/null
+++ b/tools/swap-e2e-submarine.test.js
@@ -0,0 +1,56 @@
+// End-to-end SUBMARINE swap (hal spends over Lightning) on regtest:
+// 1. `cl` issues an invoice (the payee/merchant)
+// 2. hal creates a submarine swap for it (refund key it controls)
+// 3. hal funds the on-chain lockup with expectedAmount (here: from bc)
+// 4. Boltz detects the lockup, pays the LN invoice, and claims the lockup
+// 5. the invoice is paid → hal's on-chain spend reached a LN payee
+// Proves swap creation + address + the funded-lockup → Boltz-pays path.
+// (Refund-path signing is the same buildSwapSpend validated by the reverse E2E.)
+// Run (stack up): bun tools/swap-e2e-submarine.test.js
+import { $ } from 'bun';
+import { secp256k1 } from '@noble/curves/secp256k1';
+import { ripemd160 } from '@noble/hashes/legacy';
+import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
+import { swapP2TR } from '../src/swap.js';
+
+const BOLTZ = 'http://localhost:9001';
+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
+const post = async (p, b) => {
+ const r = await fetch(BOLTZ + p, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(b) });
+ const j = await r.json(); if (!r.ok) throw new Error(`${p}: ${JSON.stringify(j)}`); return j;
+};
+const bcli = (...a) => $`docker exec bc bitcoin-cli -regtest -rpcuser=admin1 -rpcpassword=123 ${a}`.text();
+const clcli = (...a) => $`docker exec cl lightning-cli --regtest ${a}`.text();
+
+// cl (payee) issues an invoice hal wants to pay.
+const amount = 120000;
+const inv = JSON.parse(await clcli('invoice', String(amount * 1000), `hal-${Date.now()}`, 'submarine test'));
+
+// hal's refund key + submarine swap.
+const refundPriv = secp256k1.utils.randomPrivateKey();
+const refundPub = secp256k1.getPublicKey(refundPriv, true);
+const swap = await post('/v2/swap/submarine', { invoice: inv.bolt11, from: 'BTC', to: 'BTC', refundPublicKey: bytesToHex(refundPub) });
+console.log('swap', swap.id, 'lockup', swap.address, 'expected', swap.expectedAmount);
+
+// Verify the lockup address before funding (don't-trust-verify).
+const d = swapP2TR({ kind: 'submarine', preimageHash160: ripemd160(hexToBytes(inv.payment_hash)), boltzPub33: hexToBytes(swap.claimPublicKey), halPub33: refundPub, lockTime: swap.timeoutBlockHeight, network: 'regtest' });
+if (d.address !== swap.address) throw new Error('address mismatch — abort');
+console.log('lockup address verified ✓');
+
+// Fund the lockup (in real hal this is wallet coin-selection; here: from bc).
+await bcli('-rpcwallet=coinos', 'sendtoaddress', swap.address, (swap.expectedAmount / 1e8).toFixed(8));
+const mineAddr = (await bcli('-rpcwallet=coinos', 'getnewaddress')).trim();
+await bcli('generatetoaddress', '1', mineAddr); // confirm (maxZeroConfAmount=0)
+console.log('lockup funded + confirmed; waiting for Boltz to pay the invoice…');
+
+// Boltz should detect the lockup, pay the LN invoice, and claim.
+let paid = false;
+for (let i = 0; i < 45; i++) {
+ await sleep(1000);
+ const ls = JSON.parse(await clcli('listinvoices'));
+ const mine = ls.invoices?.find((x) => x.payment_hash === inv.payment_hash);
+ if (mine?.status === 'paid') { paid = true; break; }
+}
+if (!paid) throw new Error('invoice not paid by Boltz within timeout');
+console.log(`\nPASS — Boltz paid hal's submarine invoice (${amount} sat) after the on-chain lockup. hal spent on-chain → LN payee.`);
+process.exit(0);