Files
martbost f26ac15712 5xYourTeam The Draft v1 (phase 1, local only): weekly earned-order draft of holding-tank people, picks as adoptions (stub tank), coaching tools, cards, boards, trust pack
$10 one-time DraftPosition purchases (sponsor 25%, L2-5 5%, 10% Best Coach pot, 45% company) with keeper-signed hub-line
vouchers; pure draft engine with a mocked clock; no contact data anywhere; Telegram to an outbox unless TG_LIVE=1.
Unit 60/60, smoke 156/156 (Playwright 1280/390). Not deployed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 06:16:10 -06:00

63 lines
4.7 KiB
JavaScript

// Network hub lookups (Marty 2026-11-14: "want everything looped through the hub to verify membership"). The hub is the one
// record per person across the network; this asks it who a username, wallet or hub id is:
// POST /v1/resolve {ref: <username or wallet>} | {wallet} | {id: 'h_...'} -> {person: view or null}
// signed as HUB_SITE with HUB_SECRET (X-Hub-Site, X-Hub-Ts, X-Hub-Sig = hex HMAC-SHA256(ts + newline + raw body)), the same
// scheme as hubreport.js. This site cannot see emails (seeEmails false): a person is {id, username, wallet, sponsor: {id, username}, blocked, ...}.
// Answers (people and misses) are cached HUB_CACHE_MS (5 minutes). A failed call (timeout, refused, any non-200) is never
// cached: it returns {ok: false} so the caller can fall back, and the hub is skipped for 30 seconds so pages never wait on it.
// On unless HUB_LOOKUP=0; needs HUB_URL and a 32+ character HUB_SECRET (HUB_ENABLED only switches the shadow report).
'use strict';
const crypto = require('crypto');
const DEFAULT_SITE = 'fivex';
const ADDR_RE = /^0x[0-9a-f]{40}$/;
const TTL = () => Number(process.env.HUB_CACHE_MS || 5 * 60000);
const TIMEOUT = () => Number(process.env.HUB_LOOKUP_TIMEOUT_MS || 2500);
const on = () => !/^(0|false|no|off)$/i.test(String(process.env.HUB_LOOKUP || '').trim())
&& !!String(process.env.HUB_URL || '').trim() && String(process.env.HUB_SECRET || '').trim().length >= 32;
const cache = new Map(); let downUntil = 0; let lastError = null; const stats = { asked: 0, cached: 0, failed: 0 };
async function call(body) {
const raw = JSON.stringify(body); const ts = String(Date.now());
const sig = crypto.createHmac('sha256', String(process.env.HUB_SECRET).trim()).update(ts + '\n' + raw).digest('hex');
const ac = new AbortController(); const t = setTimeout(() => ac.abort(), TIMEOUT());
try {
const r = await fetch(String(process.env.HUB_URL).trim().replace(/\/+$/, '') + '/v1/resolve', { method: 'POST', body: raw, signal: ac.signal,
headers: { 'Content-Type': 'application/json', 'X-Hub-Site': String(process.env.HUB_SITE || DEFAULT_SITE).trim(), 'X-Hub-Ts': ts, 'X-Hub-Sig': sig } });
if (r.status !== 200) throw new Error('hub answered ' + r.status);
const j = await r.json();
if (!j || typeof j !== 'object' || !('person' in j)) throw new Error('hub answer has no person field');
return j.person && typeof j.person === 'object' ? j.person : null;
} finally { clearTimeout(t); }
}
// {ok: true, person: view | null} when the hub answered; {ok: false} when it is off or unreachable (the caller falls back)
async function resolve(body) {
if (!on() || Date.now() < downUntil) return { ok: false };
const k = JSON.stringify(body); const hit = cache.get(k);
if (hit && Date.now() - hit.at < TTL()) { stats.cached++; return { ok: true, person: hit.person }; }
stats.asked++;
let person;
try { person = await call(body); } catch (e) {
stats.failed++; downUntil = Date.now() + Number(process.env.HUB_DOWN_MS || 30000);
if (!lastError || Date.now() - lastError.at > 600000) console.error('hub lookup failed, falling back:', e.name === 'AbortError' ? 'timeout' : e.message);
lastError = { at: Date.now(), error: e.name === 'AbortError' ? 'timeout' : String(e.message).slice(0, 120) };
return { ok: false };
}
if (cache.size > 20000) cache.clear();
cache.set(k, { at: Date.now(), person });
if (person && person.id) cache.set(JSON.stringify({ id: person.id }), { at: Date.now(), person });
return { ok: true, person };
}
const wallet = p => (p && ADDR_RE.test(String(p.wallet || '').toLowerCase()) ? String(p.wallet).toLowerCase() : null);
// a username or wallet -> person (a blocked person counts as unknown)
async function byRef(ref) { const r = await resolve({ ref: String(ref || '').trim().toLowerCase() }); return r.ok ? { ok: true, person: r.person && !r.person.blocked ? r.person : null } : r; }
async function byWallet(w) { const r = await resolve({ wallet: String(w || '').trim().toLowerCase() }); return r.ok ? { ok: true, person: r.person && !r.person.blocked ? r.person : null } : r; }
async function byId(id) { if (!/^h_[0-9a-f]{16}$/.test(String(id || ''))) return { ok: true, person: null }; const r = await resolve({ id: String(id) }); return r.ok ? { ok: true, person: r.person && !r.person.blocked ? r.person : null } : r; }
// the person's network sponsor as a full person (with wallet), resolved through the hub by hub id. {ok, person: sponsor or null}
async function sponsorOf(p) { if (!p || !p.sponsor || !p.sponsor.id) return { ok: true, person: null }; return byId(p.sponsor.id); }
function view() { return { on: on(), down: Date.now() < downUntil, cached: cache.size, stats, lastError }; }
module.exports = { resolve, byRef, byWallet, byId, sponsorOf, wallet, view, on, _reset: () => { cache.clear(); downUntil = 0; } };