@lucid-agents/opencode-x402-plugin 0.1.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.
- package/LICENSE +21 -0
- package/README.md +177 -0
- package/dist/cache.d.ts +30 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +66 -0
- package/dist/cache.js.map +1 -0
- package/dist/index.d.ts +98 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +491 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +154 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +227 -0
- package/dist/utils.js.map +1 -0
- package/package.json +72 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenCode x402 Auth Plugin
|
|
3
|
+
*
|
|
4
|
+
* Enables OpenCode to authenticate with the x402 Inference Router using
|
|
5
|
+
* wallet-signed ERC-2612 permits for crypto-native payment.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```json
|
|
9
|
+
* // opencode.json
|
|
10
|
+
* {
|
|
11
|
+
* "plugins": ["opencode-x402-auth"],
|
|
12
|
+
* "provider": {
|
|
13
|
+
* "x402": {
|
|
14
|
+
* "npm": "@ai-sdk/openai-compatible",
|
|
15
|
+
* "options": { "baseURL": "http://localhost:8080/v1" }
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { createWalletClient, http, } from "viem";
|
|
22
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
23
|
+
import { base, baseSepolia, mainnet } from "viem/chains";
|
|
24
|
+
import { PermitCache, shouldInvalidatePermit } from "./cache";
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Constants
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/** USDC contract addresses by network */
|
|
29
|
+
const USDC_ADDRESSES = {
|
|
30
|
+
"eip155:8453": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // Base Mainnet
|
|
31
|
+
"eip155:84532": "0x036CbD53842c5426634e7929541eC2318f3dCF7e", // Base Sepolia
|
|
32
|
+
"eip155:1": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // Ethereum Mainnet
|
|
33
|
+
};
|
|
34
|
+
/** Chain configs by CAIP-2 identifier */
|
|
35
|
+
const CHAINS = {
|
|
36
|
+
"eip155:8453": base,
|
|
37
|
+
"eip155:84532": baseSepolia,
|
|
38
|
+
"eip155:1": mainnet,
|
|
39
|
+
};
|
|
40
|
+
/** Default permit cap in token units ($0.10 USDC = 100,000 with 6 decimals) */
|
|
41
|
+
const DEFAULT_PERMIT_CAP = "100000";
|
|
42
|
+
/** Default permit validity (1 hour) */
|
|
43
|
+
const DEFAULT_VALIDITY_SECONDS = 3600;
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// EIP-712 Signing
|
|
46
|
+
// ============================================================================
|
|
47
|
+
/**
|
|
48
|
+
* EIP-712 domain for ERC-2612 Permit
|
|
49
|
+
*/
|
|
50
|
+
function getPermitDomain(tokenName, tokenVersion, chainId, verifyingContract) {
|
|
51
|
+
return {
|
|
52
|
+
name: tokenName,
|
|
53
|
+
version: tokenVersion,
|
|
54
|
+
chainId,
|
|
55
|
+
verifyingContract,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* EIP-712 types for ERC-2612 Permit
|
|
60
|
+
*/
|
|
61
|
+
const PERMIT_TYPES = {
|
|
62
|
+
Permit: [
|
|
63
|
+
{ name: "owner", type: "address" },
|
|
64
|
+
{ name: "spender", type: "address" },
|
|
65
|
+
{ name: "value", type: "uint256" },
|
|
66
|
+
{ name: "nonce", type: "uint256" },
|
|
67
|
+
{ name: "deadline", type: "uint256" },
|
|
68
|
+
],
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Generate a random nonce for the permit
|
|
72
|
+
*/
|
|
73
|
+
function generateNonce() {
|
|
74
|
+
const randomBytes = new Uint8Array(32);
|
|
75
|
+
crypto.getRandomValues(randomBytes);
|
|
76
|
+
// Use first 8 bytes as a numeric nonce (sufficient uniqueness)
|
|
77
|
+
const view = new DataView(randomBytes.buffer);
|
|
78
|
+
return view.getBigUint64(0, false).toString();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Sign an ERC-2612 permit using the wallet
|
|
82
|
+
*/
|
|
83
|
+
async function signPermit(wallet, account, config, permitCap) {
|
|
84
|
+
const chainId = parseInt(config.network.split(":")[1]);
|
|
85
|
+
const deadline = Math.floor(Date.now() / 1000) + DEFAULT_VALIDITY_SECONDS;
|
|
86
|
+
const nonce = generateNonce();
|
|
87
|
+
const domain = getPermitDomain(config.tokenName, config.tokenVersion, chainId, config.asset);
|
|
88
|
+
const message = {
|
|
89
|
+
owner: account.address,
|
|
90
|
+
spender: config.facilitatorSigner,
|
|
91
|
+
value: BigInt(permitCap),
|
|
92
|
+
nonce: BigInt(nonce),
|
|
93
|
+
deadline: BigInt(deadline),
|
|
94
|
+
};
|
|
95
|
+
const signature = await wallet.signTypedData({
|
|
96
|
+
account,
|
|
97
|
+
domain,
|
|
98
|
+
types: PERMIT_TYPES,
|
|
99
|
+
primaryType: "Permit",
|
|
100
|
+
message,
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
signature,
|
|
104
|
+
nonce,
|
|
105
|
+
deadline: deadline.toString(),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a complete x402 payment payload
|
|
110
|
+
*/
|
|
111
|
+
async function createPaymentPayload(wallet, account, config, permitCap) {
|
|
112
|
+
const { signature, nonce, deadline } = await signPermit(wallet, account, config, permitCap);
|
|
113
|
+
const payload = {
|
|
114
|
+
x402Version: 2,
|
|
115
|
+
accepted: {
|
|
116
|
+
scheme: "upto",
|
|
117
|
+
network: config.network,
|
|
118
|
+
asset: config.asset,
|
|
119
|
+
payTo: config.payTo,
|
|
120
|
+
extra: {
|
|
121
|
+
name: config.tokenName,
|
|
122
|
+
version: config.tokenVersion,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
payload: {
|
|
126
|
+
authorization: {
|
|
127
|
+
from: account.address,
|
|
128
|
+
to: config.facilitatorSigner,
|
|
129
|
+
value: permitCap,
|
|
130
|
+
validBefore: deadline,
|
|
131
|
+
nonce,
|
|
132
|
+
},
|
|
133
|
+
signature,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
// Base64 encode for the header
|
|
137
|
+
return btoa(JSON.stringify(payload));
|
|
138
|
+
}
|
|
139
|
+
// ============================================================================
|
|
140
|
+
// Permit Cache Helpers
|
|
141
|
+
// ============================================================================
|
|
142
|
+
async function createCachedPermit(wallet, account, config, permitCap) {
|
|
143
|
+
const { signature, nonce, deadline } = await signPermit(wallet, account, config, permitCap);
|
|
144
|
+
const payload = {
|
|
145
|
+
x402Version: 2,
|
|
146
|
+
accepted: {
|
|
147
|
+
scheme: "upto",
|
|
148
|
+
network: config.network,
|
|
149
|
+
asset: config.asset,
|
|
150
|
+
payTo: config.payTo,
|
|
151
|
+
extra: {
|
|
152
|
+
name: config.tokenName,
|
|
153
|
+
version: config.tokenVersion,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
payload: {
|
|
157
|
+
authorization: {
|
|
158
|
+
from: account.address,
|
|
159
|
+
to: config.facilitatorSigner,
|
|
160
|
+
value: permitCap,
|
|
161
|
+
validBefore: deadline,
|
|
162
|
+
nonce,
|
|
163
|
+
},
|
|
164
|
+
signature,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
return {
|
|
168
|
+
paymentSig: btoa(JSON.stringify(payload)),
|
|
169
|
+
deadline: parseInt(deadline, 10),
|
|
170
|
+
maxValue: permitCap,
|
|
171
|
+
nonce,
|
|
172
|
+
network: config.network,
|
|
173
|
+
asset: config.asset,
|
|
174
|
+
payTo: config.payTo,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function parseErrorResponse(data) {
|
|
178
|
+
if (!data || typeof data !== "object") {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
const record = data;
|
|
182
|
+
if (typeof record.code === "string" || typeof record.error === "string") {
|
|
183
|
+
return {
|
|
184
|
+
code: typeof record.code === "string" ? record.code : undefined,
|
|
185
|
+
error: typeof record.error === "string"
|
|
186
|
+
? record.error
|
|
187
|
+
: typeof record.message === "string"
|
|
188
|
+
? record.message
|
|
189
|
+
: undefined,
|
|
190
|
+
message: typeof record.message === "string" ? record.message : undefined,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
const nested = record.error;
|
|
194
|
+
if (nested && typeof nested === "object") {
|
|
195
|
+
const nestedRecord = nested;
|
|
196
|
+
return {
|
|
197
|
+
code: typeof nestedRecord.code === "string"
|
|
198
|
+
? nestedRecord.code
|
|
199
|
+
: typeof nestedRecord.type === "string"
|
|
200
|
+
? nestedRecord.type
|
|
201
|
+
: undefined,
|
|
202
|
+
error: typeof nestedRecord.message === "string"
|
|
203
|
+
? nestedRecord.message
|
|
204
|
+
: typeof nestedRecord.error === "string"
|
|
205
|
+
? nestedRecord.error
|
|
206
|
+
: undefined,
|
|
207
|
+
message: typeof nestedRecord.message === "string" ? nestedRecord.message : undefined,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
async function extractErrorResponse(response) {
|
|
213
|
+
try {
|
|
214
|
+
const data = await response.clone().json();
|
|
215
|
+
return parseErrorResponse(data);
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// ============================================================================
|
|
222
|
+
// Router Configuration
|
|
223
|
+
// ============================================================================
|
|
224
|
+
/** Cached router config */
|
|
225
|
+
let cachedRouterConfig = null;
|
|
226
|
+
let cachedRouterUrl = null;
|
|
227
|
+
/**
|
|
228
|
+
* Fetch router configuration from /v1/config endpoint
|
|
229
|
+
*/
|
|
230
|
+
async function fetchRouterConfig(routerUrl) {
|
|
231
|
+
// Return cached config if URL hasn't changed
|
|
232
|
+
if (cachedRouterConfig && cachedRouterUrl === routerUrl) {
|
|
233
|
+
return cachedRouterConfig;
|
|
234
|
+
}
|
|
235
|
+
const response = await fetch(`${routerUrl}/v1/config`);
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
throw new Error(`Failed to fetch router config: ${response.status}`);
|
|
238
|
+
}
|
|
239
|
+
const data = (await response.json());
|
|
240
|
+
// Extract config from nested response structure
|
|
241
|
+
// The API returns networks[] array with payment config per network
|
|
242
|
+
const network = data.networks?.[0];
|
|
243
|
+
const eip712 = data.eip712_config;
|
|
244
|
+
const config = {
|
|
245
|
+
network: network?.network_id || "eip155:8453",
|
|
246
|
+
asset: network?.asset?.address || USDC_ADDRESSES["eip155:8453"],
|
|
247
|
+
payTo: network?.pay_to || "",
|
|
248
|
+
facilitatorSigner: network?.pay_to || "", // Same as payTo for now
|
|
249
|
+
tokenName: eip712?.domain_name || "USD Coin",
|
|
250
|
+
tokenVersion: eip712?.domain_version || "2",
|
|
251
|
+
};
|
|
252
|
+
cachedRouterConfig = config;
|
|
253
|
+
cachedRouterUrl = routerUrl;
|
|
254
|
+
return config;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Clear the router config cache (for testing)
|
|
258
|
+
*/
|
|
259
|
+
export function clearRouterConfigCache() {
|
|
260
|
+
cachedRouterConfig = null;
|
|
261
|
+
cachedRouterUrl = null;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Get default router config for a network (fallback)
|
|
265
|
+
*/
|
|
266
|
+
function getDefaultConfig(network) {
|
|
267
|
+
return {
|
|
268
|
+
network,
|
|
269
|
+
asset: USDC_ADDRESSES[network] || USDC_ADDRESSES["eip155:8453"],
|
|
270
|
+
payTo: "", // Must be fetched from router
|
|
271
|
+
facilitatorSigner: "", // Must be fetched from router
|
|
272
|
+
tokenName: "USD Coin",
|
|
273
|
+
tokenVersion: "2",
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
// ============================================================================
|
|
277
|
+
// Plugin Implementation
|
|
278
|
+
// ============================================================================
|
|
279
|
+
/**
|
|
280
|
+
* OpenCode x402 Auth Plugin
|
|
281
|
+
*
|
|
282
|
+
* Provides wallet-based authentication for x402 payment protocol.
|
|
283
|
+
* Signs ERC-2612 permits to authorize spending on each request.
|
|
284
|
+
*/
|
|
285
|
+
export async function X402AuthPlugin({ client }) {
|
|
286
|
+
return {
|
|
287
|
+
auth: {
|
|
288
|
+
provider: "x402",
|
|
289
|
+
/**
|
|
290
|
+
* Loader function - called to configure authentication for requests
|
|
291
|
+
*/
|
|
292
|
+
loader: async (getAuth, provider) => {
|
|
293
|
+
const auth = await getAuth();
|
|
294
|
+
// No auth configured yet
|
|
295
|
+
if (!auth || auth.type !== "wallet" || !auth.privateKey) {
|
|
296
|
+
return {};
|
|
297
|
+
}
|
|
298
|
+
// Get router URL from provider options or auth config
|
|
299
|
+
const routerUrl = provider.options?.baseURL?.replace(/\/v1\/?$/, "") ||
|
|
300
|
+
auth.routerUrl ||
|
|
301
|
+
"http://localhost:8080";
|
|
302
|
+
// Initialize wallet
|
|
303
|
+
const account = privateKeyToAccount(auth.privateKey);
|
|
304
|
+
const network = auth.network || "eip155:8453";
|
|
305
|
+
const chain = CHAINS[network] || base;
|
|
306
|
+
const wallet = createWalletClient({
|
|
307
|
+
account,
|
|
308
|
+
chain,
|
|
309
|
+
transport: http(),
|
|
310
|
+
});
|
|
311
|
+
// Get permit cap from config
|
|
312
|
+
const permitCap = auth.permitCap || DEFAULT_PERMIT_CAP;
|
|
313
|
+
// Fetch router config
|
|
314
|
+
let routerConfig;
|
|
315
|
+
try {
|
|
316
|
+
routerConfig = await fetchRouterConfig(routerUrl);
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
console.error("[x402-auth] Failed to fetch router config:", error);
|
|
320
|
+
routerConfig = getDefaultConfig(network);
|
|
321
|
+
}
|
|
322
|
+
const permitCache = new PermitCache();
|
|
323
|
+
const getOrCreatePermit = async () => {
|
|
324
|
+
const cached = permitCache.get(routerConfig.network, routerConfig.asset, routerConfig.payTo);
|
|
325
|
+
if (cached) {
|
|
326
|
+
return cached;
|
|
327
|
+
}
|
|
328
|
+
const fresh = await createCachedPermit(wallet, account, routerConfig, permitCap);
|
|
329
|
+
permitCache.set(fresh);
|
|
330
|
+
return fresh;
|
|
331
|
+
};
|
|
332
|
+
// Return custom fetch that injects payment signature
|
|
333
|
+
return {
|
|
334
|
+
fetch: async (input, init) => {
|
|
335
|
+
const url = typeof input === "string" ? input : input.toString();
|
|
336
|
+
// Only inject payment header for API requests to the router
|
|
337
|
+
if (!url.includes("/v1/") || url.includes("/v1/config") || url.includes("/v1/models")) {
|
|
338
|
+
return fetch(input, init);
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
const sendWithPermit = async (permit) => {
|
|
342
|
+
const headers = new Headers(init?.headers);
|
|
343
|
+
headers.set("PAYMENT-SIGNATURE", permit.paymentSig);
|
|
344
|
+
return fetch(input, { ...init, headers });
|
|
345
|
+
};
|
|
346
|
+
const permit = await getOrCreatePermit();
|
|
347
|
+
const response = await sendWithPermit(permit);
|
|
348
|
+
if (response.status === 402 || response.status === 401) {
|
|
349
|
+
const error = await extractErrorResponse(response);
|
|
350
|
+
if (error && shouldInvalidatePermit(error)) {
|
|
351
|
+
permitCache.invalidate(routerConfig.network, routerConfig.asset, routerConfig.payTo);
|
|
352
|
+
const refreshed = await getOrCreatePermit();
|
|
353
|
+
return sendWithPermit(refreshed);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return response;
|
|
357
|
+
}
|
|
358
|
+
catch (error) {
|
|
359
|
+
console.error("[x402-auth] Failed to create payment signature:", error);
|
|
360
|
+
// Fall back to request without payment (will get 402)
|
|
361
|
+
return fetch(input, init);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
},
|
|
366
|
+
/**
|
|
367
|
+
* Authentication methods available in /connect
|
|
368
|
+
*/
|
|
369
|
+
methods: [
|
|
370
|
+
{
|
|
371
|
+
label: "Connect Wallet (Private Key)",
|
|
372
|
+
type: "manual",
|
|
373
|
+
authorize: async () => {
|
|
374
|
+
return {
|
|
375
|
+
instructions: "Enter your wallet private key (0x-prefixed hex string).\n" +
|
|
376
|
+
"This will be stored locally and used to sign payment permits.\n" +
|
|
377
|
+
"WARNING: Never share your private key. Use a dedicated wallet for AI spending.",
|
|
378
|
+
method: "key",
|
|
379
|
+
callback: async (privateKey) => {
|
|
380
|
+
try {
|
|
381
|
+
// Validate private key format
|
|
382
|
+
if (!privateKey.startsWith("0x") || privateKey.length !== 66) {
|
|
383
|
+
console.error("[x402-auth] Invalid private key format");
|
|
384
|
+
return { type: "failed" };
|
|
385
|
+
}
|
|
386
|
+
// Verify we can create an account from it
|
|
387
|
+
const account = privateKeyToAccount(privateKey);
|
|
388
|
+
console.log(`[x402-auth] Wallet connected: ${account.address}`);
|
|
389
|
+
// Store auth config
|
|
390
|
+
await client.auth.set({
|
|
391
|
+
path: { id: "x402" },
|
|
392
|
+
body: {
|
|
393
|
+
type: "wallet",
|
|
394
|
+
privateKey,
|
|
395
|
+
routerUrl: "http://localhost:8080",
|
|
396
|
+
permitCap: DEFAULT_PERMIT_CAP,
|
|
397
|
+
network: "eip155:8453",
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
return { type: "success", key: account.address };
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
console.error("[x402-auth] Failed to connect wallet:", error);
|
|
404
|
+
return { type: "failed" };
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
label: "Configure Router URL",
|
|
412
|
+
type: "manual",
|
|
413
|
+
authorize: async () => {
|
|
414
|
+
return {
|
|
415
|
+
instructions: "Enter the x402 router URL (e.g., http://localhost:8080 or https://router.example.com)",
|
|
416
|
+
method: "key",
|
|
417
|
+
callback: async (routerUrl) => {
|
|
418
|
+
try {
|
|
419
|
+
// Validate URL
|
|
420
|
+
const url = new URL(routerUrl);
|
|
421
|
+
// Get existing auth
|
|
422
|
+
const existingAuth = await client.auth.get({ path: { id: "x402" } });
|
|
423
|
+
// Update with new router URL
|
|
424
|
+
await client.auth.set({
|
|
425
|
+
path: { id: "x402" },
|
|
426
|
+
body: {
|
|
427
|
+
...existingAuth,
|
|
428
|
+
type: existingAuth?.type || "wallet",
|
|
429
|
+
routerUrl: url.origin,
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
// Clear cached config so it refetches
|
|
433
|
+
cachedRouterConfig = null;
|
|
434
|
+
cachedRouterUrl = null;
|
|
435
|
+
return { type: "success" };
|
|
436
|
+
}
|
|
437
|
+
catch (error) {
|
|
438
|
+
console.error("[x402-auth] Invalid router URL:", error);
|
|
439
|
+
return { type: "failed" };
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
};
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
label: "Set Permit Cap",
|
|
447
|
+
type: "manual",
|
|
448
|
+
authorize: async () => {
|
|
449
|
+
return {
|
|
450
|
+
instructions: "Enter the maximum USDC spending cap per permit session.\n" +
|
|
451
|
+
"Examples: 1 = $1.00, 10 = $10.00, 100 = $100.00\n" +
|
|
452
|
+
"This limits how much can be spent before a new permit is needed.",
|
|
453
|
+
method: "key",
|
|
454
|
+
callback: async (capInput) => {
|
|
455
|
+
try {
|
|
456
|
+
const cap = parseFloat(capInput);
|
|
457
|
+
if (isNaN(cap) || cap <= 0) {
|
|
458
|
+
console.error("[x402-auth] Invalid cap amount");
|
|
459
|
+
return { type: "failed" };
|
|
460
|
+
}
|
|
461
|
+
// Convert to USDC units (6 decimals)
|
|
462
|
+
const permitCap = Math.floor(cap * 1_000_000).toString();
|
|
463
|
+
// Get existing auth
|
|
464
|
+
const existingAuth = await client.auth.get({ path: { id: "x402" } });
|
|
465
|
+
// Update with new cap
|
|
466
|
+
await client.auth.set({
|
|
467
|
+
path: { id: "x402" },
|
|
468
|
+
body: {
|
|
469
|
+
...existingAuth,
|
|
470
|
+
type: existingAuth?.type || "wallet",
|
|
471
|
+
permitCap,
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
console.log(`[x402-auth] Permit cap set to $${cap} (${permitCap} units)`);
|
|
475
|
+
return { type: "success" };
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
console.error("[x402-auth] Failed to set permit cap:", error);
|
|
479
|
+
return { type: "failed" };
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
],
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
// Default export for OpenCode plugin system
|
|
490
|
+
export default X402AuthPlugin;
|
|
491
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACL,kBAAkB,EAClB,IAAI,GAIL,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAyC,MAAM,SAAS,CAAC;AAsFrG,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,yCAAyC;AACzC,MAAM,cAAc,GAAkC;IACpD,aAAa,EAAE,4CAA4C,EAAE,eAAe;IAC5E,cAAc,EAAE,4CAA4C,EAAE,eAAe;IAC7E,UAAU,EAAE,4CAA4C,EAAE,mBAAmB;CAC9E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,GAAG,IAAI,CAAC,EAAE,CAAC;wCAC3B,OAAO,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;wCAChD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;oCAC5B,CAAC;oCAED,qCAAqC;oCACrC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAC;oCAEzD,oBAAoB;oCACpB,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;oCAErE,sBAAsB;oCACtB,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;wCACpB,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE;wCACpB,IAAI,EAAE;4CACJ,GAAG,YAAY;4CACf,IAAI,EAAE,YAAY,EAAE,IAAI,IAAI,QAAQ;4CACpC,SAAS;yCACE;qCACd,CAAC,CAAC;oCAEH,OAAO,CAAC,GAAG,CAAC,kCAAkC,GAAG,KAAK,SAAS,SAAS,CAAC,CAAC;oCAC1E,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;gCAC7B,CAAC;gCAAC,OAAO,KAAK,EAAE,CAAC;oCACf,OAAO,CAAC,KAAK,CAAC,uCAAuC,EAAE,KAAK,CAAC,CAAC;oCAC9D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;gCAC5B,CAAC;4BACH,CAAC;yBACF,CAAC;oBACJ,CAAC;iBACF;aACF;SACF;KACF,CAAC;AACJ,CAAC;AAED,4CAA4C;AAC5C,eAAe,cAAc,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for x402 auth plugin
|
|
3
|
+
* Exported separately for testing
|
|
4
|
+
*/
|
|
5
|
+
import { type WalletClient, type Account, type Chain } from "viem";
|
|
6
|
+
/** Router configuration from /v1/config endpoint */
|
|
7
|
+
export interface RouterConfig {
|
|
8
|
+
network: string;
|
|
9
|
+
asset: string;
|
|
10
|
+
payTo: string;
|
|
11
|
+
facilitatorSigner: string;
|
|
12
|
+
tokenName: string;
|
|
13
|
+
tokenVersion: string;
|
|
14
|
+
}
|
|
15
|
+
/** x402 Payment Payload structure */
|
|
16
|
+
export interface PaymentPayload {
|
|
17
|
+
x402Version: 2;
|
|
18
|
+
accepted: {
|
|
19
|
+
scheme: "upto";
|
|
20
|
+
network: string;
|
|
21
|
+
asset: string;
|
|
22
|
+
payTo: string;
|
|
23
|
+
extra: {
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
payload: {
|
|
29
|
+
authorization: {
|
|
30
|
+
from: string;
|
|
31
|
+
to: string;
|
|
32
|
+
value: string;
|
|
33
|
+
validBefore: string;
|
|
34
|
+
nonce: string;
|
|
35
|
+
};
|
|
36
|
+
signature: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Router config API response (matches actual /v1/config structure) */
|
|
40
|
+
export interface RouterConfigResponse {
|
|
41
|
+
api_version?: string;
|
|
42
|
+
networks?: Array<{
|
|
43
|
+
network_id?: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
chain_id?: number;
|
|
46
|
+
asset?: {
|
|
47
|
+
address?: string;
|
|
48
|
+
symbol?: string;
|
|
49
|
+
decimals?: number;
|
|
50
|
+
};
|
|
51
|
+
pay_to?: string;
|
|
52
|
+
active?: boolean;
|
|
53
|
+
}>;
|
|
54
|
+
payment_required?: boolean;
|
|
55
|
+
payment_header?: string;
|
|
56
|
+
eip712_config?: {
|
|
57
|
+
domain_name?: string;
|
|
58
|
+
domain_version?: string;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** USDC contract addresses by network */
|
|
62
|
+
export declare const USDC_ADDRESSES: Record<string, `0x${string}`>;
|
|
63
|
+
/** Chain configs by CAIP-2 identifier */
|
|
64
|
+
export declare const CHAINS: Record<string, Chain>;
|
|
65
|
+
/** Default permit cap in token units ($0.10 USDC = 100,000 with 6 decimals) */
|
|
66
|
+
export declare const DEFAULT_PERMIT_CAP = "100000";
|
|
67
|
+
/** Default permit validity (1 hour) */
|
|
68
|
+
export declare const DEFAULT_VALIDITY_SECONDS = 3600;
|
|
69
|
+
/**
|
|
70
|
+
* EIP-712 domain for ERC-2612 Permit
|
|
71
|
+
*/
|
|
72
|
+
export declare function getPermitDomain(tokenName: string, tokenVersion: string, chainId: number, verifyingContract: `0x${string}`): {
|
|
73
|
+
readonly name: string;
|
|
74
|
+
readonly version: string;
|
|
75
|
+
readonly chainId: number;
|
|
76
|
+
readonly verifyingContract: `0x${string}`;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* EIP-712 types for ERC-2612 Permit
|
|
80
|
+
*/
|
|
81
|
+
export declare const PERMIT_TYPES: {
|
|
82
|
+
readonly Permit: readonly [{
|
|
83
|
+
readonly name: "owner";
|
|
84
|
+
readonly type: "address";
|
|
85
|
+
}, {
|
|
86
|
+
readonly name: "spender";
|
|
87
|
+
readonly type: "address";
|
|
88
|
+
}, {
|
|
89
|
+
readonly name: "value";
|
|
90
|
+
readonly type: "uint256";
|
|
91
|
+
}, {
|
|
92
|
+
readonly name: "nonce";
|
|
93
|
+
readonly type: "uint256";
|
|
94
|
+
}, {
|
|
95
|
+
readonly name: "deadline";
|
|
96
|
+
readonly type: "uint256";
|
|
97
|
+
}];
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Generate a random nonce for the permit
|
|
101
|
+
*/
|
|
102
|
+
export declare function generateNonce(): string;
|
|
103
|
+
/**
|
|
104
|
+
* Sign an ERC-2612 permit using the wallet
|
|
105
|
+
*/
|
|
106
|
+
export declare function signPermit(wallet: WalletClient, account: Account, config: RouterConfig, permitCap: string): Promise<{
|
|
107
|
+
signature: string;
|
|
108
|
+
nonce: string;
|
|
109
|
+
deadline: string;
|
|
110
|
+
}>;
|
|
111
|
+
/**
|
|
112
|
+
* Create a complete x402 payment payload
|
|
113
|
+
*/
|
|
114
|
+
export declare function createPaymentPayload(wallet: WalletClient, account: Account, config: RouterConfig, permitCap: string): Promise<string>;
|
|
115
|
+
/**
|
|
116
|
+
* Decode a payment payload from base64
|
|
117
|
+
*/
|
|
118
|
+
export declare function decodePaymentPayload(encoded: string): PaymentPayload;
|
|
119
|
+
/**
|
|
120
|
+
* Clear the router config cache (for testing)
|
|
121
|
+
*/
|
|
122
|
+
export declare function clearRouterConfigCache(): void;
|
|
123
|
+
/**
|
|
124
|
+
* Parse router config response into RouterConfig
|
|
125
|
+
*/
|
|
126
|
+
export declare function parseRouterConfigResponse(data: RouterConfigResponse): RouterConfig;
|
|
127
|
+
/**
|
|
128
|
+
* Fetch router configuration from /v1/config endpoint
|
|
129
|
+
*/
|
|
130
|
+
export declare function fetchRouterConfig(routerUrl: string): Promise<RouterConfig>;
|
|
131
|
+
/**
|
|
132
|
+
* Get default router config for a network (fallback)
|
|
133
|
+
*/
|
|
134
|
+
export declare function getDefaultConfig(network: string): RouterConfig;
|
|
135
|
+
/**
|
|
136
|
+
* Validate a private key format
|
|
137
|
+
*/
|
|
138
|
+
export declare function isValidPrivateKey(privateKey: string): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Create a wallet client from a private key
|
|
141
|
+
*/
|
|
142
|
+
export declare function createWalletFromPrivateKey(privateKey: `0x${string}`, network?: string): {
|
|
143
|
+
wallet: WalletClient;
|
|
144
|
+
account: Account;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Convert USDC amount to token units (6 decimals)
|
|
148
|
+
*/
|
|
149
|
+
export declare function usdcToUnits(amount: number): string;
|
|
150
|
+
/**
|
|
151
|
+
* Convert token units to USDC amount
|
|
152
|
+
*/
|
|
153
|
+
export declare function unitsToUsdc(units: string): number;
|
|
154
|
+
//# sourceMappingURL=utils.d.ts.map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