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
|
// /migrate-grant's second check. The bearer token proves the caller is
// coinos.io, not that the name's own account asked. So the old account must
// have just handed itself over: coinos.io stamps `v3migrated` (a timestamp)
// only when the account's own session posts the hand-off, and the grant
// follows within seconds. A bug upstream then can't give someone else's name
// to a stranger, and a name handed over long ago can't be quietly re-granted.
//
// Not `migrated`: coinos.io has set that on every new account since 2023, so
// nearly every name passes it.
// Returns null when the grant may go ahead, else [status, error].
export const FRESH_MS = 5 * 60_000;
export async function migrateGrantRefusal(name, fetchFn = fetch, now = Date.now()) {
let u;
try {
const r = await fetchFn(`https://coinos.io/api/users/${encodeURIComponent(name)}`, { signal: AbortSignal.timeout(8000) });
if (r.status === 404) return [404, 'no such coinos.io account'];
if (r.status !== 200) throw new Error(`coinos.io said ${r.status}`);
u = await r.json();
} catch {
return [502, "couldn't check the coinos.io account, try again"];
}
if (!u || String(u.username || '').toLowerCase() !== String(name).toLowerCase()) return [404, 'no such coinos.io account'];
const at = Number(u.v3migrated);
if (!Number.isFinite(at) || at > now + 60_000 || now - at > FRESH_MS)
return [409, 'that coinos.io account has not just migrated'];
return null;
}
|