x402-express-mantle 1.0.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/README.md +155 -0
- package/lib/cjs/client/index.d.ts +243 -0
- package/lib/cjs/client/index.js +413 -0
- package/lib/cjs/client/index.js.map +1 -0
- package/lib/cjs/exact/client/index.d.ts +37 -0
- package/lib/cjs/exact/client/index.js +281 -0
- package/lib/cjs/exact/client/index.js.map +1 -0
- package/lib/cjs/exact/facilitator/index.d.ts +110 -0
- package/lib/cjs/exact/facilitator/index.js +714 -0
- package/lib/cjs/exact/facilitator/index.js.map +1 -0
- package/lib/cjs/exact/server/index.d.ts +87 -0
- package/lib/cjs/exact/server/index.js +209 -0
- package/lib/cjs/exact/server/index.js.map +1 -0
- package/lib/cjs/exact/v1/client/index.d.ts +33 -0
- package/lib/cjs/exact/v1/client/index.js +169 -0
- package/lib/cjs/exact/v1/client/index.js.map +1 -0
- package/lib/cjs/exact/v1/facilitator/index.d.ts +71 -0
- package/lib/cjs/exact/v1/facilitator/index.js +384 -0
- package/lib/cjs/exact/v1/facilitator/index.js.map +1 -0
- package/lib/cjs/facilitator/index.d.ts +192 -0
- package/lib/cjs/facilitator/index.js +398 -0
- package/lib/cjs/facilitator/index.js.map +1 -0
- package/lib/cjs/http/index.d.ts +52 -0
- package/lib/cjs/http/index.js +827 -0
- package/lib/cjs/http/index.js.map +1 -0
- package/lib/cjs/index.d.ts +145 -0
- package/lib/cjs/index.js +314 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/mechanisms-CzuGzYsS.d.ts +270 -0
- package/lib/cjs/scheme-MoBRXFM8.d.ts +29 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +1305 -0
- package/lib/cjs/server/index.js.map +1 -0
- package/lib/cjs/signer-5OVDxViv.d.ts +79 -0
- package/lib/cjs/signer-BMkbhFYE.d.ts +123 -0
- package/lib/cjs/types/index.d.ts +1 -0
- package/lib/cjs/types/index.js +66 -0
- package/lib/cjs/types/index.js.map +1 -0
- package/lib/cjs/types/v1/index.d.ts +1 -0
- package/lib/cjs/types/v1/index.js +19 -0
- package/lib/cjs/types/v1/index.js.map +1 -0
- package/lib/cjs/utils/index.d.ts +48 -0
- package/lib/cjs/utils/index.js +116 -0
- package/lib/cjs/utils/index.js.map +1 -0
- package/lib/cjs/v1/index.d.ts +12 -0
- package/lib/cjs/v1/index.js +180 -0
- package/lib/cjs/v1/index.js.map +1 -0
- package/lib/cjs/x402HTTPResourceServer-D1YtlH_r.d.ts +719 -0
- package/lib/esm/chunk-3CEIVWNN.mjs +339 -0
- package/lib/esm/chunk-3CEIVWNN.mjs.map +1 -0
- package/lib/esm/chunk-BJTO5JO5.mjs +11 -0
- package/lib/esm/chunk-BJTO5JO5.mjs.map +1 -0
- package/lib/esm/chunk-EEA7DKZI.mjs +111 -0
- package/lib/esm/chunk-EEA7DKZI.mjs.map +1 -0
- package/lib/esm/chunk-FOUXRQAV.mjs +88 -0
- package/lib/esm/chunk-FOUXRQAV.mjs.map +1 -0
- package/lib/esm/chunk-IKSTWKEM.mjs +157 -0
- package/lib/esm/chunk-IKSTWKEM.mjs.map +1 -0
- package/lib/esm/chunk-JYZWCLMP.mjs +305 -0
- package/lib/esm/chunk-JYZWCLMP.mjs.map +1 -0
- package/lib/esm/chunk-PNSAJQCF.mjs +108 -0
- package/lib/esm/chunk-PNSAJQCF.mjs.map +1 -0
- package/lib/esm/chunk-PSA4YVU2.mjs +92 -0
- package/lib/esm/chunk-PSA4YVU2.mjs.map +1 -0
- package/lib/esm/chunk-QLXM7BIB.mjs +23 -0
- package/lib/esm/chunk-QLXM7BIB.mjs.map +1 -0
- package/lib/esm/chunk-TDLQZ6MP.mjs +86 -0
- package/lib/esm/chunk-TDLQZ6MP.mjs.map +1 -0
- package/lib/esm/chunk-VE37GDG2.mjs +7 -0
- package/lib/esm/chunk-VE37GDG2.mjs.map +1 -0
- package/lib/esm/chunk-WWACQNRQ.mjs +7 -0
- package/lib/esm/chunk-WWACQNRQ.mjs.map +1 -0
- package/lib/esm/chunk-X4W4S5RB.mjs +39 -0
- package/lib/esm/chunk-X4W4S5RB.mjs.map +1 -0
- package/lib/esm/chunk-Z4QX3O5V.mjs +748 -0
- package/lib/esm/chunk-Z4QX3O5V.mjs.map +1 -0
- package/lib/esm/chunk-ZYXTTU74.mjs +88 -0
- package/lib/esm/chunk-ZYXTTU74.mjs.map +1 -0
- package/lib/esm/client/index.d.mts +243 -0
- package/lib/esm/client/index.mjs +260 -0
- package/lib/esm/client/index.mjs.map +1 -0
- package/lib/esm/exact/client/index.d.mts +37 -0
- package/lib/esm/exact/client/index.mjs +36 -0
- package/lib/esm/exact/client/index.mjs.map +1 -0
- package/lib/esm/exact/facilitator/index.d.mts +110 -0
- package/lib/esm/exact/facilitator/index.mjs +350 -0
- package/lib/esm/exact/facilitator/index.mjs.map +1 -0
- package/lib/esm/exact/server/index.d.mts +87 -0
- package/lib/esm/exact/server/index.mjs +129 -0
- package/lib/esm/exact/server/index.mjs.map +1 -0
- package/lib/esm/exact/v1/client/index.d.mts +33 -0
- package/lib/esm/exact/v1/client/index.mjs +8 -0
- package/lib/esm/exact/v1/client/index.mjs.map +1 -0
- package/lib/esm/exact/v1/facilitator/index.d.mts +71 -0
- package/lib/esm/exact/v1/facilitator/index.mjs +8 -0
- package/lib/esm/exact/v1/facilitator/index.mjs.map +1 -0
- package/lib/esm/facilitator/index.d.mts +192 -0
- package/lib/esm/facilitator/index.mjs +373 -0
- package/lib/esm/facilitator/index.mjs.map +1 -0
- package/lib/esm/http/index.d.mts +52 -0
- package/lib/esm/http/index.mjs +29 -0
- package/lib/esm/http/index.mjs.map +1 -0
- package/lib/esm/index.d.mts +145 -0
- package/lib/esm/index.mjs +277 -0
- package/lib/esm/index.mjs.map +1 -0
- package/lib/esm/mechanisms-CzuGzYsS.d.mts +270 -0
- package/lib/esm/scheme-fjF-9LhT.d.mts +29 -0
- package/lib/esm/server/index.d.mts +2 -0
- package/lib/esm/server/index.mjs +563 -0
- package/lib/esm/server/index.mjs.map +1 -0
- package/lib/esm/signer-5OVDxViv.d.mts +79 -0
- package/lib/esm/signer-BMkbhFYE.d.mts +123 -0
- package/lib/esm/types/index.d.mts +1 -0
- package/lib/esm/types/index.mjs +10 -0
- package/lib/esm/types/index.mjs.map +1 -0
- package/lib/esm/types/v1/index.d.mts +1 -0
- package/lib/esm/types/v1/index.mjs +1 -0
- package/lib/esm/types/v1/index.mjs.map +1 -0
- package/lib/esm/utils/index.d.mts +48 -0
- package/lib/esm/utils/index.mjs +20 -0
- package/lib/esm/utils/index.mjs.map +1 -0
- package/lib/esm/v1/index.d.mts +12 -0
- package/lib/esm/v1/index.mjs +13 -0
- package/lib/esm/v1/index.mjs.map +1 -0
- package/lib/esm/x402HTTPResourceServer-BIfIK5HS.d.mts +719 -0
- package/package.json +39 -0
- package/src/index.js +4 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { SchemeNetworkFacilitator, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from '@x402/core/types';
|
|
2
|
+
import { F as FacilitatorSvmSigner } from '../../../signer-BMkbhFYE.mjs';
|
|
3
|
+
import '@solana/kit';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* SVM facilitator implementation for the Exact payment scheme (V1).
|
|
7
|
+
*/
|
|
8
|
+
declare class ExactSvmSchemeV1 implements SchemeNetworkFacilitator {
|
|
9
|
+
private readonly signer;
|
|
10
|
+
readonly scheme = "exact";
|
|
11
|
+
readonly caipFamily = "solana:*";
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new ExactSvmFacilitatorV1 instance.
|
|
14
|
+
*
|
|
15
|
+
* @param signer - The SVM RPC client for facilitator operations
|
|
16
|
+
* @returns ExactSvmFacilitatorV1 instance
|
|
17
|
+
*/
|
|
18
|
+
constructor(signer: FacilitatorSvmSigner);
|
|
19
|
+
/**
|
|
20
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
21
|
+
* For SVM, this includes a randomly selected fee payer address.
|
|
22
|
+
* Random selection distributes load across multiple signers.
|
|
23
|
+
*
|
|
24
|
+
* @param _ - The network identifier (unused for SVM)
|
|
25
|
+
* @returns Extra data with feePayer address
|
|
26
|
+
*/
|
|
27
|
+
getExtra(_: string): Record<string, unknown> | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Get signer addresses used by this facilitator.
|
|
30
|
+
* For SVM, returns all available fee payer addresses.
|
|
31
|
+
*
|
|
32
|
+
* @param _ - The network identifier (unused for SVM)
|
|
33
|
+
* @returns Array of fee payer addresses
|
|
34
|
+
*/
|
|
35
|
+
getSigners(_: string): string[];
|
|
36
|
+
/**
|
|
37
|
+
* Verifies a payment payload (V1).
|
|
38
|
+
*
|
|
39
|
+
* @param payload - The payment payload to verify
|
|
40
|
+
* @param requirements - The payment requirements
|
|
41
|
+
* @returns Promise resolving to verification response
|
|
42
|
+
*/
|
|
43
|
+
verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
44
|
+
/**
|
|
45
|
+
* Settles a payment by submitting the transaction (V1).
|
|
46
|
+
* Ensures the correct signer is used based on the feePayer specified in requirements.
|
|
47
|
+
*
|
|
48
|
+
* @param payload - The payment payload to settle
|
|
49
|
+
* @param requirements - The payment requirements
|
|
50
|
+
* @returns Promise resolving to settlement response
|
|
51
|
+
*/
|
|
52
|
+
settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
|
|
53
|
+
/**
|
|
54
|
+
* Verify compute limit instruction
|
|
55
|
+
*
|
|
56
|
+
* @param instruction - The compute limit instruction
|
|
57
|
+
* @param instruction.programAddress - Program address
|
|
58
|
+
* @param instruction.data - Instruction data bytes
|
|
59
|
+
*/
|
|
60
|
+
private verifyComputeLimitInstruction;
|
|
61
|
+
/**
|
|
62
|
+
* Verify compute price instruction
|
|
63
|
+
*
|
|
64
|
+
* @param instruction - The compute price instruction
|
|
65
|
+
* @param instruction.programAddress - Program address
|
|
66
|
+
* @param instruction.data - Instruction data bytes
|
|
67
|
+
*/
|
|
68
|
+
private verifyComputePriceInstruction;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { ExactSvmSchemeV1 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator } from '../mechanisms-CzuGzYsS.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Facilitator Hook Context Interfaces
|
|
5
|
+
*/
|
|
6
|
+
interface FacilitatorVerifyContext {
|
|
7
|
+
paymentPayload: PaymentPayload;
|
|
8
|
+
requirements: PaymentRequirements;
|
|
9
|
+
}
|
|
10
|
+
interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {
|
|
11
|
+
result: VerifyResponse;
|
|
12
|
+
}
|
|
13
|
+
interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {
|
|
14
|
+
error: Error;
|
|
15
|
+
}
|
|
16
|
+
interface FacilitatorSettleContext {
|
|
17
|
+
paymentPayload: PaymentPayload;
|
|
18
|
+
requirements: PaymentRequirements;
|
|
19
|
+
}
|
|
20
|
+
interface FacilitatorSettleResultContext extends FacilitatorSettleContext {
|
|
21
|
+
result: SettleResponse;
|
|
22
|
+
}
|
|
23
|
+
interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {
|
|
24
|
+
error: Error;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Facilitator Hook Type Definitions
|
|
28
|
+
*/
|
|
29
|
+
type FacilitatorBeforeVerifyHook = (context: FacilitatorVerifyContext) => Promise<void | {
|
|
30
|
+
abort: true;
|
|
31
|
+
reason: string;
|
|
32
|
+
}>;
|
|
33
|
+
type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;
|
|
34
|
+
type FacilitatorOnVerifyFailureHook = (context: FacilitatorVerifyFailureContext) => Promise<void | {
|
|
35
|
+
recovered: true;
|
|
36
|
+
result: VerifyResponse;
|
|
37
|
+
}>;
|
|
38
|
+
type FacilitatorBeforeSettleHook = (context: FacilitatorSettleContext) => Promise<void | {
|
|
39
|
+
abort: true;
|
|
40
|
+
reason: string;
|
|
41
|
+
}>;
|
|
42
|
+
type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;
|
|
43
|
+
type FacilitatorOnSettleFailureHook = (context: FacilitatorSettleFailureContext) => Promise<void | {
|
|
44
|
+
recovered: true;
|
|
45
|
+
result: SettleResponse;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Facilitator client for the x402 payment protocol.
|
|
49
|
+
* Manages payment scheme registration, verification, and settlement.
|
|
50
|
+
*/
|
|
51
|
+
declare class x402Facilitator {
|
|
52
|
+
private readonly registeredFacilitatorSchemes;
|
|
53
|
+
private readonly extensions;
|
|
54
|
+
private beforeVerifyHooks;
|
|
55
|
+
private afterVerifyHooks;
|
|
56
|
+
private onVerifyFailureHooks;
|
|
57
|
+
private beforeSettleHooks;
|
|
58
|
+
private afterSettleHooks;
|
|
59
|
+
private onSettleFailureHooks;
|
|
60
|
+
/**
|
|
61
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
62
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
63
|
+
*
|
|
64
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
65
|
+
* @param facilitator - The scheme network facilitator to register
|
|
66
|
+
* @returns The x402Facilitator instance for chaining
|
|
67
|
+
*/
|
|
68
|
+
register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
|
|
69
|
+
/**
|
|
70
|
+
* Registers a scheme facilitator for x402 version 1.
|
|
71
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
72
|
+
*
|
|
73
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
74
|
+
* @param facilitator - The scheme network facilitator to register
|
|
75
|
+
* @returns The x402Facilitator instance for chaining
|
|
76
|
+
*/
|
|
77
|
+
registerV1(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
|
|
78
|
+
/**
|
|
79
|
+
* Registers a protocol extension.
|
|
80
|
+
*
|
|
81
|
+
* @param extension - The extension name to register (e.g., "bazaar", "sign_in_with_x")
|
|
82
|
+
* @returns The x402Facilitator instance for chaining
|
|
83
|
+
*/
|
|
84
|
+
registerExtension(extension: string): x402Facilitator;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the list of registered extensions.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of extension names
|
|
89
|
+
*/
|
|
90
|
+
getExtensions(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Register a hook to execute before facilitator payment verification.
|
|
93
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
94
|
+
*
|
|
95
|
+
* @param hook - The hook function to register
|
|
96
|
+
* @returns The x402Facilitator instance for chaining
|
|
97
|
+
*/
|
|
98
|
+
onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
|
|
99
|
+
/**
|
|
100
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
101
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
102
|
+
*
|
|
103
|
+
* @param hook - The hook function to register
|
|
104
|
+
* @returns The x402Facilitator instance for chaining
|
|
105
|
+
*/
|
|
106
|
+
onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
|
|
107
|
+
/**
|
|
108
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
109
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
110
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
111
|
+
*
|
|
112
|
+
* @param hook - The hook function to register
|
|
113
|
+
* @returns The x402Facilitator instance for chaining
|
|
114
|
+
*/
|
|
115
|
+
onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
|
|
116
|
+
/**
|
|
117
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
118
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
119
|
+
*
|
|
120
|
+
* @param hook - The hook function to register
|
|
121
|
+
* @returns The x402Facilitator instance for chaining
|
|
122
|
+
*/
|
|
123
|
+
onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
|
|
124
|
+
/**
|
|
125
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
126
|
+
*
|
|
127
|
+
* @param hook - The hook function to register
|
|
128
|
+
* @returns The x402Facilitator instance for chaining
|
|
129
|
+
*/
|
|
130
|
+
onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
|
|
131
|
+
/**
|
|
132
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
133
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
134
|
+
*
|
|
135
|
+
* @param hook - The hook function to register
|
|
136
|
+
* @returns The x402Facilitator instance for chaining
|
|
137
|
+
*/
|
|
138
|
+
onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
|
|
139
|
+
/**
|
|
140
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
141
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
142
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
143
|
+
*
|
|
144
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
145
|
+
*/
|
|
146
|
+
getSupported(): {
|
|
147
|
+
kinds: Array<{
|
|
148
|
+
x402Version: number;
|
|
149
|
+
scheme: string;
|
|
150
|
+
network: string;
|
|
151
|
+
extra?: Record<string, unknown>;
|
|
152
|
+
}>;
|
|
153
|
+
extensions: string[];
|
|
154
|
+
signers: Record<string, string[]>;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Verifies a payment payload against requirements.
|
|
158
|
+
*
|
|
159
|
+
* @param paymentPayload - The payment payload to verify
|
|
160
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
161
|
+
* @returns Promise resolving to the verification response
|
|
162
|
+
*/
|
|
163
|
+
verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
164
|
+
/**
|
|
165
|
+
* Settles a payment based on the payload and requirements.
|
|
166
|
+
*
|
|
167
|
+
* @param paymentPayload - The payment payload to settle
|
|
168
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
169
|
+
* @returns Promise resolving to the settlement response
|
|
170
|
+
*/
|
|
171
|
+
settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
|
|
172
|
+
/**
|
|
173
|
+
* Internal method to register a scheme facilitator.
|
|
174
|
+
*
|
|
175
|
+
* @param x402Version - The x402 protocol version
|
|
176
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
177
|
+
* @param facilitator - The scheme network facilitator to register
|
|
178
|
+
* @returns The x402Facilitator instance for chaining
|
|
179
|
+
*/
|
|
180
|
+
private _registerScheme;
|
|
181
|
+
/**
|
|
182
|
+
* Derives a wildcard pattern from an array of networks.
|
|
183
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
184
|
+
* Otherwise returns the first network for exact matching.
|
|
185
|
+
*
|
|
186
|
+
* @param networks - Array of networks
|
|
187
|
+
* @returns Derived pattern for matching
|
|
188
|
+
*/
|
|
189
|
+
private derivePattern;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export { type FacilitatorAfterSettleHook, type FacilitatorAfterVerifyHook, type FacilitatorBeforeSettleHook, type FacilitatorBeforeVerifyHook, type FacilitatorOnSettleFailureHook, type FacilitatorOnVerifyFailureHook, type FacilitatorSettleContext, type FacilitatorSettleFailureContext, type FacilitatorSettleResultContext, type FacilitatorVerifyContext, type FacilitatorVerifyFailureContext, type FacilitatorVerifyResultContext, x402Facilitator };
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import {
|
|
2
|
+
x402Version
|
|
3
|
+
} from "../chunk-VE37GDG2.mjs";
|
|
4
|
+
import "../chunk-BJTO5JO5.mjs";
|
|
5
|
+
|
|
6
|
+
// src/facilitator/x402Facilitator.ts
|
|
7
|
+
var x402Facilitator = class {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.registeredFacilitatorSchemes = /* @__PURE__ */ new Map();
|
|
10
|
+
this.extensions = [];
|
|
11
|
+
this.beforeVerifyHooks = [];
|
|
12
|
+
this.afterVerifyHooks = [];
|
|
13
|
+
this.onVerifyFailureHooks = [];
|
|
14
|
+
this.beforeSettleHooks = [];
|
|
15
|
+
this.afterSettleHooks = [];
|
|
16
|
+
this.onSettleFailureHooks = [];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
20
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
21
|
+
*
|
|
22
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
23
|
+
* @param facilitator - The scheme network facilitator to register
|
|
24
|
+
* @returns The x402Facilitator instance for chaining
|
|
25
|
+
*/
|
|
26
|
+
register(networks, facilitator) {
|
|
27
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
28
|
+
return this._registerScheme(x402Version, networksArray, facilitator);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Registers a scheme facilitator for x402 version 1.
|
|
32
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
33
|
+
*
|
|
34
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
35
|
+
* @param facilitator - The scheme network facilitator to register
|
|
36
|
+
* @returns The x402Facilitator instance for chaining
|
|
37
|
+
*/
|
|
38
|
+
registerV1(networks, facilitator) {
|
|
39
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
40
|
+
return this._registerScheme(1, networksArray, facilitator);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Registers a protocol extension.
|
|
44
|
+
*
|
|
45
|
+
* @param extension - The extension name to register (e.g., "bazaar", "sign_in_with_x")
|
|
46
|
+
* @returns The x402Facilitator instance for chaining
|
|
47
|
+
*/
|
|
48
|
+
registerExtension(extension) {
|
|
49
|
+
if (!this.extensions.includes(extension)) {
|
|
50
|
+
this.extensions.push(extension);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets the list of registered extensions.
|
|
56
|
+
*
|
|
57
|
+
* @returns Array of extension names
|
|
58
|
+
*/
|
|
59
|
+
getExtensions() {
|
|
60
|
+
return [...this.extensions];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Register a hook to execute before facilitator payment verification.
|
|
64
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
65
|
+
*
|
|
66
|
+
* @param hook - The hook function to register
|
|
67
|
+
* @returns The x402Facilitator instance for chaining
|
|
68
|
+
*/
|
|
69
|
+
onBeforeVerify(hook) {
|
|
70
|
+
this.beforeVerifyHooks.push(hook);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
75
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
76
|
+
*
|
|
77
|
+
* @param hook - The hook function to register
|
|
78
|
+
* @returns The x402Facilitator instance for chaining
|
|
79
|
+
*/
|
|
80
|
+
onAfterVerify(hook) {
|
|
81
|
+
this.afterVerifyHooks.push(hook);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
86
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
87
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
88
|
+
*
|
|
89
|
+
* @param hook - The hook function to register
|
|
90
|
+
* @returns The x402Facilitator instance for chaining
|
|
91
|
+
*/
|
|
92
|
+
onVerifyFailure(hook) {
|
|
93
|
+
this.onVerifyFailureHooks.push(hook);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
98
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
99
|
+
*
|
|
100
|
+
* @param hook - The hook function to register
|
|
101
|
+
* @returns The x402Facilitator instance for chaining
|
|
102
|
+
*/
|
|
103
|
+
onBeforeSettle(hook) {
|
|
104
|
+
this.beforeSettleHooks.push(hook);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
109
|
+
*
|
|
110
|
+
* @param hook - The hook function to register
|
|
111
|
+
* @returns The x402Facilitator instance for chaining
|
|
112
|
+
*/
|
|
113
|
+
onAfterSettle(hook) {
|
|
114
|
+
this.afterSettleHooks.push(hook);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
119
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
120
|
+
*
|
|
121
|
+
* @param hook - The hook function to register
|
|
122
|
+
* @returns The x402Facilitator instance for chaining
|
|
123
|
+
*/
|
|
124
|
+
onSettleFailure(hook) {
|
|
125
|
+
this.onSettleFailureHooks.push(hook);
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
130
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
131
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
132
|
+
*
|
|
133
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
134
|
+
*/
|
|
135
|
+
getSupported() {
|
|
136
|
+
const kinds = [];
|
|
137
|
+
const signersByFamily = {};
|
|
138
|
+
for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {
|
|
139
|
+
for (const schemeData of schemeDataArray) {
|
|
140
|
+
const { facilitator, networks } = schemeData;
|
|
141
|
+
const scheme = facilitator.scheme;
|
|
142
|
+
for (const network of networks) {
|
|
143
|
+
const extra = facilitator.getExtra(network);
|
|
144
|
+
kinds.push({
|
|
145
|
+
x402Version: version,
|
|
146
|
+
scheme,
|
|
147
|
+
network,
|
|
148
|
+
...extra && { extra }
|
|
149
|
+
});
|
|
150
|
+
const family = facilitator.caipFamily;
|
|
151
|
+
if (!signersByFamily[family]) {
|
|
152
|
+
signersByFamily[family] = /* @__PURE__ */ new Set();
|
|
153
|
+
}
|
|
154
|
+
facilitator.getSigners(network).forEach((signer) => signersByFamily[family].add(signer));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const signers = {};
|
|
159
|
+
for (const [family, signerSet] of Object.entries(signersByFamily)) {
|
|
160
|
+
signers[family] = Array.from(signerSet);
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
kinds,
|
|
164
|
+
extensions: this.extensions,
|
|
165
|
+
signers
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Verifies a payment payload against requirements.
|
|
170
|
+
*
|
|
171
|
+
* @param paymentPayload - The payment payload to verify
|
|
172
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
173
|
+
* @returns Promise resolving to the verification response
|
|
174
|
+
*/
|
|
175
|
+
async verify(paymentPayload, paymentRequirements) {
|
|
176
|
+
const context = {
|
|
177
|
+
paymentPayload,
|
|
178
|
+
requirements: paymentRequirements
|
|
179
|
+
};
|
|
180
|
+
for (const hook of this.beforeVerifyHooks) {
|
|
181
|
+
const result = await hook(context);
|
|
182
|
+
if (result && "abort" in result && result.abort) {
|
|
183
|
+
return {
|
|
184
|
+
isValid: false,
|
|
185
|
+
invalidReason: result.reason
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
191
|
+
if (!schemeDataArray) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
let schemeNetworkFacilitator;
|
|
197
|
+
for (const schemeData of schemeDataArray) {
|
|
198
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
199
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
200
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
204
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
205
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!schemeNetworkFacilitator) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
const verifyResult = await schemeNetworkFacilitator.verify(
|
|
216
|
+
paymentPayload,
|
|
217
|
+
paymentRequirements
|
|
218
|
+
);
|
|
219
|
+
if (!verifyResult.isValid) {
|
|
220
|
+
const failureContext = {
|
|
221
|
+
...context,
|
|
222
|
+
error: new Error(verifyResult.invalidReason || "Verification failed")
|
|
223
|
+
};
|
|
224
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
225
|
+
const result = await hook(failureContext);
|
|
226
|
+
if (result && "recovered" in result && result.recovered) {
|
|
227
|
+
const recoveredContext = {
|
|
228
|
+
...context,
|
|
229
|
+
result: result.result
|
|
230
|
+
};
|
|
231
|
+
for (const hook2 of this.afterVerifyHooks) {
|
|
232
|
+
await hook2(recoveredContext);
|
|
233
|
+
}
|
|
234
|
+
return result.result;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return verifyResult;
|
|
238
|
+
}
|
|
239
|
+
const resultContext = {
|
|
240
|
+
...context,
|
|
241
|
+
result: verifyResult
|
|
242
|
+
};
|
|
243
|
+
for (const hook of this.afterVerifyHooks) {
|
|
244
|
+
await hook(resultContext);
|
|
245
|
+
}
|
|
246
|
+
return verifyResult;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
const failureContext = {
|
|
249
|
+
...context,
|
|
250
|
+
error
|
|
251
|
+
};
|
|
252
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
253
|
+
const result = await hook(failureContext);
|
|
254
|
+
if (result && "recovered" in result && result.recovered) {
|
|
255
|
+
return result.result;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Settles a payment based on the payload and requirements.
|
|
263
|
+
*
|
|
264
|
+
* @param paymentPayload - The payment payload to settle
|
|
265
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
266
|
+
* @returns Promise resolving to the settlement response
|
|
267
|
+
*/
|
|
268
|
+
async settle(paymentPayload, paymentRequirements) {
|
|
269
|
+
const context = {
|
|
270
|
+
paymentPayload,
|
|
271
|
+
requirements: paymentRequirements
|
|
272
|
+
};
|
|
273
|
+
for (const hook of this.beforeSettleHooks) {
|
|
274
|
+
const result = await hook(context);
|
|
275
|
+
if (result && "abort" in result && result.abort) {
|
|
276
|
+
throw new Error(`Settlement aborted: ${result.reason}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
281
|
+
if (!schemeDataArray) {
|
|
282
|
+
throw new Error(
|
|
283
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
let schemeNetworkFacilitator;
|
|
287
|
+
for (const schemeData of schemeDataArray) {
|
|
288
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
289
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
290
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
294
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
295
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (!schemeNetworkFacilitator) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const settleResult = await schemeNetworkFacilitator.settle(
|
|
306
|
+
paymentPayload,
|
|
307
|
+
paymentRequirements
|
|
308
|
+
);
|
|
309
|
+
const resultContext = {
|
|
310
|
+
...context,
|
|
311
|
+
result: settleResult
|
|
312
|
+
};
|
|
313
|
+
for (const hook of this.afterSettleHooks) {
|
|
314
|
+
await hook(resultContext);
|
|
315
|
+
}
|
|
316
|
+
return settleResult;
|
|
317
|
+
} catch (error) {
|
|
318
|
+
const failureContext = {
|
|
319
|
+
...context,
|
|
320
|
+
error
|
|
321
|
+
};
|
|
322
|
+
for (const hook of this.onSettleFailureHooks) {
|
|
323
|
+
const result = await hook(failureContext);
|
|
324
|
+
if (result && "recovered" in result && result.recovered) {
|
|
325
|
+
return result.result;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Internal method to register a scheme facilitator.
|
|
333
|
+
*
|
|
334
|
+
* @param x402Version - The x402 protocol version
|
|
335
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
336
|
+
* @param facilitator - The scheme network facilitator to register
|
|
337
|
+
* @returns The x402Facilitator instance for chaining
|
|
338
|
+
*/
|
|
339
|
+
_registerScheme(x402Version2, networks, facilitator) {
|
|
340
|
+
if (!this.registeredFacilitatorSchemes.has(x402Version2)) {
|
|
341
|
+
this.registeredFacilitatorSchemes.set(x402Version2, []);
|
|
342
|
+
}
|
|
343
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version2);
|
|
344
|
+
schemeDataArray.push({
|
|
345
|
+
facilitator,
|
|
346
|
+
networks: new Set(networks),
|
|
347
|
+
pattern: this.derivePattern(networks)
|
|
348
|
+
});
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Derives a wildcard pattern from an array of networks.
|
|
353
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
354
|
+
* Otherwise returns the first network for exact matching.
|
|
355
|
+
*
|
|
356
|
+
* @param networks - Array of networks
|
|
357
|
+
* @returns Derived pattern for matching
|
|
358
|
+
*/
|
|
359
|
+
derivePattern(networks) {
|
|
360
|
+
if (networks.length === 0) return "";
|
|
361
|
+
if (networks.length === 1) return networks[0];
|
|
362
|
+
const namespaces = networks.map((n) => n.split(":")[0]);
|
|
363
|
+
const uniqueNamespaces = new Set(namespaces);
|
|
364
|
+
if (uniqueNamespaces.size === 1) {
|
|
365
|
+
return `${namespaces[0]}:*`;
|
|
366
|
+
}
|
|
367
|
+
return networks[0];
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
export {
|
|
371
|
+
x402Facilitator
|
|
372
|
+
};
|
|
373
|
+
//# sourceMappingURL=index.mjs.map
|