x402-credits / tree / scripts/self-pay-sample.mjs ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// ONE SELF indexing payment (CMM rule): buys /v1/pack/sample ($0.005) on Base from the swarm test payer, so CDP Bazaar
// indexes the credits service. Refuses to run twice (megaphone/paytest/attempts.json "credits"). The minted key goes
// to /home/box/crypto-vault/credits-sample-key.txt (chmod 600) and is never printed; the ledger stores it redacted.
import fs from 'fs';
import { x402Client, x402HTTPClient } from '@x402/core/client';
import { ExactEvmScheme } from '@x402/evm/exact/client';
import { privateKeyToAccount } from 'viem/accounts';
const URL_ = 'https://api.youraitools.co/credits/v1/pack/sample';
const PAYTO = '0x3c1a1e01e790cd326495631b3b38bd7068f08d6f'; const USDC = '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'; const PRICE = 5000n;
const LEDGER = '/workspace/crypto/megaphone/paytest/attempts.json';
const ledger = JSON.parse(fs.readFileSync(LEDGER));
if (ledger.credits) { console.log('REFUSING: already attempted credits'); process.exit(1); }
const pk = fs.readFileSync('/home/box/crypto-vault/payer-wallet.txt', 'utf8').match(/private_key = (0x[0-9a-fA-F]{64})/)[1];
const client = new x402Client(); client.register('eip155:8453', new ExactEvmScheme(privateKeyToAccount(pk)));
const http = new x402HTTPClient(client);
const r1 = await fetch(URL_, { method: 'POST' });
if (r1.status !== 402) { console.log('expected 402, got', r1.status); process.exit(1); }
let body; try { body = await r1.json(); } catch {}
const pr = http.getPaymentRequiredResponse((n) => r1.headers.get(n), body);
const req = pr.accepts.find((a) => a.scheme === 'exact' && a.network === 'eip155:8453' && a.asset.toLowerCase() === USDC && a.payTo.toLowerCase() === PAYTO && BigInt(a.amount) === PRICE);
if (!req) { console.log('SKIP: no matching requirement'); process.exit(1); }
console.log('checked: credits', 'v' + pr.x402Version, req.network, 'amount', req.amount, 'payTo', req.payTo);
const payload = await client.createPaymentPayload({ ...pr, accepts: [req] });
ledger.credits = { at: new Date().toISOString(), sent: true, note: 'SELF indexing payment (CMM rule), credits /v1/pack/sample $0.005' }; fs.writeFileSync(LEDGER, JSON.stringify(ledger, null, 2));
const r2 = await fetch(URL_, { method: 'POST', headers: http.encodePaymentSignatureHeader(payload) });
const text = await r2.text();
let settle = null; try { settle = http.getPaymentSettleResponse((n) => r2.headers.get(n)); } catch (e) { settle = { error: String(e.message || e) }; }
let j = null; try { j = JSON.parse(text); } catch {}
if (j?.api_key) { fs.writeFileSync('/home/box/crypto-vault/credits-sample-key.txt', `api_key=${j.api_key}\n`, { mode: 0o600 }); j.api_key = '[redacted: vault credits-sample-key.txt]'; }
const out = { tool: 'credits', url: URL_, status: r2.status, settle, balance_usd: j?.balance_usd, snippet: JSON.stringify(j || text).replace(/yatc_[A-Za-z0-9_-]{43}/g, 'yatc_[redacted]').slice(0, 300) };
ledger.credits = { ...ledger.credits, ...out }; fs.writeFileSync(LEDGER, JSON.stringify(ledger, null, 2));
console.log(JSON.stringify(out, null, 2));

agentgate-source: code mirror on Cloudflare Pages. All repos