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
+26
View File
@@ -138,6 +138,32 @@ const spawnLib = require('../lib/spawn');
const tokW2 = sso.mint({ memberId: 91, email: 'free@example.com', wallet: '0x' + 'ef'.repeat(20), username: 'freebie' });
await hit('/auth?t=' + tokW2); const moved = gameLib.player(91); eq([moved && moved.chapter, moved && moved.bank, !!gameLib.player(wme.me.memberId)], [4, 33, false], 'after the wallet is linked, progress moves to the on-chain id');
// ---- 2026-10-27: IAP pushes the signature-linked wallet whether or not it is on chain (memberId 0) ----
const pushW = b => { const raw = JSON.stringify(b), ts = String(Date.now()); const sig = crypto.createHmac('sha256', SECRET).update('wallet\n' + ts + '\n' + crypto.createHash('sha256').update(raw).digest('hex')).digest('hex'); return hit('/api/iap/wallet', { method: 'POST', body: raw, headers: { 'X-IAP-Ts': ts, 'X-IAP-Sig': sig } }); };
const zw = '0x' + '12'.repeat(20); let z = '';
const az = await hit('/auth?t=' + sso.mint({ memberId: 0, email: 'tank@example.com', wallet: null, username: 'tanker' })); z = jarAdd(z, az.set);
const zme = (await hit('/api/me', {}, z)).body; const zid = zme.me.memberId;
eq([zid < 0, zme.me.hasWallet], [true, false], 'a holding-tank member plays on the email id with hasWallet false');
store.update('accounts', {}, all => { all[String(zid)] = { memberId: zid, unpaid: 0.02, paidTotal: 0, spentTotal: 0, earnedTotal: 0.02 }; return all; }); // POL earned before the wallet
const z1 = await pushW({ email: 'tank@example.com', wallet: zw, username: 'tanker', memberId: 0 });
eq([z1.status, z1.body.changed, z1.body.wallet, z1.body.unpaid], [200, true, zw, 0.02], 'push with memberId 0 fills the email-keyed wallet (200 changed:true)');
eq([(await pushW({ email: 'tank@example.com', wallet: zw })).body.changed, (await pushW({ email: 'tank@example.com', wallet: '0x' + '34'.repeat(20), memberId: 0 })).status], [false, 409], 'same wallet again (memberId missing) is a no-op; a different wallet is refused 409');
eq((await pushW({ email: 'nobody@example.com', wallet: zw, memberId: 0 })).status, 404, 'an unknown email is still 404');
eq((await hit('/api/me', {}, z)).body.me.hasWallet, true, 'the session that never saw the wallet now reports hasWallet');
await admin('/api/admin/settings', { thresholdPol: 0.005 });
const dz = ledger.payable().filter(x => x.memberId === zid); eq([dz.length, dz[0] && dz[0].wallet, dz[0] && dz[0].pol], [1, zw, 0.02], 'the payout sweep pays the linked wallet with memberId 0');
// a hand-off with memberId 0 and a wallet fills an empty account wallet too (POL earned so far pays on the next sweep)
const yw = '0x' + '56'.repeat(20);
await hit('/auth?t=' + sso.mint({ memberId: 0, email: 'tank2@example.com', wallet: null, username: 'tanker2' })); const yid = sso.emailId('tank2@example.com');
store.update('accounts', {}, all => { all[String(yid)] = { memberId: yid, unpaid: 0.01, paidTotal: 0, spentTotal: 0, earnedTotal: 0.01 }; return all; });
await hit('/auth?t=' + sso.mint({ memberId: 0, email: 'tank2@example.com', wallet: yw, username: 'tanker2' }));
const dy = ledger.payable().filter(x => x.memberId === yid); eq([ledger.account(yid).wallet, dy.length, dy[0] && dy[0].wallet], [yw, 1, yw], '/auth with memberId 0 stores the wallet and the sweep pays it');
await hit('/auth?t=' + sso.mint({ memberId: 0, email: 'tank2@example.com', wallet: '0x' + '78'.repeat(20), username: 'tanker2' }));
eq(ledger.account(yid).wallet, yw, 'a later hand-off never replaces the account wallet that pays');
// the member activates later: the push with the real id moves everything (wallet included) onto it
const z9 = await pushW({ email: 'tank@example.com', wallet: zw, username: 'tanker', memberId: 300 });
eq([z9.status, ledger.account(300).wallet, !!gameLib.player(zid)], [200, zw, false], 'activation later: the email-keyed history and wallet move to the on-chain id');
console.log(fails ? '\n' + fails + ' FAILED' : '\nall good');
child.kill(); fs.rmSync(DIR, { recursive: true, force: true }); process.exit(fails ? 1 : 0);
})().catch(e => { console.error(e); child.kill(); process.exit(1); });