// Node 20+: free previews and unsigned challenge only; never sends funds. const BASE='https://paidhandshake.com'; export async function get(path, query={}) { const url=new URL(path,BASE); if(url.origin!==BASE) throw Error('unexpected host'); Object.entries(query).forEach(([k,v])=>url.searchParams.set(k,String(v))); const r=await fetch(url,{redirect:'error',signal:AbortSignal.timeout(20000)}); if(!r.ok) throw Error(`HTTP ${r.status}`); return r.json(); } export const searchRfp=(q,state='')=>get('/api/v1/rfp/preview',{q,state}); export const localIntel=(zip,radius=25)=>get('/api/v1/local-intel/preview',{zip,radius}); export const searchBusinesses=(q,area='')=>get('/api/v1/business/search',{q,area}); export async function unsignedChallenge(path,maximumCents,expectedReceiver) { if(!path.startsWith('/api/x402/v1/catalog/')||path.includes('?'))throw Error('fixed resource required'); const url=BASE+path,r=await fetch(url,{redirect:'error',signal:AbortSignal.timeout(20000)}); if(r.status!==402)throw Error(`Expected HTTP 402; got ${r.status}`); const c=JSON.parse(Buffer.from(r.headers.get('payment-required')||'','base64url').toString('utf8')),a=c.accepts[0]; if(c.x402Version!==2||a.scheme!=='exact'||a.payTo.toLowerCase()!==expectedReceiver.toLowerCase()||BigInt(a.amount)<=0n||c.resource.url!==url||a.network!=='eip155:8453'||a.asset.toLowerCase()!=='0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'||BigInt(a.amount)>BigInt(maximumCents)*10000n)throw Error('unexpected payment terms'); return c; // use your separately authorized x402 client only after reviewing terms }