@agentbadge/circle-payments 0.1.18 → 0.1.20
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/README.md +77 -0
- package/dist/buyer/accepts.d.ts +33 -0
- package/dist/buyer/accepts.js +150 -0
- package/dist/buyer/caps.d.ts +27 -0
- package/dist/buyer/caps.js +67 -0
- package/dist/buyer/client.d.ts +66 -0
- package/dist/buyer/client.js +282 -0
- package/dist/buyer/guard.d.ts +9 -0
- package/dist/buyer/guard.js +1 -0
- package/dist/buyer/paginate.d.ts +6 -0
- package/dist/buyer/paginate.js +21 -0
- package/dist/buyer/risk-guard.d.ts +10 -0
- package/dist/buyer/risk-guard.js +47 -0
- package/dist/buyer/session.d.ts +18 -0
- package/dist/buyer/session.js +31 -0
- package/dist/chains.js +1 -1
- package/dist/cli/pay.d.ts +7 -0
- package/dist/cli/pay.js +275 -0
- package/dist/escrow/erc8183.d.ts +215 -1
- package/dist/escrow/erc8183.js +181 -12
- package/dist/gateway/deposit.d.ts +1 -1
- package/dist/gateway-probe.d.ts +1 -1
- package/dist/identity/erc8004.d.ts +18 -0
- package/dist/identity/erc8004.js +18 -1
- package/dist/identity/verify-passport.d.ts +1 -0
- package/dist/identity/verify-passport.js +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/middleware.d.ts +11 -0
- package/dist/middleware.js +40 -2
- package/dist/signer/arc-signer.d.ts +29 -0
- package/dist/signer/arc-signer.js +8 -0
- package/dist/signer/env-key.d.ts +20 -0
- package/dist/signer/env-key.js +58 -0
- package/dist/signer/protocol.d.ts +46 -0
- package/dist/signer/protocol.js +34 -0
- package/dist/signer/socket.d.ts +19 -0
- package/dist/signer/socket.js +139 -0
- package/package.json +5 -2
package/dist/escrow/erc8183.js
CHANGED
|
@@ -256,6 +256,136 @@ export const ERC8183_ACP_ABI = [
|
|
|
256
256
|
anonymous: false,
|
|
257
257
|
},
|
|
258
258
|
];
|
|
259
|
+
export const ERC8183_REF_ABI = [
|
|
260
|
+
{
|
|
261
|
+
type: "function",
|
|
262
|
+
name: "createJob",
|
|
263
|
+
stateMutability: "nonpayable",
|
|
264
|
+
inputs: [
|
|
265
|
+
{ name: "provider", type: "address" },
|
|
266
|
+
{ name: "evaluator", type: "address" },
|
|
267
|
+
{ name: "expiredAt", type: "uint48" },
|
|
268
|
+
{ name: "description", type: "string" },
|
|
269
|
+
{ name: "hook", type: "address" },
|
|
270
|
+
{ name: "providerAgentId", type: "uint256" },
|
|
271
|
+
],
|
|
272
|
+
outputs: [{ name: "jobId", type: "uint256" }],
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
type: "function",
|
|
276
|
+
name: "setProvider",
|
|
277
|
+
stateMutability: "nonpayable",
|
|
278
|
+
inputs: [
|
|
279
|
+
{ name: "jobId", type: "uint256" },
|
|
280
|
+
{ name: "provider", type: "address" },
|
|
281
|
+
{ name: "agentId", type: "uint256" },
|
|
282
|
+
],
|
|
283
|
+
outputs: [],
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
type: "function",
|
|
287
|
+
name: "setBudget",
|
|
288
|
+
stateMutability: "nonpayable",
|
|
289
|
+
inputs: [
|
|
290
|
+
{ name: "jobId", type: "uint256" },
|
|
291
|
+
{ name: "token", type: "address" },
|
|
292
|
+
{ name: "amount", type: "uint256" },
|
|
293
|
+
{ name: "optParams", type: "bytes" },
|
|
294
|
+
],
|
|
295
|
+
outputs: [],
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
type: "function",
|
|
299
|
+
name: "fund",
|
|
300
|
+
stateMutability: "nonpayable",
|
|
301
|
+
inputs: [
|
|
302
|
+
{ name: "jobId", type: "uint256" },
|
|
303
|
+
{ name: "expectedToken", type: "address" },
|
|
304
|
+
{ name: "expectedBudget", type: "uint256" },
|
|
305
|
+
{ name: "optParams", type: "bytes" },
|
|
306
|
+
],
|
|
307
|
+
outputs: [],
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
type: "function",
|
|
311
|
+
name: "submit",
|
|
312
|
+
stateMutability: "nonpayable",
|
|
313
|
+
inputs: [
|
|
314
|
+
{ name: "jobId", type: "uint256" },
|
|
315
|
+
{ name: "deliverable", type: "bytes32" },
|
|
316
|
+
{ name: "optParams", type: "bytes" },
|
|
317
|
+
],
|
|
318
|
+
outputs: [],
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
type: "function",
|
|
322
|
+
name: "complete",
|
|
323
|
+
stateMutability: "nonpayable",
|
|
324
|
+
inputs: [
|
|
325
|
+
{ name: "jobId", type: "uint256" },
|
|
326
|
+
{ name: "reason", type: "bytes32" },
|
|
327
|
+
{ name: "optParams", type: "bytes" },
|
|
328
|
+
],
|
|
329
|
+
outputs: [],
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
type: "function",
|
|
333
|
+
name: "reject",
|
|
334
|
+
stateMutability: "nonpayable",
|
|
335
|
+
inputs: [
|
|
336
|
+
{ name: "jobId", type: "uint256" },
|
|
337
|
+
{ name: "reason", type: "bytes32" },
|
|
338
|
+
{ name: "optParams", type: "bytes" },
|
|
339
|
+
],
|
|
340
|
+
outputs: [],
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
type: "function",
|
|
344
|
+
name: "claimRefund",
|
|
345
|
+
stateMutability: "nonpayable",
|
|
346
|
+
inputs: [{ name: "jobId", type: "uint256" }],
|
|
347
|
+
outputs: [],
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
type: "function",
|
|
351
|
+
name: "getJob",
|
|
352
|
+
stateMutability: "view",
|
|
353
|
+
inputs: [{ name: "jobId", type: "uint256" }],
|
|
354
|
+
outputs: [
|
|
355
|
+
{
|
|
356
|
+
type: "tuple",
|
|
357
|
+
components: [
|
|
358
|
+
{ name: "client", type: "address" },
|
|
359
|
+
{ name: "status", type: "uint8" },
|
|
360
|
+
{ name: "provider", type: "address" },
|
|
361
|
+
{ name: "expiredAt", type: "uint48" },
|
|
362
|
+
{ name: "evaluator", type: "address" },
|
|
363
|
+
{ name: "submittedAt", type: "uint48" },
|
|
364
|
+
{ name: "budget", type: "uint256" },
|
|
365
|
+
{ name: "hook", type: "address" },
|
|
366
|
+
{ name: "paymentToken", type: "address" },
|
|
367
|
+
{ name: "providerAgentId", type: "uint256" },
|
|
368
|
+
{ name: "description", type: "string" },
|
|
369
|
+
{ name: "settledAmount", type: "uint256" },
|
|
370
|
+
{ name: "payoutReceiver", type: "address" },
|
|
371
|
+
],
|
|
372
|
+
},
|
|
373
|
+
],
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
type: "event",
|
|
377
|
+
name: "JobCreated",
|
|
378
|
+
inputs: [
|
|
379
|
+
{ indexed: true, name: "jobId", type: "uint256" },
|
|
380
|
+
{ indexed: true, name: "client", type: "address" },
|
|
381
|
+
{ indexed: true, name: "provider", type: "address" },
|
|
382
|
+
{ indexed: false, name: "evaluator", type: "address" },
|
|
383
|
+
{ indexed: false, name: "expiredAt", type: "uint48" },
|
|
384
|
+
{ indexed: false, name: "hook", type: "address" },
|
|
385
|
+
],
|
|
386
|
+
anonymous: false,
|
|
387
|
+
},
|
|
388
|
+
];
|
|
259
389
|
const ERC20_APPROVE_ABI = parseAbi([
|
|
260
390
|
"function approve(address spender, uint256 amount) returns (bool)",
|
|
261
391
|
]);
|
|
@@ -278,7 +408,7 @@ export class Erc8183Error extends Error {
|
|
|
278
408
|
const EMPTY_PARAMS = "0x";
|
|
279
409
|
function extractJobId(receipt) {
|
|
280
410
|
for (const log of receipt.logs) {
|
|
281
|
-
for (const abi of [ERC8183_ABI, ERC8183_ACP_ABI]) {
|
|
411
|
+
for (const abi of [ERC8183_ABI, ERC8183_ACP_ABI, ERC8183_REF_ABI]) {
|
|
282
412
|
try {
|
|
283
413
|
const decoded = decodeEventLog({
|
|
284
414
|
abi,
|
|
@@ -299,22 +429,36 @@ function extractJobId(receipt) {
|
|
|
299
429
|
export function createErc8183(cfg) {
|
|
300
430
|
const contract = cfg.contract ?? AGENTIC_COMMERCE_ARC_TESTNET;
|
|
301
431
|
const variant = cfg.variant ?? "circle";
|
|
302
|
-
const abi = variant === "acp"
|
|
432
|
+
const abi = variant === "acp"
|
|
433
|
+
? ERC8183_ACP_ABI
|
|
434
|
+
: variant === "ref"
|
|
435
|
+
? ERC8183_REF_ABI
|
|
436
|
+
: ERC8183_ABI;
|
|
303
437
|
return {
|
|
304
438
|
contract,
|
|
305
439
|
async createJob(wallet, args) {
|
|
306
440
|
try {
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
abi,
|
|
310
|
-
functionName: "createJob",
|
|
311
|
-
args: [
|
|
441
|
+
const createArgs = variant === "ref"
|
|
442
|
+
? [
|
|
312
443
|
args.provider,
|
|
313
444
|
args.evaluator,
|
|
314
445
|
args.expiredAt,
|
|
315
446
|
args.description,
|
|
316
447
|
args.hook ?? ZERO_HOOK,
|
|
317
|
-
|
|
448
|
+
args.providerAgentId ?? 0n,
|
|
449
|
+
]
|
|
450
|
+
: [
|
|
451
|
+
args.provider,
|
|
452
|
+
args.evaluator,
|
|
453
|
+
args.expiredAt,
|
|
454
|
+
args.description,
|
|
455
|
+
args.hook ?? ZERO_HOOK,
|
|
456
|
+
];
|
|
457
|
+
const txHash = await wallet.writeContract({
|
|
458
|
+
address: contract,
|
|
459
|
+
abi,
|
|
460
|
+
functionName: "createJob",
|
|
461
|
+
args: createArgs,
|
|
318
462
|
});
|
|
319
463
|
const receipt = await awaitReceipt(cfg.read, txHash);
|
|
320
464
|
return { jobId: extractJobId(receipt), txHash };
|
|
@@ -327,11 +471,14 @@ export function createErc8183(cfg) {
|
|
|
327
471
|
},
|
|
328
472
|
async setBudget(wallet, args) {
|
|
329
473
|
try {
|
|
474
|
+
const args2 = variant === "ref"
|
|
475
|
+
? [args.jobId, cfg.usdc, args.amount, args.optParams ?? EMPTY_PARAMS]
|
|
476
|
+
: [args.jobId, args.amount, args.optParams ?? EMPTY_PARAMS];
|
|
330
477
|
return await wallet.writeContract({
|
|
331
478
|
address: contract,
|
|
332
479
|
abi,
|
|
333
480
|
functionName: "setBudget",
|
|
334
|
-
args:
|
|
481
|
+
args: args2,
|
|
335
482
|
});
|
|
336
483
|
}
|
|
337
484
|
catch (e) {
|
|
@@ -347,9 +494,11 @@ export function createErc8183(cfg) {
|
|
|
347
494
|
args: [contract, args.amount],
|
|
348
495
|
});
|
|
349
496
|
await awaitReceipt(cfg.read, approveTx);
|
|
350
|
-
const fundArgs = variant === "
|
|
351
|
-
? [args.jobId, args.amount, args.optParams ?? EMPTY_PARAMS]
|
|
352
|
-
:
|
|
497
|
+
const fundArgs = variant === "ref"
|
|
498
|
+
? [args.jobId, cfg.usdc, args.amount, args.optParams ?? EMPTY_PARAMS]
|
|
499
|
+
: variant === "acp"
|
|
500
|
+
? [args.jobId, args.amount, args.optParams ?? EMPTY_PARAMS]
|
|
501
|
+
: [args.jobId, args.optParams ?? EMPTY_PARAMS];
|
|
353
502
|
const fundTx = await wallet.writeContract({
|
|
354
503
|
address: contract,
|
|
355
504
|
abi,
|
|
@@ -420,6 +569,26 @@ export function createErc8183(cfg) {
|
|
|
420
569
|
},
|
|
421
570
|
async getJob(jobId) {
|
|
422
571
|
try {
|
|
572
|
+
if (variant === "ref") {
|
|
573
|
+
const raw = (await cfg.read.readContract({
|
|
574
|
+
address: contract,
|
|
575
|
+
abi,
|
|
576
|
+
functionName: "getJob",
|
|
577
|
+
args: [jobId],
|
|
578
|
+
}));
|
|
579
|
+
return {
|
|
580
|
+
id: jobId,
|
|
581
|
+
client: raw.client,
|
|
582
|
+
provider: raw.provider,
|
|
583
|
+
evaluator: raw.evaluator,
|
|
584
|
+
description: raw.description,
|
|
585
|
+
budget: raw.budget,
|
|
586
|
+
expiredAt: raw.expiredAt,
|
|
587
|
+
status: JOB_STATUS[raw.status] ?? "Open",
|
|
588
|
+
hook: raw.hook,
|
|
589
|
+
token: raw.paymentToken,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
423
592
|
if (variant === "acp") {
|
|
424
593
|
const raw = (await cfg.read.readContract({
|
|
425
594
|
address: contract,
|
|
@@ -42,5 +42,5 @@ export declare function gatewayBalance(args: {
|
|
|
42
42
|
depositor: `0x${string}`;
|
|
43
43
|
chain: SupportedChain;
|
|
44
44
|
apiUrl?: string;
|
|
45
|
-
fetchImpl?:
|
|
45
|
+
fetchImpl?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
46
46
|
}): Promise<GatewayBalanceInfo | null>;
|
package/dist/gateway-probe.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export interface GatewayProbeOptions {
|
|
|
3
3
|
probePath?: string;
|
|
4
4
|
intervalMs?: number;
|
|
5
5
|
downMs?: number;
|
|
6
|
-
fetchImpl?:
|
|
6
|
+
fetchImpl?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
7
7
|
onChange?: (up: boolean, reason: string) => void;
|
|
8
8
|
}
|
|
9
9
|
export interface GatewayProbe {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { Hex } from "viem";
|
|
2
2
|
import type { ReadClient, WriteClient } from "../escrow/erc8183.js";
|
|
3
3
|
export declare const ERC8004_IDENTITY_REGISTRY: "0x8004A818BFB912233c491871b3d84c89A494BD9e";
|
|
4
|
+
export declare const ERC8004_IDENTITY_REGISTRY_ARC_MAINNET: "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
|
|
5
|
+
export declare function erc8004IdentityRegistry(chainId?: number): `0x${string}`;
|
|
4
6
|
export declare const ERC8004_REPUTATION_REGISTRY: "0x8004B663056A597Dffe9eCcC1965A193B7388713";
|
|
5
7
|
export declare const ERC8004_VALIDATION_REGISTRY: "0x8004Cb1BF31DAf7788923b405b754f57acEB4272";
|
|
6
8
|
export declare const ERC8004_ABI: readonly [{
|
|
@@ -15,6 +17,21 @@ export declare const ERC8004_ABI: readonly [{
|
|
|
15
17
|
readonly name: "agentId";
|
|
16
18
|
readonly type: "uint256";
|
|
17
19
|
}];
|
|
20
|
+
}, {
|
|
21
|
+
readonly type: "function";
|
|
22
|
+
readonly name: "transferFrom";
|
|
23
|
+
readonly stateMutability: "nonpayable";
|
|
24
|
+
readonly inputs: readonly [{
|
|
25
|
+
readonly name: "from";
|
|
26
|
+
readonly type: "address";
|
|
27
|
+
}, {
|
|
28
|
+
readonly name: "to";
|
|
29
|
+
readonly type: "address";
|
|
30
|
+
}, {
|
|
31
|
+
readonly name: "tokenId";
|
|
32
|
+
readonly type: "uint256";
|
|
33
|
+
}];
|
|
34
|
+
readonly outputs: readonly [];
|
|
18
35
|
}, {
|
|
19
36
|
readonly type: "function";
|
|
20
37
|
readonly name: "ownerOf";
|
|
@@ -160,6 +177,7 @@ export declare class Erc8004Error extends Error {
|
|
|
160
177
|
}
|
|
161
178
|
export interface Erc8004Config {
|
|
162
179
|
read: ReadClient;
|
|
180
|
+
chainId?: number;
|
|
163
181
|
identity?: `0x${string}`;
|
|
164
182
|
reputation?: `0x${string}`;
|
|
165
183
|
validation?: `0x${string}`;
|
package/dist/identity/erc8004.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { decodeEventLog } from "viem";
|
|
2
2
|
export const ERC8004_IDENTITY_REGISTRY = "0x8004A818BFB912233c491871b3d84c89A494BD9e";
|
|
3
|
+
export const ERC8004_IDENTITY_REGISTRY_ARC_MAINNET = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
|
|
4
|
+
export function erc8004IdentityRegistry(chainId) {
|
|
5
|
+
return chainId === 5042
|
|
6
|
+
? ERC8004_IDENTITY_REGISTRY_ARC_MAINNET
|
|
7
|
+
: ERC8004_IDENTITY_REGISTRY;
|
|
8
|
+
}
|
|
3
9
|
export const ERC8004_REPUTATION_REGISTRY = "0x8004B663056A597Dffe9eCcC1965A193B7388713";
|
|
4
10
|
export const ERC8004_VALIDATION_REGISTRY = "0x8004Cb1BF31DAf7788923b405b754f57acEB4272";
|
|
5
11
|
export const ERC8004_ABI = [
|
|
@@ -10,6 +16,17 @@ export const ERC8004_ABI = [
|
|
|
10
16
|
inputs: [{ name: "metadataURI", type: "string" }],
|
|
11
17
|
outputs: [{ name: "agentId", type: "uint256" }],
|
|
12
18
|
},
|
|
19
|
+
{
|
|
20
|
+
type: "function",
|
|
21
|
+
name: "transferFrom",
|
|
22
|
+
stateMutability: "nonpayable",
|
|
23
|
+
inputs: [
|
|
24
|
+
{ name: "from", type: "address" },
|
|
25
|
+
{ name: "to", type: "address" },
|
|
26
|
+
{ name: "tokenId", type: "uint256" },
|
|
27
|
+
],
|
|
28
|
+
outputs: [],
|
|
29
|
+
},
|
|
13
30
|
{
|
|
14
31
|
type: "function",
|
|
15
32
|
name: "ownerOf",
|
|
@@ -119,7 +136,7 @@ function extractAgentId(receipt) {
|
|
|
119
136
|
throw new Erc8004Error("Transfer event not found in receipt", "register");
|
|
120
137
|
}
|
|
121
138
|
export function createErc8004(cfg) {
|
|
122
|
-
const identity = cfg.identity ??
|
|
139
|
+
const identity = cfg.identity ?? erc8004IdentityRegistry(cfg.chainId);
|
|
123
140
|
const reputation = cfg.reputation ?? ERC8004_REPUTATION_REGISTRY;
|
|
124
141
|
const validation = cfg.validation ?? ERC8004_VALIDATION_REGISTRY;
|
|
125
142
|
return {
|
|
@@ -13,5 +13,6 @@ export interface PassportCheck {
|
|
|
13
13
|
interface PaymentRequiredLike {
|
|
14
14
|
extensions?: Record<string, unknown> | undefined;
|
|
15
15
|
}
|
|
16
|
+
export declare function decode402(input: string | PaymentRequiredLike): PaymentRequiredLike | undefined;
|
|
16
17
|
export declare function verifyPassport(paymentRequired: string | PaymentRequiredLike): PassportCheck;
|
|
17
18
|
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -22,3 +22,14 @@ export * from "./memo/memo.js";
|
|
|
22
22
|
export * from "./agent-wallet/index.js";
|
|
23
23
|
export * from "./payer-bind.js";
|
|
24
24
|
export * from "./chain/index.js";
|
|
25
|
+
export * from "./signer/arc-signer.js";
|
|
26
|
+
export * from "./signer/env-key.js";
|
|
27
|
+
export * from "./signer/protocol.js";
|
|
28
|
+
export * from "./signer/socket.js";
|
|
29
|
+
export * from "./buyer/caps.js";
|
|
30
|
+
export * from "./buyer/session.js";
|
|
31
|
+
export * from "./buyer/accepts.js";
|
|
32
|
+
export * from "./buyer/client.js";
|
|
33
|
+
export * from "./buyer/guard.js";
|
|
34
|
+
export * from "./buyer/risk-guard.js";
|
|
35
|
+
export * from "./buyer/paginate.js";
|
package/dist/index.js
CHANGED
|
@@ -22,3 +22,14 @@ export * from "./memo/memo.js";
|
|
|
22
22
|
export * from "./agent-wallet/index.js";
|
|
23
23
|
export * from "./payer-bind.js";
|
|
24
24
|
export * from "./chain/index.js";
|
|
25
|
+
export * from "./signer/arc-signer.js";
|
|
26
|
+
export * from "./signer/env-key.js";
|
|
27
|
+
export * from "./signer/protocol.js";
|
|
28
|
+
export * from "./signer/socket.js";
|
|
29
|
+
export * from "./buyer/caps.js";
|
|
30
|
+
export * from "./buyer/session.js";
|
|
31
|
+
export * from "./buyer/accepts.js";
|
|
32
|
+
export * from "./buyer/client.js";
|
|
33
|
+
export * from "./buyer/guard.js";
|
|
34
|
+
export * from "./buyer/risk-guard.js";
|
|
35
|
+
export * from "./buyer/paginate.js";
|
package/dist/middleware.d.ts
CHANGED
|
@@ -27,6 +27,17 @@ export interface RequirePaymentOptions extends PaymentRouterConfig {
|
|
|
27
27
|
}) => Promise<void> | void;
|
|
28
28
|
legacyHeader?: boolean;
|
|
29
29
|
extraRequirements?: Record<string, unknown>;
|
|
30
|
+
onRailError?: (args: {
|
|
31
|
+
c: Context;
|
|
32
|
+
phase: "verify" | "settle";
|
|
33
|
+
rail: string;
|
|
34
|
+
scheme: string;
|
|
35
|
+
network: string;
|
|
36
|
+
path: string;
|
|
37
|
+
requestId?: string;
|
|
38
|
+
message: string;
|
|
39
|
+
error: unknown;
|
|
40
|
+
}) => void;
|
|
30
41
|
}
|
|
31
42
|
export interface PaymentInfo {
|
|
32
43
|
payer?: string;
|
package/dist/middleware.js
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { recordFailure, } from "./ledger.js";
|
|
2
2
|
import { classifyRail, createPaymentRouter, } from "./router.js";
|
|
3
3
|
const b64encode = (o) => Buffer.from(JSON.stringify(o)).toString("base64");
|
|
4
|
+
function reportRailError(opts, c, phase, requirements, err) {
|
|
5
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
6
|
+
const info = {
|
|
7
|
+
phase,
|
|
8
|
+
rail: classifyRail(requirements) ?? "unknown",
|
|
9
|
+
scheme: requirements.scheme,
|
|
10
|
+
network: requirements.network,
|
|
11
|
+
path: c.req.path,
|
|
12
|
+
requestId: typeof c.get("requestId") === "string"
|
|
13
|
+
? c.get("requestId")
|
|
14
|
+
: undefined,
|
|
15
|
+
message,
|
|
16
|
+
};
|
|
17
|
+
if (opts.onRailError) {
|
|
18
|
+
opts.onRailError({ c, ...info, error: err });
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
console.error(`[x402] rail error ${JSON.stringify(info)}`);
|
|
22
|
+
}
|
|
4
23
|
function b64decode(s) {
|
|
5
24
|
try {
|
|
6
25
|
return JSON.parse(Buffer.from(s, "base64").toString("utf-8"));
|
|
@@ -110,7 +129,14 @@ export function requirePayment(price, opts) {
|
|
|
110
129
|
return paymentRequired(c, accepts, opts, `Price $${priceUsd} exceeds declared X-Max-Amount "${maxHeader}"`);
|
|
111
130
|
}
|
|
112
131
|
}
|
|
113
|
-
|
|
132
|
+
let verifyResult;
|
|
133
|
+
try {
|
|
134
|
+
verifyResult = await router.verify(paymentPayload, requirements);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
reportRailError(opts, c, "verify", requirements, err);
|
|
138
|
+
return paymentRequired(c, accepts, opts, `verify_unavailable: ${err instanceof Error ? err.message : String(err)}`);
|
|
139
|
+
}
|
|
114
140
|
if (!verifyResult.isValid) {
|
|
115
141
|
return paymentRequired(c, accepts, opts, verifyResult.invalidReason ?? "Payment verification failed");
|
|
116
142
|
}
|
|
@@ -124,7 +150,19 @@ export function requirePayment(price, opts) {
|
|
|
124
150
|
if (deny instanceof Response)
|
|
125
151
|
return deny;
|
|
126
152
|
}
|
|
127
|
-
|
|
153
|
+
let settleResult;
|
|
154
|
+
try {
|
|
155
|
+
settleResult = await router.settle(paymentPayload, requirements);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
reportRailError(opts, c, "settle", requirements, err);
|
|
159
|
+
await opts.onSettleResult?.({
|
|
160
|
+
c,
|
|
161
|
+
ok: false,
|
|
162
|
+
error: "settle_unavailable",
|
|
163
|
+
});
|
|
164
|
+
return paymentRequired(c, accepts, opts, `settle_unavailable: ${err instanceof Error ? err.message : String(err)}`);
|
|
165
|
+
}
|
|
128
166
|
if (!settleResult.success) {
|
|
129
167
|
await opts.onSettleResult?.({
|
|
130
168
|
c,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Hex } from "viem";
|
|
2
|
+
export interface SignerTxRequest {
|
|
3
|
+
to: `0x${string}`;
|
|
4
|
+
data: Hex;
|
|
5
|
+
value?: bigint;
|
|
6
|
+
}
|
|
7
|
+
export interface SignerWriteCall {
|
|
8
|
+
address: `0x${string}`;
|
|
9
|
+
abi: unknown;
|
|
10
|
+
functionName: string;
|
|
11
|
+
args: readonly unknown[];
|
|
12
|
+
}
|
|
13
|
+
export interface ArcSigner {
|
|
14
|
+
readonly address: `0x${string}`;
|
|
15
|
+
signMessage(message: string | {
|
|
16
|
+
raw: Hex;
|
|
17
|
+
}): Promise<Hex>;
|
|
18
|
+
signTypedData?(args: {
|
|
19
|
+
domain: Record<string, unknown>;
|
|
20
|
+
types: Record<string, unknown>;
|
|
21
|
+
primaryType: string;
|
|
22
|
+
message: Record<string, unknown>;
|
|
23
|
+
}): Promise<Hex>;
|
|
24
|
+
sendTransaction(tx: SignerTxRequest): Promise<Hex>;
|
|
25
|
+
}
|
|
26
|
+
export declare class SignerUnavailableError extends Error {
|
|
27
|
+
readonly operation: string;
|
|
28
|
+
constructor(operation: string, reason: string);
|
|
29
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Account, type Chain, type Hex } from "viem";
|
|
2
|
+
import { type ArcSigner, type SignerWriteCall } from "./arc-signer.js";
|
|
3
|
+
export interface EnvKeySignerOptions {
|
|
4
|
+
rpcUrl?: string;
|
|
5
|
+
chain?: Chain | null;
|
|
6
|
+
broadcast?: (tx: {
|
|
7
|
+
account: Account;
|
|
8
|
+
to: `0x${string}`;
|
|
9
|
+
data: Hex;
|
|
10
|
+
value?: bigint;
|
|
11
|
+
chain: Chain | null;
|
|
12
|
+
}) => Promise<Hex>;
|
|
13
|
+
}
|
|
14
|
+
export interface EnvKeySigner extends ArcSigner {
|
|
15
|
+
writeContract(call: SignerWriteCall): Promise<Hex>;
|
|
16
|
+
}
|
|
17
|
+
export declare function createEnvKeySigner(privateKey: `0x${string}`, opts?: EnvKeySignerOptions): EnvKeySigner;
|
|
18
|
+
export declare function arcSignerAsWriteClient(signer: ArcSigner): {
|
|
19
|
+
writeContract: (call: SignerWriteCall) => Promise<Hex>;
|
|
20
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createWalletClient, encodeFunctionData, http, } from "viem";
|
|
2
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
3
|
+
import { SignerUnavailableError, } from "./arc-signer.js";
|
|
4
|
+
const KEY_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
5
|
+
export function createEnvKeySigner(privateKey, opts = {}) {
|
|
6
|
+
if (!KEY_RE.test(privateKey)) {
|
|
7
|
+
throw new TypeError("createEnvKeySigner: expected 0x-prefixed 64-hex private key");
|
|
8
|
+
}
|
|
9
|
+
const account = privateKeyToAccount(privateKey);
|
|
10
|
+
const wallet = opts.rpcUrl && !opts.broadcast
|
|
11
|
+
? createWalletClient({
|
|
12
|
+
account,
|
|
13
|
+
transport: http(opts.rpcUrl),
|
|
14
|
+
...(opts.chain != null ? { chain: opts.chain } : {}),
|
|
15
|
+
})
|
|
16
|
+
: null;
|
|
17
|
+
const sendTransaction = async (tx) => {
|
|
18
|
+
const req = {
|
|
19
|
+
account,
|
|
20
|
+
to: tx.to,
|
|
21
|
+
data: tx.data,
|
|
22
|
+
...(tx.value !== undefined ? { value: tx.value } : {}),
|
|
23
|
+
chain: opts.chain ?? null,
|
|
24
|
+
};
|
|
25
|
+
if (opts.broadcast)
|
|
26
|
+
return opts.broadcast(req);
|
|
27
|
+
if (!wallet) {
|
|
28
|
+
throw new SignerUnavailableError("sendTransaction", "no rpcUrl or broadcast bound to this signer");
|
|
29
|
+
}
|
|
30
|
+
return wallet.sendTransaction(req);
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
address: account.address,
|
|
34
|
+
signMessage: (message) => account.signMessage(typeof message === "string" ? { message } : { message }),
|
|
35
|
+
signTypedData: (args) => account.signTypedData(args),
|
|
36
|
+
sendTransaction,
|
|
37
|
+
writeContract: (call) => sendTransaction({
|
|
38
|
+
to: call.address,
|
|
39
|
+
data: encodeFunctionData({
|
|
40
|
+
abi: call.abi,
|
|
41
|
+
functionName: call.functionName,
|
|
42
|
+
args: call.args,
|
|
43
|
+
}),
|
|
44
|
+
}),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function arcSignerAsWriteClient(signer) {
|
|
48
|
+
return {
|
|
49
|
+
writeContract: (call) => signer.sendTransaction({
|
|
50
|
+
to: call.address,
|
|
51
|
+
data: encodeFunctionData({
|
|
52
|
+
abi: call.abi,
|
|
53
|
+
functionName: call.functionName,
|
|
54
|
+
args: call.args,
|
|
55
|
+
}),
|
|
56
|
+
}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Hex } from "viem";
|
|
2
|
+
import type { SignerTxRequest } from "./arc-signer.js";
|
|
3
|
+
export declare const SIGNER_METHODS: readonly ["getAddress", "health", "signMessage", "signTypedData", "sendTransaction"];
|
|
4
|
+
export type SignerMethod = (typeof SIGNER_METHODS)[number];
|
|
5
|
+
export interface SignerRpcRequest {
|
|
6
|
+
id: string;
|
|
7
|
+
method: SignerMethod;
|
|
8
|
+
params?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export type SignerRpcResponse = {
|
|
11
|
+
id: string;
|
|
12
|
+
ok: true;
|
|
13
|
+
result: unknown;
|
|
14
|
+
} | {
|
|
15
|
+
id: string;
|
|
16
|
+
ok: false;
|
|
17
|
+
error: {
|
|
18
|
+
code: SignerErrorCode;
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export type SignerErrorCode = "policy_denied" | "unavailable" | "bad_request" | "internal";
|
|
23
|
+
export interface SignMessageParams {
|
|
24
|
+
message: string | {
|
|
25
|
+
raw: Hex;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface SignTypedDataParams {
|
|
29
|
+
domain: Record<string, unknown>;
|
|
30
|
+
types: Record<string, unknown>;
|
|
31
|
+
primaryType: string;
|
|
32
|
+
message: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export interface SendTransactionParams extends Omit<SignerTxRequest, "value"> {
|
|
35
|
+
value?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface HealthResult {
|
|
38
|
+
status: "ok";
|
|
39
|
+
address: `0x${string}`;
|
|
40
|
+
uptimeSec: number;
|
|
41
|
+
chainId?: number;
|
|
42
|
+
}
|
|
43
|
+
export declare function encodeRpcLine(msg: SignerRpcRequest | SignerRpcResponse): string;
|
|
44
|
+
export declare function createLineDecoder<T = SignerRpcRequest | SignerRpcResponse>(): {
|
|
45
|
+
push(chunk: string): T[];
|
|
46
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const SIGNER_METHODS = [
|
|
2
|
+
"getAddress",
|
|
3
|
+
"health",
|
|
4
|
+
"signMessage",
|
|
5
|
+
"signTypedData",
|
|
6
|
+
"sendTransaction",
|
|
7
|
+
];
|
|
8
|
+
const MAX_LINE_BYTES = 256 * 1024;
|
|
9
|
+
export function encodeRpcLine(msg) {
|
|
10
|
+
return `${JSON.stringify(msg)}\n`;
|
|
11
|
+
}
|
|
12
|
+
export function createLineDecoder() {
|
|
13
|
+
let buf = "";
|
|
14
|
+
return {
|
|
15
|
+
push(chunk) {
|
|
16
|
+
buf += chunk;
|
|
17
|
+
const out = [];
|
|
18
|
+
for (;;) {
|
|
19
|
+
const nl = buf.indexOf("\n");
|
|
20
|
+
if (nl < 0) {
|
|
21
|
+
if (buf.length > MAX_LINE_BYTES) {
|
|
22
|
+
throw new Error("signer rpc frame exceeds 256KB");
|
|
23
|
+
}
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
const line = buf.slice(0, nl).trim();
|
|
27
|
+
buf = buf.slice(nl + 1);
|
|
28
|
+
if (line.length > 0)
|
|
29
|
+
out.push(JSON.parse(line));
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|