x402z-facilitator 0.0.7 → 0.0.9
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 +6 -0
- package/dist/chunk-3XCMVK47.mjs +576 -0
- package/dist/index.d.mts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +19 -19
- package/dist/index.mjs +10 -10
- package/dist/service/bootstrap.d.mts +8 -0
- package/dist/service/bootstrap.d.ts +8 -0
- package/dist/service/bootstrap.js +581 -0
- package/dist/service/bootstrap.mjs +8 -0
- package/package.json +3 -3
|
@@ -0,0 +1,581 @@
|
|
|
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/service/bootstrap.ts
|
|
21
|
+
var bootstrap_exports = {};
|
|
22
|
+
__export(bootstrap_exports, {
|
|
23
|
+
createFacilitatorFromEnv: () => createFacilitatorFromEnv,
|
|
24
|
+
startFacilitator: () => startFacilitator
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(bootstrap_exports);
|
|
27
|
+
var import_facilitator = require("@x402/core/facilitator");
|
|
28
|
+
var import_evm = require("@x402/evm");
|
|
29
|
+
var import_viem2 = require("viem");
|
|
30
|
+
var import_accounts = require("viem/accounts");
|
|
31
|
+
|
|
32
|
+
// src/service/service.ts
|
|
33
|
+
var import_node_http = require("http");
|
|
34
|
+
async function readJson(req) {
|
|
35
|
+
const chunks = [];
|
|
36
|
+
for await (const chunk of req) {
|
|
37
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
38
|
+
}
|
|
39
|
+
const body = Buffer.concat(chunks).toString("utf8");
|
|
40
|
+
return JSON.parse(body);
|
|
41
|
+
}
|
|
42
|
+
function sendJson(res, status, payload) {
|
|
43
|
+
res.writeHead(status, { "content-type": "application/json" });
|
|
44
|
+
res.end(JSON.stringify(payload));
|
|
45
|
+
}
|
|
46
|
+
function createFacilitatorService(config) {
|
|
47
|
+
const server = (0, import_node_http.createServer)(async (req, res) => {
|
|
48
|
+
try {
|
|
49
|
+
const method = req.method ?? "GET";
|
|
50
|
+
const url = req.url ?? "/";
|
|
51
|
+
if (process.env.X402Z_DEBUG === "1") {
|
|
52
|
+
console.debug(`[x402z-facilitator] ${method} ${url}`);
|
|
53
|
+
}
|
|
54
|
+
if (!req.url) {
|
|
55
|
+
sendJson(res, 404, { error: "not_found" });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (req.method === "GET" && req.url === "/supported") {
|
|
59
|
+
sendJson(res, 200, config.facilitator.getSupported());
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (req.method === "POST" && req.url === "/verify") {
|
|
63
|
+
const body = await readJson(req);
|
|
64
|
+
const result = await config.facilitator.verify(body.paymentPayload, body.paymentRequirements);
|
|
65
|
+
sendJson(res, 200, result);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (req.method === "POST" && req.url === "/settle") {
|
|
69
|
+
const body = await readJson(req);
|
|
70
|
+
const result = await config.facilitator.settle(body.paymentPayload, body.paymentRequirements);
|
|
71
|
+
sendJson(res, 200, result);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
sendJson(res, 404, { error: "not_found" });
|
|
75
|
+
} catch (error) {
|
|
76
|
+
const message = error instanceof Error ? error.message : "internal_error";
|
|
77
|
+
sendJson(res, 500, { error: message });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return server;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/scheme/scheme.ts
|
|
84
|
+
var import_viem = require("viem");
|
|
85
|
+
var import_x402z_shared = require("x402z-shared");
|
|
86
|
+
var batcherAbi = [
|
|
87
|
+
{
|
|
88
|
+
inputs: [
|
|
89
|
+
{ internalType: "address", name: "token", type: "address" },
|
|
90
|
+
{
|
|
91
|
+
components: [
|
|
92
|
+
{
|
|
93
|
+
components: [
|
|
94
|
+
{ internalType: "address", name: "holder", type: "address" },
|
|
95
|
+
{ internalType: "address", name: "payee", type: "address" },
|
|
96
|
+
{ internalType: "uint256", name: "maxClearAmount", type: "uint256" },
|
|
97
|
+
{ internalType: "bytes32", name: "resourceHash", type: "bytes32" },
|
|
98
|
+
{ internalType: "uint48", name: "validAfter", type: "uint48" },
|
|
99
|
+
{ internalType: "uint48", name: "validBefore", type: "uint48" },
|
|
100
|
+
{ internalType: "bytes32", name: "nonce", type: "bytes32" },
|
|
101
|
+
{ internalType: "bytes32", name: "encryptedAmountHash", type: "bytes32" }
|
|
102
|
+
],
|
|
103
|
+
internalType: "struct IFHEToken.ConfidentialPayment",
|
|
104
|
+
name: "p",
|
|
105
|
+
type: "tuple"
|
|
106
|
+
},
|
|
107
|
+
{ internalType: "externalEuint64", name: "encryptedAmountInput", type: "bytes32" },
|
|
108
|
+
{ internalType: "bytes", name: "inputProof", type: "bytes" },
|
|
109
|
+
{ internalType: "bytes", name: "sig", type: "bytes" }
|
|
110
|
+
],
|
|
111
|
+
internalType: "struct FHETokenBatcher.Request[]",
|
|
112
|
+
name: "requests",
|
|
113
|
+
type: "tuple[]"
|
|
114
|
+
}
|
|
115
|
+
],
|
|
116
|
+
name: "batchConfidentialTransferWithAuthorization",
|
|
117
|
+
outputs: [
|
|
118
|
+
{ internalType: "bool[]", name: "successes", type: "bool[]" },
|
|
119
|
+
{ internalType: "bytes32[]", name: "transferredHandles", type: "bytes32[]" }
|
|
120
|
+
],
|
|
121
|
+
stateMutability: "nonpayable",
|
|
122
|
+
type: "function"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
anonymous: false,
|
|
126
|
+
inputs: [
|
|
127
|
+
{ indexed: true, internalType: "uint256", name: "index", type: "uint256" },
|
|
128
|
+
{ indexed: false, internalType: "bytes32", name: "transferredHandle", type: "bytes32" }
|
|
129
|
+
],
|
|
130
|
+
name: "BatchItemSuccess",
|
|
131
|
+
type: "event"
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
anonymous: false,
|
|
135
|
+
inputs: [
|
|
136
|
+
{ indexed: true, internalType: "uint256", name: "index", type: "uint256" },
|
|
137
|
+
{ indexed: false, internalType: "bytes", name: "reason", type: "bytes" }
|
|
138
|
+
],
|
|
139
|
+
name: "BatchItemFailure",
|
|
140
|
+
type: "event"
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
var X402zEvmFacilitator = class {
|
|
144
|
+
constructor(config) {
|
|
145
|
+
this.config = config;
|
|
146
|
+
this.scheme = "erc7984-mind-v1";
|
|
147
|
+
this.caipFamily = "eip155:*";
|
|
148
|
+
this.queue = [];
|
|
149
|
+
this.hashFn = config.hashEncryptedAmountInput ?? import_x402z_shared.hashEncryptedAmountInput;
|
|
150
|
+
this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
|
|
151
|
+
this.checkUsedNonces = config.checkUsedNonces ?? true;
|
|
152
|
+
this.waitForReceipt = config.waitForReceipt ?? true;
|
|
153
|
+
this.batchIntervalMs = Math.max(0, config.batchIntervalMs ?? 15e3);
|
|
154
|
+
this.receiptOptions = config.receipt;
|
|
155
|
+
this.batcherAddress = (0, import_viem.getAddress)(config.batcherAddress);
|
|
156
|
+
}
|
|
157
|
+
getExtra(_) {
|
|
158
|
+
return void 0;
|
|
159
|
+
}
|
|
160
|
+
getSigners(_) {
|
|
161
|
+
return [...this.config.signer.getAddresses()];
|
|
162
|
+
}
|
|
163
|
+
async verify(payload, requirements) {
|
|
164
|
+
const confidentialPayload = payload.payload;
|
|
165
|
+
if (payload.accepted.scheme !== this.scheme || requirements.scheme !== this.scheme) {
|
|
166
|
+
return {
|
|
167
|
+
isValid: false,
|
|
168
|
+
invalidReason: "unsupported_scheme",
|
|
169
|
+
payer: confidentialPayload?.authorization?.holder
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (payload.accepted.network !== requirements.network) {
|
|
173
|
+
return {
|
|
174
|
+
isValid: false,
|
|
175
|
+
invalidReason: "network_mismatch",
|
|
176
|
+
payer: confidentialPayload.authorization.holder
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const extra = requirements.extra;
|
|
180
|
+
const eip712 = extra?.eip712;
|
|
181
|
+
if (!eip712?.name || !eip712?.version) {
|
|
182
|
+
return {
|
|
183
|
+
isValid: false,
|
|
184
|
+
invalidReason: "missing_eip712_domain",
|
|
185
|
+
payer: confidentialPayload.authorization.holder
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const now = this.clock();
|
|
189
|
+
const validAfter = Number(confidentialPayload.authorization.validAfter);
|
|
190
|
+
const validBefore = Number(confidentialPayload.authorization.validBefore);
|
|
191
|
+
if (Number.isNaN(validAfter) || Number.isNaN(validBefore)) {
|
|
192
|
+
return {
|
|
193
|
+
isValid: false,
|
|
194
|
+
invalidReason: "invalid_validity_window",
|
|
195
|
+
payer: confidentialPayload.authorization.holder
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if (now < validAfter || now > validBefore) {
|
|
199
|
+
return {
|
|
200
|
+
isValid: false,
|
|
201
|
+
invalidReason: "authorization_expired",
|
|
202
|
+
payer: confidentialPayload.authorization.holder
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
if (!(0, import_viem.isAddressEqual)((0, import_viem.getAddress)(confidentialPayload.authorization.payee), (0, import_viem.getAddress)(requirements.payTo))) {
|
|
206
|
+
return {
|
|
207
|
+
isValid: false,
|
|
208
|
+
invalidReason: "recipient_mismatch",
|
|
209
|
+
payer: confidentialPayload.authorization.holder
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
const computedHash = this.hashFn(confidentialPayload.encryptedAmountInput);
|
|
213
|
+
if (computedHash !== confidentialPayload.authorization.encryptedAmountHash) {
|
|
214
|
+
return {
|
|
215
|
+
isValid: false,
|
|
216
|
+
invalidReason: "encrypted_amount_mismatch",
|
|
217
|
+
payer: confidentialPayload.authorization.holder
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
221
|
+
const isValidSignature = await this.config.signer.verifyTypedData({
|
|
222
|
+
address: confidentialPayload.authorization.holder,
|
|
223
|
+
domain: {
|
|
224
|
+
name: eip712.name,
|
|
225
|
+
version: eip712.version,
|
|
226
|
+
chainId,
|
|
227
|
+
verifyingContract: (0, import_viem.getAddress)(requirements.asset)
|
|
228
|
+
},
|
|
229
|
+
types: import_x402z_shared.confidentialPaymentTypes,
|
|
230
|
+
primaryType: "ConfidentialPayment",
|
|
231
|
+
message: {
|
|
232
|
+
holder: (0, import_viem.getAddress)(confidentialPayload.authorization.holder),
|
|
233
|
+
payee: (0, import_viem.getAddress)(confidentialPayload.authorization.payee),
|
|
234
|
+
maxClearAmount: BigInt(confidentialPayload.authorization.maxClearAmount),
|
|
235
|
+
resourceHash: confidentialPayload.authorization.resourceHash,
|
|
236
|
+
validAfter: BigInt(confidentialPayload.authorization.validAfter),
|
|
237
|
+
validBefore: BigInt(confidentialPayload.authorization.validBefore),
|
|
238
|
+
nonce: confidentialPayload.authorization.nonce,
|
|
239
|
+
encryptedAmountHash: confidentialPayload.authorization.encryptedAmountHash
|
|
240
|
+
},
|
|
241
|
+
signature: confidentialPayload.signature
|
|
242
|
+
});
|
|
243
|
+
if (!isValidSignature) {
|
|
244
|
+
return {
|
|
245
|
+
isValid: false,
|
|
246
|
+
invalidReason: "invalid_signature",
|
|
247
|
+
payer: confidentialPayload.authorization.holder
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
if (this.checkUsedNonces) {
|
|
251
|
+
const used = await this.config.signer.readContract({
|
|
252
|
+
address: (0, import_viem.getAddress)(requirements.asset),
|
|
253
|
+
abi: import_x402z_shared.confidentialTokenAbi,
|
|
254
|
+
functionName: "usedNonces",
|
|
255
|
+
args: [confidentialPayload.authorization.holder, confidentialPayload.authorization.nonce]
|
|
256
|
+
});
|
|
257
|
+
if (used) {
|
|
258
|
+
return {
|
|
259
|
+
isValid: false,
|
|
260
|
+
invalidReason: "nonce_already_used",
|
|
261
|
+
payer: confidentialPayload.authorization.holder
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
isValid: true,
|
|
267
|
+
payer: confidentialPayload.authorization.holder
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async settle(payload, requirements) {
|
|
271
|
+
const valid = await this.verify(payload, requirements);
|
|
272
|
+
const confidentialPayload = payload.payload;
|
|
273
|
+
if (!valid.isValid) {
|
|
274
|
+
return {
|
|
275
|
+
success: false,
|
|
276
|
+
errorReason: valid.invalidReason ?? "invalid_payment",
|
|
277
|
+
payer: confidentialPayload.authorization.holder,
|
|
278
|
+
transaction: "",
|
|
279
|
+
network: requirements.network
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return new Promise((resolve) => {
|
|
283
|
+
this.queue.push({ payload, requirements, resolve });
|
|
284
|
+
this.scheduleFlush();
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
scheduleFlush() {
|
|
288
|
+
if (this.flushTimer) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const delay = this.batchIntervalMs;
|
|
292
|
+
this.flushTimer = setTimeout(() => {
|
|
293
|
+
this.flushTimer = void 0;
|
|
294
|
+
void this.flushQueue();
|
|
295
|
+
}, delay);
|
|
296
|
+
}
|
|
297
|
+
async flushQueue() {
|
|
298
|
+
if (this.queue.length === 0) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const queued = this.queue;
|
|
302
|
+
this.queue = [];
|
|
303
|
+
const byToken = /* @__PURE__ */ new Map();
|
|
304
|
+
for (const entry of queued) {
|
|
305
|
+
const tokenKey = (0, import_viem.getAddress)(entry.requirements.asset);
|
|
306
|
+
const group = byToken.get(tokenKey) ?? [];
|
|
307
|
+
group.push(entry);
|
|
308
|
+
byToken.set(tokenKey, group);
|
|
309
|
+
}
|
|
310
|
+
for (const [tokenAddress, entries] of byToken.entries()) {
|
|
311
|
+
await this.flushBatch(tokenAddress, entries);
|
|
312
|
+
}
|
|
313
|
+
if (this.queue.length > 0) {
|
|
314
|
+
this.scheduleFlush();
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async flushBatch(tokenAddress, entries) {
|
|
318
|
+
const requests = entries.map((entry) => {
|
|
319
|
+
const confidentialPayload = entry.payload.payload;
|
|
320
|
+
return {
|
|
321
|
+
p: confidentialPayload.authorization,
|
|
322
|
+
encryptedAmountInput: confidentialPayload.encryptedAmountInput,
|
|
323
|
+
inputProof: confidentialPayload.inputProof,
|
|
324
|
+
sig: confidentialPayload.signature
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
const txRequest = {
|
|
328
|
+
address: this.batcherAddress,
|
|
329
|
+
abi: batcherAbi,
|
|
330
|
+
functionName: "batchConfidentialTransferWithAuthorization",
|
|
331
|
+
args: [tokenAddress, requests]
|
|
332
|
+
};
|
|
333
|
+
if (process.env.X402Z_DEBUG === "1") {
|
|
334
|
+
console.debug("[x402z-facilitator] settle tx", {
|
|
335
|
+
batcherAddress: this.batcherAddress,
|
|
336
|
+
tokenAddress,
|
|
337
|
+
functionName: txRequest.functionName,
|
|
338
|
+
to: txRequest.address,
|
|
339
|
+
size: requests.length
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
let txHash;
|
|
343
|
+
try {
|
|
344
|
+
txHash = await this.config.signer.writeContract(txRequest);
|
|
345
|
+
} catch (error) {
|
|
346
|
+
for (const entry of entries) {
|
|
347
|
+
const confidentialPayload = entry.payload.payload;
|
|
348
|
+
entry.resolve({
|
|
349
|
+
success: false,
|
|
350
|
+
errorReason: "settlement_failed",
|
|
351
|
+
payer: confidentialPayload.authorization.holder,
|
|
352
|
+
transaction: "",
|
|
353
|
+
network: entry.requirements.network
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (process.env.X402Z_DEBUG === "1") {
|
|
359
|
+
console.debug("[x402z-facilitator] tx submitted", txHash);
|
|
360
|
+
}
|
|
361
|
+
let receipt;
|
|
362
|
+
if (this.waitForReceipt) {
|
|
363
|
+
receipt = await this.config.signer.waitForTransactionReceipt({ hash: txHash });
|
|
364
|
+
if (process.env.X402Z_DEBUG === "1") {
|
|
365
|
+
console.debug("[x402z-facilitator] tx receipt", receipt);
|
|
366
|
+
}
|
|
367
|
+
if (receipt.status !== "success") {
|
|
368
|
+
for (const entry of entries) {
|
|
369
|
+
const confidentialPayload = entry.payload.payload;
|
|
370
|
+
entry.resolve({
|
|
371
|
+
success: false,
|
|
372
|
+
errorReason: "settlement_failed",
|
|
373
|
+
payer: confidentialPayload.authorization.holder,
|
|
374
|
+
transaction: txHash,
|
|
375
|
+
network: entry.requirements.network
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (!this.waitForReceipt) {
|
|
382
|
+
entries.forEach((entry) => {
|
|
383
|
+
const confidentialPayload = entry.payload.payload;
|
|
384
|
+
entry.resolve({
|
|
385
|
+
success: true,
|
|
386
|
+
payer: confidentialPayload.authorization.holder,
|
|
387
|
+
transaction: txHash,
|
|
388
|
+
network: entry.requirements.network
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const batchResults = /* @__PURE__ */ new Map();
|
|
394
|
+
if (receipt?.logs) {
|
|
395
|
+
for (const log of receipt.logs) {
|
|
396
|
+
if (!log?.address) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (!(0, import_viem.isAddressEqual)((0, import_viem.getAddress)(log.address), this.batcherAddress)) {
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const decoded = (0, import_viem.decodeEventLog)({
|
|
403
|
+
abi: batcherAbi,
|
|
404
|
+
data: log.data,
|
|
405
|
+
topics: log.topics
|
|
406
|
+
});
|
|
407
|
+
if (process.env.X402Z_DEBUG === "1") {
|
|
408
|
+
console.debug("[x402z-facilitator] batch log", decoded);
|
|
409
|
+
}
|
|
410
|
+
if (decoded.eventName === "BatchItemSuccess") {
|
|
411
|
+
const args = decoded.args;
|
|
412
|
+
batchResults.set(Number(args.index), {
|
|
413
|
+
success: true,
|
|
414
|
+
transferredHandle: args.transferredHandle
|
|
415
|
+
});
|
|
416
|
+
} else if (decoded.eventName === "BatchItemFailure") {
|
|
417
|
+
const args = decoded.args;
|
|
418
|
+
batchResults.set(Number(args.index), {
|
|
419
|
+
success: false,
|
|
420
|
+
failureReason: args.reason
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
entries.forEach((entry, index) => {
|
|
426
|
+
const confidentialPayload = entry.payload.payload;
|
|
427
|
+
const batchResult = batchResults.get(index);
|
|
428
|
+
if (!batchResult || !batchResult.success) {
|
|
429
|
+
entry.resolve(
|
|
430
|
+
{
|
|
431
|
+
success: false,
|
|
432
|
+
errorReason: "settlement_failed",
|
|
433
|
+
payer: confidentialPayload.authorization.holder,
|
|
434
|
+
transaction: txHash,
|
|
435
|
+
network: entry.requirements.network,
|
|
436
|
+
...batchResult ? { batch: { index, ...batchResult } } : {}
|
|
437
|
+
}
|
|
438
|
+
);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
entry.resolve(
|
|
442
|
+
{
|
|
443
|
+
success: true,
|
|
444
|
+
payer: confidentialPayload.authorization.holder,
|
|
445
|
+
transaction: txHash,
|
|
446
|
+
network: entry.requirements.network,
|
|
447
|
+
batch: { index, ...batchResult }
|
|
448
|
+
}
|
|
449
|
+
);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// src/service/bootstrap.ts
|
|
455
|
+
function requireEnv(key) {
|
|
456
|
+
const value = process.env[key];
|
|
457
|
+
if (!value) {
|
|
458
|
+
throw new Error(`Missing required env var: ${key}`);
|
|
459
|
+
}
|
|
460
|
+
return value;
|
|
461
|
+
}
|
|
462
|
+
function createFacilitatorFromEnv() {
|
|
463
|
+
const privateKey = requireEnv("FACILITATOR_EVM_PRIVATE_KEY");
|
|
464
|
+
const chainId = Number(process.env.FACILITATOR_EVM_CHAIN_ID ?? "11155111");
|
|
465
|
+
const rpcUrl = requireEnv("FACILITATOR_EVM_RPC_URL");
|
|
466
|
+
const networks = (process.env.FACILITATOR_NETWORKS ?? "eip155:11155111").split(",").map((network) => network.trim()).filter(Boolean);
|
|
467
|
+
const waitForReceipt = (process.env.FACILITATOR_WAIT_FOR_RECEIPT ?? "true") === "true";
|
|
468
|
+
const batcherAddress = process.env.FACILITATOR_BATCHER_ADDRESS;
|
|
469
|
+
if (!batcherAddress) {
|
|
470
|
+
throw new Error("FACILITATOR_BATCHER_ADDRESS is required");
|
|
471
|
+
}
|
|
472
|
+
const batchIntervalMs = process.env.FACILITATOR_BATCH_INTERVAL_MS ? Number(process.env.FACILITATOR_BATCH_INTERVAL_MS) : void 0;
|
|
473
|
+
const receiptTimeoutMs = process.env.FACILITATOR_RECEIPT_TIMEOUT_MS ? Number(process.env.FACILITATOR_RECEIPT_TIMEOUT_MS) : void 0;
|
|
474
|
+
const receiptConfirmations = process.env.FACILITATOR_RECEIPT_CONFIRMATIONS ? Number(process.env.FACILITATOR_RECEIPT_CONFIRMATIONS) : void 0;
|
|
475
|
+
const receiptPollingIntervalMs = process.env.FACILITATOR_RECEIPT_POLLING_INTERVAL_MS ? Number(process.env.FACILITATOR_RECEIPT_POLLING_INTERVAL_MS) : void 0;
|
|
476
|
+
const gasMultiplier = process.env.FACILITATOR_GAS_MULTIPLIER ? Number(process.env.FACILITATOR_GAS_MULTIPLIER) : void 0;
|
|
477
|
+
const debugEnabled = process.env.X402Z_DEBUG === "1";
|
|
478
|
+
const account = (0, import_accounts.privateKeyToAccount)(privateKey);
|
|
479
|
+
if (debugEnabled) {
|
|
480
|
+
console.debug("[x402z-facilitator] config", {
|
|
481
|
+
chainId,
|
|
482
|
+
networks,
|
|
483
|
+
rpcUrl,
|
|
484
|
+
waitForReceipt,
|
|
485
|
+
batcherAddress,
|
|
486
|
+
gasMultiplier,
|
|
487
|
+
batchIntervalMs,
|
|
488
|
+
receipt: {
|
|
489
|
+
confirmations: receiptConfirmations,
|
|
490
|
+
timeoutMs: receiptTimeoutMs,
|
|
491
|
+
pollingIntervalMs: receiptPollingIntervalMs
|
|
492
|
+
},
|
|
493
|
+
address: account.address
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
const client = (0, import_viem2.createWalletClient)({
|
|
497
|
+
account,
|
|
498
|
+
chain: {
|
|
499
|
+
id: chainId,
|
|
500
|
+
name: "custom",
|
|
501
|
+
nativeCurrency: { name: "native", symbol: "NATIVE", decimals: 18 },
|
|
502
|
+
rpcUrls: { default: { http: [rpcUrl] } }
|
|
503
|
+
},
|
|
504
|
+
transport: (0, import_viem2.http)(rpcUrl)
|
|
505
|
+
}).extend(import_viem2.publicActions);
|
|
506
|
+
const signer = (0, import_evm.toFacilitatorEvmSigner)({
|
|
507
|
+
address: account.address,
|
|
508
|
+
readContract: (args) => client.readContract({
|
|
509
|
+
...args,
|
|
510
|
+
args: args.args || []
|
|
511
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
512
|
+
}),
|
|
513
|
+
verifyTypedData: (args) => client.verifyTypedData(args),
|
|
514
|
+
writeContract: async (args) => {
|
|
515
|
+
let gas;
|
|
516
|
+
if (gasMultiplier && gasMultiplier > 0) {
|
|
517
|
+
try {
|
|
518
|
+
const estimated = await client.estimateContractGas({
|
|
519
|
+
...args,
|
|
520
|
+
args: args.args || [],
|
|
521
|
+
account
|
|
522
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
523
|
+
});
|
|
524
|
+
const scale = BigInt(Math.round(gasMultiplier * 1e3));
|
|
525
|
+
const scaled = estimated * scale / 1000n;
|
|
526
|
+
gas = scaled > estimated ? scaled : estimated;
|
|
527
|
+
} catch (error) {
|
|
528
|
+
if (debugEnabled) {
|
|
529
|
+
console.debug("[x402z-facilitator] gas estimate failed", error);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return client.writeContract({
|
|
534
|
+
...args,
|
|
535
|
+
args: args.args || [],
|
|
536
|
+
...gas ? { gas } : {}
|
|
537
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
538
|
+
});
|
|
539
|
+
},
|
|
540
|
+
sendTransaction: (args) => (
|
|
541
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
542
|
+
client.sendTransaction({ to: args.to, data: args.data })
|
|
543
|
+
),
|
|
544
|
+
waitForTransactionReceipt: (args) => client.waitForTransactionReceipt(args),
|
|
545
|
+
getCode: (args) => client.getCode(args)
|
|
546
|
+
});
|
|
547
|
+
const facilitator = new import_facilitator.x402Facilitator();
|
|
548
|
+
for (const network of networks) {
|
|
549
|
+
facilitator.register(
|
|
550
|
+
network,
|
|
551
|
+
new X402zEvmFacilitator({
|
|
552
|
+
signer,
|
|
553
|
+
waitForReceipt,
|
|
554
|
+
batcherAddress,
|
|
555
|
+
batchIntervalMs,
|
|
556
|
+
receipt: {
|
|
557
|
+
confirmations: receiptConfirmations,
|
|
558
|
+
timeoutMs: receiptTimeoutMs,
|
|
559
|
+
pollingIntervalMs: receiptPollingIntervalMs
|
|
560
|
+
}
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
return facilitator;
|
|
565
|
+
}
|
|
566
|
+
function startFacilitator() {
|
|
567
|
+
const facilitator = createFacilitatorFromEnv();
|
|
568
|
+
const port = Number(process.env.FACILITATOR_PORT ?? "8040");
|
|
569
|
+
const server = createFacilitatorService({ facilitator, port });
|
|
570
|
+
server.listen(port);
|
|
571
|
+
return { port };
|
|
572
|
+
}
|
|
573
|
+
if (require.main === module) {
|
|
574
|
+
const { port } = startFacilitator();
|
|
575
|
+
console.log(`Confidential facilitator listening on :${port}`);
|
|
576
|
+
}
|
|
577
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
578
|
+
0 && (module.exports = {
|
|
579
|
+
createFacilitatorFromEnv,
|
|
580
|
+
startFacilitator
|
|
581
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402z-facilitator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"@x402/core": "^2.0.0",
|
|
12
12
|
"@x402/evm": "^2.0.0",
|
|
13
13
|
"viem": "^2.39.3",
|
|
14
|
-
"x402z-shared": "0.0.
|
|
14
|
+
"x402z-shared": "0.0.9"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"jest": "^29.7.0",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@types/node": "^22.13.4"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "tsup src/index.ts src/bootstrap.ts --format cjs,esm --dts",
|
|
23
|
+
"build": "tsup src/index.ts src/service/bootstrap.ts --format cjs,esm --dts",
|
|
24
24
|
"test": "jest",
|
|
25
25
|
"start": "node dist/bootstrap.js"
|
|
26
26
|
}
|