aboutsummaryrefslogtreecommitdiffstats
path: root/src/ark/send.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/ark/send.js')
-rw-r--r--src/ark/send.js52
1 files changed, 45 insertions, 7 deletions
diff --git a/src/ark/send.js b/src/ark/send.js
index f0e471a..9cf79b4 100644
--- a/src/ark/send.js
+++ b/src/ark/send.js
@@ -272,11 +272,49 @@ function encodeVtxo({ amountSat, expiryHeight, serverPubkey, exitDelta, anchorPo
const DUST_SAT = 330; // P2TR dust threshold
-// Mirror of bark's ArkoorBuilder::new_isolate_dust: the server rejects an
-// arkoor whose outputs mix dust and non-dust, so dust outputs are routed
-// through a combined isolation output (spent by a fanout tx). When the dust
-// alone can't reach the threshold, a non-dust output is split to pad it.
-function isolateDust(outputs) {
+// Mirror of bark's ArkoorBuilder::new_isolate_dust (server-0.9.0). Dust
+// outputs are routed through one combined isolation output on the checkpoint
+// (spent by a fanout tx), so every checkpoint output is standard. Since
+// server-0.9.0 (!2822) the server refuses a cosign whose checkpoint carries
+// a sub-dust output unless the input itself is dust, so this must match
+// upstream case for case:
+// - no dust, or a dust input: nothing isolated
+// - dust summing to ≥ 330: all of it isolated
+// - otherwise pad the dust from the first non-dust output that stays ≥ 330
+// after giving up the deficit; failing that, isolate the smallest non-dust
+// output whole (each is then under 330 + deficit ≤ 660, which isolation
+// allows). Both used to "accept the mix", which 0.9.0 refuses.
+// - every output dust (from an input ≥ 330): all of it isolated, the
+// checkpoint holding only the isolation output.
+//
+// `legacy`: a server before 0.9.0 (it advertises no ArkInfo field 23) refuses
+// the two new shapes instead — "At least one output is required" for an
+// all-isolated checkpoint, checked on regtest 0.8.0 — and takes the old mix,
+// so it gets the old split (isolateDustLegacy) until it upgrades.
+export function isolateDust(outputs, inputSat, { legacy = false } = {}) {
+ if (legacy) return isolateDustLegacy(outputs);
+ const sum = (l) => l.reduce((n, o) => n + o.amountSat, 0);
+ if (outputs.every((o) => o.amountSat >= DUST_SAT)) return { outs: outputs, isolated: [] };
+ if (inputSat < DUST_SAT) return { outs: outputs, isolated: [] };
+ const dust = outputs.filter((o) => o.amountSat < DUST_SAT);
+ const nonDust = outputs.filter((o) => o.amountSat >= DUST_SAT);
+ if (sum(dust) >= DUST_SAT) return { outs: nonDust, isolated: dust };
+ const deficit = DUST_SAT - sum(dust);
+ const idx = nonDust.findIndex((o) => o.amountSat - deficit >= DUST_SAT);
+ if (idx >= 0) {
+ const outs = nonDust.slice();
+ outs[idx] = { amountSat: outs[idx].amountSat - deficit, policy: outs[idx].policy };
+ return { outs, isolated: [{ amountSat: deficit, policy: nonDust[idx].policy }, ...dust] };
+ }
+ let small = -1;
+ nonDust.forEach((o, i) => { if (small < 0 || o.amountSat < nonDust[small].amountSat) small = i; });
+ if (small < 0) return { outs: [], isolated: dust }; // unreachable: dust alone sums to the input ≥ 330
+ const outs = nonDust.filter((_, i) => i !== small);
+ return { outs, isolated: [nonDust[small], ...dust] };
+}
+
+// bark ≤ 0.8's new_isolate_dust, for servers before 0.9.0
+function isolateDustLegacy(outputs) {
const sum = (l) => l.reduce((n, o) => n + o.amountSat, 0);
const dust = outputs.filter((o) => o.amountSat < DUST_SAT);
const nonDust = outputs.filter((o) => o.amountSat >= DUST_SAT);
@@ -298,7 +336,7 @@ function isolateDust(outputs) {
// — destination first, change last. NB an output may come back SPLIT across
// two result vtxos (dust padding), so callers should consume
// buildAllSignedVtxos() and classify by policy rather than by index.
-export function buildArkoorSend({ input, outputs, serverPubkey, vtxoKeys }) {
+export function buildArkoorSend({ input, outputs, serverPubkey, vtxoKeys, legacyDust = false }) {
const userPub = vtxoKeys.pubkey;
if (hex.encode(userPub) !== policyOwnerPubkey(input.policy)) throw new Error('input not owned by our key');
@@ -307,7 +345,7 @@ export function buildArkoorSend({ input, outputs, serverPubkey, vtxoKeys }) {
amountSat: o.amountSat,
policy: o.policy || { type: 'pubkey', userPubkey: hex.encode(o.userPubkey) },
}));
- const { outs, isolated } = isolateDust(normalized);
+ const { outs, isolated } = isolateDust(normalized, input.amountSat, { legacy: legacyDust });
const isolationSat = isolated.reduce((n, o) => n + o.amountSat, 0);
const inputTaproot = policyTaproot(input.policy, serverPubkey, input.exitDelta);