diff options
Diffstat (limited to 'tools/ark-refund-mailbox.test.js')
| -rw-r--r-- | tools/ark-refund-mailbox.test.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/ark-refund-mailbox.test.js b/tools/ark-refund-mailbox.test.js new file mode 100644 index 0000000..03a76da --- /dev/null +++ b/tools/ark-refund-mailbox.test.js @@ -0,0 +1,47 @@ +// bun test tools/ark-refund-mailbox.test.js — a failed Lightning payment's +// refund that the wallet lost (the server cosigned the revocation, the page +// was reloaded before the coin was written down: json534543, 2026-11-20, +// 14,786 sats) comes back through the mailbox to the refund key. The wallet +// takes a coin paid to any key it has handed out, closes the payment it +// refunds as failed, and still refuses a coin to a key that is not its own. +import { test, expect, mock } from 'bun:test'; +import { hex } from '@scure/base'; +import * as validate from '../src/ark/validate.js'; +const seen = []; +mock.module('../src/ark/validate.js', () => ({ ...validate, validateVtxo: async (v, o) => { seen.push(o.expectPubkeys); if (!o.expectPubkeys.includes(v.policy.userPubkey)) throw new validate.VtxoValidationError('vtxo not paid to one of our keys'); } })); +const { ArkManager } = await import('../src/ark/manager.js'); +const pk = (i) => hex.encode(Uint8Array.from({ length: 33 }, () => i + 2)); +const mgr = () => { + const m = Object.create(ArkManager.prototype); + m.state = { vtxos: [{ id: 'htlc', state: 'pending', amountSat: 14786, policy: 'serverHtlcSend' }], movements: [], nextKeyIndex: 9, mailboxCheckpoint: 0, + actions: [{ type: 'ln-pay', step: 'revoking', amountSat: 14770, invoice: 'lnbc1', paymentHash: 'aa', htlcVtxoIds: ['htlc'], htlcKeyIndex: 6, revKeyIndex: 7 }] }; + m._key = (i) => ({ pubkey: hex.decode(pk(i)), privkey: new Uint8Array(32) }); + m._tipMemo = async () => 900000; m._save = () => {}; m._estimateReceiveTs = () => 1; + m._movement = (x) => m.state.movements.push(x); + return m; +}; +const coin = (id, amountSat, key) => ({ id, amountSat, expiryHeight: 985313, policy: { type: 'pubkey', userPubkey: pk(key) }, _raw: { bytes: Uint8Array.of(1, 2, 3) } }); +test('a refund delivered to the refund key is taken and closes the payment', async () => { + const m = mgr(); + const changed = await m._handleMailboxMessage({ kind: 'arkoor', checkpoint: 0, vtxos: [coin('refund', 14786, 7), coin('dust', 2, 7)] }); + expect(changed).toBe(true); + const held = Object.fromEntries(m.state.vtxos.map((v) => [v.id, v])); + expect(held.refund.state).toBe('spendable'); expect(held.refund.keyIndex).toBe(7); + expect(held.dust.keyIndex).toBe(7); + expect(held.htlc.state).toBe('spent'); + expect(m.state.actions[0].step).toBe('failed'); + expect(m.state.movements).toEqual([{ type: 'ln-send', amountSat: 14770, status: 'failed', detail: 'payment failed, funds returned', invoice: 'lnbc1' }]); +}); +test('an ordinary receive to key 0 is a receive, as before', async () => { + const m = mgr(); + await m._handleMailboxMessage({ kind: 'arkoor', checkpoint: 0, vtxos: [coin('gift', 500, 0)] }); + expect(m.state.vtxos.find((v) => v.id === 'gift').keyIndex).toBe(0); + expect(m.state.movements[0].type).toBe('receive'); expect(m.state.movements[0].status).toBe('complete'); + expect(m.state.actions[0].step).toBe('revoking'); +}); +test('a coin to a key that is not ours is refused', async () => { + const m = mgr(); + await m._handleMailboxMessage({ kind: 'arkoor', checkpoint: 0, vtxos: [coin('theirs', 500, 200)] }); + expect(m.state.vtxos.some((v) => v.id === 'theirs')).toBe(false); + expect(m.state.movements[0].status).toBe('rejected'); +}); |
