Referral layer: guest play from share links (/r/<username>), guest clears paid on sign-in, sharer's link via their IAP join page, 10% referrer slice of each referral refuel's pool income (IAP sponsor), share buttons + stats in the lobby and on chapter clears, wallet-less progress follows the member once a wallet is linked; 11 new checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-30 17:14:08 -05:00
parent 83da8c33f2
commit 06daa4ef0a
10 changed files with 203 additions and 25 deletions
+19 -3
View File
@@ -16,7 +16,24 @@ const b64u = b => Buffer.from(b).toString('base64').replace(/=+$/, '').replace(/
const unb64u = s => Buffer.from(String(s).replace(/-/g, '+').replace(/_/g, '/'), 'base64');
const sign = payload => b64u(crypto.createHmac('sha256', SECRET).update(payload).digest());
const secure = () => process.env.NODE_ENV === 'production' ? '; Secure' : '';
function enabled() { return SECRET.length >= 32; }
// the temporary id of a member with no on-chain id yet (no wallet): negative, derived from the email
function emailId(email) { return -parseInt(crypto.createHash('sha256').update(String(email).toLowerCase()).digest('hex').slice(0, 11), 16); }
// guests (a share link, no sign-in yet): their own negative range, far below any email id
function guestFrom(req) {
const m = /(?:^|;\s*)dr\.gid=([a-f0-9]{32})/.exec(req.headers.cookie || ''); if (!m) return null;
const g = store.read('guests', {})[m[1]]; if (!g) return null;
return { gid: m[1], memberId: g.memberId, guest: true, username: null, wallet: null, email: null, ref: g.ref || null };
}
function startGuest(ref) {
const gid = crypto.randomBytes(16).toString('hex'); const memberId = -(2 ** 45 + crypto.randomInt(2 ** 40));
store.update('guests', {}, g => { const cut = Date.now() - 30 * 86400000; for (const k of Object.keys(g)) if (g[k].since < cut) delete g[k]; g[gid] = { memberId, since: Date.now(), ref: ref || null }; return g; });
return { gid, memberId };
}
function endGuest(gid) { store.update('guests', {}, g => { delete g[gid]; return g; }); }
function guestCookie(gid) { return 'dr.gid=' + gid + '; Path=/; Max-Age=' + 30 * 86400 + '; HttpOnly; SameSite=Lax' + secure(); }
function clearGuestCookie() { return 'dr.gid=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax' + secure(); }
function mint(claims, ttlMs) {
const payload = JSON.stringify(Object.assign({ iat: Date.now(), exp: Date.now() + (ttlMs || 5 * 60000), nonce: crypto.randomBytes(12).toString('hex') }, claims));
return b64u(payload) + '.' + sign(payload);
@@ -39,7 +56,7 @@ function verify(token) {
const c = r.claims;
if (!c.email) return { error: 'That sign-in link is missing your account.' };
// a free member with no wallet has no on-chain id yet: key them on their email until they link one
if (!(Number(c.memberId) > 0)) c.memberId = -parseInt(crypto.createHash('sha256').update(String(c.email).toLowerCase()).digest('hex').slice(0, 11), 16);
if (!(Number(c.memberId) > 0)) c.memberId = emailId(c.email);
for (const [n, exp] of used) if (exp < Date.now()) used.delete(n);
if (used.has(c.nonce)) return { error: 'That sign-in link was already used.' };
used.set(c.nonce, c.exp);
@@ -55,7 +72,6 @@ function startSession(claims) {
});
return id;
}
const secure = () => process.env.NODE_ENV === 'production' ? '; Secure' : '';
function cookie(id) { return 'dr.sid=' + id + '; Path=/; Max-Age=' + Math.floor(SESSION_TTL / 1000) + '; HttpOnly; SameSite=Lax' + secure(); }
function clearCookie() { return 'dr.sid=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax' + secure(); }
function fromRequest(req) {
@@ -68,4 +84,4 @@ function fromRequest(req) {
function endSession(sid) { store.update('sessions', {}, s => { delete s[sid]; return s; }); }
function refresh(sid, claims) { store.update('sessions', {}, s => { if (s[sid]) { s[sid].wallet = claims.wallet ? String(claims.wallet).toLowerCase() : null; s[sid].username = claims.username || s[sid].username; } return s; }); }
module.exports = { enabled, mint, open, verify, startSession, cookie, clearCookie, fromRequest, endSession, refresh };
module.exports = { enabled, mint, open, verify, startSession, cookie, clearCookie, fromRequest, endSession, refresh, emailId, guestFrom, startGuest, endGuest, guestCookie, clearGuestCookie };