yeetful 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ import { WalletClient } from 'viem';
2
+ import { X as X402Network } from './types-DzGpKiV3.js';
3
+
4
+ /**
5
+ * yeetful/agent — the "agent expense account."
6
+ *
7
+ * Wrap your agent's HTTP calls in a single grant-aware `pay()`. Before any x402
8
+ * payment is signed it enforces a spend grant — an allowlist of hosts plus
9
+ * per-call / per-day / lifetime USD caps and an expiry — then pays via the
10
+ * standard x402 client and emits a receipt for every call.
11
+ *
12
+ * One grant authorizes MANY endpoints (the allowlist). It's a guardrail for
13
+ * your own agents (runaway loops, bugs, prompt-injected tool calls) and the
14
+ * receipt feed behind budgets + audit. Enforcement here is local + instant;
15
+ * for hard, adversarial guarantees back the grant with an on-chain Spend
16
+ * Permission (the wallet contract caps spend regardless of this SDK).
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { yeetful } from 'yeetful/agent'
21
+ *
22
+ * const pay = yeetful({
23
+ * wallet, // viem WalletClient
24
+ * grant: {
25
+ * id: 'cmbq…', // hosted grant id (yeetful.com)
26
+ * allow: ['tripadvisor.x402.paysponge.com', 'anthropic.yeetful.com'],
27
+ * perCallUsd: 0.05,
28
+ * perDayUsd: 2,
29
+ * expiresAt: '2026-12-31',
30
+ * },
31
+ * apiKey: process.env.YEETFUL_API_KEY, // yf_… → receipts sync to your dashboard
32
+ * onReceipt: (r) => console.log(r.host, r.amountUsd, r.txHash),
33
+ * })
34
+ *
35
+ * const res = await pay('https://tripadvisor.x402.paysponge.com/api/v1/location/search?searchQuery=tokyo')
36
+ * // throws GrantError on NOT_ALLOWED / OVER_PER_CALL / BUDGET_EXCEEDED / EXPIRED
37
+ * ```
38
+ */
39
+
40
+ type GrantViolation = 'EXPIRED' | 'REVOKED' | 'NOT_ALLOWED' | 'OVER_PER_CALL' | 'BUDGET_EXCEEDED';
41
+ declare class GrantError extends Error {
42
+ code: GrantViolation;
43
+ constructor(code: GrantViolation, message: string);
44
+ }
45
+ /** A scoped spend authorization (mirrors the hosted SpendGrant). */
46
+ interface GrantPolicy {
47
+ /** Optional id of the hosted grant this mirrors. */
48
+ id?: string;
49
+ /** Exact hostnames this grant may pay (e.g. "tripadvisor.x402.paysponge.com"). */
50
+ allow: string[];
51
+ perCallUsd: number;
52
+ perDayUsd: number;
53
+ /** Optional lifetime cap across the life of this client instance. */
54
+ totalUsd?: number | null;
55
+ /** Unix ms, ISO string, or Date. Omit for no expiry. */
56
+ expiresAt?: number | string | Date;
57
+ /** 'active' | 'revoked'. Defaults to active. */
58
+ status?: string;
59
+ }
60
+ /** A single authorization decision — the audit trail + x402 receipt. */
61
+ interface Receipt {
62
+ host: string;
63
+ amountUsd: number;
64
+ ok: boolean;
65
+ txHash?: string;
66
+ /** "settled" on success, or the GrantViolation code on a denial. */
67
+ note: string;
68
+ ts: number;
69
+ }
70
+ interface AgentOptions {
71
+ /** viem WalletClient that signs the EIP-3009 payment. */
72
+ wallet: WalletClient;
73
+ /** The spend grant to enforce. */
74
+ grant: GrantPolicy;
75
+ /** Underlying fetch (defaults to global fetch). */
76
+ fetch?: typeof fetch;
77
+ /** Restrict payments to specific networks (defaults to all). */
78
+ allowedNetworks?: X402Network[];
79
+ /** Called after every decision — wire this to your ledger / dashboard. */
80
+ onReceipt?: (receipt: Receipt) => void | Promise<void>;
81
+ /** Human-readable progress logging. */
82
+ onEvent?: (message: string) => void;
83
+ /**
84
+ * Yeetful API key (`yf_…`, minted at yeetful.com while signed in). Together
85
+ * with `grant.id` it turns on hosted-ledger sync: every receipt is POSTed to
86
+ * `{ledgerUrl}/api/grants/{grant.id}/ledger` with Bearer auth, so the
87
+ * dashboard's budgets/audit trail include this agent's calls. Sync is
88
+ * best-effort and never blocks or fails a payment.
89
+ */
90
+ apiKey?: string;
91
+ /** Base URL of the hosted ledger. Defaults to https://yeetful.com. */
92
+ ledgerUrl?: string;
93
+ }
94
+ interface PayFn {
95
+ (input: string | URL | Request, init?: RequestInit): Promise<Response>;
96
+ /** USD spent under this grant since UTC midnight (this client instance). */
97
+ spentTodayUsd(): number;
98
+ /** USD remaining in today's budget. */
99
+ remainingTodayUsd(): number;
100
+ /** USD spent over the life of this client instance. */
101
+ spentTotalUsd(): number;
102
+ /**
103
+ * Resolves once every hosted-ledger sync issued so far has settled (no-op
104
+ * without `apiKey`). Await this before a short-lived script exits so the
105
+ * last receipts aren't dropped with the process.
106
+ */
107
+ flushLedger(): Promise<void>;
108
+ }
109
+ /**
110
+ * Create a grant-aware paid `fetch`. Enforces the grant locally before signing
111
+ * any x402 payment, pays with the wallet, and emits a receipt per call.
112
+ */
113
+ declare function yeetful(options: AgentOptions): PayFn;
114
+
115
+ export { type AgentOptions, GrantError, type GrantPolicy, type GrantViolation, type PayFn, type Receipt, yeetful };
package/dist/agent.js ADDED
@@ -0,0 +1,285 @@
1
+ // src/utils.ts
2
+ var utf8Encoder = new TextEncoder();
3
+ var utf8Decoder = new TextDecoder();
4
+ function encodePayment(value) {
5
+ const bytes = utf8Encoder.encode(JSON.stringify(value));
6
+ if (typeof Buffer !== "undefined") {
7
+ return Buffer.from(bytes).toString("base64");
8
+ }
9
+ let binary = "";
10
+ for (const byte of bytes) binary += String.fromCharCode(byte);
11
+ return btoa(binary);
12
+ }
13
+ function decodePayment(b64) {
14
+ let bytes;
15
+ if (typeof Buffer !== "undefined") {
16
+ bytes = new Uint8Array(Buffer.from(b64, "base64"));
17
+ } else {
18
+ const binary = atob(b64);
19
+ bytes = new Uint8Array(binary.length);
20
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
21
+ }
22
+ return JSON.parse(utf8Decoder.decode(bytes));
23
+ }
24
+ function randomNonce() {
25
+ const bytes = new Uint8Array(32);
26
+ globalThis.crypto.getRandomValues(bytes);
27
+ return "0x" + Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
28
+ }
29
+
30
+ // src/client.ts
31
+ function createPaymentClient(options) {
32
+ const baseFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
33
+ return async function payFetch(input, init = {}) {
34
+ const first = await baseFetch(input, init);
35
+ if (first.status !== 402) return first;
36
+ const requirements = await parsePaymentRequired(first);
37
+ const requirement = selectRequirement(requirements.accepts, options);
38
+ if (!requirement) {
39
+ throw new PaymentError("No acceptable payment requirement matched client constraints", requirements);
40
+ }
41
+ if (options.onPaymentRequired) {
42
+ const approved = await options.onPaymentRequired(requirement);
43
+ if (!approved) throw new PaymentError("Payment rejected by user", requirements);
44
+ }
45
+ const payment = await signPayment(options.wallet, requirement);
46
+ const headers = new Headers(init.headers);
47
+ headers.set("X-PAYMENT", encodePayment(payment));
48
+ return baseFetch(input, { ...init, headers });
49
+ };
50
+ }
51
+ var PaymentError = class extends Error {
52
+ requirements;
53
+ constructor(message, requirements) {
54
+ super(message);
55
+ this.name = "PaymentError";
56
+ this.requirements = requirements;
57
+ }
58
+ };
59
+ async function parsePaymentRequired(res) {
60
+ try {
61
+ return await res.clone().json();
62
+ } catch {
63
+ throw new PaymentError("402 response did not contain a valid x402 discovery body");
64
+ }
65
+ }
66
+ function selectRequirement(accepts, opts) {
67
+ const filtered = accepts.filter((a) => a.scheme === "exact").filter((a) => !opts.allowedNetworks || opts.allowedNetworks.includes(a.network)).filter((a) => !opts.maxAmountAtomic || BigInt(a.maxAmountRequired) <= opts.maxAmountAtomic);
68
+ return filtered.sort(
69
+ (a, b) => BigInt(a.maxAmountRequired) < BigInt(b.maxAmountRequired) ? -1 : 1
70
+ )[0];
71
+ }
72
+ async function signPayment(wallet, requirement) {
73
+ const account = wallet.account;
74
+ if (!account) throw new PaymentError("Wallet has no account attached");
75
+ const now = Math.floor(Date.now() / 1e3);
76
+ const validAfter = BigInt(now - 60);
77
+ const validBefore = BigInt(now + (requirement.maxTimeoutSeconds ?? 600));
78
+ const nonce = randomNonce();
79
+ const tokenName = requirement.extra?.name ?? "USD Coin";
80
+ const tokenVersion = requirement.extra?.version ?? "2";
81
+ const signature = await wallet.signTypedData({
82
+ account,
83
+ domain: {
84
+ name: tokenName,
85
+ version: tokenVersion,
86
+ chainId: chainIdForNetwork(requirement.network),
87
+ verifyingContract: requirement.asset
88
+ },
89
+ types: {
90
+ TransferWithAuthorization: [
91
+ { name: "from", type: "address" },
92
+ { name: "to", type: "address" },
93
+ { name: "value", type: "uint256" },
94
+ { name: "validAfter", type: "uint256" },
95
+ { name: "validBefore", type: "uint256" },
96
+ { name: "nonce", type: "bytes32" }
97
+ ]
98
+ },
99
+ primaryType: "TransferWithAuthorization",
100
+ message: {
101
+ from: account.address,
102
+ to: requirement.payTo,
103
+ value: BigInt(requirement.maxAmountRequired),
104
+ validAfter,
105
+ validBefore,
106
+ nonce
107
+ }
108
+ });
109
+ return {
110
+ x402Version: 1,
111
+ scheme: "exact",
112
+ network: requirement.network,
113
+ payload: {
114
+ signature,
115
+ authorization: {
116
+ from: account.address,
117
+ to: requirement.payTo,
118
+ value: requirement.maxAmountRequired,
119
+ validAfter: validAfter.toString(),
120
+ validBefore: validBefore.toString(),
121
+ nonce
122
+ }
123
+ }
124
+ };
125
+ }
126
+ function chainIdForNetwork(network) {
127
+ switch (network) {
128
+ case "base":
129
+ return 8453;
130
+ case "base-sepolia":
131
+ return 84532;
132
+ case "ethereum":
133
+ return 1;
134
+ case "optimism":
135
+ return 10;
136
+ case "arbitrum":
137
+ return 42161;
138
+ case "polygon":
139
+ return 137;
140
+ }
141
+ }
142
+
143
+ // src/agent.ts
144
+ var GrantError = class extends Error {
145
+ constructor(code, message) {
146
+ super(message);
147
+ this.code = code;
148
+ this.name = "GrantError";
149
+ }
150
+ code;
151
+ };
152
+ function expiryMs(expiresAt) {
153
+ if (expiresAt == null) return Infinity;
154
+ if (expiresAt instanceof Date) return expiresAt.getTime();
155
+ if (typeof expiresAt === "number") return expiresAt;
156
+ const t = new Date(expiresAt).getTime();
157
+ return Number.isNaN(t) ? Infinity : t;
158
+ }
159
+ function utcDayIndex(ms) {
160
+ return Math.floor(ms / 864e5);
161
+ }
162
+ function hostOf(input) {
163
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
164
+ try {
165
+ return new URL(url).host.toLowerCase();
166
+ } catch {
167
+ return "";
168
+ }
169
+ }
170
+ function txHashOf(res) {
171
+ const header = res.headers.get("x-payment-response");
172
+ if (!header) return void 0;
173
+ try {
174
+ return decodePayment(header).transaction;
175
+ } catch {
176
+ return void 0;
177
+ }
178
+ }
179
+ function yeetful(options) {
180
+ const { wallet, grant, onReceipt, onEvent } = options;
181
+ const log = onEvent ?? (() => {
182
+ });
183
+ let spentToday = 0;
184
+ let spentTotal = 0;
185
+ let dayIndex = utcDayIndex(Date.now());
186
+ const ledgerEndpoint = options.apiKey && grant.id ? `${(options.ledgerUrl ?? "https://yeetful.com").replace(/\/+$/, "")}/api/grants/${grant.id}/ledger` : null;
187
+ if (options.apiKey && !grant.id) {
188
+ log("hosted-ledger sync disabled: grant.id is not set (use the id of your yeetful.com grant)");
189
+ }
190
+ let ledgerChain = Promise.resolve();
191
+ const ledgerFetch = options.fetch ?? globalThis.fetch;
192
+ const sync = (r) => {
193
+ if (!ledgerEndpoint) return;
194
+ ledgerChain = ledgerChain.then(async () => {
195
+ const res = await ledgerFetch(ledgerEndpoint, {
196
+ method: "POST",
197
+ headers: {
198
+ "content-type": "application/json",
199
+ authorization: `Bearer ${options.apiKey}`
200
+ },
201
+ body: JSON.stringify({
202
+ host: r.host,
203
+ amountUsd: r.amountUsd,
204
+ ok: r.ok,
205
+ txHash: r.txHash,
206
+ note: r.note
207
+ })
208
+ });
209
+ if (!res.ok) log(`ledger sync \u2192 ${res.status} for ${r.host}`);
210
+ }).catch((err) => {
211
+ log(`ledger sync failed for ${r.host}: ${err instanceof Error ? err.message : err}`);
212
+ });
213
+ };
214
+ const emit = (r) => {
215
+ sync(r);
216
+ void Promise.resolve(onReceipt?.(r)).catch(() => {
217
+ });
218
+ };
219
+ const deny = (host, code, msg) => {
220
+ emit({ host, amountUsd: 0, ok: false, note: code, ts: Date.now() });
221
+ log(`\u2717 ${host} \u2014 ${code}: ${msg}`);
222
+ throw new GrantError(code, msg);
223
+ };
224
+ const pay = async function pay2(input, init = {}) {
225
+ const today = utcDayIndex(Date.now());
226
+ if (today !== dayIndex) {
227
+ dayIndex = today;
228
+ spentToday = 0;
229
+ }
230
+ const host = hostOf(input);
231
+ if ((grant.status ?? "active") === "revoked") deny(host, "REVOKED", "grant is revoked");
232
+ if (Date.now() > expiryMs(grant.expiresAt)) deny(host, "EXPIRED", "grant has expired");
233
+ if (!grant.allow.map((h) => h.toLowerCase()).includes(host)) {
234
+ deny(host, "NOT_ALLOWED", `${host} is not in this grant's allowlist`);
235
+ }
236
+ let pricedUsd = 0;
237
+ const client = createPaymentClient({
238
+ wallet,
239
+ fetch: options.fetch,
240
+ allowedNetworks: options.allowedNetworks,
241
+ // Per-call enforcement lives in the hook (not maxAmountAtomic) so an
242
+ // over-cap call surfaces a clean GrantError instead of a filtered no-match.
243
+ onPaymentRequired: (req) => {
244
+ const price = Number(req.maxAmountRequired) / 1e6;
245
+ if (price > grant.perCallUsd) {
246
+ deny(host, "OVER_PER_CALL", `$${price.toFixed(4)} exceeds per-call cap $${grant.perCallUsd}`);
247
+ }
248
+ if (spentToday + price > grant.perDayUsd) {
249
+ deny(host, "BUDGET_EXCEEDED", `$${(spentToday + price).toFixed(2)} exceeds today's cap $${grant.perDayUsd}`);
250
+ }
251
+ if (grant.totalUsd != null && spentTotal + price > grant.totalUsd) {
252
+ deny(host, "BUDGET_EXCEEDED", `$${(spentTotal + price).toFixed(2)} exceeds lifetime cap $${grant.totalUsd}`);
253
+ }
254
+ pricedUsd = price;
255
+ return true;
256
+ }
257
+ });
258
+ let res;
259
+ try {
260
+ res = await client(input, init);
261
+ } catch (err) {
262
+ if (err instanceof GrantError) throw err;
263
+ const note = err instanceof PaymentError ? "payment-failed" : "error";
264
+ emit({ host, amountUsd: 0, ok: false, note, ts: Date.now() });
265
+ throw err;
266
+ }
267
+ if (pricedUsd > 0) {
268
+ spentToday += pricedUsd;
269
+ spentTotal += pricedUsd;
270
+ }
271
+ const txHash = txHashOf(res);
272
+ emit({ host, amountUsd: pricedUsd, ok: true, txHash, note: "settled", ts: Date.now() });
273
+ log(`\u2713 ${host} \u2014 $${pricedUsd.toFixed(4)} \xB7 today $${spentToday.toFixed(2)}/$${grant.perDayUsd}`);
274
+ return res;
275
+ };
276
+ pay.spentTodayUsd = () => spentToday;
277
+ pay.remainingTodayUsd = () => Math.max(0, grant.perDayUsd - spentToday);
278
+ pay.spentTotalUsd = () => spentTotal;
279
+ pay.flushLedger = () => ledgerChain;
280
+ return pay;
281
+ }
282
+
283
+ export { GrantError, yeetful };
284
+ //# sourceMappingURL=agent.js.map
285
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/client.ts","../src/agent.ts"],"names":["pay"],"mappings":";AAgCA,IAAM,WAAA,GAAc,IAAI,WAAA,EAAY;AACpC,IAAM,WAAA,GAAc,IAAI,WAAA,EAAY;AAG7B,SAAS,cAAc,KAAA,EAAwB;AACpD,EAAA,MAAM,QAAQ,WAAA,CAAY,MAAA,CAAO,IAAA,CAAK,SAAA,CAAU,KAAK,CAAC,CAAA;AACtD,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC,IAAA,OAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,CAAE,SAAS,QAAQ,CAAA;AAAA,EAC7C;AACA,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA,EAAO,MAAA,IAAU,MAAA,CAAO,aAAa,IAAI,CAAA;AAC5D,EAAA,OAAO,KAAK,MAAM,CAAA;AACpB;AAGO,SAAS,cAA2B,GAAA,EAAgB;AACzD,EAAA,IAAI,KAAA;AACJ,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC,IAAA,KAAA,GAAQ,IAAI,UA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","sourcesContent":["import type { X402Network } from './types.js'\n\nconst USDC_BY_NETWORK: Record<X402Network, `0x${string}`> = {\n base: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',\n 'base-sepolia': '0x036CbD53842c5426634e7929541eC2318f3dCF7e',\n ethereum: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',\n optimism: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',\n arbitrum: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',\n polygon: '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359',\n}\n\nexport const USDC_DECIMALS = 6\n\n/** Returns the canonical USDC contract address for a supported network. */\nexport function usdcAddress(network: X402Network): `0x${string}` {\n return USDC_BY_NETWORK[network]\n}\n\n/**\n * Convert a human-friendly USD amount (e.g. \"0.01\") into atomic USDC units.\n * Fixed-point to avoid float drift — safer than Number math for payments.\n */\nexport function usdToAtomic(amount: string | number, decimals = USDC_DECIMALS): string {\n const str = typeof amount === 'number' ? amount.toString() : amount\n if (!/^\\d+(\\.\\d+)?$/.test(str)) {\n throw new Error(`Invalid amount: ${str}`)\n }\n const [whole, frac = ''] = str.split('.')\n const padded = frac.slice(0, decimals).padEnd(decimals, '0')\n return (BigInt(whole ?? '0') * 10n ** BigInt(decimals) + BigInt(padded || '0')).toString()\n}\n\nconst utf8Encoder = new TextEncoder()\nconst utf8Decoder = new TextDecoder()\n\n/** Base64 encode a JSON value — works in Node 18+ and browsers. */\nexport function encodePayment(value: unknown): string {\n const bytes = utf8Encoder.encode(JSON.stringify(value))\n if (typeof Buffer !== 'undefined') {\n return Buffer.from(bytes).toString('base64')\n }\n let binary = ''\n for (const byte of bytes) binary += String.fromCharCode(byte)\n return btoa(binary)\n}\n\n/** Base64 decode a payment header value back into JSON. */\nexport function decodePayment<T = unknown>(b64: string): T {\n let bytes: Uint8Array\n if (typeof Buffer !== 'undefined') {\n bytes = new Uint8Array(Buffer.from(b64, 'base64'))\n } else {\n const binary = atob(b64)\n bytes = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)\n }\n return JSON.parse(utf8Decoder.decode(bytes)) as T\n}\n\n/** Generate a random 32-byte nonce as a 0x-prefixed hex string. */\nexport function randomNonce(): `0x${string}` {\n const bytes = new Uint8Array(32)\n globalThis.crypto.getRandomValues(bytes)\n return ('0x' + Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')) as `0x${string}`\n}\n","import type { Address, Hex, WalletClient } from 'viem'\nimport type {\n PaymentPayload,\n PaymentRequiredResponse,\n PaymentRequirement,\n X402Network,\n} from './types.js'\nimport { encodePayment, randomNonce } from './utils.js'\n\nexport interface ClientOptions {\n /** A viem WalletClient able to sign EIP-712 typed data. */\n wallet: WalletClient\n /** Underlying fetch to wrap (defaults to global fetch). */\n fetch?: typeof fetch\n /**\n * Hook called before signing. Return `false` to reject the payment.\n * Useful for showing a confirmation UI to the user.\n */\n onPaymentRequired?: (requirement: PaymentRequirement) => boolean | Promise<boolean>\n /** Only allow payments up to this atomic-units cap. Safety belt. */\n maxAmountAtomic?: bigint\n /** Restrict to specific networks (defaults to all). */\n allowedNetworks?: X402Network[]\n}\n\n/**\n * Create a fetch wrapper that transparently handles x402 payments.\n *\n * On a 402 response, it picks the cheapest acceptable requirement,\n * signs an EIP-3009 authorization with the provided wallet, and retries\n * the request with an `X-PAYMENT` header.\n *\n * @example\n * ```ts\n * const pay = createPaymentClient({ wallet })\n * const res = await pay('https://api.example.com/premium')\n * ```\n */\nexport function createPaymentClient(options: ClientOptions) {\n const baseFetch = options.fetch ?? globalThis.fetch.bind(globalThis)\n\n return async function payFetch(\n input: string | URL | Request,\n init: RequestInit = {},\n ): Promise<Response> {\n const first = await baseFetch(input, init)\n if (first.status !== 402) return first\n\n const requirements = await parsePaymentRequired(first)\n const requirement = selectRequirement(requirements.accepts, options)\n if (!requirement) {\n throw new PaymentError('No acceptable payment requirement matched client constraints', requirements)\n }\n\n if (options.onPaymentRequired) {\n const approved = await options.onPaymentRequired(requirement)\n if (!approved) throw new PaymentError('Payment rejected by user', requirements)\n }\n\n const payment = await signPayment(options.wallet, requirement)\n const headers = new Headers(init.headers)\n headers.set('X-PAYMENT', encodePayment(payment))\n\n return baseFetch(input, { ...init, headers })\n }\n}\n\nexport class PaymentError extends Error {\n readonly requirements?: PaymentRequiredResponse\n constructor(message: string, requirements?: PaymentRequiredResponse) {\n super(message)\n this.name = 'PaymentError'\n this.requirements = requirements\n }\n}\n\nasync function parsePaymentRequired(res: Response): Promise<PaymentRequiredResponse> {\n try {\n return (await res.clone().json()) as PaymentRequiredResponse\n } catch {\n throw new PaymentError('402 response did not contain a valid x402 discovery body')\n }\n}\n\nfunction selectRequirement(\n accepts: PaymentRequirement[],\n opts: ClientOptions,\n): PaymentRequirement | undefined {\n const filtered = accepts\n .filter((a) => a.scheme === 'exact')\n .filter((a) => !opts.allowedNetworks || opts.allowedNetworks.includes(a.network))\n .filter((a) => !opts.maxAmountAtomic || BigInt(a.maxAmountRequired) <= opts.maxAmountAtomic)\n\n return filtered.sort((a, b) =>\n BigInt(a.maxAmountRequired) < BigInt(b.maxAmountRequired) ? -1 : 1,\n )[0]\n}\n\n/** Sign an EIP-3009 `TransferWithAuthorization` for the given requirement. */\nexport async function signPayment(\n wallet: WalletClient,\n requirement: PaymentRequirement,\n): Promise<PaymentPayload> {\n const account = wallet.account\n if (!account) throw new PaymentError('Wallet has no account attached')\n\n const now = Math.floor(Date.now() / 1000)\n const validAfter = BigInt(now - 60)\n const validBefore = BigInt(now + (requirement.maxTimeoutSeconds ?? 600))\n const nonce = randomNonce()\n\n const tokenName =\n (requirement.extra?.name as string | undefined) ?? 'USD Coin'\n const tokenVersion =\n (requirement.extra?.version as string | undefined) ?? '2'\n\n const signature = (await wallet.signTypedData({\n account,\n domain: {\n name: tokenName,\n version: tokenVersion,\n chainId: chainIdForNetwork(requirement.network),\n verifyingContract: requirement.asset,\n },\n types: {\n TransferWithAuthorization: [\n { name: 'from', type: 'address' },\n { name: 'to', type: 'address' },\n { name: 'value', type: 'uint256' },\n { name: 'validAfter', type: 'uint256' },\n { name: 'validBefore', type: 'uint256' },\n { name: 'nonce', type: 'bytes32' },\n ],\n },\n primaryType: 'TransferWithAuthorization',\n message: {\n from: account.address as Address,\n to: requirement.payTo,\n value: BigInt(requirement.maxAmountRequired),\n validAfter,\n validBefore,\n nonce,\n },\n })) as Hex\n\n return {\n x402Version: 1,\n scheme: 'exact',\n network: requirement.network,\n payload: {\n signature,\n authorization: {\n from: account.address as Address,\n to: requirement.payTo,\n value: requirement.maxAmountRequired,\n validAfter: validAfter.toString(),\n validBefore: validBefore.toString(),\n nonce,\n },\n },\n }\n}\n\nfunction chainIdForNetwork(network: X402Network): number {\n switch (network) {\n case 'base':\n return 8453\n case 'base-sepolia':\n return 84532\n case 'ethereum':\n return 1\n case 'optimism':\n return 10\n case 'arbitrum':\n return 42161\n case 'polygon':\n return 137\n }\n}\n","/**\n * yeetful/agent — the \"agent expense account.\"\n *\n * Wrap your agent's HTTP calls in a single grant-aware `pay()`. Before any x402\n * payment is signed it enforces a spend grant — an allowlist of hosts plus\n * per-call / per-day / lifetime USD caps and an expiry — then pays via the\n * standard x402 client and emits a receipt for every call.\n *\n * One grant authorizes MANY endpoints (the allowlist). It's a guardrail for\n * your own agents (runaway loops, bugs, prompt-injected tool calls) and the\n * receipt feed behind budgets + audit. Enforcement here is local + instant;\n * for hard, adversarial guarantees back the grant with an on-chain Spend\n * Permission (the wallet contract caps spend regardless of this SDK).\n *\n * @example\n * ```ts\n * import { yeetful } from 'yeetful/agent'\n *\n * const pay = yeetful({\n * wallet, // viem WalletClient\n * grant: {\n * id: 'cmbq…', // hosted grant id (yeetful.com)\n * allow: ['tripadvisor.x402.paysponge.com', 'anthropic.yeetful.com'],\n * perCallUsd: 0.05,\n * perDayUsd: 2,\n * expiresAt: '2026-12-31',\n * },\n * apiKey: process.env.YEETFUL_API_KEY, // yf_… → receipts sync to your dashboard\n * onReceipt: (r) => console.log(r.host, r.amountUsd, r.txHash),\n * })\n *\n * const res = await pay('https://tripadvisor.x402.paysponge.com/api/v1/location/search?searchQuery=tokyo')\n * // throws GrantError on NOT_ALLOWED / OVER_PER_CALL / BUDGET_EXCEEDED / EXPIRED\n * ```\n */\n\nimport type { WalletClient } from 'viem'\nimport { createPaymentClient, PaymentError } from './client.js'\nimport { decodePayment } from './utils.js'\nimport type { PaymentRequirement, SettleResult, X402Network } from './types.js'\n\nexport type GrantViolation =\n | 'EXPIRED'\n | 'REVOKED'\n | 'NOT_ALLOWED'\n | 'OVER_PER_CALL'\n | 'BUDGET_EXCEEDED'\n\nexport class GrantError extends Error {\n constructor(\n public code: GrantViolation,\n message: string,\n ) {\n super(message)\n this.name = 'GrantError'\n }\n}\n\n/** A scoped spend authorization (mirrors the hosted SpendGrant). */\nexport interface GrantPolicy {\n /** Optional id of the hosted grant this mirrors. */\n id?: string\n /** Exact hostnames this grant may pay (e.g. \"tripadvisor.x402.paysponge.com\"). */\n allow: string[]\n perCallUsd: number\n perDayUsd: number\n /** Optional lifetime cap across the life of this client instance. */\n totalUsd?: number | null\n /** Unix ms, ISO string, or Date. Omit for no expiry. */\n expiresAt?: number | string | Date\n /** 'active' | 'revoked'. Defaults to active. */\n status?: string\n}\n\n/** A single authorization decision — the audit trail + x402 receipt. */\nexport interface Receipt {\n host: string\n amountUsd: number\n ok: boolean\n txHash?: string\n /** \"settled\" on success, or the GrantViolation code on a denial. */\n note: string\n ts: number\n}\n\nexport interface AgentOptions {\n /** viem WalletClient that signs the EIP-3009 payment. */\n wallet: WalletClient\n /** The spend grant to enforce. */\n grant: GrantPolicy\n /** Underlying fetch (defaults to global fetch). */\n fetch?: typeof fetch\n /** Restrict payments to specific networks (defaults to all). */\n allowedNetworks?: X402Network[]\n /** Called after every decision — wire this to your ledger / dashboard. */\n onReceipt?: (receipt: Receipt) => void | Promise<void>\n /** Human-readable progress logging. */\n onEvent?: (message: string) => void\n /**\n * Yeetful API key (`yf_…`, minted at yeetful.com while signed in). Together\n * with `grant.id` it turns on hosted-ledger sync: every receipt is POSTed to\n * `{ledgerUrl}/api/grants/{grant.id}/ledger` with Bearer auth, so the\n * dashboard's budgets/audit trail include this agent's calls. Sync is\n * best-effort and never blocks or fails a payment.\n */\n apiKey?: string\n /** Base URL of the hosted ledger. Defaults to https://yeetful.com. */\n ledgerUrl?: string\n}\n\nexport interface PayFn {\n (input: string | URL | Request, init?: RequestInit): Promise<Response>\n /** USD spent under this grant since UTC midnight (this client instance). */\n spentTodayUsd(): number\n /** USD remaining in today's budget. */\n remainingTodayUsd(): number\n /** USD spent over the life of this client instance. */\n spentTotalUsd(): number\n /**\n * Resolves once every hosted-ledger sync issued so far has settled (no-op\n * without `apiKey`). Await this before a short-lived script exits so the\n * last receipts aren't dropped with the process.\n */\n flushLedger(): Promise<void>\n}\n\nfunction expiryMs(expiresAt: GrantPolicy['expiresAt']): number {\n if (expiresAt == null) return Infinity\n if (expiresAt instanceof Date) return expiresAt.getTime()\n if (typeof expiresAt === 'number') return expiresAt\n const t = new Date(expiresAt).getTime()\n return Number.isNaN(t) ? Infinity : t\n}\n\nfunction utcDayIndex(ms: number): number {\n return Math.floor(ms / 86_400_000)\n}\n\nfunction hostOf(input: string | URL | Request): string {\n const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url\n try {\n return new URL(url).host.toLowerCase()\n } catch {\n return ''\n }\n}\n\n/** Pull the settlement tx hash from the X-PAYMENT-RESPONSE header, if present. */\nfunction txHashOf(res: Response): string | undefined {\n const header = res.headers.get('x-payment-response')\n if (!header) return undefined\n try {\n return decodePayment<SettleResult>(header).transaction\n } catch {\n return undefined\n }\n}\n\n/**\n * Create a grant-aware paid `fetch`. Enforces the grant locally before signing\n * any x402 payment, pays with the wallet, and emits a receipt per call.\n */\nexport function yeetful(options: AgentOptions): PayFn {\n const { wallet, grant, onReceipt, onEvent } = options\n const log = onEvent ?? (() => {})\n\n let spentToday = 0\n let spentTotal = 0\n let dayIndex = utcDayIndex(Date.now())\n\n // ── Hosted-ledger sync (optional): receipts → POST /api/grants/:id/ledger ──\n const ledgerEndpoint =\n options.apiKey && grant.id\n ? `${(options.ledgerUrl ?? 'https://yeetful.com').replace(/\\/+$/, '')}/api/grants/${grant.id}/ledger`\n : null\n if (options.apiKey && !grant.id) {\n log('hosted-ledger sync disabled: grant.id is not set (use the id of your yeetful.com grant)')\n }\n // A chain (not fire-and-forget) so receipts land in order and flushLedger()\n // can await them; one failed POST is logged and never poisons the chain.\n let ledgerChain: Promise<void> = Promise.resolve()\n const ledgerFetch = options.fetch ?? globalThis.fetch\n const sync = (r: Receipt) => {\n if (!ledgerEndpoint) return\n ledgerChain = ledgerChain\n .then(async () => {\n const res = await ledgerFetch(ledgerEndpoint, {\n method: 'POST',\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${options.apiKey}`,\n },\n body: JSON.stringify({\n host: r.host,\n amountUsd: r.amountUsd,\n ok: r.ok,\n txHash: r.txHash,\n note: r.note,\n }),\n })\n if (!res.ok) log(`ledger sync → ${res.status} for ${r.host}`)\n })\n .catch((err) => {\n log(`ledger sync failed for ${r.host}: ${err instanceof Error ? err.message : err}`)\n })\n }\n\n const emit = (r: Receipt) => {\n sync(r)\n void Promise.resolve(onReceipt?.(r)).catch(() => {})\n }\n const deny = (host: string, code: GrantViolation, msg: string): never => {\n emit({ host, amountUsd: 0, ok: false, note: code, ts: Date.now() })\n log(`✗ ${host} — ${code}: ${msg}`)\n throw new GrantError(code, msg)\n }\n\n const pay = async function pay(\n input: string | URL | Request,\n init: RequestInit = {},\n ): Promise<Response> {\n // Roll the daily budget at UTC midnight.\n const today = utcDayIndex(Date.now())\n if (today !== dayIndex) {\n dayIndex = today\n spentToday = 0\n }\n\n const host = hostOf(input)\n\n // ── Pre-flight policy (no network): status → expiry → allowlist ─────────\n if ((grant.status ?? 'active') === 'revoked') deny(host, 'REVOKED', 'grant is revoked')\n if (Date.now() > expiryMs(grant.expiresAt)) deny(host, 'EXPIRED', 'grant has expired')\n if (!grant.allow.map((h) => h.toLowerCase()).includes(host)) {\n deny(host, 'NOT_ALLOWED', `${host} is not in this grant's allowlist`)\n }\n\n // Price is only known from the 402 challenge — check caps in the hook,\n // which runs after the challenge is parsed and before the payment is signed.\n let pricedUsd = 0\n const client = createPaymentClient({\n wallet,\n fetch: options.fetch,\n allowedNetworks: options.allowedNetworks,\n // Per-call enforcement lives in the hook (not maxAmountAtomic) so an\n // over-cap call surfaces a clean GrantError instead of a filtered no-match.\n onPaymentRequired: (req: PaymentRequirement) => {\n const price = Number(req.maxAmountRequired) / 1e6\n if (price > grant.perCallUsd) {\n deny(host, 'OVER_PER_CALL', `$${price.toFixed(4)} exceeds per-call cap $${grant.perCallUsd}`)\n }\n if (spentToday + price > grant.perDayUsd) {\n deny(host, 'BUDGET_EXCEEDED', `$${(spentToday + price).toFixed(2)} exceeds today's cap $${grant.perDayUsd}`)\n }\n if (grant.totalUsd != null && spentTotal + price > grant.totalUsd) {\n deny(host, 'BUDGET_EXCEEDED', `$${(spentTotal + price).toFixed(2)} exceeds lifetime cap $${grant.totalUsd}`)\n }\n pricedUsd = price\n return true\n },\n })\n\n let res: Response\n try {\n res = await client(input, init)\n } catch (err) {\n if (err instanceof GrantError) throw err // already denied + receipted\n // A rejection from the underlying client (e.g. no acceptable requirement).\n const note = err instanceof PaymentError ? 'payment-failed' : 'error'\n emit({ host, amountUsd: 0, ok: false, note, ts: Date.now() })\n throw err\n }\n\n // Settled (or a free, non-402 call where pricedUsd stayed 0).\n if (pricedUsd > 0) {\n spentToday += pricedUsd\n spentTotal += pricedUsd\n }\n const txHash = txHashOf(res)\n emit({ host, amountUsd: pricedUsd, ok: true, txHash, note: 'settled', ts: Date.now() })\n log(`✓ ${host} — $${pricedUsd.toFixed(4)} · today $${spentToday.toFixed(2)}/$${grant.perDayUsd}`)\n return res\n } as PayFn\n\n pay.spentTodayUsd = () => spentToday\n pay.remainingTodayUsd = () => Math.max(0, grant.perDayUsd - spentToday)\n pay.spentTotalUsd = () => spentTotal\n pay.flushLedger = () => ledgerChain\n return pay\n}\n"]}
package/dist/index.cjs CHANGED
@@ -163,6 +163,146 @@ function chainIdForNetwork(network) {
163
163
  }
164
164
  }
165
165
 
166
+ // src/agent.ts
167
+ var GrantError = class extends Error {
168
+ constructor(code, message) {
169
+ super(message);
170
+ this.code = code;
171
+ this.name = "GrantError";
172
+ }
173
+ code;
174
+ };
175
+ function expiryMs(expiresAt) {
176
+ if (expiresAt == null) return Infinity;
177
+ if (expiresAt instanceof Date) return expiresAt.getTime();
178
+ if (typeof expiresAt === "number") return expiresAt;
179
+ const t = new Date(expiresAt).getTime();
180
+ return Number.isNaN(t) ? Infinity : t;
181
+ }
182
+ function utcDayIndex(ms) {
183
+ return Math.floor(ms / 864e5);
184
+ }
185
+ function hostOf(input) {
186
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
187
+ try {
188
+ return new URL(url).host.toLowerCase();
189
+ } catch {
190
+ return "";
191
+ }
192
+ }
193
+ function txHashOf(res) {
194
+ const header = res.headers.get("x-payment-response");
195
+ if (!header) return void 0;
196
+ try {
197
+ return decodePayment(header).transaction;
198
+ } catch {
199
+ return void 0;
200
+ }
201
+ }
202
+ function yeetful(options) {
203
+ const { wallet, grant, onReceipt, onEvent } = options;
204
+ const log = onEvent ?? (() => {
205
+ });
206
+ let spentToday = 0;
207
+ let spentTotal = 0;
208
+ let dayIndex = utcDayIndex(Date.now());
209
+ const ledgerEndpoint = options.apiKey && grant.id ? `${(options.ledgerUrl ?? "https://yeetful.com").replace(/\/+$/, "")}/api/grants/${grant.id}/ledger` : null;
210
+ if (options.apiKey && !grant.id) {
211
+ log("hosted-ledger sync disabled: grant.id is not set (use the id of your yeetful.com grant)");
212
+ }
213
+ let ledgerChain = Promise.resolve();
214
+ const ledgerFetch = options.fetch ?? globalThis.fetch;
215
+ const sync = (r) => {
216
+ if (!ledgerEndpoint) return;
217
+ ledgerChain = ledgerChain.then(async () => {
218
+ const res = await ledgerFetch(ledgerEndpoint, {
219
+ method: "POST",
220
+ headers: {
221
+ "content-type": "application/json",
222
+ authorization: `Bearer ${options.apiKey}`
223
+ },
224
+ body: JSON.stringify({
225
+ host: r.host,
226
+ amountUsd: r.amountUsd,
227
+ ok: r.ok,
228
+ txHash: r.txHash,
229
+ note: r.note
230
+ })
231
+ });
232
+ if (!res.ok) log(`ledger sync \u2192 ${res.status} for ${r.host}`);
233
+ }).catch((err) => {
234
+ log(`ledger sync failed for ${r.host}: ${err instanceof Error ? err.message : err}`);
235
+ });
236
+ };
237
+ const emit = (r) => {
238
+ sync(r);
239
+ void Promise.resolve(onReceipt?.(r)).catch(() => {
240
+ });
241
+ };
242
+ const deny = (host, code, msg) => {
243
+ emit({ host, amountUsd: 0, ok: false, note: code, ts: Date.now() });
244
+ log(`\u2717 ${host} \u2014 ${code}: ${msg}`);
245
+ throw new GrantError(code, msg);
246
+ };
247
+ const pay = async function pay2(input, init = {}) {
248
+ const today = utcDayIndex(Date.now());
249
+ if (today !== dayIndex) {
250
+ dayIndex = today;
251
+ spentToday = 0;
252
+ }
253
+ const host = hostOf(input);
254
+ if ((grant.status ?? "active") === "revoked") deny(host, "REVOKED", "grant is revoked");
255
+ if (Date.now() > expiryMs(grant.expiresAt)) deny(host, "EXPIRED", "grant has expired");
256
+ if (!grant.allow.map((h) => h.toLowerCase()).includes(host)) {
257
+ deny(host, "NOT_ALLOWED", `${host} is not in this grant's allowlist`);
258
+ }
259
+ let pricedUsd = 0;
260
+ const client = createPaymentClient({
261
+ wallet,
262
+ fetch: options.fetch,
263
+ allowedNetworks: options.allowedNetworks,
264
+ // Per-call enforcement lives in the hook (not maxAmountAtomic) so an
265
+ // over-cap call surfaces a clean GrantError instead of a filtered no-match.
266
+ onPaymentRequired: (req) => {
267
+ const price = Number(req.maxAmountRequired) / 1e6;
268
+ if (price > grant.perCallUsd) {
269
+ deny(host, "OVER_PER_CALL", `$${price.toFixed(4)} exceeds per-call cap $${grant.perCallUsd}`);
270
+ }
271
+ if (spentToday + price > grant.perDayUsd) {
272
+ deny(host, "BUDGET_EXCEEDED", `$${(spentToday + price).toFixed(2)} exceeds today's cap $${grant.perDayUsd}`);
273
+ }
274
+ if (grant.totalUsd != null && spentTotal + price > grant.totalUsd) {
275
+ deny(host, "BUDGET_EXCEEDED", `$${(spentTotal + price).toFixed(2)} exceeds lifetime cap $${grant.totalUsd}`);
276
+ }
277
+ pricedUsd = price;
278
+ return true;
279
+ }
280
+ });
281
+ let res;
282
+ try {
283
+ res = await client(input, init);
284
+ } catch (err) {
285
+ if (err instanceof GrantError) throw err;
286
+ const note = err instanceof PaymentError ? "payment-failed" : "error";
287
+ emit({ host, amountUsd: 0, ok: false, note, ts: Date.now() });
288
+ throw err;
289
+ }
290
+ if (pricedUsd > 0) {
291
+ spentToday += pricedUsd;
292
+ spentTotal += pricedUsd;
293
+ }
294
+ const txHash = txHashOf(res);
295
+ emit({ host, amountUsd: pricedUsd, ok: true, txHash, note: "settled", ts: Date.now() });
296
+ log(`\u2713 ${host} \u2014 $${pricedUsd.toFixed(4)} \xB7 today $${spentToday.toFixed(2)}/$${grant.perDayUsd}`);
297
+ return res;
298
+ };
299
+ pay.spentTodayUsd = () => spentToday;
300
+ pay.remainingTodayUsd = () => Math.max(0, grant.perDayUsd - spentToday);
301
+ pay.spentTotalUsd = () => spentTotal;
302
+ pay.flushLedger = () => ledgerChain;
303
+ return pay;
304
+ }
305
+
166
306
  // src/facilitator.ts
167
307
  var Facilitator = class {
168
308
  url;
@@ -289,6 +429,7 @@ function paymentRequiredResponse(accepts, error) {
289
429
 
290
430
  exports.DEFAULT_FACILITATOR_URL = DEFAULT_FACILITATOR_URL;
291
431
  exports.Facilitator = Facilitator;
432
+ exports.GrantError = GrantError;
292
433
  exports.PaymentError = PaymentError;
293
434
  exports.createPaymentClient = createPaymentClient;
294
435
  exports.decodePayment = decodePayment;
@@ -297,5 +438,6 @@ exports.gate = gate;
297
438
  exports.signPayment = signPayment;
298
439
  exports.usdToAtomic = usdToAtomic;
299
440
  exports.usdcAddress = usdcAddress;
441
+ exports.yeetful = yeetful;
300
442
  //# sourceMappingURL=index.cjs.map
301
443
  //# sourceMappingURL=index.cjs.map