diff options
| -rw-r--r-- | src/api.js | 8 | ||||
| -rw-r--r-- | src/ark/manager.js | 21 | ||||
| -rw-r--r-- | src/ark/migrate.js | 74 | ||||
| -rw-r--r-- | src/features/ark.js | 50 | ||||
| -rw-r--r-- | src/i18n.js | 2 |
5 files changed, 147 insertions, 8 deletions
@@ -232,7 +232,13 @@ export function getBoltzWs() { const p = getBoltzProvider(); return p.ws || deri const ARK_PRESETS_BY_NET = { mainnet: [ { id: 'off', label: 'Off', ark: '', esplora: '' }, - { id: 'coinos', label: 'coinos (ark.coinos.io)', ark: 'https://ark.coinos.io', esplora: 'https://mempool.space/api', honorsExpired: true }, + // ark2.coinos.io took over on 2026-12-04: the first server's key was + // stolen. Same preset id, so everyone on "coinos" follows; ark state is + // kept per server URL, so the old coins stay under the old URL until the + // wallet moves them (src/ark/migrate.js through the migrator). + { id: 'coinos', label: 'coinos (ark2.coinos.io)', ark: 'https://ark2.coinos.io', esplora: 'https://mempool.space/api', honorsExpired: true }, + { id: 'coinos-retired', label: 'coinos, retired (ark.coinos.io)', ark: 'https://ark.coinos.io', esplora: 'https://mempool.space/api', honorsExpired: true, + hidden: true, retiring: true, successor: 'coinos', migrator: 'https://migrate.coinos.io/migrate' }, { id: 'second', label: 'Second (ark.second.tech)', ark: 'https://ark.second.tech', esplora: 'https://mempool.second.tech/api' }, { id: 'custom', label: 'Custom…', ark: '', esplora: '' }, ], diff --git a/src/ark/manager.js b/src/ark/manager.js index 8be9d58..fc298d9 100644 --- a/src/ark/manager.js +++ b/src/ark/manager.js @@ -506,15 +506,30 @@ export class ArkManager { // ---- public surface ---- address() { - const k0 = this._key(0); - const blinded = blindMailboxId(this._mailboxKey().pubkey, hex.decode(this.info.mailboxPubkey), k0.privkey); + return this._addressFor(0); + } + // The address of one of this wallet's keys. The mailbox id is blinded with + // that key, as the server unblinds it against the key the address names. + _addressFor(keyIndex) { + const k = this._key(keyIndex); + const blinded = blindMailboxId(this._mailboxKey().pubkey, hex.decode(this.info.mailboxPubkey), k.privkey); return encodeAddress({ testnet: this.info.network !== 'bitcoin', serverPubkey: this.serverPub, - userPubkey: k0.pubkey, + userPubkey: k.pubkey, blindedMailboxId: blinded, }); } + // A receive address on a key of its own, for a service that has to tell + // its payers apart (the server-to-server migrator hands one out per + // migration). Coins paid to it arrive through the same mailbox and are + // recorded with that keyIndex. + freshAddress() { + const keyIndex = this.state.nextKeyIndex || 1; + this.state.nextKeyIndex = keyIndex + 1; + this._save(); + return { keyIndex, address: this._addressFor(keyIndex) }; + } // A coin past its expiry is money only the server still honors: the // recipient's validator refuses anything expired at pickup time, so sending 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; +} diff --git a/src/features/ark.js b/src/features/ark.js index 2c1a581..b95f8c0 100644 --- a/src/features/ark.js +++ b/src/features/ark.js @@ -9,6 +9,7 @@ import { HDKey } from '@scure/bip32'; import { hex, base32nopad, bech32 } from '@scure/base'; import { sha256 } from '@noble/hashes/sha256'; import { ArkManager, remoteRenewalsInFlight, lnFoldedIds, sameDelivery, renewPolicy, CUSTODY_TIERS } from '../ark/manager.js'; +import { migrateWallet, awaitMigration } from '../ark/migrate.js'; export { remoteRenewalsInFlight }; // Coins this device may offer: spendable, and not held by a renewal another @@ -575,7 +576,15 @@ export function arkFeature(ctx) { const cfg = getArkConfig(); const st = cfg && peekArk(wallet, cfg.ark); return !!(st && ((st.vtxos || []).length || (st.movements || []).length - || (st.actions || []).some((a) => !['done', 'failed'].includes(a.step)))); + || (st.actions || []).some((a) => !['done', 'failed'].includes(a.step)))) + || retiringWithCoins().length > 0; // coins on a retired server: connect so they can move + } + // Retired servers this wallet still holds spendable coins on, whose + // successor is the server it's on now (ark.coinos.io → ark2.coinos.io). + function retiringWithCoins() { + const cur = getArkProviderId(); + return arkPresets(getNetwork()).filter((p) => p.retiring && p.successor === cur && p.migrator && p.ark + && (peekArk(wallet, p.ark)?.vtxos || []).some((v) => v.state === 'spendable')); } function initArk() { @@ -593,7 +602,7 @@ export function arkFeature(ctx) { return; } if (safeBoot()) return; // safe mode: the coins show from the store; nothing dials out unasked - if (arkAvailable() && arkWanted()) { lastAutoInit = Date.now(); connectArk().then(() => rescueOtherServers()).catch(() => {}); } + if (arkAvailable() && arkWanted()) { lastAutoInit = Date.now(); connectArk().then(() => migrateRetiring()).then(() => rescueOtherServers()).catch(() => {}); } } // Automatic re-inits (a synced snapshot arriving) must not restart a @@ -608,6 +617,39 @@ export function arkFeature(ctx) { // every other server this wallet holds state with gets a short visit — its // own manager, a receive rescan, a sync to claim — and a toast says what // arrived and where. The coins stay with that server until a switch back. + // coinos retired its first Ark server (its key was stolen 2026-12-04). A + // wallet still holding coins there moves them to the successor on its + // own: the migrator gives a deposit address on the old server and pays the + // same amount to this wallet's address on the new one. Once per session. + let migratedRetiring = false; + async function migrateRetiring() { + if (migratedRetiring || wallet.watchOnly || !ark) return; + const list = retiringWithCoins(); + if (!list.length) return; + migratedRetiring = true; + for (const p of list) { + try { + const mgr = new ArkManager({ + account: wallet.account(), + storage: { + load: () => wallet.loadArkState(p.ark), + adopt: (serverPubkey) => wallet.adoptArkState(serverPubkey, p.ark), + save: (s) => { wallet.saveArkState(mergeArkStates(s, wallet.loadArkState(p.ark)), p.ark); }, + }, + arkUrl: p.ark, esploraUrl: p.esplora, network: getNetwork(), honorsExpired: !!p.honorsExpired, + }); + await mgr.init(); + await mgr.sync(); + if (!mgr.balance().spendableSat) continue; + const res = await migrateWallet(mgr, { migratorUrl: p.migrator, dest: ark.address(), log: (m) => console.log('ark: ' + m) }); + if (!res.sentSat) continue; + const fin = await awaitMigration(p.migrator, res.id, { timeoutMs: 90000 }); + await ark.sync().catch(() => {}); + toast(t(fin?.status === 'done' ? 'arkMigrated' : 'arkMigratePending', { amount: fmtSats(res.sentSat) }), 9000); + render(); + } catch (e) { console.warn('ark: moving coins off ' + p.ark + ' failed', e); } + } + } let rescuedServers = false; async function rescueOtherServers() { if (rescuedServers || wallet.watchOnly) return; @@ -2846,7 +2888,7 @@ export function arkFeature(ctx) { return parts.join(', '); }; function arkServerCompare(presets) { - const cols = presets.filter((p) => p.ark && p.id !== 'custom'); + const cols = presets.filter((p) => p.ark && p.id !== 'custom' && !p.hidden); if (cols.length < 2) return null; const infos = cols.map((p) => serverInfo(p.ark)); const cell = (i, f) => (i === undefined ? '…' : i === null ? '—' : f(i)); @@ -2904,7 +2946,7 @@ export function arkFeature(ctx) { h('h3', {}, t('arkServerTitle')), h('p', { class: 'small muted', style: 'margin:0' }, t('arkServerDesc')), h('select', { onChange: (e) => pick(e.target.value) }, - ...presets.map((p) => h('option', { value: p.id, selected: p.id === (c || cur) }, p.label))), + ...presets.filter((p) => !p.hidden || p.id === (c || cur)).map((p) => h('option', { value: p.id, selected: p.id === (c || cur) }, p.label))), h('button', { class: 'linklike small', style: 'align-self:flex-start', onClick: () => { ui.arkCmpOpen = !ui.arkCmpOpen; render(); } }, ui.arkCmpOpen ? t('arkCmpHide') : t('arkCmpShow')), ui.arkCmpOpen ? arkServerCompare(presets) : null, (c === 'custom' || cur === 'custom') ? h('div', { class: 'col', style: 'gap:6px' }, diff --git a/src/i18n.js b/src/i18n.js index 2b513c6..5d5384b 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -1417,6 +1417,8 @@ const STR = { arkCmpNote: 'Fees graded by expiry cost more the earlier a coin is renewed or exited. Figures come from each server’s own published info, read just now. Either server pays Lightning invoices and takes Lightning payments to your coinos name.', arkServerSwitched: 'Ark server switched', arkOtherServerReceipt: '{amount} sats arrived with {server}. Switch to that server in Settings → Advanced to spend them.', + arkMigrated: '{amount} sats moved to coinos’s new Ark server. Nothing for you to do.', + arkMigratePending: '{amount} sats are on their way to coinos’s new Ark server.', arkForeignBip21: 'Their Ark address is on a different Ark server, so the on-chain address is paid instead.', arkForeignServer: 'That Ark address belongs to a different Ark server, so it can’t be paid from here directly. Ask for a Lightning invoice, or pay their coinos name or npub. Those fall back to Lightning by themselves.', arkLnFromSavingsTitle: 'Lightning pays from Spending', |
