aboutsummaryrefslogtreecommitdiffstats
path: root/src/ark/migrate.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/ark/migrate.js')
-rw-r--r--src/ark/migrate.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/ark/migrate.js b/src/ark/migrate.js
new file mode 100644
index 0000000..a05e60d
--- /dev/null
+++ b/src/ark/migrate.js
@@ -0,0 +1,79 @@
+// Moving a wallet's Spending coins off a retiring Ark server.
+//
+// coinos retired its first Ark server (ark.coinos.io) after its server key was
+// stolen on 2026-12-04; ark2.coinos.io took over. A wallet still holding coins
+// on the old server hands them to coinos's migrator, which pays the same amount
+// to the wallet's address on the new server (arkoor has no fee, so nothing is
+// lost). The wallet does this on its own: the user does nothing.
+//
+// The old server can't run consolidation rounds any more, so a wallet with
+// more coins than one payment can carry sends them in batches of the largest
+// coins first, each batch one payment to the same deposit address.
+
+export const MAX_BATCH_INPUTS = 48; // manager.js MAX_SEND_INPUTS
+
+// The next batch: the coins the manager's own selection would take first,
+// at most maxInputs of them. Sending exactly their total makes send() pick
+// those same coins (it accumulates in the same order: coins still inside
+// their timelock first, largest first), so no batch goes over the input cap.
+// A coin at the server's exit-depth cap is never an input; those stay behind
+// and only a unilateral exit can move them.
+function nextBatch(mgr, maxInputs) {
+ const tip = mgr._tipH || 0;
+ const busy = mgr._remoteBusy();
+ const spendable = mgr.state.vtxos.filter((v) => mgr._free(v, busy) && !mgr._expired(v, tip) && !mgr._capped(v));
+ if (!spendable.length) return null;
+ const n = Math.min(maxInputs, spendable.length);
+ return mgr._accumulate(spendable, tip, (set) => set.length >= n);
+}
+
+// Ask the migrator for a deposit address that pays `dest` on the new server.
+export async function startMigration(migratorUrl, dest, fetchFn = fetch) {
+ const r = await fetchFn(`${migratorUrl}/start`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ dest }),
+ });
+ const j = await r.json();
+ if (!r.ok || !j.depositAddress) throw new Error(j.error || `migrator: HTTP ${r.status}`);
+ return j;
+}
+
+export async function migrationStatus(migratorUrl, id, fetchFn = fetch) {
+ const r = await fetchFn(`${migratorUrl}/status/${id}`);
+ return r.json();
+}
+
+// Send everything movable from oldMgr to the migrator for `dest`.
+// Returns { id, sentSat, leftSat } — leftSat is what couldn't be sent
+// (coins at the exit-depth cap, or a batch the server refused).
+export async function migrateWallet(oldMgr, { migratorUrl, dest, fetchFn = fetch, log = () => {}, maxInputs = MAX_BATCH_INPUTS }) {
+ if ((oldMgr.pendingActions?.() || []).length) throw new Error('old-server wallet has payments in progress');
+ const session = await startMigration(migratorUrl, dest, fetchFn);
+ let sentSat = 0;
+ for (let round = 0; round < 50; round++) {
+ const batch = nextBatch(oldMgr, maxInputs);
+ if (!batch?.length) break;
+ const amount = batch.reduce((t, v) => t + v.amountSat, 0);
+ try {
+ await oldMgr.send(session.depositAddress, amount);
+ sentSat += amount;
+ log(`migrate: sent ${amount} sat in ${batch.length} coin(s)`);
+ } catch (e) {
+ log('migrate: batch failed: ' + e.message);
+ break;
+ }
+ }
+ const leftSat = oldMgr.balance().spendableSat;
+ return { id: session.id, sentSat, leftSat };
+}
+
+// Wait (up to ~timeoutMs) for the migrator to report the payout done.
+export async function awaitMigration(migratorUrl, id, { timeoutMs = 60000, fetchFn = fetch } = {}) {
+ const until = Date.now() + timeoutMs;
+ let s;
+ while (Date.now() < until) {
+ s = await migrationStatus(migratorUrl, id, fetchFn).catch(() => null);
+ if (s && (s.status === 'done' || s.status.startsWith('held') || s.status === 'waiting-liquidity')) return s;
+ await new Promise((r) => setTimeout(r, 2000));
+ }
+ return s;
+}