1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import { db } from "$lib/db";
import { warn } from "$lib/logging";
import { fail } from "$lib/utils";
// The v3 registrar (coinos v3 / halwallet) owns any name it has a record for:
// routes/lnurl.ts serves a claimed name's lightning address from the registrar
// ahead of the local account. So a local account taking a name the registrar
// holds silently hands its incoming payments to whoever holds the v3 name.
// Both places that assign a username — registration and the profile rename —
// must therefore refuse a name the registrar holds, unless the account's own
// nostr key is the holder (a user re-creating their migrated name is fine).
const NAMES_URL = process.env.NAMES_URL || "https://names.coinos.io";
const DOMAIN = "coinos.io";
// Fail CLOSED: if the registrar can't be reached we don't know who owns the
// name, and handing out a squatted name is worse than a retry.
export const assertNameFree = async (username: string, ownPubkey?: string) => {
const name = username.toLowerCase();
let body: any;
try {
const r = await fetch(
`${NAMES_URL}/name/${encodeURIComponent(name)}?domain=${DOMAIN}`,
{ signal: AbortSignal.timeout(3000) },
);
if (!r.ok) throw new Error(`registrar said ${r.status}`);
body = await r.json();
} catch (e: any) {
warn("names registrar unreachable checking", name, e?.message);
fail("Username unavailable right now, please try again");
}
// `pubkey` is only present for a real v3 record. `taken` without one means
// the registrar is echoing our own account list back at us — not a claim.
const holder = body?.taken && typeof body.pubkey === "string" ? body.pubkey : null;
if (holder && holder !== ownPubkey) {
warn("username held by v3 registrar", name, holder.slice(0, 8));
fail(`Username ${name} taken`);
}
};
// Registrar holders we refuse to honour (valkey set `names:blocked_holders`,
// full hex pubkeys). A name whose v3 record belongs to one of them is treated
// as unclaimed: its lightning address, NIP-05 and legacy forwarding all stay
// with the local account. This is the containment for a name hijack on the
// registrar side; it needs no deploy there. Fails open: if the registrar or
// the db can't answer, callers behave as they did before.
const blockedCache = new Map<string, { at: number; blocked: boolean }>();
export const holderBlocked = async (username: string): Promise<boolean> => {
const name = String(username || "").toLowerCase();
if (!name) return false;
const hit = blockedCache.get(name);
if (hit && Date.now() - hit.at < 60_000) return hit.blocked;
try {
if (!(await db.sCard("names:blocked_holders"))) return false;
const r = await fetch(
`${NAMES_URL}/name/${encodeURIComponent(name)}?domain=${DOMAIN}`,
{ signal: AbortSignal.timeout(3000) },
);
if (!r.ok) return false;
const body: any = await r.json();
const pk = body?.taken && typeof body.pubkey === "string" ? body.pubkey : null;
const mgr = typeof body?.manager === "string" ? body.manager : null;
const blocked =
(!!pk && (await db.sIsMember("names:blocked_holders", pk))) ||
(!!mgr && (await db.sIsMember("names:blocked_holders", mgr)));
blockedCache.set(name, { at: Date.now(), blocked });
if (blocked) warn("registrar holder blocked, serving local account for", name);
return blocked;
} catch (e: any) {
warn("holderBlocked check failed for", name, e?.message);
return false;
}
};
|