opentool 0.10.1 → 0.10.3
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/dist/cli/index.d.ts +3 -4
- package/dist/cli/index.js +93 -78
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +436 -417
- package/dist/index.js.map +1 -1
- package/dist/payment-orkZA9se.d.ts +96 -0
- package/dist/{validate-BgNU5laL.d.ts → validate-DbhJ_r0Z.d.ts} +1 -1
- package/dist/x402/index.d.ts +2 -96
- package/dist/x402/index.js +157 -157
- package/dist/x402/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/base/package.json +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
interface X402VerificationResult {
|
|
2
|
+
success: boolean;
|
|
3
|
+
metadata?: {
|
|
4
|
+
optionId: string;
|
|
5
|
+
verifier: string;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
failure?: {
|
|
9
|
+
reason: string;
|
|
10
|
+
code: string;
|
|
11
|
+
};
|
|
12
|
+
responseHeaders?: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
interface X402FacilitatorConfig {
|
|
15
|
+
url: string;
|
|
16
|
+
verifyPath?: string;
|
|
17
|
+
settlePath?: string;
|
|
18
|
+
apiKeyHeader?: string;
|
|
19
|
+
}
|
|
20
|
+
interface CurrencySpec {
|
|
21
|
+
decimals: number;
|
|
22
|
+
symbol: string;
|
|
23
|
+
network: string;
|
|
24
|
+
assetAddress: string;
|
|
25
|
+
}
|
|
26
|
+
declare const SUPPORTED_CURRENCIES: Record<string, CurrencySpec>;
|
|
27
|
+
declare const DEFAULT_FACILITATOR: X402FacilitatorConfig;
|
|
28
|
+
|
|
29
|
+
interface X402PaymentDefinition {
|
|
30
|
+
amount: string;
|
|
31
|
+
currency: {
|
|
32
|
+
code: string;
|
|
33
|
+
symbol: string;
|
|
34
|
+
decimals: number;
|
|
35
|
+
};
|
|
36
|
+
asset: {
|
|
37
|
+
symbol: string;
|
|
38
|
+
network: string;
|
|
39
|
+
address: string;
|
|
40
|
+
decimals: number;
|
|
41
|
+
};
|
|
42
|
+
payTo: string;
|
|
43
|
+
resource?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
scheme: string;
|
|
46
|
+
network: string;
|
|
47
|
+
facilitator: X402FacilitatorConfig;
|
|
48
|
+
metadata?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
declare const PAYMENT_HEADERS: readonly ["X-PAYMENT", "X-PAYMENT-RESPONSE"];
|
|
51
|
+
|
|
52
|
+
interface DefineX402PaymentConfig {
|
|
53
|
+
amount: string | number;
|
|
54
|
+
payTo: string;
|
|
55
|
+
currency?: string;
|
|
56
|
+
message?: string;
|
|
57
|
+
resource?: string;
|
|
58
|
+
network?: string;
|
|
59
|
+
assetAddress?: string;
|
|
60
|
+
scheme?: "exact" | "bounded";
|
|
61
|
+
facilitator?: string | X402FacilitatorConfig;
|
|
62
|
+
metadata?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
interface X402Payment {
|
|
65
|
+
definition: X402PaymentDefinition;
|
|
66
|
+
metadata?: Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
interface RequireX402PaymentOptions {
|
|
69
|
+
settle?: boolean;
|
|
70
|
+
fetchImpl?: typeof fetch;
|
|
71
|
+
onFailure?: (result: X402VerificationResult) => Response;
|
|
72
|
+
}
|
|
73
|
+
interface RequireX402PaymentSuccess {
|
|
74
|
+
payment: {
|
|
75
|
+
optionId: string;
|
|
76
|
+
verifier: string;
|
|
77
|
+
amount: string;
|
|
78
|
+
currency: string;
|
|
79
|
+
network: string;
|
|
80
|
+
};
|
|
81
|
+
headers: Record<string, string>;
|
|
82
|
+
result: X402VerificationResult;
|
|
83
|
+
}
|
|
84
|
+
type RequireX402PaymentOutcome = Response | RequireX402PaymentSuccess;
|
|
85
|
+
declare class X402PaymentRequiredError extends Error {
|
|
86
|
+
readonly response: Response;
|
|
87
|
+
readonly verification: X402VerificationResult | undefined;
|
|
88
|
+
constructor(response: Response, verification?: X402VerificationResult);
|
|
89
|
+
}
|
|
90
|
+
type X402PaymentContext = RequireX402PaymentSuccess;
|
|
91
|
+
declare function getX402PaymentContext(request: Request): X402PaymentContext | undefined;
|
|
92
|
+
declare function defineX402Payment(config: DefineX402PaymentConfig): X402Payment;
|
|
93
|
+
declare function requireX402Payment(request: Request, payment: X402Payment | X402PaymentDefinition, options?: RequireX402PaymentOptions): Promise<RequireX402PaymentOutcome>;
|
|
94
|
+
declare function withX402Payment(handler: (request: Request) => Promise<Response> | Response, payment: X402Payment | X402PaymentDefinition, options?: RequireX402PaymentOptions): (request: Request) => Promise<Response>;
|
|
95
|
+
|
|
96
|
+
export { type CurrencySpec as C, DEFAULT_FACILITATOR as D, PAYMENT_HEADERS as P, type RequireX402PaymentOptions as R, SUPPORTED_CURRENCIES as S, type X402Payment as X, type DefineX402PaymentConfig as a, type RequireX402PaymentOutcome as b, type RequireX402PaymentSuccess as c, type X402FacilitatorConfig as d, type X402PaymentContext as e, type X402PaymentDefinition as f, X402PaymentRequiredError as g, type X402VerificationResult as h, defineX402Payment as i, getX402PaymentContext as j, requireX402Payment as r, withX402Payment as w };
|
package/dist/x402/index.d.ts
CHANGED
|
@@ -1,56 +1,6 @@
|
|
|
1
|
+
export { C as CurrencySpec, D as DEFAULT_FACILITATOR, a as DefineX402PaymentConfig, P as PAYMENT_HEADERS, R as RequireX402PaymentOptions, b as RequireX402PaymentOutcome, c as RequireX402PaymentSuccess, S as SUPPORTED_CURRENCIES, d as X402FacilitatorConfig, X as X402Payment, e as X402PaymentContext, f as X402PaymentDefinition, g as X402PaymentRequiredError, h as X402VerificationResult, i as defineX402Payment, j as getX402PaymentContext, r as requireX402Payment, w as withX402Payment } from '../payment-orkZA9se.js';
|
|
1
2
|
import { Address, WalletClient } from 'viem';
|
|
2
3
|
|
|
3
|
-
interface X402VerificationResult {
|
|
4
|
-
success: boolean;
|
|
5
|
-
metadata?: {
|
|
6
|
-
optionId: string;
|
|
7
|
-
verifier: string;
|
|
8
|
-
[key: string]: unknown;
|
|
9
|
-
};
|
|
10
|
-
failure?: {
|
|
11
|
-
reason: string;
|
|
12
|
-
code: string;
|
|
13
|
-
};
|
|
14
|
-
responseHeaders?: Record<string, string>;
|
|
15
|
-
}
|
|
16
|
-
interface X402FacilitatorConfig {
|
|
17
|
-
url: string;
|
|
18
|
-
verifyPath?: string;
|
|
19
|
-
settlePath?: string;
|
|
20
|
-
apiKeyHeader?: string;
|
|
21
|
-
}
|
|
22
|
-
interface CurrencySpec {
|
|
23
|
-
decimals: number;
|
|
24
|
-
symbol: string;
|
|
25
|
-
network: string;
|
|
26
|
-
assetAddress: string;
|
|
27
|
-
}
|
|
28
|
-
declare const SUPPORTED_CURRENCIES: Record<string, CurrencySpec>;
|
|
29
|
-
declare const DEFAULT_FACILITATOR: X402FacilitatorConfig;
|
|
30
|
-
|
|
31
|
-
interface X402PaymentDefinition {
|
|
32
|
-
amount: string;
|
|
33
|
-
currency: {
|
|
34
|
-
code: string;
|
|
35
|
-
symbol: string;
|
|
36
|
-
decimals: number;
|
|
37
|
-
};
|
|
38
|
-
asset: {
|
|
39
|
-
symbol: string;
|
|
40
|
-
network: string;
|
|
41
|
-
address: string;
|
|
42
|
-
decimals: number;
|
|
43
|
-
};
|
|
44
|
-
payTo: string;
|
|
45
|
-
resource?: string;
|
|
46
|
-
description?: string;
|
|
47
|
-
scheme: string;
|
|
48
|
-
network: string;
|
|
49
|
-
facilitator: X402FacilitatorConfig;
|
|
50
|
-
metadata?: Record<string, unknown>;
|
|
51
|
-
}
|
|
52
|
-
declare const PAYMENT_HEADERS: readonly ["X-PAYMENT", "X-PAYMENT-RESPONSE"];
|
|
53
|
-
|
|
54
4
|
interface X402ClientConfig {
|
|
55
5
|
privateKey: `0x${string}`;
|
|
56
6
|
rpcUrl?: string;
|
|
@@ -107,48 +57,4 @@ declare class X402BrowserClient {
|
|
|
107
57
|
}
|
|
108
58
|
declare function payX402WithWallet(walletClient: WalletClient, chainId: number, request: X402PayRequest): Promise<X402PayResult>;
|
|
109
59
|
|
|
110
|
-
|
|
111
|
-
amount: string | number;
|
|
112
|
-
payTo: string;
|
|
113
|
-
currency?: string;
|
|
114
|
-
message?: string;
|
|
115
|
-
resource?: string;
|
|
116
|
-
network?: string;
|
|
117
|
-
assetAddress?: string;
|
|
118
|
-
scheme?: "exact" | "bounded";
|
|
119
|
-
facilitator?: string | X402FacilitatorConfig;
|
|
120
|
-
metadata?: Record<string, unknown>;
|
|
121
|
-
}
|
|
122
|
-
interface X402Payment {
|
|
123
|
-
definition: X402PaymentDefinition;
|
|
124
|
-
metadata?: Record<string, unknown>;
|
|
125
|
-
}
|
|
126
|
-
interface RequireX402PaymentOptions {
|
|
127
|
-
settle?: boolean;
|
|
128
|
-
fetchImpl?: typeof fetch;
|
|
129
|
-
onFailure?: (result: X402VerificationResult) => Response;
|
|
130
|
-
}
|
|
131
|
-
interface RequireX402PaymentSuccess {
|
|
132
|
-
payment: {
|
|
133
|
-
optionId: string;
|
|
134
|
-
verifier: string;
|
|
135
|
-
amount: string;
|
|
136
|
-
currency: string;
|
|
137
|
-
network: string;
|
|
138
|
-
};
|
|
139
|
-
headers: Record<string, string>;
|
|
140
|
-
result: X402VerificationResult;
|
|
141
|
-
}
|
|
142
|
-
type RequireX402PaymentOutcome = Response | RequireX402PaymentSuccess;
|
|
143
|
-
declare class X402PaymentRequiredError extends Error {
|
|
144
|
-
readonly response: Response;
|
|
145
|
-
readonly verification: X402VerificationResult | undefined;
|
|
146
|
-
constructor(response: Response, verification?: X402VerificationResult);
|
|
147
|
-
}
|
|
148
|
-
type X402PaymentContext = RequireX402PaymentSuccess;
|
|
149
|
-
declare function getX402PaymentContext(request: Request): X402PaymentContext | undefined;
|
|
150
|
-
declare function defineX402Payment(config: DefineX402PaymentConfig): X402Payment;
|
|
151
|
-
declare function requireX402Payment(request: Request, payment: X402Payment | X402PaymentDefinition, options?: RequireX402PaymentOptions): Promise<RequireX402PaymentOutcome>;
|
|
152
|
-
declare function withX402Payment(handler: (request: Request) => Promise<Response> | Response, payment: X402Payment | X402PaymentDefinition, options?: RequireX402PaymentOptions): (request: Request) => Promise<Response>;
|
|
153
|
-
|
|
154
|
-
export { type CurrencySpec, DEFAULT_FACILITATOR, type DefineX402PaymentConfig, type EIP3009Authorization, PAYMENT_HEADERS, type RequireX402PaymentOptions, type RequireX402PaymentOutcome, type RequireX402PaymentSuccess, SUPPORTED_CURRENCIES, X402BrowserClient, type X402BrowserClientConfig, X402Client, type X402ClientConfig, type X402FacilitatorConfig, type X402PayRequest, type X402PayResult, type X402Payment, type X402PaymentContext, type X402PaymentDefinition, X402PaymentRequiredError, type X402VerificationResult, defineX402Payment, getX402PaymentContext, payX402, payX402WithWallet, requireX402Payment, withX402Payment };
|
|
60
|
+
export { type EIP3009Authorization, X402BrowserClient, type X402BrowserClientConfig, X402Client, type X402ClientConfig, type X402PayRequest, type X402PayResult, payX402, payX402WithWallet };
|
package/dist/x402/index.js
CHANGED
|
@@ -308,6 +308,163 @@ function ensureTrailingSlash(url) {
|
|
|
308
308
|
return url.endsWith("/") ? url : `${url}/`;
|
|
309
309
|
}
|
|
310
310
|
var PAYMENT_HEADERS = [HEADER_X402, HEADER_PAYMENT_RESPONSE];
|
|
311
|
+
|
|
312
|
+
// src/x402/payment.ts
|
|
313
|
+
var PAYMENT_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("opentool.x402.context");
|
|
314
|
+
var X402PaymentRequiredError = class extends Error {
|
|
315
|
+
constructor(response, verification) {
|
|
316
|
+
super("X402 Payment required");
|
|
317
|
+
this.name = "X402PaymentRequiredError";
|
|
318
|
+
this.response = response;
|
|
319
|
+
this.verification = verification;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
function setPaymentContext(request, context) {
|
|
323
|
+
try {
|
|
324
|
+
Object.defineProperty(request, PAYMENT_CONTEXT_SYMBOL, {
|
|
325
|
+
value: context,
|
|
326
|
+
configurable: true,
|
|
327
|
+
enumerable: false,
|
|
328
|
+
writable: true
|
|
329
|
+
});
|
|
330
|
+
} catch {
|
|
331
|
+
request[PAYMENT_CONTEXT_SYMBOL] = context;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function getX402PaymentContext(request) {
|
|
335
|
+
return request[PAYMENT_CONTEXT_SYMBOL];
|
|
336
|
+
}
|
|
337
|
+
function defineX402Payment(config) {
|
|
338
|
+
const currencyCode = normalizeCurrency(config.currency);
|
|
339
|
+
const currencySpec = SUPPORTED_CURRENCIES[currencyCode];
|
|
340
|
+
if (!currencySpec) {
|
|
341
|
+
throw new Error(`Unsupported currency for x402 payments: ${currencyCode}`);
|
|
342
|
+
}
|
|
343
|
+
const network = config.network ?? currencySpec.network;
|
|
344
|
+
const assetAddress = config.assetAddress ?? currencySpec.assetAddress;
|
|
345
|
+
if (!network || !assetAddress) {
|
|
346
|
+
throw new Error(
|
|
347
|
+
"x402 payments require a network and assetAddress; supply them or choose a supported currency."
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const facilitator = resolveFacilitator(config.facilitator);
|
|
351
|
+
const value = toDecimalString(config.amount);
|
|
352
|
+
const definition = {
|
|
353
|
+
amount: value,
|
|
354
|
+
currency: {
|
|
355
|
+
code: currencyCode,
|
|
356
|
+
symbol: currencySpec.symbol,
|
|
357
|
+
decimals: currencySpec.decimals
|
|
358
|
+
},
|
|
359
|
+
asset: {
|
|
360
|
+
symbol: currencySpec.symbol,
|
|
361
|
+
network,
|
|
362
|
+
address: assetAddress,
|
|
363
|
+
decimals: currencySpec.decimals
|
|
364
|
+
},
|
|
365
|
+
payTo: config.payTo,
|
|
366
|
+
scheme: config.scheme ?? "exact",
|
|
367
|
+
network,
|
|
368
|
+
facilitator
|
|
369
|
+
};
|
|
370
|
+
if (config.resource) {
|
|
371
|
+
definition.resource = config.resource;
|
|
372
|
+
}
|
|
373
|
+
if (config.message) {
|
|
374
|
+
definition.description = config.message;
|
|
375
|
+
}
|
|
376
|
+
if (config.metadata) {
|
|
377
|
+
definition.metadata = config.metadata;
|
|
378
|
+
}
|
|
379
|
+
const baseMetadata = {
|
|
380
|
+
amountUSDC: currencyCode === "USDC" ? Number(value) : void 0,
|
|
381
|
+
facilitator: "x402rs",
|
|
382
|
+
network
|
|
383
|
+
};
|
|
384
|
+
const metadata = config.metadata ? { ...baseMetadata, ...config.metadata } : baseMetadata;
|
|
385
|
+
return {
|
|
386
|
+
definition,
|
|
387
|
+
metadata
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
async function requireX402Payment(request, payment, options = {}) {
|
|
391
|
+
const definition = isX402Payment(payment) ? payment.definition : payment;
|
|
392
|
+
const attempt = extractX402Attempt(request);
|
|
393
|
+
if (!attempt) {
|
|
394
|
+
const response = createX402PaymentRequired(definition);
|
|
395
|
+
throw new X402PaymentRequiredError(response);
|
|
396
|
+
}
|
|
397
|
+
const verifyOptions = {
|
|
398
|
+
settle: options.settle !== void 0 ? options.settle : true
|
|
399
|
+
};
|
|
400
|
+
if (options.fetchImpl !== void 0) {
|
|
401
|
+
verifyOptions.fetchImpl = options.fetchImpl;
|
|
402
|
+
}
|
|
403
|
+
const verification = await verifyX402Payment(attempt, definition, verifyOptions);
|
|
404
|
+
if (!verification.success || !verification.metadata) {
|
|
405
|
+
if (options.onFailure) {
|
|
406
|
+
return options.onFailure(verification);
|
|
407
|
+
}
|
|
408
|
+
const response = createX402PaymentRequired(definition);
|
|
409
|
+
throw new X402PaymentRequiredError(response, verification);
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
payment: verification.metadata,
|
|
413
|
+
headers: verification.responseHeaders ?? {},
|
|
414
|
+
result: verification
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function withX402Payment(handler, payment, options = {}) {
|
|
418
|
+
return async (request) => {
|
|
419
|
+
const verification = await requireX402Payment(request, payment, options);
|
|
420
|
+
if (verification instanceof Response) {
|
|
421
|
+
return verification;
|
|
422
|
+
}
|
|
423
|
+
setPaymentContext(request, verification);
|
|
424
|
+
const response = await Promise.resolve(handler(request));
|
|
425
|
+
return applyPaymentHeaders(response, verification.headers);
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function applyPaymentHeaders(response, headers) {
|
|
429
|
+
const entries = Object.entries(headers ?? {});
|
|
430
|
+
if (entries.length === 0) {
|
|
431
|
+
return response;
|
|
432
|
+
}
|
|
433
|
+
let mutated = false;
|
|
434
|
+
const merged = new Headers(response.headers);
|
|
435
|
+
for (const [key, value] of entries) {
|
|
436
|
+
if (!merged.has(key)) {
|
|
437
|
+
merged.set(key, value);
|
|
438
|
+
mutated = true;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (!mutated) {
|
|
442
|
+
return response;
|
|
443
|
+
}
|
|
444
|
+
return new Response(response.body, {
|
|
445
|
+
status: response.status,
|
|
446
|
+
statusText: response.statusText,
|
|
447
|
+
headers: merged
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
function isX402Payment(value) {
|
|
451
|
+
return !!value && typeof value === "object" && "definition" in value && value.definition !== void 0;
|
|
452
|
+
}
|
|
453
|
+
function resolveFacilitator(value) {
|
|
454
|
+
if (!value) {
|
|
455
|
+
return DEFAULT_FACILITATOR;
|
|
456
|
+
}
|
|
457
|
+
if (typeof value === "string") {
|
|
458
|
+
return { ...DEFAULT_FACILITATOR, url: value };
|
|
459
|
+
}
|
|
460
|
+
return value;
|
|
461
|
+
}
|
|
462
|
+
function normalizeCurrency(currency) {
|
|
463
|
+
return (currency ?? "USDC").toUpperCase();
|
|
464
|
+
}
|
|
465
|
+
function toDecimalString(value) {
|
|
466
|
+
return typeof value === "number" ? value.toString() : value;
|
|
467
|
+
}
|
|
311
468
|
var X402Client = class {
|
|
312
469
|
constructor(config) {
|
|
313
470
|
this.account = privateKeyToAccount(config.privateKey);
|
|
@@ -596,163 +753,6 @@ async function payX402WithWallet(walletClient, chainId, request) {
|
|
|
596
753
|
return client.pay(request);
|
|
597
754
|
}
|
|
598
755
|
|
|
599
|
-
// src/x402/index.ts
|
|
600
|
-
var PAYMENT_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("opentool.x402.context");
|
|
601
|
-
var X402PaymentRequiredError = class extends Error {
|
|
602
|
-
constructor(response, verification) {
|
|
603
|
-
super("X402 Payment required");
|
|
604
|
-
this.name = "X402PaymentRequiredError";
|
|
605
|
-
this.response = response;
|
|
606
|
-
this.verification = verification;
|
|
607
|
-
}
|
|
608
|
-
};
|
|
609
|
-
function setPaymentContext(request, context) {
|
|
610
|
-
try {
|
|
611
|
-
Object.defineProperty(request, PAYMENT_CONTEXT_SYMBOL, {
|
|
612
|
-
value: context,
|
|
613
|
-
configurable: true,
|
|
614
|
-
enumerable: false,
|
|
615
|
-
writable: true
|
|
616
|
-
});
|
|
617
|
-
} catch {
|
|
618
|
-
request[PAYMENT_CONTEXT_SYMBOL] = context;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
function getX402PaymentContext(request) {
|
|
622
|
-
return request[PAYMENT_CONTEXT_SYMBOL];
|
|
623
|
-
}
|
|
624
|
-
function defineX402Payment(config) {
|
|
625
|
-
const currencyCode = normalizeCurrency(config.currency);
|
|
626
|
-
const currencySpec = SUPPORTED_CURRENCIES[currencyCode];
|
|
627
|
-
if (!currencySpec) {
|
|
628
|
-
throw new Error(`Unsupported currency for x402 payments: ${currencyCode}`);
|
|
629
|
-
}
|
|
630
|
-
const network = config.network ?? currencySpec.network;
|
|
631
|
-
const assetAddress = config.assetAddress ?? currencySpec.assetAddress;
|
|
632
|
-
if (!network || !assetAddress) {
|
|
633
|
-
throw new Error(
|
|
634
|
-
"x402 payments require a network and assetAddress; supply them or choose a supported currency."
|
|
635
|
-
);
|
|
636
|
-
}
|
|
637
|
-
const facilitator = resolveFacilitator(config.facilitator);
|
|
638
|
-
const value = toDecimalString(config.amount);
|
|
639
|
-
const definition = {
|
|
640
|
-
amount: value,
|
|
641
|
-
currency: {
|
|
642
|
-
code: currencyCode,
|
|
643
|
-
symbol: currencySpec.symbol,
|
|
644
|
-
decimals: currencySpec.decimals
|
|
645
|
-
},
|
|
646
|
-
asset: {
|
|
647
|
-
symbol: currencySpec.symbol,
|
|
648
|
-
network,
|
|
649
|
-
address: assetAddress,
|
|
650
|
-
decimals: currencySpec.decimals
|
|
651
|
-
},
|
|
652
|
-
payTo: config.payTo,
|
|
653
|
-
scheme: config.scheme ?? "exact",
|
|
654
|
-
network,
|
|
655
|
-
facilitator
|
|
656
|
-
};
|
|
657
|
-
if (config.resource) {
|
|
658
|
-
definition.resource = config.resource;
|
|
659
|
-
}
|
|
660
|
-
if (config.message) {
|
|
661
|
-
definition.description = config.message;
|
|
662
|
-
}
|
|
663
|
-
if (config.metadata) {
|
|
664
|
-
definition.metadata = config.metadata;
|
|
665
|
-
}
|
|
666
|
-
const baseMetadata = {
|
|
667
|
-
amountUSDC: currencyCode === "USDC" ? Number(value) : void 0,
|
|
668
|
-
facilitator: "x402rs",
|
|
669
|
-
network
|
|
670
|
-
};
|
|
671
|
-
const metadata = config.metadata ? { ...baseMetadata, ...config.metadata } : baseMetadata;
|
|
672
|
-
return {
|
|
673
|
-
definition,
|
|
674
|
-
metadata
|
|
675
|
-
};
|
|
676
|
-
}
|
|
677
|
-
async function requireX402Payment(request, payment, options = {}) {
|
|
678
|
-
const definition = isX402Payment(payment) ? payment.definition : payment;
|
|
679
|
-
const attempt = extractX402Attempt(request);
|
|
680
|
-
if (!attempt) {
|
|
681
|
-
const response = createX402PaymentRequired(definition);
|
|
682
|
-
throw new X402PaymentRequiredError(response);
|
|
683
|
-
}
|
|
684
|
-
const verifyOptions = {
|
|
685
|
-
settle: options.settle !== void 0 ? options.settle : true
|
|
686
|
-
};
|
|
687
|
-
if (options.fetchImpl !== void 0) {
|
|
688
|
-
verifyOptions.fetchImpl = options.fetchImpl;
|
|
689
|
-
}
|
|
690
|
-
const verification = await verifyX402Payment(attempt, definition, verifyOptions);
|
|
691
|
-
if (!verification.success || !verification.metadata) {
|
|
692
|
-
if (options.onFailure) {
|
|
693
|
-
return options.onFailure(verification);
|
|
694
|
-
}
|
|
695
|
-
const response = createX402PaymentRequired(definition);
|
|
696
|
-
throw new X402PaymentRequiredError(response, verification);
|
|
697
|
-
}
|
|
698
|
-
return {
|
|
699
|
-
payment: verification.metadata,
|
|
700
|
-
headers: verification.responseHeaders ?? {},
|
|
701
|
-
result: verification
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
function withX402Payment(handler, payment, options = {}) {
|
|
705
|
-
return async (request) => {
|
|
706
|
-
const verification = await requireX402Payment(request, payment, options);
|
|
707
|
-
if (verification instanceof Response) {
|
|
708
|
-
return verification;
|
|
709
|
-
}
|
|
710
|
-
setPaymentContext(request, verification);
|
|
711
|
-
const response = await Promise.resolve(handler(request));
|
|
712
|
-
return applyPaymentHeaders(response, verification.headers);
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
function applyPaymentHeaders(response, headers) {
|
|
716
|
-
const entries = Object.entries(headers ?? {});
|
|
717
|
-
if (entries.length === 0) {
|
|
718
|
-
return response;
|
|
719
|
-
}
|
|
720
|
-
let mutated = false;
|
|
721
|
-
const merged = new Headers(response.headers);
|
|
722
|
-
for (const [key, value] of entries) {
|
|
723
|
-
if (!merged.has(key)) {
|
|
724
|
-
merged.set(key, value);
|
|
725
|
-
mutated = true;
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
if (!mutated) {
|
|
729
|
-
return response;
|
|
730
|
-
}
|
|
731
|
-
return new Response(response.body, {
|
|
732
|
-
status: response.status,
|
|
733
|
-
statusText: response.statusText,
|
|
734
|
-
headers: merged
|
|
735
|
-
});
|
|
736
|
-
}
|
|
737
|
-
function isX402Payment(value) {
|
|
738
|
-
return !!value && typeof value === "object" && "definition" in value && value.definition !== void 0;
|
|
739
|
-
}
|
|
740
|
-
function resolveFacilitator(value) {
|
|
741
|
-
if (!value) {
|
|
742
|
-
return DEFAULT_FACILITATOR;
|
|
743
|
-
}
|
|
744
|
-
if (typeof value === "string") {
|
|
745
|
-
return { ...DEFAULT_FACILITATOR, url: value };
|
|
746
|
-
}
|
|
747
|
-
return value;
|
|
748
|
-
}
|
|
749
|
-
function normalizeCurrency(currency) {
|
|
750
|
-
return (currency ?? "USDC").toUpperCase();
|
|
751
|
-
}
|
|
752
|
-
function toDecimalString(value) {
|
|
753
|
-
return typeof value === "number" ? value.toString() : value;
|
|
754
|
-
}
|
|
755
|
-
|
|
756
756
|
export { DEFAULT_FACILITATOR, PAYMENT_HEADERS, SUPPORTED_CURRENCIES, X402BrowserClient, X402Client, X402PaymentRequiredError, defineX402Payment, getX402PaymentContext, payX402, payX402WithWallet, requireX402Payment, withX402Payment };
|
|
757
757
|
//# sourceMappingURL=index.js.map
|
|
758
758
|
//# sourceMappingURL=index.js.map
|