aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ark-refund-mailbox.test.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-22 09:31:49 -0700
committerAdam Soltys <[email protected]>2026-11-22 09:31:49 -0700
commitf39513ec0a1bc178d8569116286f2b5b90ef3822 (patch)
treec12db644ee9bc64aef5ad77dd053928748594648 /tools/ark-refund-mailbox.test.js
parent9d73d5deb959712dd6007d639f278a8db9e6f2aa (diff)
downloadcoinosv3-f39513ec0a1bc178d8569116286f2b5b90ef3822.tar.gz
coinosv3-f39513ec0a1bc178d8569116286f2b5b90ef3822.zip
A payment's refund the wallet lost can come back through its mailbox. Handoff from the coinos-server agent on desk, 2026-11-22 ("V3 wallet lost a Lightning refund", user json534543): on 2026-11-20 a 14,770-sat Lightning payment failed six times in two minutes; after the last failure the server cosigned the revocation at 12:41:05 UTC and holds the refund as two spendable coins (14,786 and 2 sats) paid to the payment's refund key, but the page was reloaded in that second and the wallet never wrote them down, so it shows a balance of 0. The wallet builds the refund coin itself and nothing gave it back afterwards: the mailbox was the only way in, and it took coins paid to key 0 only. A coin from the mailbox is now accepted when it is paid to any key this wallet has handed out, and is kept under that key. When that key is a payment's refund key the payment is closed as failed with "payment failed, funds returned" and its HTLC coins marked spent, instead of a "Received" row. A coin to a key that is not ours is refused as before. A build from before this change refuses such a coin and moves the mailbox cursor past it, so once a session, while such a refusal is on record, the mailbox is read again from the start and what the server still holds for one of our keys is adopted. New ark-refund-mailbox test (3 cases); ark-htlc-orphan, ark-htlc-held, ark-reconcile-race and features-smoke pass. Both stored coins pass the wallet's own validation against the chain.
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6Upic5L6MMJzkTn8zg
Diffstat (limited to 'tools/ark-refund-mailbox.test.js')
-rw-r--r--tools/ark-refund-mailbox.test.js47
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');
+});