aboutsummaryrefslogtreecommitdiffstats
path: root/src/ark/migrate.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-12-04 13:18:47 -0700
committerAdam Soltys <[email protected]>2026-12-06 00:01:58 -0700
commitbefa01bc9b2c144c020b403429db20bc5278db93 (patch)
treef23b8c68b215f77066b3a7272f05b5fc13a8cbf5 /src/ark/migrate.js
parente694a17af529614ba2cb477b01d0d5d12eb20072 (diff)
downloadcoinosv3-befa01bc9b2c144c020b403429db20bc5278db93.tar.gz
coinosv3-befa01bc9b2c144c020b403429db20bc5278db93.zip
Spending moves to ark2.coinos.io by itself: coins on the retired server go across through coinos's migrator.
ark.coinos.io's server key was stolen in the 2026-12-04 breach, so coinos stood up ark2.coinos.io with a fresh key. The "coinos" preset now points there (same id, so everyone on coinos follows), and the old server stays as a hidden, retiring preset whose successor is "coinos" and whose migrator is migrate.coinos.io. After connecting, a wallet that still holds spendable coins on the old server opens that wallet (same seed, same keys), asks the migrator for a deposit address naming its own ark2 address, and sends everything there in batches of the largest coins (at most 48 per payment, as the old server can't run consolidation rounds). The migrator pays the same amount on ark2; a toast says it arrived. Coins at the exit-depth cap stay behind. arkWanted() also counts coins waiting on a retiring server, so a wallet whose coins are all there still connects. src/ark/migrate.js holds the logic apart from the UI. ArkManager gains freshAddress()/_addressFor(i): an address on a key of its own, mailbox id blinded with that key (what the migrator hands out per migration). Tested end to end on regtest against two captaind 0.8.0-coinos servers and the real migrator (coinosv3-server migrator/e2e-test.js: 19 checks, incl. batching, caps and restart-safety). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar
Diffstat (limited to 'src/ark/migrate.js')
-rw-r--r--src/ark/migrate.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/ark/migrate.js b/src/ark/migrate.js
new file mode 100644
index 0000000..694b692
--- /dev/null
+++ b/src/ark/migrate.js
@@ -0,0 +1,74 @@
+// 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
+
+// Spendable coins the old server will still cosign, largest first.
+// (A coin at the server's exit-depth cap can't be an input; the server refuses
+// to cosign it. Those stay behind and only a unilateral exit can move them.)
+function movableCoins(oldMgr) {
+ return oldMgr.vtxos()
+ .filter((v) => v.state === 'spendable' && !v.expiryRejected && !oldMgr._capped(v))
+ .sort((a, b) => b.amountSat - a.amountSat);
+}
+
+// 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 coins = movableCoins(oldMgr);
+ if (!coins.length) break;
+ const batch = coins.slice(0, maxInputs);
+ 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;
+}