diff --git a/server.js b/server.js index d01b671..e7d2523 100644 --- a/server.js +++ b/server.js @@ -180,6 +180,36 @@ const server = http.createServer(async (req, res) => { const accts = store.read('accounts', {}), players = store.read('players', {}); const rows = Object.entries(accts).filter(([k, a]) => Number(a.unpaid) > 0 && !a.wallet).map(([k, a]) => ({ memberId: Number(k), username: a.username || (players[k] || {}).username || null, email: (players[k] || {}).email || null, unpaid: Number(Number(a.unpaid).toFixed(4)) })); return json(res, 200, { ok: true, game: 'driprunner', rows }); } + // InstantAdPay pushes the wallet a member linked after their last hand-off (2026-10-25: solanaclick had 1.5 POL stuck behind + // a remembered game session that never saw the wallet). POST JSON {email, wallet, username, memberId}; headers X-IAP-Ts and + // X-IAP-Sig = hex HMAC-SHA256(shared secret, 'wallet\n' + ts + '\n' + hex sha256(body)), 10-minute window. It fills an EMPTY + // wallet through the same setWallet path /auth uses; the same wallet again is a no-op; a different wallet is refused, never replaced. + if (p === '/api/iap/wallet' && req.method === 'POST') { + const raw = await new Promise(ok => { let d = ''; req.on('data', c => { d += c; if (d.length > 20000) req.destroy(); }); req.on('end', () => ok(d)); req.on('error', () => ok('')); }); + const ts = String(req.headers['x-iap-ts'] || ''), sig = String(req.headers['x-iap-sig'] || ''); + if (SECRET.length < 32 || !ts || Math.abs(Date.now() - Number(ts)) > 10 * 60000) return json(res, 401, { error: 'bad signature' }); + const want = crypto.createHmac('sha256', SECRET).update('wallet\n' + ts + '\n' + crypto.createHash('sha256').update(raw).digest('hex')).digest('hex'); + if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return json(res, 401, { error: 'bad signature' }); + let b; try { b = JSON.parse(raw); } catch (e) { return json(res, 400, { error: 'bad body' }); } + const em = String((b && b.email) || '').trim().toLowerCase(), w = String((b && b.wallet) || '').trim(), uname = (b && b.username) ? String(b.username).slice(0, 40) : null; + const mid = Number(b && b.memberId) || 0; + if (!em || !(mid > 0) || !/^0x[a-f0-9]{40}$/i.test(w)) return json(res, 400, { error: 'bad email, member id or wallet' }); + try { + // Drip Runner keys players by member id; a wallet-less member played under sso.emailId(email) (negative). /auth moves that + // history onto the real id with game.adopt; the push does the same, then fills the wallet on the account. + const wl = w.toLowerCase(), eid = sso.emailId(em), acc0 = store.read('accounts', {}), pls0 = store.read('players', {}); + if (!acc0[String(mid)] && !pls0[String(mid)] && !acc0[String(eid)] && !pls0[String(eid)]) return json(res, 404, { error: 'unknown player' }); + const had = (acc0[String(mid)] && acc0[String(mid)].wallet) || null, mask = em.replace(/^(..).*(@.*)$/, '$1***$2'); + if (had && had.toLowerCase() !== wl) { console.log(new Date().toISOString(), 'iap wallet push REFUSED (a different wallet is set)', mask, mid, had, wl); return json(res, 409, { error: 'a different wallet is already set', wallet: had }); } + let adopted = false; if (pls0[String(eid)]) adopted = !!game.adopt(eid, mid).moved; + store.update('accounts', {}, all => { const x = all[String(mid)]; if (x && !x.wallet) { x.wallet = wl; x.username = x.username || uname || null; x.email = x.email || em; } return all; }); + if (store.read('players', {})[String(mid)]) game.updPlayer(mid, x => { x.email = x.email || em; x.username = uname || x.username || null; if (!x.wallet) x.wallet = wl; }); + const acc = store.read('accounts', {})[String(mid)] || {}, unpaid = Number(Number(acc.unpaid || 0).toFixed(4)), changed = !had && !!acc.wallet; + if (changed || adopted) store.update('iapwallets', [], l => { l.unshift({ at: Date.now(), email: em, memberId: mid, wallet: wl, adopted, unpaid }); return l.slice(0, 300); }); + console.log(new Date().toISOString(), 'iap wallet push', changed ? 'FILLED' : had ? 'already set' : 'no account', mask, mid, wl, adopted ? 'adopted ' + eid : '', 'unpaid', unpaid); + return json(res, 200, { ok: true, changed, adopted, wallet: acc.wallet || null, unpaid }); + } catch (e) { console.error('iap wallet push', e.message); return json(res, 500, { error: 'failed' }); } + } // frames (2026-10-03): public pages show inside traffic exchanges; admin and the sign-in hand-offs never do. A framed // visit to the game itself lands on the landing page, whose buttons open the game in a new tab (cookies die in frames) if (/^\/(admin|auth|signin|logout)(\/|$)/.test(p) || p.startsWith('/api/admin')) res.setHeader('X-Frame-Options', 'DENY');