diff options
| author | Adam Soltys <[email protected]> | 2026-06-27 08:41:08 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-06-27 08:41:08 -0700 |
| commit | 10f9ebc0d047f1599f4665fda0b073c449bb8f64 (patch) | |
| tree | b5e68e8044cabd9d878b8ed8e3715d46e5cc9d0c /tools/swap-manager.test.js | |
| parent | a9a903e7a1f80ed4a6f4163419b25d4e6de5db01 (diff) | |
| download | coinosv3-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-manager.test.js')
| -rw-r--r-- | tools/swap-manager.test.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/swap-manager.test.js b/tools/swap-manager.test.js new file mode 100644 index 0000000..7a83be1 --- /dev/null +++ b/tools/swap-manager.test.js @@ -0,0 +1,43 @@ +// Drives the SwapManager reverse flow end-to-end with a minimal wallet shim: +// startReverse -> pay the hold invoice from cl -> manager auto-claims on-chain. +// Run (stack up): bun tools/swap-manager.test.js +import { HDKey } from '@scure/bip32'; +import * as btc from '@scure/btc-signer'; +import { secp256k1 } from '@noble/curves/secp256k1'; +import { randomBytes } from '@noble/hashes/utils'; +import { SwapManager, REGTEST } from '../src/swap.js'; + +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); +const ESPLORA = 'http://localhost:3000'; +// Fresh random seed per run (real wallet persists swaps so swapNode indices +// increment; this shim's store is in-memory, so randomize to avoid preimage reuse). +const acct = HDKey.fromMasterSeed(randomBytes(64)).derive("m/84'/1'/0'"); + +let store = []; +const wallet = { + loadSwaps: () => store, + saveSwaps: (l) => { store = l; }, + nextSwapIndex: () => store.reduce((m, s) => Math.max(m, (s.swapIndex ?? -1) + 1), 0), + swapNode: (i) => acct.deriveChild(2).deriveChild(i), + freshReceive: () => ({ address: btc.p2wpkh(secp256k1.getPublicKey(acct.deriveChild(0).deriveChild(7).privateKey, true), REGTEST).address }), + api: { + addressUtxos: async (a) => { const r = await fetch(`${ESPLORA}/address/${a}/utxo`); return r.ok ? r.json() : []; }, + broadcast: async (hex) => { const r = await fetch(`${ESPLORA}/tx`, { method: 'POST', body: hex }); const t = await r.text(); if (!r.ok) throw new Error(t); return t; }, + }, +}; + +const sm = new SwapManager({ wallet, network: 'regtest', getApi: () => 'http://localhost:9001', feeRate: 2, onUpdate: () => {} }); +const rec = await sm.startReverse(120000); +console.log('startReverse ->', rec.id, rec.status, 'lockup', rec.lockupAddress); + +console.log('paying hold invoice from cl…'); +Bun.spawn(['docker', 'exec', 'cl', 'lightning-cli', '--regtest', 'pay', rec.invoice], { stdout: 'ignore', stderr: 'ignore' }); + +for (let i = 0; i < 60; i++) { + await sleep(1000); + const r = sm.list().find((s) => s.id === rec.id); + if (r.status === 'claimed') { console.log(`\nPASS — SwapManager auto-claimed: ${r.received} sats, txid ${r.claimTxid}`); process.exit(0); } + if (r.lastError && i % 5 === 0) console.log(' …', r.lastError); +} +console.log('FAIL — not claimed:', JSON.stringify(sm.list())); +process.exit(1); |
