Pay game POL to any signature-linked wallet, on chain or not (2026-10-27)

InstantAdPay now pushes a member's signature-linked wallet whether or not it is activated (memberId 0 when not on chain).
- POST /api/iap/wallet accepts memberId 0/missing: the wallet fills the email-keyed player (sso.emailId) and its account;
  a player with no account row yet gets one holding the wallet. Signature check, fill-empty-only (different wallet 409),
  same wallet no-op and 404 for an unknown player are unchanged; a real memberId still adopts the email history first.
- /auth: a hand-off wallet fills an EMPTY account wallet so POL earned so far pays on the next sweep (never replaces one).
- game.adopt carries the wallet when the target account has none.
- /api/me: me.hasWallet (own wallet on file, also when it arrived by the push after the session began).
- play.js: no-wallet players see "Link your wallet to receive this POL" (IAP /my#wallet, new tab) on the chapter-clear
  result, the lobby member line and the balance card (which also lost a line that always overwrote the waiting-POL note);
  players with a wallet see the same text as before. play.js?v=20261027lw.
- test/run.js: memberId 0 push -> 200 changed:true -> sweep pays it; /auth memberId 0 wallet; 409; activation later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-27 09:18:09 -05:00
parent eb2cb822ac
commit b70ecf6f75
5 changed files with 56 additions and 15 deletions
+17 -8
View File
@@ -192,20 +192,25 @@ const server = http.createServer(async (req, res) => {
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' });
const mid0 = Number(b && b.memberId) || 0;
// since 2026-10-27 IAP pushes the signature-linked wallet whether or not it is activated on chain: memberId is 0 (or missing)
// for a member who is not on chain yet, and that player lives here under sso.emailId(email), so the wallet goes there
if (!em || mid0 < 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', {});
const wl = w.toLowerCase(), eid = sso.emailId(em), mid = mid0 > 0 ? mid0 : eid, 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; });
let adopted = false; if (mid !== eid && pls0[String(eid)]) adopted = !!game.adopt(eid, mid).moved;
// fill an empty wallet only; a player with no account row yet (nothing earned so far) gets one holding the wallet, so the
// POL they earn next is paid by the sweep even when their game session never saw the wallet
const hasPlayer = !!store.read('players', {})[String(mid)];
store.update('accounts', {}, all => { let x = all[String(mid)]; if (!x && hasPlayer) x = all[String(mid)] = { memberId: mid, unpaid: 0, paidTotal: 0, spentTotal: 0, earnedTotal: 0 }; if (x && !x.wallet) { x.wallet = wl; x.username = x.username || uname || null; x.email = x.email || em; } return all; });
if (hasPlayer) 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); });
if (changed || adopted) store.update('iapwallets', [], l => { l.unshift({ at: Date.now(), email: em, memberId: mid0, local: 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' }); }
@@ -245,6 +250,9 @@ const server = http.createServer(async (req, res) => {
if (guest) sso.endGuest(guest.gid);
const first = !existedBefore;
game.updPlayer(r.claims.memberId, x => { x.email = String(r.claims.email).toLowerCase(); x.username = r.claims.username || x.username || null; x.wallet = r.claims.wallet ? String(r.claims.wallet).toLowerCase() : x.wallet || null; x.lastSeen = Date.now(); });
// the hand-off wallet is the member's signature-linked wallet (on chain or not, 2026-10-27): it fills an EMPTY account wallet
// so POL already earned pays on the next sweep; an account that already has a wallet keeps it
if (r.claims.wallet && /^0x[a-f0-9]{40}$/i.test(String(r.claims.wallet))) store.update('accounts', {}, all => { const x = all[String(r.claims.memberId)]; if (x && !x.wallet) x.wallet = String(r.claims.wallet).toLowerCase(); return all; });
game.updPlayer(r.claims.memberId, x => { if (!x.sponsorRef && r.claims.sponsorRef) x.sponsorRef = String(r.claims.sponsorRef).toLowerCase(); if (r.claims.joinedRef && !x.joinedRef) x.joinedRef = r.claims.joinedRef; });
starter.grantPerk(r.claims.memberId); // a member (memberId > 0) gets one free hangar upgrade, once per account (2026-10-05)
if (r.claims.gamePass != null) ledger.gp.rememberPass(ledger.iapId(r.claims), r.claims.email, r.claims.gamePass); // the Game Pass flag rides on the hand-off as gamePass:{active,until} (GAME-PASS.md 3a item 7)
@@ -400,10 +408,11 @@ async function memberApi(req, res, u, p, me) {
try {
{
if (p === '/api/me' && req.method === 'GET') {
// me.hasWallet: the player's own wallet is on file (it can arrive by the IAP push after this session began, 2026-10-27)
const pl = ledger.pool();
if (!me.guest && ledger.gp.on()) ledger.gp.fetchPass(ledger.iapId(me), me.email).catch(() => {}); // keep the Game Pass flag fresh (display + the shield perk; grant answers carry the truth)
const refs = me.guest ? [] : game.referralsOf(me);
return json(res, 200, { me: { memberId: me.memberId, username: me.username, wallet: me.wallet, guest: !!me.guest }, player: game.view(me), balance: ledger.balance(me.memberId), pool: { available: pl.available, capLeft: pl.capLeft, dripPol: Number(ledger.settings().dripPol) }, iap: IAP, site: SITE, tips: await tips.list(ledger.settings()),
return json(res, 200, { me: { memberId: me.memberId, username: me.username, wallet: me.wallet, guest: !!me.guest, hasWallet: !me.guest && !!(me.wallet || ledger.account(me.memberId).wallet || (game.player(me.memberId) || {}).wallet) }, player: game.view(me), balance: ledger.balance(me.memberId), pool: { available: pl.available, capLeft: pl.capLeft, dripPol: Number(ledger.settings().dripPol) }, iap: IAP, site: SITE, tips: await tips.list(ledger.settings()),
signin: me.guest ? signinUrl(req) : null, share: (!me.guest && me.username) ? SITE + '/r/' + encodeURIComponent(me.username) : null, referrals: { count: refs.length, clicks: (store.read('share-hits', {})[String(me.username || '').toLowerCase()] || 0), earned: ledger.balance(me.memberId).refTotal, pct: Number(ledger.settings().refSharePct) || 0 },
perk: me.guest ? null : starter.takeNotice(me.memberId), // the member perk's one-time message (2026-10-05)
gamepass: me.guest ? null : ledger.gp.view(ledger.iapId(me), me.email) }); // Game Pass: active until <date>, or the link to get it