@bankofai/x402-core 2.6.0-beta.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 +293 -0
- package/dist/cjs/assetRegistry-CL0zA4s0.d.ts +831 -0
- package/dist/cjs/assetRegistry-CRVM0KEs.d.ts +831 -0
- package/dist/cjs/client/index.d.ts +329 -0
- package/dist/cjs/client/index.js +712 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +206 -0
- package/dist/cjs/facilitator/index.js +625 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +51 -0
- package/dist/cjs/http/index.js +1178 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +250 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mechanisms-q7I6xfUE.d.ts +726 -0
- package/dist/cjs/schemas/index.d.ts +825 -0
- package/dist/cjs/schemas/index.js +212 -0
- package/dist/cjs/schemas/index.js.map +1 -0
- package/dist/cjs/server/index.d.ts +2 -0
- package/dist/cjs/server/index.js +1782 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.js +72 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/v1/index.d.ts +1 -0
- package/dist/cjs/types/v1/index.js +19 -0
- package/dist/cjs/types/v1/index.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +48 -0
- package/dist/cjs/utils/index.js +116 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/x402HTTPResourceServer-BFVo1_74.d.ts +433 -0
- package/dist/cjs/x402HTTPResourceServer-DaU2yFzy.d.ts +434 -0
- package/dist/cjs/x402HTTPResourceServer-DswI2hZQ.d.ts +434 -0
- package/dist/esm/assetRegistry-CRVM0KEs.d.mts +831 -0
- package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
- package/dist/esm/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/esm/chunk-DACUCTGT.mjs +891 -0
- package/dist/esm/chunk-DACUCTGT.mjs.map +1 -0
- package/dist/esm/chunk-DFUINDLZ.mjs +221 -0
- package/dist/esm/chunk-DFUINDLZ.mjs.map +1 -0
- package/dist/esm/chunk-HRQUGJ3Y.mjs +45 -0
- package/dist/esm/chunk-HRQUGJ3Y.mjs.map +1 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs +86 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +329 -0
- package/dist/esm/client/index.mjs +330 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +206 -0
- package/dist/esm/facilitator/index.mjs +398 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +51 -0
- package/dist/esm/http/index.mjs +29 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +13 -0
- package/dist/esm/index.mjs +14 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/schemas/index.d.mts +825 -0
- package/dist/esm/schemas/index.mjs +158 -0
- package/dist/esm/schemas/index.mjs.map +1 -0
- package/dist/esm/server/index.d.mts +2 -0
- package/dist/esm/server/index.mjs +712 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/types/index.d.mts +1 -0
- package/dist/esm/types/index.mjs +10 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/esm/types/v1/index.d.mts +1 -0
- package/dist/esm/types/v1/index.mjs +1 -0
- package/dist/esm/types/v1/index.mjs.map +1 -0
- package/dist/esm/utils/index.d.mts +48 -0
- package/dist/esm/utils/index.mjs +20 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/dist/esm/x402HTTPResourceServer-B6uf_UDm.d.mts +434 -0
- package/package.json +139 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/facilitator/index.ts
|
|
21
|
+
var facilitator_exports = {};
|
|
22
|
+
__export(facilitator_exports, {
|
|
23
|
+
x402Facilitator: () => x402Facilitator
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(facilitator_exports);
|
|
26
|
+
|
|
27
|
+
// src/registry/assetRegistry.ts
|
|
28
|
+
var AssetRegistry = class {
|
|
29
|
+
/** Creates a new AssetRegistry pre-populated with built-in token data. */
|
|
30
|
+
constructor() {
|
|
31
|
+
this.assets = /* @__PURE__ */ new Map();
|
|
32
|
+
this.defaults = /* @__PURE__ */ new Map();
|
|
33
|
+
this.registerBuiltins();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register a single asset for a network.
|
|
37
|
+
*
|
|
38
|
+
* @param network - The network identifier (e.g. "eip155:1")
|
|
39
|
+
* @param symbol - The token symbol (e.g. "USDT")
|
|
40
|
+
* @param info - Asset metadata
|
|
41
|
+
*/
|
|
42
|
+
register(network, symbol, info) {
|
|
43
|
+
if (!this.assets.has(network)) {
|
|
44
|
+
this.assets.set(network, /* @__PURE__ */ new Map());
|
|
45
|
+
}
|
|
46
|
+
this.assets.get(network).set(symbol, info);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Batch-register multiple assets for a network.
|
|
50
|
+
*
|
|
51
|
+
* @param network - The network identifier
|
|
52
|
+
* @param assets - Map of symbol to AssetInfo
|
|
53
|
+
*/
|
|
54
|
+
registerAll(network, assets) {
|
|
55
|
+
for (const [symbol, info] of Object.entries(assets)) {
|
|
56
|
+
this.register(network, symbol, info);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Set the default asset symbol for a network.
|
|
61
|
+
*
|
|
62
|
+
* @param network - The network identifier
|
|
63
|
+
* @param symbol - The symbol to set as default
|
|
64
|
+
*/
|
|
65
|
+
setDefault(network, symbol) {
|
|
66
|
+
if (!this.has(network, symbol)) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Cannot set default: asset "${symbol}" is not registered on network "${network}"`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
this.defaults.set(network, symbol);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Resolve a symbol to its AssetInfo on a network.
|
|
75
|
+
*
|
|
76
|
+
* @param network - The network identifier
|
|
77
|
+
* @param symbol - The token symbol
|
|
78
|
+
* @returns The resolved AssetInfo
|
|
79
|
+
* @throws If the asset is not registered
|
|
80
|
+
*/
|
|
81
|
+
resolve(network, symbol) {
|
|
82
|
+
const networkAssets = this.assets.get(network);
|
|
83
|
+
if (!networkAssets || !networkAssets.has(symbol)) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Asset "${symbol}" is not registered on network "${network}". Available: ${networkAssets ? Array.from(networkAssets.keys()).join(", ") : "none"}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return networkAssets.get(symbol);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get the default asset for a network.
|
|
92
|
+
*
|
|
93
|
+
* @param network - The network identifier
|
|
94
|
+
* @returns The default symbol and its AssetInfo
|
|
95
|
+
* @throws If no default is configured
|
|
96
|
+
*/
|
|
97
|
+
getDefault(network) {
|
|
98
|
+
const symbol = this.defaults.get(network);
|
|
99
|
+
if (!symbol) {
|
|
100
|
+
throw new Error(`No default asset configured for network "${network}"`);
|
|
101
|
+
}
|
|
102
|
+
return { symbol, info: this.resolve(network, symbol) };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* List all registered symbols for a network.
|
|
106
|
+
*
|
|
107
|
+
* @param network - The network identifier
|
|
108
|
+
* @returns Array of registered symbols
|
|
109
|
+
*/
|
|
110
|
+
getSymbols(network) {
|
|
111
|
+
const networkAssets = this.assets.get(network);
|
|
112
|
+
return networkAssets ? Array.from(networkAssets.keys()) : [];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Check if an asset is registered on a network.
|
|
116
|
+
*
|
|
117
|
+
* @param network - The network identifier
|
|
118
|
+
* @param symbol - The token symbol
|
|
119
|
+
* @returns True if the asset is registered
|
|
120
|
+
*/
|
|
121
|
+
has(network, symbol) {
|
|
122
|
+
return this.assets.get(network)?.has(symbol) ?? false;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Register built-in known assets.
|
|
126
|
+
* Data sourced from mechanism defaults and built-in registry.
|
|
127
|
+
*/
|
|
128
|
+
registerBuiltins() {
|
|
129
|
+
this.registerAll("eip155:1", {
|
|
130
|
+
USDC: {
|
|
131
|
+
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
132
|
+
decimals: 6,
|
|
133
|
+
name: "USD Coin",
|
|
134
|
+
version: "2"
|
|
135
|
+
},
|
|
136
|
+
USDT: {
|
|
137
|
+
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
138
|
+
decimals: 6,
|
|
139
|
+
name: "Tether USD",
|
|
140
|
+
version: "1",
|
|
141
|
+
assetTransferMethod: "permit2"
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
this.defaults.set("eip155:1", "USDC");
|
|
145
|
+
this.registerAll("eip155:56", {
|
|
146
|
+
USDC: {
|
|
147
|
+
address: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
|
|
148
|
+
decimals: 18,
|
|
149
|
+
name: "USD Coin",
|
|
150
|
+
version: "1",
|
|
151
|
+
assetTransferMethod: "permit2"
|
|
152
|
+
},
|
|
153
|
+
USDT: {
|
|
154
|
+
address: "0x55d398326f99059fF775485246999027B3197955",
|
|
155
|
+
decimals: 18,
|
|
156
|
+
name: "Tether USD",
|
|
157
|
+
version: "1",
|
|
158
|
+
assetTransferMethod: "permit2"
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
this.defaults.set("eip155:56", "USDC");
|
|
162
|
+
this.registerAll("eip155:97", {
|
|
163
|
+
USDT: {
|
|
164
|
+
address: "0x337610d27c682E347C9cD60BD4b3b107C9d34dDd",
|
|
165
|
+
decimals: 18,
|
|
166
|
+
name: "Tether USD",
|
|
167
|
+
version: "1",
|
|
168
|
+
assetTransferMethod: "permit2"
|
|
169
|
+
},
|
|
170
|
+
USDC: {
|
|
171
|
+
address: "0x64544969ed7EBf5f083679233325356EbE738930",
|
|
172
|
+
decimals: 18,
|
|
173
|
+
name: "USD Coin",
|
|
174
|
+
version: "1",
|
|
175
|
+
assetTransferMethod: "permit2"
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this.defaults.set("eip155:97", "USDT");
|
|
179
|
+
this.registerAll("tron:mainnet", {
|
|
180
|
+
USDT: {
|
|
181
|
+
address: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
|
182
|
+
decimals: 6,
|
|
183
|
+
name: "Tether USD",
|
|
184
|
+
version: "1",
|
|
185
|
+
assetTransferMethod: "permit2"
|
|
186
|
+
},
|
|
187
|
+
USDD: {
|
|
188
|
+
address: "TXDk8mbtRbXeYuMNS83CfKPaYYT8XWv9Hz",
|
|
189
|
+
decimals: 18,
|
|
190
|
+
name: "Decentralized USD",
|
|
191
|
+
version: "1",
|
|
192
|
+
supportsEip2612: true,
|
|
193
|
+
assetTransferMethod: "permit2"
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
this.defaults.set("tron:mainnet", "USDT");
|
|
197
|
+
this.register("tron:shasta", "USDT", {
|
|
198
|
+
address: "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
|
|
199
|
+
decimals: 6,
|
|
200
|
+
name: "Tether USD",
|
|
201
|
+
version: "1",
|
|
202
|
+
assetTransferMethod: "permit2"
|
|
203
|
+
});
|
|
204
|
+
this.defaults.set("tron:shasta", "USDT");
|
|
205
|
+
this.registerAll("tron:nile", {
|
|
206
|
+
USDT: {
|
|
207
|
+
address: "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf",
|
|
208
|
+
decimals: 6,
|
|
209
|
+
name: "Tether USD",
|
|
210
|
+
version: "1",
|
|
211
|
+
assetTransferMethod: "permit2"
|
|
212
|
+
},
|
|
213
|
+
USDD: {
|
|
214
|
+
address: "TGjgvdTWWrybVLaVeFqSyVqJQWjxqRYbaK",
|
|
215
|
+
decimals: 18,
|
|
216
|
+
name: "Decentralized USD",
|
|
217
|
+
version: "1",
|
|
218
|
+
supportsEip2612: true,
|
|
219
|
+
assetTransferMethod: "permit2"
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
this.defaults.set("tron:nile", "USDT");
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
// src/registry/index.ts
|
|
227
|
+
var globalAssetRegistry = new AssetRegistry();
|
|
228
|
+
|
|
229
|
+
// src/index.ts
|
|
230
|
+
var x402Version = 2;
|
|
231
|
+
|
|
232
|
+
// src/facilitator/x402Facilitator.ts
|
|
233
|
+
var x402Facilitator = class {
|
|
234
|
+
constructor() {
|
|
235
|
+
this.registeredFacilitatorSchemes = /* @__PURE__ */ new Map();
|
|
236
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
237
|
+
this.beforeVerifyHooks = [];
|
|
238
|
+
this.afterVerifyHooks = [];
|
|
239
|
+
this.onVerifyFailureHooks = [];
|
|
240
|
+
this.beforeSettleHooks = [];
|
|
241
|
+
this.afterSettleHooks = [];
|
|
242
|
+
this.onSettleFailureHooks = [];
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Registers a scheme facilitator for the current x402 version.
|
|
246
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
247
|
+
*
|
|
248
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
249
|
+
* @param facilitator - The scheme network facilitator to register
|
|
250
|
+
* @returns The x402Facilitator instance for chaining
|
|
251
|
+
*/
|
|
252
|
+
register(networks, facilitator) {
|
|
253
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
254
|
+
return this._registerScheme(x402Version, networksArray, facilitator);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Registers a scheme facilitator for x402 version 1.
|
|
258
|
+
* Networks are stored and used for getSupported() - no need to specify them later.
|
|
259
|
+
*
|
|
260
|
+
* @param networks - Single network or array of networks this facilitator supports
|
|
261
|
+
* @param facilitator - The scheme network facilitator to register
|
|
262
|
+
* @returns The x402Facilitator instance for chaining
|
|
263
|
+
*/
|
|
264
|
+
registerV1(networks, facilitator) {
|
|
265
|
+
const networksArray = Array.isArray(networks) ? networks : [networks];
|
|
266
|
+
return this._registerScheme(1, networksArray, facilitator);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Registers a protocol extension.
|
|
270
|
+
*
|
|
271
|
+
* @param extension - The extension object to register
|
|
272
|
+
* @returns The x402Facilitator instance for chaining
|
|
273
|
+
*/
|
|
274
|
+
registerExtension(extension) {
|
|
275
|
+
this.extensions.set(extension.key, extension);
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Gets the list of registered extension keys.
|
|
280
|
+
*
|
|
281
|
+
* @returns Array of extension key strings
|
|
282
|
+
*/
|
|
283
|
+
getExtensions() {
|
|
284
|
+
return Array.from(this.extensions.keys());
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Gets a registered extension by key.
|
|
288
|
+
*
|
|
289
|
+
* @param key - The extension key to look up
|
|
290
|
+
* @returns The extension object, or undefined if not registered
|
|
291
|
+
*/
|
|
292
|
+
getExtension(key) {
|
|
293
|
+
return this.extensions.get(key);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Register a hook to execute before facilitator payment verification.
|
|
297
|
+
* Can abort verification by returning { abort: true, reason: string }
|
|
298
|
+
*
|
|
299
|
+
* @param hook - The hook function to register
|
|
300
|
+
* @returns The x402Facilitator instance for chaining
|
|
301
|
+
*/
|
|
302
|
+
onBeforeVerify(hook) {
|
|
303
|
+
this.beforeVerifyHooks.push(hook);
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Register a hook to execute after successful facilitator payment verification (isValid: true).
|
|
308
|
+
* This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
|
|
309
|
+
*
|
|
310
|
+
* @param hook - The hook function to register
|
|
311
|
+
* @returns The x402Facilitator instance for chaining
|
|
312
|
+
*/
|
|
313
|
+
onAfterVerify(hook) {
|
|
314
|
+
this.afterVerifyHooks.push(hook);
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Register a hook to execute when facilitator payment verification fails.
|
|
319
|
+
* Called when: verification returns isValid: false, or an exception is thrown during verification.
|
|
320
|
+
* Can recover from failure by returning { recovered: true, result: VerifyResponse }
|
|
321
|
+
*
|
|
322
|
+
* @param hook - The hook function to register
|
|
323
|
+
* @returns The x402Facilitator instance for chaining
|
|
324
|
+
*/
|
|
325
|
+
onVerifyFailure(hook) {
|
|
326
|
+
this.onVerifyFailureHooks.push(hook);
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Register a hook to execute before facilitator payment settlement.
|
|
331
|
+
* Can abort settlement by returning { abort: true, reason: string }
|
|
332
|
+
*
|
|
333
|
+
* @param hook - The hook function to register
|
|
334
|
+
* @returns The x402Facilitator instance for chaining
|
|
335
|
+
*/
|
|
336
|
+
onBeforeSettle(hook) {
|
|
337
|
+
this.beforeSettleHooks.push(hook);
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Register a hook to execute after successful facilitator payment settlement.
|
|
342
|
+
*
|
|
343
|
+
* @param hook - The hook function to register
|
|
344
|
+
* @returns The x402Facilitator instance for chaining
|
|
345
|
+
*/
|
|
346
|
+
onAfterSettle(hook) {
|
|
347
|
+
this.afterSettleHooks.push(hook);
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Register a hook to execute when facilitator payment settlement fails.
|
|
352
|
+
* Can recover from failure by returning { recovered: true, result: SettleResponse }
|
|
353
|
+
*
|
|
354
|
+
* @param hook - The hook function to register
|
|
355
|
+
* @returns The x402Facilitator instance for chaining
|
|
356
|
+
*/
|
|
357
|
+
onSettleFailure(hook) {
|
|
358
|
+
this.onSettleFailureHooks.push(hook);
|
|
359
|
+
return this;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Gets supported payment kinds, extensions, and signers.
|
|
363
|
+
* Uses networks registered during register() calls - no parameters needed.
|
|
364
|
+
* Returns flat array format for backward compatibility with V1 clients.
|
|
365
|
+
*
|
|
366
|
+
* @returns Supported response with kinds as array (with version in each element), extensions, and signers
|
|
367
|
+
*/
|
|
368
|
+
getSupported() {
|
|
369
|
+
const kinds = [];
|
|
370
|
+
const signersByFamily = {};
|
|
371
|
+
for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {
|
|
372
|
+
for (const schemeData of schemeDataArray) {
|
|
373
|
+
const { facilitator, networks } = schemeData;
|
|
374
|
+
const scheme = facilitator.scheme;
|
|
375
|
+
for (const network of networks) {
|
|
376
|
+
const extra = facilitator.getExtra(network);
|
|
377
|
+
kinds.push({
|
|
378
|
+
x402Version: version,
|
|
379
|
+
scheme,
|
|
380
|
+
network,
|
|
381
|
+
...extra && { extra }
|
|
382
|
+
});
|
|
383
|
+
const family = facilitator.caipFamily;
|
|
384
|
+
if (!signersByFamily[family]) {
|
|
385
|
+
signersByFamily[family] = /* @__PURE__ */ new Set();
|
|
386
|
+
}
|
|
387
|
+
facilitator.getSigners(network).forEach((signer) => signersByFamily[family].add(signer));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const signers = {};
|
|
392
|
+
for (const [family, signerSet] of Object.entries(signersByFamily)) {
|
|
393
|
+
signers[family] = Array.from(signerSet);
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
kinds,
|
|
397
|
+
extensions: this.getExtensions(),
|
|
398
|
+
signers
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Verifies a payment payload against requirements.
|
|
403
|
+
*
|
|
404
|
+
* @param paymentPayload - The payment payload to verify
|
|
405
|
+
* @param paymentRequirements - The payment requirements to verify against
|
|
406
|
+
* @returns Promise resolving to the verification response
|
|
407
|
+
*/
|
|
408
|
+
async verify(paymentPayload, paymentRequirements) {
|
|
409
|
+
const context = {
|
|
410
|
+
paymentPayload,
|
|
411
|
+
requirements: paymentRequirements
|
|
412
|
+
};
|
|
413
|
+
for (const hook of this.beforeVerifyHooks) {
|
|
414
|
+
const result = await hook(context);
|
|
415
|
+
if (result && "abort" in result && result.abort) {
|
|
416
|
+
return {
|
|
417
|
+
isValid: false,
|
|
418
|
+
invalidReason: result.reason
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
424
|
+
if (!schemeDataArray) {
|
|
425
|
+
throw new Error(
|
|
426
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
let schemeNetworkFacilitator;
|
|
430
|
+
for (const schemeData of schemeDataArray) {
|
|
431
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
432
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
433
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
437
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
438
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (!schemeNetworkFacilitator) {
|
|
444
|
+
throw new Error(
|
|
445
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
const facilitatorContext = this.buildFacilitatorContext();
|
|
449
|
+
const verifyResult = await schemeNetworkFacilitator.verify(
|
|
450
|
+
paymentPayload,
|
|
451
|
+
paymentRequirements,
|
|
452
|
+
facilitatorContext
|
|
453
|
+
);
|
|
454
|
+
if (!verifyResult.isValid) {
|
|
455
|
+
const failureContext = {
|
|
456
|
+
...context,
|
|
457
|
+
error: new Error(verifyResult.invalidReason || "Verification failed")
|
|
458
|
+
};
|
|
459
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
460
|
+
const result = await hook(failureContext);
|
|
461
|
+
if (result && "recovered" in result && result.recovered) {
|
|
462
|
+
const recoveredContext = {
|
|
463
|
+
...context,
|
|
464
|
+
result: result.result
|
|
465
|
+
};
|
|
466
|
+
for (const hook2 of this.afterVerifyHooks) {
|
|
467
|
+
await hook2(recoveredContext);
|
|
468
|
+
}
|
|
469
|
+
return result.result;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return verifyResult;
|
|
473
|
+
}
|
|
474
|
+
const resultContext = {
|
|
475
|
+
...context,
|
|
476
|
+
result: verifyResult
|
|
477
|
+
};
|
|
478
|
+
for (const hook of this.afterVerifyHooks) {
|
|
479
|
+
await hook(resultContext);
|
|
480
|
+
}
|
|
481
|
+
return verifyResult;
|
|
482
|
+
} catch (error) {
|
|
483
|
+
const failureContext = {
|
|
484
|
+
...context,
|
|
485
|
+
error
|
|
486
|
+
};
|
|
487
|
+
for (const hook of this.onVerifyFailureHooks) {
|
|
488
|
+
const result = await hook(failureContext);
|
|
489
|
+
if (result && "recovered" in result && result.recovered) {
|
|
490
|
+
return result.result;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
throw error;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Settles a payment based on the payload and requirements.
|
|
498
|
+
*
|
|
499
|
+
* @param paymentPayload - The payment payload to settle
|
|
500
|
+
* @param paymentRequirements - The payment requirements for settlement
|
|
501
|
+
* @returns Promise resolving to the settlement response
|
|
502
|
+
*/
|
|
503
|
+
async settle(paymentPayload, paymentRequirements) {
|
|
504
|
+
const context = {
|
|
505
|
+
paymentPayload,
|
|
506
|
+
requirements: paymentRequirements
|
|
507
|
+
};
|
|
508
|
+
for (const hook of this.beforeSettleHooks) {
|
|
509
|
+
const result = await hook(context);
|
|
510
|
+
if (result && "abort" in result && result.abort) {
|
|
511
|
+
throw new Error(`Settlement aborted: ${result.reason}`);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
try {
|
|
515
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
|
|
516
|
+
if (!schemeDataArray) {
|
|
517
|
+
throw new Error(
|
|
518
|
+
`No facilitator registered for x402 version: ${paymentPayload.x402Version}`
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
let schemeNetworkFacilitator;
|
|
522
|
+
for (const schemeData of schemeDataArray) {
|
|
523
|
+
if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
|
|
524
|
+
if (schemeData.networks.has(paymentRequirements.network)) {
|
|
525
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
|
|
529
|
+
if (patternRegex.test(paymentRequirements.network)) {
|
|
530
|
+
schemeNetworkFacilitator = schemeData.facilitator;
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if (!schemeNetworkFacilitator) {
|
|
536
|
+
throw new Error(
|
|
537
|
+
`No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
const facilitatorContext = this.buildFacilitatorContext();
|
|
541
|
+
const settleResult = await schemeNetworkFacilitator.settle(
|
|
542
|
+
paymentPayload,
|
|
543
|
+
paymentRequirements,
|
|
544
|
+
facilitatorContext
|
|
545
|
+
);
|
|
546
|
+
const resultContext = {
|
|
547
|
+
...context,
|
|
548
|
+
result: settleResult
|
|
549
|
+
};
|
|
550
|
+
for (const hook of this.afterSettleHooks) {
|
|
551
|
+
await hook(resultContext);
|
|
552
|
+
}
|
|
553
|
+
return settleResult;
|
|
554
|
+
} catch (error) {
|
|
555
|
+
const failureContext = {
|
|
556
|
+
...context,
|
|
557
|
+
error
|
|
558
|
+
};
|
|
559
|
+
for (const hook of this.onSettleFailureHooks) {
|
|
560
|
+
const result = await hook(failureContext);
|
|
561
|
+
if (result && "recovered" in result && result.recovered) {
|
|
562
|
+
return result.result;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
throw error;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Builds a FacilitatorContext from the registered extensions map.
|
|
570
|
+
* Passed to mechanism verify/settle so they can access extension capabilities.
|
|
571
|
+
*
|
|
572
|
+
* @returns A FacilitatorContext backed by this facilitator's registered extensions
|
|
573
|
+
*/
|
|
574
|
+
buildFacilitatorContext() {
|
|
575
|
+
const extensionsMap = this.extensions;
|
|
576
|
+
return {
|
|
577
|
+
getExtension(key) {
|
|
578
|
+
return extensionsMap.get(key);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Internal method to register a scheme facilitator.
|
|
584
|
+
*
|
|
585
|
+
* @param x402Version - The x402 protocol version
|
|
586
|
+
* @param networks - Array of concrete networks this facilitator supports
|
|
587
|
+
* @param facilitator - The scheme network facilitator to register
|
|
588
|
+
* @returns The x402Facilitator instance for chaining
|
|
589
|
+
*/
|
|
590
|
+
_registerScheme(x402Version2, networks, facilitator) {
|
|
591
|
+
if (!this.registeredFacilitatorSchemes.has(x402Version2)) {
|
|
592
|
+
this.registeredFacilitatorSchemes.set(x402Version2, []);
|
|
593
|
+
}
|
|
594
|
+
const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version2);
|
|
595
|
+
schemeDataArray.push({
|
|
596
|
+
facilitator,
|
|
597
|
+
networks: new Set(networks),
|
|
598
|
+
pattern: this.derivePattern(networks)
|
|
599
|
+
});
|
|
600
|
+
return this;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Derives a wildcard pattern from an array of networks.
|
|
604
|
+
* If all networks share the same namespace, returns wildcard pattern.
|
|
605
|
+
* Otherwise returns the first network for exact matching.
|
|
606
|
+
*
|
|
607
|
+
* @param networks - Array of networks
|
|
608
|
+
* @returns Derived pattern for matching
|
|
609
|
+
*/
|
|
610
|
+
derivePattern(networks) {
|
|
611
|
+
if (networks.length === 0) return "";
|
|
612
|
+
if (networks.length === 1) return networks[0];
|
|
613
|
+
const namespaces = networks.map((n) => n.split(":")[0]);
|
|
614
|
+
const uniqueNamespaces = new Set(namespaces);
|
|
615
|
+
if (uniqueNamespaces.size === 1) {
|
|
616
|
+
return `${namespaces[0]}:*`;
|
|
617
|
+
}
|
|
618
|
+
return networks[0];
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
622
|
+
0 && (module.exports = {
|
|
623
|
+
x402Facilitator
|
|
624
|
+
});
|
|
625
|
+
//# sourceMappingURL=index.js.map
|