askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
16 hours ago
History
cli.ts · 2.4 KB · 66 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * `x402-proxy` — run the payment proxy on 127.0.0.1:8402. |
| 4 | * |
| 5 | * Uses a throwaway test key by default; wire a real signer (session hot wallet) once |
| 6 | * that task lands. Prints where to trust the root CA so https interception works. |
| 7 | */ |
| 8 | |
| 9 | import { join } from "node:path"; |
| 10 | import { homedir } from "node:os"; |
| 11 | import { privateKeyToAccount } from "viem/accounts"; |
| 12 | import { PolicyEngine, Ledger, X402Engine, formatUsd } from "@dbbasic/x402-engine"; |
| 13 | import { X402Proxy } from "./server.js"; |
| 14 | |
| 15 | const PORT = Number(process.env.X402_PROXY_PORT) || 8402; |
| 16 | |
| 17 | // TEST KEY ONLY. Never fund this. Replaced by the session hot wallet task. |
| 18 | const TEST_KEY = (process.env.X402_PRIVATE_KEY ?? |
| 19 | "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d") as `0x${string}`; |
| 20 | |
| 21 | const account = privateKeyToAccount(TEST_KEY); |
| 22 | const policy = new PolicyEngine(); |
| 23 | const ledger = new Ledger(); |
| 24 | |
| 25 | const engine = new X402Engine({ |
| 26 | account, |
| 27 | policy, |
| 28 | ledger, |
| 29 | approve: async (req) => { |
| 30 | // Headless: anything needing a prompt is refused. The browser wires a real UI here. |
| 31 | console.log(`[x402] prompt required, refusing (headless): ${req.reason}`); |
| 32 | return false; |
| 33 | }, |
| 34 | onEvent: (e) => { |
| 35 | if (e.type === "paid") { |
| 36 | const r = e.receipt; |
| 37 | console.log(`[x402] paid ${formatUsd(r.priceUsdMicro)} ${r.assetSymbol} -> ${r.destOrigin} (${r.status}${r.txHash ? " " + r.txHash : ""})`); |
| 38 | } else if (e.type === "skipped") { |
| 39 | console.log(`[x402] skipped ${e.resourceUrl}: ${e.reason}`); |
| 40 | } |
| 41 | }, |
| 42 | }); |
| 43 | |
| 44 | const proxy = new X402Proxy({ |
| 45 | engine, |
| 46 | onError: (err, url) => console.error(`[x402] error ${url}: ${String(err)}`), |
| 47 | }); |
| 48 | |
| 49 | const caPath = join(homedir(), ".dbbasic-browser", "ca-cert.pem"); |
| 50 | |
| 51 | proxy.listen(PORT).then(({ host, port }) => { |
| 52 | console.log(`x402 proxy on http://${host}:${port}`); |
| 53 | console.log(`signer: ${account.address} (TEST KEY — do not fund)`); |
| 54 | console.log(`spent this window: ${formatUsd(policy.spentUsdMicro())}`); |
| 55 | console.log(""); |
| 56 | console.log("To intercept https, trust the root CA once:"); |
| 57 | console.log(` macOS: sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ${caPath}`); |
| 58 | console.log("Then point a client at the proxy:"); |
| 59 | console.log(` curl -x http://${host}:${port} --cacert ${caPath} https://<x402-url>`); |
| 60 | }); |
| 61 | |
| 62 | process.on("SIGINT", async () => { |
| 63 | console.log(`\nfinal spend: ${formatUsd(policy.spentUsdMicro())}, receipts: ${ledger.all().length}`); |
| 64 | await proxy.close(); |
| 65 | process.exit(0); |
| 66 | }); |