Downline Banners (Marty 2026-11-13: keep the name): renamed everywhere members see it, new stacked-banner logo + favicon; network sign-in (arrive signed in from InstantAdPay/LinkSpin as your wallet); share links, sponsor and directs use network usernames (?ref=username resolves through InstantAdPay)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Banner Line web app (downlinebanners.com). Plain Node http server, no framework (InstantAdPay pattern).
|
||||
// Downline Banners web app (downlinebanners.com). Plain Node http server, no framework (InstantAdPay pattern).
|
||||
// Pages: / (gate, then board + join), /board, /join (buy), /my (My Spots + dashboard), /how.
|
||||
// Chain index: chain.js. Board/rotation: board.js. Creative + link checks: safety.js. Keeper: keeper.js.
|
||||
const http = require('http');
|
||||
@@ -13,6 +13,7 @@ const board = require('./board');
|
||||
const safety = require('./safety');
|
||||
const keeper = require('./keeper');
|
||||
const { iface } = require('./abi');
|
||||
const networksso = require('./networksso'); // network sign-in: arrive signed in from InstantAdPay or LinkSpin (2026-11-13), see networksso.js
|
||||
const SEL = { buy: iface.getFunction('buy').selector, buyFor: iface.getFunction('buyFor').selector }; // the browser builds calldata from these (ABI stays in abi.js)
|
||||
|
||||
const C = cfgMod.get();
|
||||
@@ -72,18 +73,29 @@ function limited(req, name, max, windowMs) {
|
||||
return ++b.n > max;
|
||||
}
|
||||
|
||||
// ---- sponsor ref (?ref=<address or InstantAdPay username>) ----
|
||||
const refCache = new Map();
|
||||
// ---- network usernames (Marty 2026-11-13: share links use the member's network username, not the wallet) ----
|
||||
// One lookup for both directions. Today the usernames live in InstantAdPay (GET /api/network/member ?u= / ?a=); when the
|
||||
// network hub goes live this is the one place to point at it. Cached an hour, misses too.
|
||||
const netCache = new Map();
|
||||
async function netMember(q) {
|
||||
const k = q.u ? 'u:' + q.u.toLowerCase() : 'a:' + q.a.toLowerCase();
|
||||
const hit = netCache.get(k); if (hit && Date.now() - hit.at < 3600000) return hit.m;
|
||||
let m = null;
|
||||
try { const ac = new AbortController(); setTimeout(() => ac.abort(), 4000).unref();
|
||||
const qs = q.u ? 'u=' + encodeURIComponent(q.u) : 'a=' + encodeURIComponent(q.a);
|
||||
const j = await (await fetch(C.networkLookupUrl + '?' + qs, { signal: ac.signal })).json();
|
||||
if (j && ADDR_RE.test(j.address || '') && j.username) m = { address: j.address.toLowerCase(), username: String(j.username) }; } catch (e) { return null; } // a failed call is not cached
|
||||
netCache.set(k, { at: Date.now(), m }); if (netCache.size > 20000) netCache.clear();
|
||||
if (m) netCache.set('a:' + m.address, { at: Date.now(), m });
|
||||
return m;
|
||||
}
|
||||
const nameOf = async addr => { const m = await netMember({ a: addr }); return m && m.address === addr.toLowerCase() ? m.username : null; };
|
||||
// ---- sponsor ref (?ref=<address or network username>) ----
|
||||
async function resolveRef(raw) {
|
||||
const r = String(raw || '').trim();
|
||||
if (ADDR_RE.test(r)) return r.toLowerCase();
|
||||
if (!/^[a-zA-Z0-9_.-]{2,32}$/.test(r) || !C.iapResolveUrl) return null;
|
||||
const hit = refCache.get(r.toLowerCase()); if (hit && Date.now() - hit.at < 3600000) return hit.a;
|
||||
let a = null;
|
||||
try { const ac = new AbortController(); setTimeout(() => ac.abort(), 4000).unref();
|
||||
const j = await (await fetch(C.iapResolveUrl + encodeURIComponent(r), { signal: ac.signal })).json();
|
||||
if (j && ADDR_RE.test(j.address || '')) a = j.address.toLowerCase(); } catch (e) {}
|
||||
refCache.set(r.toLowerCase(), { at: Date.now(), a }); return a;
|
||||
if (!/^[a-zA-Z0-9_]{1,20}$/.test(r) || !C.networkLookupUrl) return null;
|
||||
const m = await netMember({ u: r }); return m ? m.address : null;
|
||||
}
|
||||
const sponsorOf = req => { const v = cookies(req)['bl.ref']; return ADDR_RE.test(v || '') ? v.toLowerCase() : null; };
|
||||
|
||||
@@ -132,18 +144,29 @@ function spotView(s) {
|
||||
clicks: store.clicks(s.id), daysLeft: left, delivering: s.kind !== 3 && left > 0 && v.total < C.viewsOwed,
|
||||
img: m.img || '', link: m.link || '', showing: cr };
|
||||
}
|
||||
function myData(addr) {
|
||||
async function myData(addr) {
|
||||
const a = addr.toLowerCase();
|
||||
const mine = chain.spotsOf(a).map(spotView);
|
||||
const w = chain.walletTotals(a);
|
||||
const dir = chain.directs(a).map(m => { const ss = chain.spotsOf(m.address); return { address: m.address, joinedAt: m.joinedAt, spots: ss.length, paidSpots: ss.filter(x => x.kind === 0).length,
|
||||
paidInWei: ss.filter(x => x.kind === 0 || x.kind === 1).reduce((t, x) => t + BigInt(x.valueWei), 0n).toString() }; });
|
||||
const me = chain.member(a);
|
||||
return { address: a, member: chain.isMember(a), sponsor: me ? me.sponsor : null, spots: mine, paidInWei: w.inWei, paidOutWei: w.outWei, byRole: w.byRole,
|
||||
payouts: chain.paidOf(a).slice(0, 50), directs: dir, defaults: store.memberDefault(a) || {}, share: 'https://' + C.site + '/?ref=' + a };
|
||||
const [username, sponsorName] = await Promise.all([nameOf(a), me && me.sponsor ? nameOf(me.sponsor) : null]);
|
||||
await Promise.all(dir.map(async d => { d.username = await nameOf(d.address); }));
|
||||
return { address: a, username, sponsorName, member: chain.isMember(a), sponsor: me ? me.sponsor : null, spots: mine, paidInWei: w.inWei, paidOutWei: w.outWei, byRole: w.byRole,
|
||||
payouts: chain.paidOf(a).slice(0, 50), directs: dir, defaults: store.memberDefault(a) || {}, share: 'https://' + C.site + '/?ref=' + (username || a) };
|
||||
}
|
||||
|
||||
// ---- server ----
|
||||
// Downline Banners only RECEIVES network sign-ins: a member signed in on InstantAdPay (or LinkSpin) arrives signed in as the wallet that
|
||||
// site vouches for (the one they signed in with there, else the main wallet their account linked by signature). No email here.
|
||||
networksso.init({ self: 'bannerline', peer: 'iap', peerBase: () => C.iapBase, from: { iap: () => C.iapBase, linkspin: () => C.lsBase },
|
||||
isProd: C.isProd, dataDir: C.dataDir, defaultPath: '/my',
|
||||
member: async req => { const s = session(req); return s ? { address: s.address } : null; },
|
||||
accept: async (email, req, v) => (v && v.wallet ? { ok: true } : { fallback: 'no wallet in the token' }),
|
||||
signIn: async (email, req, v) => { if (v.name) netCache.set('a:' + v.wallet, { at: Date.now(), m: { address: v.wallet, username: v.name } }); return cookie('bl.sid', store.mintSession(v.wallet), store.SESSION_TTL / 1000, true); },
|
||||
landing: to => to, signinPath: to => to });
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const u = new URL(req.url, 'http://x'); const p = u.pathname;
|
||||
try {
|
||||
@@ -151,6 +174,9 @@ const server = http.createServer(async (req, res) => {
|
||||
const ck = cookies(req); const setVid = !ck['bl.vid'] && !isBot(req) ? cookie('bl.vid', crypto.randomBytes(9).toString('hex'), 365 * 86400, true) : null;
|
||||
const extra = setVid ? { 'Set-Cookie': setVid } : {};
|
||||
|
||||
// -- network sign-in (2026-11-13): /sso/start, /sso/in, /api/sso/status
|
||||
if (req.method === 'GET' && (p.startsWith('/sso/') || p === '/api/sso/status') && await networksso.handle(req, res, u)) return;
|
||||
|
||||
// -- pages
|
||||
if (req.method === 'GET' || req.method === 'HEAD') {
|
||||
const PAGES = { '/': 'index.html', '/board': 'board.html', '/join': 'join.html', '/my': 'my.html', '/how': 'how.html' };
|
||||
@@ -163,7 +189,7 @@ const server = http.createServer(async (req, res) => {
|
||||
return sendFile(res, path.join(PUB, PAGES[p]), ex);
|
||||
}
|
||||
if (/^\/r\/[^/]+$/.test(p)) { res.writeHead(302, { Location: '/?ref=' + encodeURIComponent(decodeURIComponent(p.slice(3))) }); return res.end(); }
|
||||
if (p.startsWith('/assets/') || p.startsWith('/art/house-') || p === '/logo-icon.png' || p === '/robots.txt') {
|
||||
if (p.startsWith('/assets/') || p.startsWith('/art/house-') || p === '/art/mark.svg' || p === '/logo-icon.png' || p === '/robots.txt') {
|
||||
const f = path.normalize(path.join(PUB, p)); if (!f.startsWith(PUB)) { res.writeHead(400); return res.end(); }
|
||||
return sendFile(res, f);
|
||||
}
|
||||
@@ -252,7 +278,7 @@ const server = http.createServer(async (req, res) => {
|
||||
if (limited(req, 'auth', 30, 600000)) return json(res, 429, { error: 'Too many sign-in attempts. Wait a few minutes.' });
|
||||
const b = await readBody(req); if (!ADDR_RE.test(b.address || '')) return json(res, 400, { error: 'Bad address' });
|
||||
const a = b.address.toLowerCase(); const nonce = crypto.randomBytes(16).toString('hex');
|
||||
const message = C.site + ' wants you to sign in with your wallet:\n' + getAddress(a) + '\n\nBanner Line sign-in. This signature is free and cannot move funds or approve anything.\n\nChain ID: ' + C.chainId + '\nNonce: ' + nonce + '\nIssued At: ' + new Date().toISOString();
|
||||
const message = C.site + ' wants you to sign in with your wallet:\n' + getAddress(a) + '\n\nDownline Banners sign-in. This signature is free and cannot move funds or approve anything.\n\nChain ID: ' + C.chainId + '\nNonce: ' + nonce + '\nIssued At: ' + new Date().toISOString();
|
||||
challenges.set(a, { message, exp: Date.now() + 600000 });
|
||||
if (challenges.size > 20000) challenges.clear();
|
||||
return json(res, 200, { message });
|
||||
@@ -268,7 +294,7 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
if (p === '/api/auth/logout' && req.method === 'POST') {
|
||||
const s = session(req); if (s) store.dropSession(s.token);
|
||||
return json(res, 200, { ok: true }, { 'Set-Cookie': cookie('bl.sid', '', 0, true) });
|
||||
return json(res, 200, { ok: true }, { 'Set-Cookie': [cookie('bl.sid', '', 0, true), networksso.offCookie()] }); // signed out stays signed out here for 12 hours
|
||||
}
|
||||
if (p === '/api/me' && req.method === 'GET') {
|
||||
const s = session(req); if (!s) return json(res, 200, { signedIn: false, sponsor: sponsorOf(req) });
|
||||
@@ -280,7 +306,7 @@ const server = http.createServer(async (req, res) => {
|
||||
if (p.startsWith('/api/my')) {
|
||||
const s = session(req); if (!s) return json(res, 401, { error: 'Sign in with your wallet first.' });
|
||||
const me = s.address;
|
||||
if (p === '/api/my' && req.method === 'GET') return json(res, 200, myData(me));
|
||||
if (p === '/api/my' && req.method === 'GET') return json(res, 200, await myData(me));
|
||||
let m = /^\/api\/my\/spot\/(\d+)$/.exec(p);
|
||||
if (m && req.method === 'POST') {
|
||||
const sp = chain.spot(m[1]); if (!sp || sp.owner !== me) return json(res, 403, { error: 'That spot is not yours.' });
|
||||
@@ -319,13 +345,13 @@ const server = http.createServer(async (req, res) => {
|
||||
|
||||
if (p.startsWith('/api/')) return json(res, 404, { error: 'Not found' });
|
||||
res.writeHead(404, headers({ 'Content-Type': 'text/html; charset=utf-8' }));
|
||||
return res.end('<!doctype html><title>Not found</title><p style="font-family:sans-serif">Not found. <a href="/">Banner Line home</a></p>');
|
||||
return res.end('<!doctype html><title>Not found</title><p style="font-family:sans-serif">Not found. <a href="/">Downline Banners home</a></p>');
|
||||
} catch (e) {
|
||||
console.error('request', p, e.message);
|
||||
if (!res.headersSent) json(res, 500, { error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(C.port, () => console.log('Banner Line on :' + C.port + ' chain ' + C.chainName + ' (' + C.chainId + ') contract ' + (C.contract || '(not set)')));
|
||||
server.listen(C.port, () => console.log('Downline Banners on :' + C.port + ' chain ' + C.chainName + ' (' + C.chainId + ') contract ' + (C.contract || '(not set)')));
|
||||
process.on('SIGTERM', () => { store.flush(); process.exit(0); });
|
||||
process.on('SIGINT', () => { store.flush(); process.exit(0); });
|
||||
|
||||
Reference in New Issue
Block a user