From 18813a4127e37d5e3081fe86b10c9e1c2610d64e Mon Sep 17 00:00:00 2001 From: martbost Date: Mon, 5 Oct 2026 07:56:44 -0500 Subject: [PATCH] PolyWarriors paid mints: /pw/mint checkout, 100 credits from package credits only (earned never counts) Co-Authored-By: Claude Opus 5.5 --- ads.js | Bin 139312 -> 140527 bytes public/assets/my.js | 2 ++ public/my.html | 2 +- server.js | 68 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/ads.js b/ads.js index a9e2bde31ac400e1723a040ce84febb3865b615c..8179d27164583d38fca635d39e7320cabce7e62a 100644 GIT binary patch delta 776 zcmZ9KJ8u&~6oqxcL?+@TFf=jIH4*VHT5p^{AX?)@5fu^$3M2|BGTt5RQTColRDVAl3JA>BOAx%ZoM=kwR}?smHU1@>3QH1zy1 z5c?}&D5d^dH*18s4_v#6WpwEpzoq3?Nicb35IW0!MEY{lv6BQ`ZY675Tn z6+fzQwfoV8R7yPg1!vEUN*`#tQgiQR=iT>7(JjD)xW5CRUd+s(U*}RCP^(X(JoN15 zW5PM}cz_Ob!$8Bi;@r)`Y_U+BR}J)HV4s+)q)O4`)MApYMr}*pAd%bw3z=M+Ufm5# zN1uc&NZ#tz&G8zwPLCt9XV}@=&Yr`Kc!$#K1|j+tvGf~qnRN}DDwY?qsgpIrY%Jiq zc(nl9wZ2oyDSkzFs8gZ*t`%n@w*%o~IUy9wKOrNY@4|&U$*qPJ8idA3tw}Iy8Hq!7 zNtD<|fEWoSOk_1vLGveY~5AtJiGqu!te=8$k52nsL9;etK0fuCix_Ta4qV-mOu=Za_ fi>*C4Q5fP4%8k;}i*e*h3o$SL!|}A delta 66 zcmV-I0KNb3$q2B(2!ON!_h6U(U;#Rlanu^Od0_z}0+T3=9hWy~0VKEiV*vyNljYnT Yw>D@2OoWpnz!;aH>;cNRyYT@uGsgiOQvd(} diff --git a/public/assets/my.js b/public/assets/my.js index c9dbece..ae9c2d4 100644 --- a/public/assets/my.js +++ b/public/assets/my.js @@ -844,6 +844,8 @@ if (me.returnTo === 'gemkingdom' && !render.toGame) { render.toGame = true; location.href = '/api/my/gemkingdom'; return; } // Gem Kingdom sent them here to sign in (2026-10-01) if (me.returnTo === 'gkshop' && !render.toGame) { render.toGame = true; location.href = '/gk/shop/resume'; return; } if (me.returnTo === 'spookydoors' && !render.toGame) { render.toGame = true; location.href = '/api/my/spookydoors'; return; } // Spooky Doors sent them here to sign in (2026-10-02) + if (me.returnTo === 'pwmint' && !render.toGame) { render.toGame = true; location.href = '/pw/mint/resume'; return; } // PolyWarriors paid mint (2026-10-05) + if (me.pwMint && !me.gkShop && !me.sdShop && !document.getElementById('pwBack')) { const d = document.createElement('a'); d.id = 'pwBack'; d.href = '/pw/mint/resume'; d.textContent = 'Finish minting your PolyWarriors fighter →'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:14px;z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:linear-gradient(180deg,#22E5FF,#7A2CFF);color:#fff;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); } if (me.returnTo === 'sdshop' && !render.toGame) { render.toGame = true; location.href = '/sd/shop/resume'; return; } if (me.sdShop && !document.getElementById('sdBack')) { const d = document.createElement('a'); d.id = 'sdBack'; d.href = '/sd/shop/resume'; d.textContent = 'Finish your Spooky Doors costume order \u2192'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:' + (me.gkShop ? '74px' : '14px') + ';z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:#FF8A1F;color:#2a1200;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 5px 0 #C25A06,0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); } if (me.gkShop && !document.getElementById('gkBack')) { const d = document.createElement('a'); d.id = 'gkBack'; d.href = '/gk/shop/resume'; d.textContent = 'Finish your Gem Kingdom Crowns order \u2192'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:14px;z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:linear-gradient(180deg,#E8455A,#A8172B);color:#fff;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); } diff --git a/public/my.html b/public/my.html index d6b2bb7..43a3b88 100644 --- a/public/my.html +++ b/public/my.html @@ -1131,7 +1131,7 @@ - + diff --git a/server.js b/server.js index 8aac95e..ca66fb4 100644 --- a/server.js +++ b/server.js @@ -1336,6 +1336,25 @@ async function swCallback(cbUrl, receipt) { if (!swIsGameUrl(cbUrl)) return false; try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('spellwheel callback', e.message); return false; } } +// -- PolyWarriors paid mints (2026-10-05): the first mint is free in the game; later mints cost PW_MINT_CREDITS (100) from +// PURCHASED credits only (package credits on a position; earned credits never count, Marty's rule). The game signs an order +// with PW_SSO_SECRET (kind 'pw-mint'), the member confirms here, ads.gamePurchase(purchasedOnly) charges, and a signed +// receipt goes back to the game twice (server callback + redirect), whichever lands first. The game mints on the receipt. +function pwOpen(token) { + if (PW_SECRET().length < 32) return { error: 'PolyWarriors is not connected yet.' }; + const [p, sig] = String(token || '').split('.'); if (!p || !sig) return { error: 'That link is not valid.' }; + let payload; try { payload = Buffer.from(String(p).replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'); } catch (e) { return { error: 'That link is not valid.' }; } + const want = pwSign(payload); if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return { error: 'That link is not valid.' }; + let c; try { c = JSON.parse(payload); } catch (e) { return { error: 'That link is not valid.' }; } + if (!c.exp || Date.now() > c.exp) return { error: 'That mint order has expired. Tap Mint in PolyWarriors again.' }; + return { ok: true, claims: c }; +} +const pwOrders = persistedMap('pworders'); // order -> { status: 'paying'|'paid', email, campaignId, ts } +const pwIsGameUrl = x => [PW_URL()].concat(PW_ALT()).some(o => String(x || '').startsWith(o + '/')); +async function pwCallback(cbUrl, receipt) { + if (!pwIsGameUrl(cbUrl)) return false; + try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('polywarriors callback', e.message); return false; } +} async function gkCallback(cbUrl, receipt) { if (!String(cbUrl || '').startsWith(GK_URL() + '/')) return false; try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('gemkingdom callback', e.message); return false; } @@ -2098,7 +2117,7 @@ const server = http.createServer(async (req, res) => { const _defSpon = Number(siteConfig().defaultSponsorId) || 1; if (!sponsorId && memberId !== _defSpon) sponsorId = _defSpon; // orphan fallback → #1 (never self-sponsor) if (memberId && acct && acct.memberId !== memberId) accounts.setMemberId(acct.email, memberId).catch(() => {}); - const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], returnTo: ['polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null, + const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], pwMint: !!parseCookies(req)['iap.pwmint'], returnTo: ['polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop', 'pwmint'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null, address: s.address || (acct && acct.address) || null, memberId, username: (acct && acct.username) || null, refCode: (acct && acct.code) || null, sponsorId, @@ -2907,6 +2926,53 @@ const server = http.createServer(async (req, res) => { + 'Buy a credit packagePick a smaller pack' + '

