SpellWheel hookups: in-game sign-in (game 'spellwheel', SW_SSO_SECRET), one-tap /api/my/spellwheel, sponsor-visit hints through the shared refuel flow (sw-hint receipts)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-04 08:56:40 -05:00
parent 376a4b04f2
commit e09babc1ff
3 changed files with 46 additions and 7 deletions
+42 -3
View File
@@ -1307,6 +1307,26 @@ async function seekCallback(cbUrl, receipt) {
if (!seekIsGameUrl(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('clutterhunt callback', e.message); return false; }
}
// -- SpellWheel (2026-10-04): the letter-wheel word game at spellwheel.com; its own secret SW_SSO_SECRET (also on the game)
const SW_SECRET = () => String(process.env.SW_SSO_SECRET || '').trim();
const SW_URL = () => String(process.env.SW_URL || 'https://spellwheel.com').replace(/\/+$/, '');
const SW_ALT = () => String(process.env.SW_ALT_URLS || 'https://www.spellwheel.com,https://spellwheel.saasy.top').split(',').map(x => x.trim().replace(/\/+$/, '')).filter(Boolean);
const swSign = p => drB64u(crypto.createHmac('sha256', SW_SECRET()).update(p).digest());
function swMint(claims, ttlMs) { const payload = JSON.stringify(Object.assign({ iat: Date.now(), exp: Date.now() + (ttlMs || 300000), nonce: crypto.randomBytes(12).toString('hex') }, claims)); return drB64u(payload) + '.' + swSign(payload); }
function swOpen(token) {
if (SW_SECRET().length < 32) return { error: 'SpellWheel 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 = swSign(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 hint link has expired. Go back to SpellWheel and tap Hint again.' };
return { ok: true, claims: c };
}
const swIsGameUrl = u => [SW_URL()].concat(SW_ALT()).some(o => String(u || '').startsWith(o + '/'));
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; }
}
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; }
@@ -1471,6 +1491,8 @@ const GAME_AUTH = {
sponsorRef: a.sponsorRef || null, joinedRef: a.joinedRef || null, joinedAt: a.created || null }) },
seek: { name: 'ClutterHunt', origins: () => [SEEK_URL()].concat(SEEK_ALT()), host: () => SEEK_URL().replace(/^https?:\/\//, ''),
mint: (a, memberId) => seekMint({ memberId: Number(memberId) || 0, email: String(a.email).toLowerCase(), username: a.username || null, wallet: a.address || null }) },
spellwheel: { name: 'SpellWheel', origins: () => [SW_URL()].concat(SW_ALT()), host: () => SW_URL().replace(/^https?:\/\//, ''),
mint: (a, memberId) => swMint({ memberId: Number(memberId) || 0, email: String(a.email).toLowerCase(), username: a.username || null, wallet: a.address || null }) },
gemkingdom: { name: 'Gem Kingdom', origins: () => [GK_URL(), 'https://gemkingdom.saasy.top'], host: () => GK_URL().replace(/^https?:\/\//, ''),
mint: (a, memberId) => gkMint({ memberId: Number(memberId) || 0, email: String(a.email).toLowerCase(), username: a.username || null, wallet: a.address || null }) },
};
@@ -1880,7 +1902,7 @@ const server = http.createServer(async (req, res) => {
let memberId = 0;
if (r.account.address) { try { memberId = await chain.memberIdByAccount(r.account.address); } catch (err) {} }
const token = await auth.mintSession({ email: r.account.email, address: r.account.address, memberId });
return json(res, 200, { ok: true, account: r.account, created: !!r.created, next: ({ clutterhunt: '/api/my/clutterhunt', driprunner: '/api/my/driprunner', passivedrip: '/api/my/passivedrip', gemkingdom: '/api/my/gemkingdom', gkshop: '/gk/shop/resume', spookydoors: '/api/my/spookydoors', sdshop: '/sd/shop/resume' })[parseCookies(req)['iap.return']] || null }, { 'Set-Cookie': auth.sessionCookie(token) }); // next: Drip Runner sent them here to sign in (2026-09-30)
return json(res, 200, { ok: true, account: r.account, created: !!r.created, next: ({ clutterhunt: '/api/my/clutterhunt', spellwheel: '/api/my/spellwheel', driprunner: '/api/my/driprunner', passivedrip: '/api/my/passivedrip', gemkingdom: '/api/my/gemkingdom', gkshop: '/gk/shop/resume', spookydoors: '/api/my/spookydoors', sdshop: '/sd/shop/resume' })[parseCookies(req)['iap.return']] || null }, { 'Set-Cookie': auth.sessionCookie(token) }); // next: Drip Runner sent them here to sign in (2026-09-30)
}
// -- in-game sign-in (2026-10-03): see GAME_AUTH. Mirrors /api/auth/email/start + verify, minus cookies.
@@ -2793,6 +2815,15 @@ const server = http.createServer(async (req, res) => {
sponsorRef: (acct && acct.sponsorRef) || null, joinedRef: (acct && acct.joinedRef) || null, joinedAt: (acct && acct.created) || null }); // the game credits referrals to the IAP sponsor
res.writeHead(302, { Location: DR_URL() + '/auth?t=' + tok, 'Cache-Control': 'no-store', 'Set-Cookie': 'iap.return=; Path=/; SameSite=Lax; Max-Age=0' + (IS_PROD ? '; Secure' : '') }); return res.end();
}
// -- SpellWheel hand-off (2026-10-04): sign the member across; the game verifies with SW_SSO_SECRET
if (p === '/api/my/spellwheel' && req.method === 'GET') {
const s = await auth.fromRequest(req);
if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Set-Cookie': gkCookie('iap.return', 'spellwheel', 1800), 'Cache-Control': 'no-store' }); return res.end(); }
if (SW_SECRET().length < 32) return json(res, 503, { error: 'SpellWheel is not connected yet.' });
const acct = await accounts.byEmail(s.email);
const tok = swMint({ memberId: Number((acct && acct.memberId) || s.memberId || 0), email: String(s.email).toLowerCase(), username: (acct && acct.username) || null, wallet: (acct && acct.address) || null });
res.writeHead(302, { Location: SW_URL() + '/auth?t=' + tok, 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end();
}
// -- ClutterHunt hand-off (2026-10-03): sign the member across; the game verifies with SEEK_SSO_SECRET
if (p === '/api/my/clutterhunt' && req.method === 'GET') {
const s = await auth.fromRequest(req);
@@ -2982,11 +3013,13 @@ const server = http.createServer(async (req, res) => {
let r = drOpen(u.searchParams.get('t')), game = 'driprunner';
if (r.error) { const g = gkOpen(u.searchParams.get('t')); if (!g.error && g.claims.game === 'gemkingdom') { r = g; game = 'gemkingdom'; } }
if (r.error) { const g = seekOpen(u.searchParams.get('t')); if (!g.error && g.claims.game === 'seek') { r = g; game = 'seek'; } }
if (r.error) { const g = swOpen(u.searchParams.get('t')); if (!g.error && g.claims.game === 'spellwheel') { r = g; game = 'spellwheel'; } }
if (r.error) return json(res, 400, { error: r.error });
const c = r.claims; if (c.kind !== 'refuel' || !c.nonce || !c.email || !c.ret) return json(res, 400, { error: 'That is not a refuel link.' });
if (game === 'gemkingdom' && !String(c.ret).startsWith(GK_URL() + '/')) return json(res, 400, { error: 'That is not a Gem Kingdom link.' });
if (game === 'seek' && !seekIsGameUrl(c.ret)) return json(res, 400, { error: 'That is not a ClutterHunt link.' });
const G = game === 'gemkingdom' ? { game: 'Gem Kingdom', label: 'continue' } : game === 'seek' ? { game: 'ClutterHunt', label: 'hint' } : { game: 'Drip Runner', label: 'refuel' };
if (game === 'spellwheel' && !swIsGameUrl(c.ret)) return json(res, 400, { error: 'That is not a SpellWheel link.' });
const G = game === 'gemkingdom' ? { game: 'Gem Kingdom', label: 'continue' } : game === 'seek' ? { game: 'ClutterHunt', label: 'hint' } : game === 'spellwheel' ? { game: 'SpellWheel', label: 'hint' } : { game: 'Drip Runner', label: 'refuel' };
for (const [n, v] of refuelTokens) if (v.ts + 30 * 60000 < Date.now()) refuelTokens.delete(n);
let t = refuelTokens.get(c.nonce);
if (t && t.done) return json(res, 200, Object.assign({ done: true, ret: t.doneRet || t.ret }, G));
@@ -3029,12 +3062,18 @@ const server = http.createServer(async (req, res) => {
if (!tier) return json(res, 400, { error: 'That video just ran out of budget. Go back and try again.' });
await ads.markVideoSeen(t.email, t.id); credits = tier.cost;
} else {
const r = await ads.completeVisit(t.email, t.id, { reward: false, note: t.game === 'gemkingdom' ? 'Gem Kingdom continue' : t.game === 'seek' ? 'ClutterHunt hint' : 'Drip Runner refuel' });
const r = await ads.completeVisit(t.email, t.id, { reward: false, note: t.game === 'gemkingdom' ? 'Gem Kingdom continue' : t.game === 'seek' ? 'ClutterHunt hint' : t.game === 'spellwheel' ? 'SpellWheel hint' : 'Drip Runner refuel' });
if (r.error) return json(res, 400, r);
credits = ads.rates().visitCostPerVisit || 3;
{ const camp = await ads.campaignById(t.id).catch(() => null); if (camp && camp.house) credits = 0; } // a house ad costs nobody anything: no POL pool income (2026-10-03)
}
t.done = true;
if (t.game === 'spellwheel') { // SpellWheel hint: signed receipt to the game's callback and on the ret page
const receipt = swMint({ kind: 'sw-hint', nonce, email: t.email, campaignId: t.id, credits }, 15 * 60000);
const posted = await swCallback(t.cb, receipt);
t.doneRet = t.ret + (t.ret.includes('?') ? '&' : '?') + 'ok=1&r=' + encodeURIComponent(receipt);
return json(res, 200, { ok: true, posted, ret: t.doneRet, game: 'SpellWheel', label: 'hint' });
}
if (t.game === 'seek') { // ClutterHunt hint: signed receipt to the game's callback and on the ret page
const receipt = seekMint({ kind: 'seek-hint', nonce, email: t.email, campaignId: t.id, credits }, 15 * 60000);
const posted = await seekCallback(t.cb, receipt);