killswitch-agentkit 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/dist/index.js +75 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2040,9 +2040,68 @@ function authorizeTransferCall(account, token, a) {
2040
2040
  })
2041
2041
  };
2042
2042
  }
2043
- function x402PaymentHeader(p) {
2044
- const body = { x402Version: 1, scheme: "exact", network: p.network, payload: { signature: p.signature, authorization: p.authorization } };
2045
- return btoa(JSON.stringify(body));
2043
+ var b64encode = (s) => btoa(Array.from(new TextEncoder().encode(s), (b) => String.fromCharCode(b)).join(""));
2044
+ var b64decode = (s) => new TextDecoder().decode(Uint8Array.from(atob(s), (c) => c.charCodeAt(0)));
2045
+ var x402NetworkIds = (chainId) => [X402_NETWORKS[chainId], `eip155:${chainId}`].filter(Boolean);
2046
+ async function readX402Ask(res) {
2047
+ const header = res.headers.get("payment-required");
2048
+ if (header) {
2049
+ const pr = JSON.parse(b64decode(header));
2050
+ return {
2051
+ version: 2,
2052
+ resource: pr.resource,
2053
+ extensions: pr.extensions,
2054
+ error: pr.error,
2055
+ offers: (pr.accepts ?? []).map((r) => ({
2056
+ version: 2,
2057
+ scheme: r.scheme,
2058
+ network: r.network,
2059
+ asset: r.asset,
2060
+ amount: String(r.amount),
2061
+ payTo: r.payTo,
2062
+ maxTimeoutSeconds: r.maxTimeoutSeconds,
2063
+ resource: pr.resource?.url ?? "",
2064
+ description: pr.resource?.description,
2065
+ extra: r.extra,
2066
+ raw: r
2067
+ }))
2068
+ };
2069
+ }
2070
+ const body = await res.json().catch(() => null);
2071
+ if (!body || !Array.isArray(body.accepts)) return null;
2072
+ return {
2073
+ version: 1,
2074
+ error: body.error,
2075
+ offers: body.accepts.map((r) => ({
2076
+ version: 1,
2077
+ scheme: r.scheme,
2078
+ network: r.network,
2079
+ asset: r.asset,
2080
+ amount: String(r.maxAmountRequired),
2081
+ payTo: r.payTo,
2082
+ maxTimeoutSeconds: r.maxTimeoutSeconds,
2083
+ resource: r.resource ?? "",
2084
+ description: r.description,
2085
+ extra: r.extra,
2086
+ raw: r
2087
+ }))
2088
+ };
2089
+ }
2090
+ function x402PaymentHeaders(ask, offer, payload) {
2091
+ if (offer.version === 2) {
2092
+ const body = { x402Version: 2, payload, extensions: ask.extensions, resource: ask.resource, accepted: offer.raw };
2093
+ return { "PAYMENT-SIGNATURE": b64encode(JSON.stringify(body)) };
2094
+ }
2095
+ return { "X-PAYMENT": b64encode(JSON.stringify({ x402Version: 1, scheme: offer.scheme, network: offer.network, payload })) };
2096
+ }
2097
+ function readX402Receipt(res) {
2098
+ const h = res.headers.get("payment-response") ?? res.headers.get("x-payment-response");
2099
+ if (!h) return void 0;
2100
+ try {
2101
+ return JSON.parse(b64decode(h));
2102
+ } catch {
2103
+ return void 0;
2104
+ }
2046
2105
  }
2047
2106
 
2048
2107
  // packages/sdk/index.ts
@@ -2073,7 +2132,7 @@ function createKillswitchAgent(opts) {
2073
2132
  const c = await config();
2074
2133
  const out = {};
2075
2134
  for (const [sym, raw] of Object.entries(a.balances)) {
2076
- const dec = sym === "ETH" ? 18 : c.tokens[sym]?.decimals ?? 18;
2135
+ const dec = sym === "ETH" ? 18 : (c.tokens[sym] ?? Object.values(c.tokens).find((t) => t.symbol === sym))?.decimals ?? 18;
2077
2136
  out[sym] = (Number(raw) / 10 ** dec).toString();
2078
2137
  }
2079
2138
  return out;
@@ -2128,18 +2187,18 @@ function createKillswitchAgent(opts) {
2128
2187
  const get = o.fetch ?? fetch;
2129
2188
  const first = await get(url, init);
2130
2189
  if (first.status !== 402) return { paid: false, status: first.status, body: await first.text(), note: "no payment was asked" };
2131
- const ask = await first.json().catch(() => null);
2132
- const network = X402_NETWORKS[c.chainId];
2190
+ const ask = await readX402Ask(first);
2191
+ const networks = x402NetworkIds(c.chainId);
2133
2192
  const tokens = Object.values(c.tokens);
2134
- const req = (ask?.accepts ?? []).find(
2135
- (r) => r.scheme === "exact" && (!network || r.network === network) && tokens.some((t2) => t2.address.toLowerCase() === r.asset?.toLowerCase())
2193
+ const req = (ask?.offers ?? []).find(
2194
+ (r) => r.scheme === "exact" && networks.includes(r.network) && tokens.some((t2) => t2.address.toLowerCase() === r.asset?.toLowerCase()) && (r.extra?.assetTransferMethod ?? "eip3009") === "eip3009"
2136
2195
  );
2137
- if (!req) {
2138
- const offered = (ask?.accepts ?? []).map((r) => `${r.scheme} on ${r.network}`).join(", ") || "nothing readable";
2139
- return { paid: false, status: 402, body: "", note: `this site asks for a payment this wallet can't make (it offers: ${offered}; this wallet pays on ${network ?? `chain ${c.chainId}`})` };
2196
+ if (!ask || !req) {
2197
+ const offered = (ask?.offers ?? []).map((r) => `${r.scheme} on ${r.network}${r.extra?.assetTransferMethod === "permit2" ? " (Permit2)" : ""}`).join(", ") || "nothing readable";
2198
+ return { paid: false, status: 402, body: "", note: `this site asks for a payment this wallet can't make (it offers: ${offered}; this wallet pays on ${networks.join(" / ")})` };
2140
2199
  }
2141
2200
  const t = tokens.find((x) => x.address.toLowerCase() === req.asset.toLowerCase());
2142
- const value = BigInt(req.maxAmountRequired);
2201
+ const value = BigInt(req.amount);
2143
2202
  const price = `${formatUnits2(value, t.decimals)} ${t.symbol}`;
2144
2203
  if (o.maxAmount !== void 0 && value > parseUnits(o.maxAmount, t.decimals)) {
2145
2204
  return { paid: false, status: 402, body: "", price, note: `the site asks ${price}, more than the ${o.maxAmount} ${t.symbol} you allowed for this request` };
@@ -2161,9 +2220,8 @@ function createKillswitchAgent(opts) {
2161
2220
  op = { ...op, status: after.status, txHash: after.tx_hash ?? void 0, reasons: after.reasons ?? op.reasons };
2162
2221
  }
2163
2222
  if (op.status !== "confirmed") return { paid: false, status: 402, body: "", price, op, note: `Killswitch did not approve the payment (${op.status})` };
2164
- const header = x402PaymentHeader({ network: req.network, authorization, signature: opts.agentId });
2165
- const paidRes = await get(url, { ...init, headers: { ...o.headers ?? {}, "X-PAYMENT": header } });
2166
- const receipt = paidRes.headers.get("x-payment-response");
2223
+ const payment = x402PaymentHeaders(ask, req, { authorization, signature: opts.agentId });
2224
+ const paidRes = await get(url, { ...init, headers: { ...o.headers ?? {}, ...payment } });
2167
2225
  return {
2168
2226
  paid: paidRes.ok,
2169
2227
  status: paidRes.status,
@@ -2171,7 +2229,8 @@ function createKillswitchAgent(opts) {
2171
2229
  price,
2172
2230
  payTo: req.payTo,
2173
2231
  op,
2174
- settlement: receipt ? JSON.parse(atob(receipt)) : void 0,
2232
+ settlement: readX402Receipt(paidRes),
2233
+ x402Version: req.version,
2175
2234
  note: paidRes.ok ? void 0 : "the site refused the payment after Killswitch approved it; the approval expires unused"
2176
2235
  };
2177
2236
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "killswitch-agentkit",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Killswitch for Coinbase AgentKit: give an AgentKit agent a wallet it can only spend inside the owner's on-chain limits.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",