A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you straight back to finish this order.

'); } + // ---- PolyWarriors paid mint checkout + if (p === '/pw/mint' && req.method === 'GET') { + const t = u.searchParams.get('t') || ''; const r = pwOpen(t); + if (r.error) return gkPage(res, 400, 'Mint problem', '

That mint did not work

' + gkEsc(r.error) + '

Back to PolyWarriors'); + const c = r.claims; if (c.kind !== 'pw-mint' || !c.order || !(c.credits > 0) || !(c.serial > 0)) return gkPage(res, 400, 'Mint problem', '

That is not a mint order

'); + const done = pwOrders.get(c.order); + if (done && done.status === 'paid') { const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: c.email, serial: c.serial, credits: c.credits, paid: c.credits, ref: done.campaignId }, 24 * 3600000); res.writeHead(302, { Location: (pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc, 'Cache-Control': 'no-store' }); return res.end(); } + const s = await auth.fromRequest(req); + if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.pwmint', t, 7200), gkCookie('iap.return', 'pwmint', 1800)] }); return res.end(); } + if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Different account', '

Different account

This mint was started by the PolyWarriors player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account and tap Mint again.

'); + const { ids } = await myMemberIds(s); const bal = await ads.balances(ids, s.email); + const best = (bal.per || []).slice().sort((a, b) => b.avail - a.avail)[0] || { avail: 0 }; + const head = '

