aboutsummaryrefslogtreecommitdiffstats
path: root/tools/swap-live.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-live.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-live.test.js')
-rw-r--r--tools/swap-live.test.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/tools/swap-live.test.js b/tools/swap-live.test.js
new file mode 100644
index 0000000..f56105a
--- /dev/null
+++ b/tools/swap-live.test.js
@@ -0,0 +1,57 @@
+// Live integration check against the local Boltz (http://localhost:9001) +
+// regtest CLN. Confirms Hal's locally-derived swap address + tapleaves match
+// Boltz's lockupAddress + swapTree for BOTH flows — proving the MuSig2
+// internal-key order ([boltzKey, halKey]), the taptweak, and leaf construction.
+// Run (regtest Boltz stack up): bun tools/swap-live.test.js
+import { $ } from 'bun';
+import { secp256k1 } from '@noble/curves/secp256k1';
+import { sha256 } from '@noble/hashes/sha2';
+import { ripemd160 } from '@noble/hashes/legacy';
+import { randomBytes, bytesToHex, hexToBytes } from '@noble/hashes/utils';
+import { hash160, swapP2TR, buildSwapTree } from '../src/swap.js';
+
+const BOLTZ = process.env.BOLTZ_API || 'http://localhost:9001';
+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} -> ${r.status} ${JSON.stringify(j)}`);
+ return j;
+};
+const kp = () => { const sk = secp256k1.utils.randomPrivateKey(); return { sk, pk: secp256k1.getPublicKey(sk, true) }; };
+let fail = 0;
+const eq = (n, g, w) => { if (g === w) console.log(` ok ${n}`); else { fail++; console.log(`FAIL ${n}\n got: ${g}\n want: ${w}`); } };
+
+// ---------- REVERSE (hal receives over LN; hal claims) ----------
+{
+ const claim = kp();
+ const preimage = randomBytes(32);
+ const res = await post('/v2/swap/reverse', {
+ invoiceAmount: 100000, from: 'BTC', to: 'BTC',
+ claimPublicKey: bytesToHex(claim.pk), preimageHash: bytesToHex(sha256(preimage)),
+ });
+ const boltzPub = hexToBytes(res.refundPublicKey); // Boltz refunds on reverse
+ const d = swapP2TR({ kind: 'reverse', preimageHash160: hash160(preimage), boltzPub33: boltzPub, halPub33: claim.pk, lockTime: res.timeoutBlockHeight, network: 'regtest' });
+ eq('reverse lockup address', d.address, res.lockupAddress);
+ eq('reverse claim leaf', bytesToHex(d.claimLeaf), res.swapTree.claimLeaf.output);
+ eq('reverse refund leaf', bytesToHex(d.refundLeaf), res.swapTree.refundLeaf.output);
+}
+
+// ---------- SUBMARINE (hal spends over LN; hal refunds) ----------
+{
+ const inv = JSON.parse(await $`docker exec clb lightning-cli --regtest invoice 100000000 hal-sub-${Date.now()} test`.text());
+ const payHash = inv.payment_hash;
+ const refund = kp(); // hal's refund key
+ const res = await post('/v2/swap/submarine', {
+ invoice: inv.bolt11, from: 'BTC', to: 'BTC', refundPublicKey: bytesToHex(refund.pk),
+ });
+ const boltzPub = hexToBytes(res.claimPublicKey); // Boltz claims on submarine
+ // tapscript embeds hash160(preimage) = ripemd160(payment_hash)
+ const ph160 = ripemd160(hexToBytes(payHash));
+ const d = swapP2TR({ kind: 'submarine', preimageHash160: ph160, boltzPub33: boltzPub, halPub33: refund.pk, lockTime: res.timeoutBlockHeight, network: 'regtest' });
+ eq('submarine lockup address', d.address, res.address);
+ eq('submarine claim leaf', bytesToHex(d.claimLeaf), res.swapTree.claimLeaf.output);
+ eq('submarine refund leaf', bytesToHex(d.refundLeaf), res.swapTree.refundLeaf.output);
+}
+
+console.log(fail ? `\n${fail} FAILED` : '\nall good');
+process.exit(fail ? 1 : 0);