VBBShop hooks (inert until configured, nothing deployed): GET /api/my/voucher signs an EIP-712 sponsor voucher with KEEPER_KEY when the hub lookup names the member's network sponsor (new optional sponsor field; none or self = no voucher); shop.js watcher reads Purchase logs (CONTRACT_ADDRESS + CHAIN_RPC, confirmed blocks, 1,000-block ranges) and credits packs once per txHash:logIndex, dedupe marks saved in vbb.json with the credits; admin status shows the watcher, never the key. Packages page stays coming soon. Smoke 117/117 (fake chain + voucher checks)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+62
-8
@@ -1,8 +1,9 @@
|
||||
// VBB local smoke test. Boots the app in prototype mode (no DOCSTORE_URL: files in a throwaway data dir under qa/out),
|
||||
// with a tiny loop (5 places, 3 visitors per shift) so shifts and cycles happen in seconds, then checks the splash,
|
||||
// verified visitors (bots, early, repeats, the per-IP cap), shifts + a cycle + the reward ledger, posting banners
|
||||
// (upload, checks, credits), network sign-in, the leaderboard, the admin endpoint, a restart, and page renders.
|
||||
// Nothing leaves this machine: the network lookup is off, the link probe is off, no real wallet, no money.
|
||||
// (upload, checks, credits), network sign-in, the leaderboard, the admin endpoint, a restart, the VBBShop hooks (sponsor vouchers,
|
||||
// purchase crediting from a fake chain), and page renders.
|
||||
// Nothing leaves this machine: the network lookup and the chain are local stand-ins, the link probe is off, no real wallet, no money.
|
||||
//
|
||||
// node qa/smoke.mjs (Playwright renders too, if it is installed at PLAYWRIGHT)
|
||||
// SKIP_UI=1 node qa/smoke.mjs (HTTP checks only)
|
||||
@@ -23,12 +24,22 @@ const PW = process.env.PLAYWRIGHT || 'D:/Projects/MarketingAgent/qa-tester/node_
|
||||
const PORT = Number(process.env.SMOKE_APP_PORT || 3217), BASE = 'http://127.0.0.1:' + PORT;
|
||||
const ADMIN = 'smoke-admin-token', SSO_SECRET = crypto.randomBytes(32).toString('hex');
|
||||
// stand-in for the network member lookup: wallets in NET are network members, everyone else is a stranger
|
||||
const NET = new Set(); const LOOKUP_PORT = PORT + 1;
|
||||
// NETSP: wallet -> that member's network sponsor wallet (the hub's `sponsor` field)
|
||||
const NET = new Set(); const NETSP = new Map(); const LOOKUP_PORT = PORT + 1;
|
||||
require('node:http').createServer((q, r) => { const a = (new URL(q.url, 'http://x').searchParams.get('a') || '').toLowerCase();
|
||||
r.setHeader('Content-Type', 'application/json'); r.end(JSON.stringify(NET.has(a) ? { address: a, username: 'net' + a.slice(2, 10) } : {})); }).listen(LOOKUP_PORT, '127.0.0.1').unref();
|
||||
r.setHeader('Content-Type', 'application/json'); r.end(JSON.stringify(NET.has(a) ? { address: a, username: 'net' + a.slice(2, 10), sponsor: NETSP.get(a) || null } : {})); }).listen(LOOKUP_PORT, '127.0.0.1').unref();
|
||||
// stand-in Polygon JSON-RPC for the purchase watcher: CHAIN.logs are served by block range (or all of them when replayAll is on,
|
||||
// like a flaky RPC re-sending old logs). Nothing here is a real chain.
|
||||
const CHAIN = { head: 1, logs: [], replayAll: false }; const CHAIN_PORT = PORT + 2;
|
||||
const SHOP = '0x00000000000000000000000000000000000B0B05', KEEPER_PK = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; // anvil's public test key #1
|
||||
require('node:http').createServer(async (q, r) => { let b = ''; for await (const c of q) b += c; const j = JSON.parse(b || '{}'); let result = null;
|
||||
if (j.method === 'eth_blockNumber') result = '0x' + CHAIN.head.toString(16);
|
||||
if (j.method === 'eth_getLogs') { const f = j.params[0], lo = parseInt(f.fromBlock, 16), hi = parseInt(f.toBlock, 16); result = CHAIN.logs.filter(l => CHAIN.replayAll || (parseInt(l.blockNumber, 16) >= lo && parseInt(l.blockNumber, 16) <= hi)); }
|
||||
r.setHeader('Content-Type', 'application/json'); r.end(JSON.stringify({ jsonrpc: '2.0', id: j.id, result })); }).listen(CHAIN_PORT, '127.0.0.1').unref();
|
||||
const ENV = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, ADMIN_TOKEN: ADMIN, NODE_ENV: 'development', DOCSTORE_URL: '',
|
||||
LOOP_SIZE: '5', VISITORS_PER_SHIFT: '3', VIEWS_PER_CYCLE: '150', VISIT_SECONDS: '2', VISITS_PER_IP_DAY: '2', LINK_PROBE: '0', NETWORK_LOOKUP_URL: 'http://127.0.0.1:' + (PORT + 1) + '/api/network/member',
|
||||
NETWORK_SSO_SECRET: SSO_SECRET, IAP_URL: 'https://iap.invalid', LS_URL: 'https://ls.invalid' });
|
||||
NETWORK_SSO_SECRET: SSO_SECRET, IAP_URL: 'https://iap.invalid', LS_URL: 'https://ls.invalid',
|
||||
CONTRACT_ADDRESS: SHOP, CHAIN_RPC: 'http://127.0.0.1:' + CHAIN_PORT, KEEPER_KEY: KEEPER_PK, SHOP_START_BLOCK: '1', SHOP_CONFIRMATIONS: '0', SHOP_POLL_MS: '200' });
|
||||
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
||||
let pass = 0, fail = 0; let srv = null;
|
||||
const ok = (cond, what, extra) => { if (cond) { pass++; console.log(' ok ' + what); } else { fail++; console.log(' FAIL ' + what + (extra !== undefined ? ' -> ' + JSON.stringify(extra).slice(0, 400) : '')); } };
|
||||
@@ -69,6 +80,7 @@ async function stop() { const p = srv; srv = null; if (!p) return; const done =
|
||||
// a signed-in member by wallet signature
|
||||
async function walletMember(o = {}) {
|
||||
const w = ethers.Wallet.createRandom(); const c = client(); if (!o.stranger) NET.add(w.address.toLowerCase());
|
||||
if (o.sponsor) NETSP.set(w.address.toLowerCase(), o.sponsor === 'self' ? w.address.toLowerCase() : o.sponsor.toLowerCase());
|
||||
const ch = await c.json('/api/auth/challenge', { address: w.address });
|
||||
const v = await c.json('/api/auth/verify', { address: w.address, signature: await w.signMessage(ch.message) });
|
||||
c.address = w.address.toLowerCase(); c.ok = v.ok === true && !!c.jar['vbb.sid'];
|
||||
@@ -246,9 +258,51 @@ async function main() {
|
||||
ok(safety.frameVerdict({}) === true && safety.frameVerdict({ 'x-frame-options': 'DENY' }) === false && safety.frameVerdict({ 'content-security-policy': "frame-ancestors 'self'" }) === false && safety.frameVerdict({ 'content-security-policy': 'frame-ancestors *' }) === true, 'frameVerdict reads X-Frame-Options and frame-ancestors');
|
||||
ok(safety.privateIp('10.1.2.3') && safety.privateIp('127.0.0.1') && safety.privateIp('::1') && safety.privateIp('169.254.1.1') && !safety.privateIp('8.8.8.8'), 'private address check');
|
||||
|
||||
// ---- 12. page renders (Playwright, if installed) ----
|
||||
console.log('12. VBBShop hooks: sponsor vouchers + purchase crediting (fake chain)');
|
||||
const shopUnit = require(path.join(APP, 'shop.js')); shopUnit.init({ contractAddress: '', chainRpc: '', chainId: 137 });
|
||||
ok(!shopUnit.watching() && !shopUnit.voucherOn(), 'without CONTRACT_ADDRESS / CHAIN_RPC the watcher is off; without the key, vouchers are off');
|
||||
const keeperAddr = new ethers.Wallet(KEEPER_PK).address;
|
||||
const spW = ethers.Wallet.createRandom().address.toLowerCase();
|
||||
const V = await walletMember({ sponsor: spW });
|
||||
let vo = await V.json('/api/my/voucher');
|
||||
const vt = { Voucher: [{ name: 'buyer', type: 'address' }, { name: 'sponsor', type: 'address' }, { name: 'deadline', type: 'uint256' }] };
|
||||
const vdom = { name: 'VBBShop', version: '1', chainId: 137, verifyingContract: ethers.getAddress(SHOP) };
|
||||
ok(vo.voucher && vo.voucher.buyer.toLowerCase() === V.address && vo.voucher.sponsor.toLowerCase() === spW && vo.voucher.contract === ethers.getAddress(SHOP) && vo.voucher.chainId === 137, 'voucher names the member, their network sponsor, the contract and the chain', vo);
|
||||
ok(vo.voucher && ethers.verifyTypedData(vdom, vt, { buyer: vo.voucher.buyer, sponsor: vo.voucher.sponsor, deadline: vo.voucher.deadline }, vo.voucher.sig) === keeperAddr, 'voucher is an EIP-712 signature by the keeper key');
|
||||
const nowS = Math.floor(Date.now() / 1000);
|
||||
ok(vo.voucher && vo.voucher.deadline > nowS + 1700 && vo.voucher.deadline <= nowS + 1801, 'voucher is good for 30 minutes', vo.voucher && vo.voucher.deadline - nowS);
|
||||
ok(!JSON.stringify(vo).includes(KEEPER_PK.slice(2, 12)), 'the key never appears in the answer');
|
||||
vo = await A.json('/api/my/voucher'); ok(vo._status === 200 && vo.voucher === null && vo.why === 'no-sponsor', 'no network sponsor known: no voucher', vo);
|
||||
const SELF = await walletMember({ sponsor: 'self' }); vo = await SELF.json('/api/my/voucher'); ok(vo.voucher === null && vo.why === 'no-sponsor', 'the hub naming the member as their own sponsor: no voucher', vo);
|
||||
vo = await X.json('/api/my/voucher'); ok(vo.voucher === null, 'a wallet the network does not know: no voucher', vo);
|
||||
r = await anon.req('/api/my/voucher'); ok(r.status === 401, 'voucher needs sign-in');
|
||||
// a Purchase log for V: 2 x pack10 = 20 credits
|
||||
const SHOP_IF = new ethers.Interface(['event Purchase(uint256 indexed orderId, address indexed buyer, uint256 indexed sku, uint256 qty, uint256 ref, uint256 credits, uint256 usdCents, uint256 weiPaid, address sponsor, uint256 sponsorPaid)']);
|
||||
const mkLog = o => { const e = SHOP_IF.encodeEventLog('Purchase', [o.orderId, o.buyer, o.sku, o.qty, o.ref || 0, o.credits, o.usdCents, o.wei, o.sponsor || ethers.ZeroAddress, o.sponsorPaid || 0]);
|
||||
return { address: o.address || SHOP, topics: e.topics, data: e.data, blockNumber: '0x' + o.block.toString(16), transactionHash: o.tx, logIndex: '0x' + o.logIndex.toString(16), removed: false }; };
|
||||
const credits0 = (await V.json('/api/my')).credits;
|
||||
const tx1 = '0x' + 'ab'.repeat(32);
|
||||
CHAIN.logs.push(mkLog({ orderId: 1, buyer: V.address, sku: 1, qty: 2, credits: 20, usdCents: 600, wei: 70000000000000000000n, sponsor: spW, sponsorPaid: 21000000000000000000n, block: 3, tx: tx1, logIndex: 4 }));
|
||||
CHAIN.head = 5; await sleep(900);
|
||||
my = await V.json('/api/my'); ok(my.credits === credits0 + 20, 'a Purchase event credits the pack (2 x 10 credits)', { before: credits0, after: my.credits });
|
||||
// the same log again: in a later block range, and re-sent on every poll
|
||||
CHAIN.logs.push(Object.assign({}, CHAIN.logs[0], { blockNumber: '0x7' })); CHAIN.replayAll = true; CHAIN.head = 9; await sleep(900);
|
||||
my = await V.json('/api/my'); ok(my.credits === credits0 + 20, 'the same txHash:logIndex delivered again credits nothing', my.credits);
|
||||
// a log from another contract address is ignored; a second real order credits
|
||||
CHAIN.logs.push(mkLog({ address: '0x000000000000000000000000000000000000dEaD', orderId: 9, buyer: V.address, sku: 2, qty: 1, credits: 40, usdCents: 1000, wei: 1n, block: 10, tx: '0x' + 'cd'.repeat(32), logIndex: 0 }));
|
||||
CHAIN.logs.push(mkLog({ orderId: 2, buyer: V.address, sku: 2, qty: 1, credits: 40, usdCents: 1000, wei: 118000000000000000000n, sponsor: spW, sponsorPaid: 35400000000000000000n, block: 10, tx: tx1, logIndex: 9 }));
|
||||
CHAIN.head = 12; await sleep(900);
|
||||
my = await V.json('/api/my'); ok(my.credits === credits0 + 60, 'another contract log is ignored; a second order (same tx, other log index) credits 40', my.credits);
|
||||
const ads = await anon.json('/api/admin/status', undefined, { 'x-admin-token': ADMIN });
|
||||
ok(ads.shop && ads.shop.watching && ads.shop.vouchers && ads.shop.purchases === 2 && ads.shop.creditsSold === 60 && ads.shop.dupes >= 2 && ads.shop.lastBlock === 12 && ads.shop.keeper === keeperAddr && !JSON.stringify(ads).includes(KEEPER_PK.slice(2, 12)), 'admin sees the watcher: 2 purchases, 60 credits, duplicates skipped, no key', ads.shop);
|
||||
// restart: the dedupe marks survive, the replaying RPC still credits nothing
|
||||
await sleep(1200); await stop(); await boot(); await sleep(900);
|
||||
my = await V.json('/api/my'); ok(my.credits === credits0 + 60, 'after a restart the replayed logs still credit nothing', my.credits);
|
||||
const cfgNow = await (await fetch(BASE + '/api/config')).json(); ok(!JSON.stringify(cfgNow).toLowerCase().includes(SHOP.slice(2).toLowerCase()), 'public config does not offer the contract yet (packages stay coming soon)');
|
||||
|
||||
// ---- 13. page renders (Playwright, if installed) ----
|
||||
if (!process.env.SKIP_UI && fs.existsSync(PW)) {
|
||||
console.log('12. page renders (Playwright)');
|
||||
console.log('13. page renders (Playwright)');
|
||||
const { chromium } = require(PW);
|
||||
const browser = await chromium.launch();
|
||||
const errs = [];
|
||||
@@ -284,7 +338,7 @@ async function main() {
|
||||
}
|
||||
ok(errs.length === 0, 'no page errors', errs);
|
||||
await browser.close();
|
||||
} else console.log('12. page renders skipped (SKIP_UI or no Playwright at ' + PW + ')');
|
||||
} else console.log('13. page renders skipped (SKIP_UI or no Playwright at ' + PW + ')');
|
||||
|
||||
await stop();
|
||||
console.log('\n' + pass + ' passed, ' + fail + ' failed');
|
||||
|
||||
Reference in New Issue
Block a user