aboutsummaryrefslogtreecommitdiffstats
path: root/names/forget.js
diff options
context:
space:
mode:
Diffstat (limited to 'names/forget.js')
-rw-r--r--names/forget.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/names/forget.js b/names/forget.js
new file mode 100644
index 0000000..b02fe8c
--- /dev/null
+++ b/names/forget.js
@@ -0,0 +1,83 @@
+// "Delete my account" (a v3 user via Adam, 2026-11-10: "we allowed that in
+// v2 … free up the name once the deletion is done and remove any records we
+// have on them on the server").
+//
+// An account here is a key and what hangs off it: the names it owns or
+// manages (a login identity's name is kept up to date by its wallet's key,
+// the record's `manager`), and everything filed under those names or keys.
+// Money is never deleted: while a payment is still on its way to them, or a
+// withdraw link of theirs still holds sats, the account stays (blockers).
+// What is kept afterwards is only what stops a payment being made twice
+// (delivered, withdrawPaid, inflight) and the unpaid-invoice map, which ages
+// out in a week and has to outlive a payer who is slow.
+
+const HEX = /^[0-9a-f]{64}$/;
+
+// The keys and names one signer answers for.
+export function accountOf(state, signer) {
+ const keys = new Set([signer]);
+ const names = [];
+ for (const [key, rec] of Object.entries(state.names || {})) {
+ if (rec.pubkey !== signer && rec.manager !== signer) continue;
+ names.push(key);
+ for (const k of [rec.pubkey, rec.manager]) if (HEX.test(k || '')) keys.add(k);
+ }
+ return { keys, names };
+}
+
+// Sats still owed to this account, or held for it. Non-zero means "not yet".
+export function accountBlockers(state, { keys, names }) {
+ const mine = (p) => keys.has(p.pubkey) || names.includes(p.name);
+ let pendingSat = 0, withdrawSat = 0, paying = 0;
+ for (const p of [...(state.pending || []), ...(state.parked || [])]) if (mine(p)) pendingSat += Number(p.sat) || 0;
+ for (const v of Object.values(state.inflight || {})) if (names.includes(v.name)) pendingSat += Number(v.sat) || 0;
+ for (const w of Object.values(state.withdraws || {})) {
+ if (!keys.has(w.pubkey)) continue;
+ if ((w.claims || []).some((c) => c.status === 'paying')) paying++;
+ if (w.status === 'open') withdrawSat += Math.max(0, (w.totalSat || 0) - (w.spentSat || 0));
+ }
+ return { pendingSat, withdrawSat, paying, any: pendingSat > 0 || withdrawSat > 0 || paying > 0 };
+}
+
+// Take the account out of `state`. Returns what the caller still has to undo
+// outside it: DNS records to delete and node offers to disable.
+export function forgetAccount(state, { keys, names }) {
+ const dns = [], offers = [], removed = {};
+ const count = (what, n = 1) => { if (n) removed[what] = (removed[what] || 0) + n; };
+ const named = new Set(names);
+ for (const key of names) {
+ const rec = state.names[key];
+ if (!rec) continue;
+ if (rec.recordId) dns.push({ domain: rec.domain || key.split('@')[1], recordId: rec.recordId });
+ if (rec.offerId) offers.push(rec.offerId);
+ delete state.names[key];
+ count('names');
+ }
+ for (const [id, o] of Object.entries(state.offers || {})) {
+ if (!named.has(o.key) && !keys.has(o.pubkey)) continue;
+ offers.push(id); delete state.offers[id]; count('offers');
+ }
+ const byKey = (coll, field = 'key') => {
+ for (const [id, v] of Object.entries(state[coll] || {})) if (named.has(v?.[field])) { delete state[coll][id]; count(coll); }
+ };
+ byKey('posTokens'); byKey('posInvoices');
+ for (const coll of ['hats', 'hatsMut', 'welcomed'])
+ for (const k of keys) if (state[coll] && k in state[coll]) { delete state[coll][k]; count(coll); }
+ for (const [id, v] of Object.entries(state.hatInvoices || {})) if (keys.has(v?.pubkey)) { delete state.hatInvoices[id]; count('hatInvoices'); }
+ for (const key of names) if (state.lnReceipts && key in state.lnReceipts) { delete state.lnReceipts[key]; count('lnReceipts'); }
+ // a grant left behind would keep the name reserved for the key that just left
+ for (const [key, g] of Object.entries(state.migrations || {}))
+ if (named.has(key) || keys.has(g?.pubkey) || keys.has(g?.manager)) { delete state.migrations[key]; count('migrations'); }
+ if (Array.isArray(state.webhookQueue)) {
+ const before = state.webhookQueue.length;
+ state.webhookQueue = state.webhookQueue.filter((q) => !named.has(q.key));
+ count('webhookQueue', before - state.webhookQueue.length);
+ }
+ // withdraw links: only closed and never-funded ones are left (blockers)
+ for (const [id, w] of Object.entries(state.withdraws || {})) {
+ if (!keys.has(w.pubkey)) continue;
+ if (w.status === 'unfunded' && w.fundHash && state.withdrawFunding) delete state.withdrawFunding[w.fundHash];
+ delete state.withdraws[id]; count('withdraws');
+ }
+ return { dns, offers: [...new Set(offers)], removed };
+}