PolyWarriors · Mint to your wallet

' + gkEsc(c.label || ('Warrior #' + c.serial)) + '

' + Number(c.credits) + ' credits
' + + '
Paid fromcredits from your packages
' + + '
Your package credits' + Number(best.avail || 0).toLocaleString('en-US') + '
'; + if (best.avail >= c.credits) { + const csrf = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase()); + return gkPage(res, 200, 'Confirm your mint', head + '
' + + 'Cancel and go back

Minting puts this warrior on Polygon in the wallet on your InstantAdPay account. Earned credits cannot be used for mints; credits from your packages can. The game pays the network fee.

'); + } + return gkPage(res, 200, 'Package credits needed', head + '

Mints after your first free one are paid with credits from your packages, and you need ' + Math.max(0, c.credits - best.avail).toLocaleString('en-US') + ' more. Earned credits do not count for mints.

' + + 'Buy a credit packageBack to PolyWarriors' + + '

A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you back to finish this mint.

'); + } + if (p === '/pw/mint/topup' && req.method === 'GET') { const t = u.searchParams.get('t') || ''; if (pwOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); } res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.pwmint', t, 7200) }); return res.end(); } + if (p === '/pw/mint/resume' && req.method === 'GET') { const t = parseCookies(req)['iap.pwmint']; res.writeHead(302, { Location: t ? '/pw/mint?t=' + encodeURIComponent(t) : PW_URL() + '/play', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end(); } + if (p === '/pw/mint/confirm' && req.method === 'POST') { + const f = await gkFormBody(req); const t = f.get('t') || ''; const r = pwOpen(t); + if (r.error) return gkPage(res, 400, 'Mint problem', '

That mint did not work

' + gkEsc(r.error) + '

'); + const c = r.claims; const s = await auth.fromRequest(req); + if (!s || !s.email || String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Sign in', '

Please sign in again

Continue'); + const want = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase()); const got = String(f.get('csrf') || ''); + if (want.length !== got.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(got))) return gkPage(res, 403, 'Mint problem', '

That form expired

Try again'); + const prev = pwOrders.get(c.order); + if (prev && (prev.status === 'paid' || (prev.status === 'paying' && Date.now() - prev.ts < 60000))) { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); } + pwOrders.set(c.order, { status: 'paying', email: s.email, ts: Date.now() }); + const { ids } = await myMemberIds(s); + const out = await ads.gamePurchase(s.email, ids, Number(c.credits), 'PolyWarriors: mint warrior #' + c.serial, PW_URL(), { purchasedOnly: true }); + if (out.error) { pwOrders.delete(c.order); if (out.error === 'short') { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); } return gkPage(res, 500, 'Mint problem', '

Something went wrong

' + gkEsc(out.error) + '

'); } + pwOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() }); + console.log('polywarriors mint fee', s.email, 'serial', c.serial, c.credits, 'purchased credits, campaign', out.campaignId); + const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: String(c.email).toLowerCase(), serial: Number(c.serial), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000); + pwCallback(c.cb, rc); + const back = gkEsc((pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc); + res.setHeader('Set-Cookie', gkCookie('iap.pwmint', '', 0)); + return gkPage(res, 200, 'Paid', '

PolyWarriors · Mint

Paid!

Minting #' + Number(c.serial) + '

Taking you back to PolyWarriors...

Back to PolyWarriors'); + } if (p === '/gk/shop/topup' && req.method === 'GET') { // the detour to Buy packages, remembered for the way back const t = u.searchParams.get('t') || ''; if (gkOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); } res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.gkshop', t, 7200) }); return res.end();