A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
ledger.ts · 3.2 KB · 109 lines
1/**
2 * Receipt ledger.
3 *
4 * Every signed authorization is recorded BEFORE it goes out, because the moment we
5 * hand over a signature the money is potentially gone — whether or not the server
6 * ever gives us the resource. Settlement (and delivery) is reconciled afterwards, so
7 * a payment that was signed but never settled, or settled but never delivered, stays
8 * visible instead of vanishing.
9 */
10
11import type { Eip3009Authorization, Quote, SettlementResponse } from "./types.js";
12
13export type ReceiptStatus = "signed" | "settled" | "delivered" | "failed";
14
15export interface Receipt {
16 id: string;
17 at: number;
18 topOrigin: string;
19 destOrigin: string;
20 /** The URL the server's 402 claimed as the resource (challenge.resource.url). */
21 resourceUrl: string;
22 /** The URL we actually requested. Can differ from resourceUrl — worth surfacing. */
23 requestedUrl: string;
24 priceUsdMicro: bigint;
25 assetSymbol: string;
26 network: string;
27 payTo: string;
28 nonce: string;
29 validBefore: number;
30 /** The full signed authorization and its signature, for inspection. */
31 authorization?: Eip3009Authorization;
32 signature?: string;
33 status: ReceiptStatus;
34 txHash?: string;
35 error?: string;
36}
37
38export class Ledger {
39 private receipts: Receipt[] = [];
40 private seq = 0;
41
42 constructor(private now: () => number = Date.now) {}
43
44 open(args: {
45 quote: Quote;
46 topOrigin: string;
47 destOrigin: string;
48 resourceUrl: string;
49 requestedUrl: string;
50 authorization: Eip3009Authorization;
51 signature: string;
52 }): Receipt {
53 const r: Receipt = {
54 id: `rcpt_${++this.seq}`,
55 at: this.now(),
56 topOrigin: args.topOrigin,
57 destOrigin: args.destOrigin,
58 resourceUrl: args.resourceUrl,
59 requestedUrl: args.requestedUrl,
60 priceUsdMicro: args.quote.priceUsdMicro,
61 assetSymbol: args.quote.assetSymbol,
62 network: args.quote.requirements.network,
63 payTo: args.quote.requirements.payTo,
64 nonce: args.authorization.nonce,
65 validBefore: Number(args.authorization.validBefore),
66 authorization: args.authorization,
67 signature: args.signature,
68 status: "signed",
69 };
70 this.receipts.push(r);
71 return r;
72 }
73
74 settle(id: string, s: SettlementResponse | null, delivered: boolean): void {
75 const r = this.receipts.find(x => x.id === id);
76 if (!r) return;
77 if (s && !s.success) {
78 r.status = "failed";
79 r.error = s.errorReason ?? "settlement failed";
80 return;
81 }
82 if (s?.transaction) r.txHash = s.transaction;
83 r.status = delivered ? "delivered" : s ? "settled" : "signed";
84 }
85
86 fail(id: string, error: string): void {
87 const r = this.receipts.find(x => x.id === id);
88 if (r) {
89 r.status = "failed";
90 r.error = error;
91 }
92 }
93
94 all(): readonly Receipt[] {
95 return this.receipts;
96 }
97
98 /** Signed but never confirmed settled or delivered, and still settleable. Money at risk. */
99 outstanding(): Receipt[] {
100 const nowSec = Math.floor(this.now() / 1000);
101 return this.receipts.filter(r => r.status === "signed" && r.validBefore > nowSec);
102 }
103
104 totalUsdMicro(): bigint {
105 return this.receipts
106 .filter(r => r.status !== "failed")
107 .reduce((a, r) => a + r.priceUsdMicro, 0n);
108 }
109}