@bankofai/x402 0.5.8 → 0.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 +14 -0
- package/dist/client/x402Client.js +1 -1
- package/dist/client/x402Client.js.map +1 -1
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -1
- package/dist/facilitator/client.d.ts +77 -0
- package/dist/facilitator/client.d.ts.map +1 -0
- package/dist/facilitator/client.js +130 -0
- package/dist/facilitator/client.js.map +1 -0
- package/dist/facilitator/index.d.ts +14 -0
- package/dist/facilitator/index.d.ts.map +1 -0
- package/dist/facilitator/index.js +12 -0
- package/dist/facilitator/index.js.map +1 -0
- package/dist/facilitator/x402Facilitator.d.ts +97 -0
- package/dist/facilitator/x402Facilitator.d.ts.map +1 -0
- package/dist/facilitator/x402Facilitator.js +212 -0
- package/dist/facilitator/x402Facilitator.js.map +1 -0
- package/dist/http/client.d.ts +66 -17
- package/dist/http/client.d.ts.map +1 -1
- package/dist/http/client.js +90 -31
- package/dist/http/client.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/mechanisms/_base/index.d.ts +19 -0
- package/dist/mechanisms/_base/index.d.ts.map +1 -0
- package/dist/mechanisms/_base/index.js +17 -0
- package/dist/mechanisms/_base/index.js.map +1 -0
- package/dist/mechanisms/_exact_base/adapter.d.ts +23 -0
- package/dist/mechanisms/_exact_base/adapter.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/adapter.js +9 -0
- package/dist/mechanisms/_exact_base/adapter.js.map +1 -0
- package/dist/mechanisms/_exact_base/evmAdapter.d.ts +10 -0
- package/dist/mechanisms/_exact_base/evmAdapter.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/evmAdapter.js +28 -0
- package/dist/mechanisms/_exact_base/evmAdapter.js.map +1 -0
- package/dist/mechanisms/_exact_base/facilitator.d.ts +46 -0
- package/dist/mechanisms/_exact_base/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/facilitator.js +87 -0
- package/dist/mechanisms/_exact_base/facilitator.js.map +1 -0
- package/dist/mechanisms/_exact_base/index.d.ts +12 -0
- package/dist/mechanisms/_exact_base/index.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/index.js +10 -0
- package/dist/mechanisms/_exact_base/index.js.map +1 -0
- package/dist/mechanisms/_exact_base/server.d.ts +40 -0
- package/dist/mechanisms/_exact_base/server.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/server.js +62 -0
- package/dist/mechanisms/_exact_base/server.js.map +1 -0
- package/dist/mechanisms/_exact_base/tronAdapter.d.ts +11 -0
- package/dist/mechanisms/_exact_base/tronAdapter.d.ts.map +1 -0
- package/dist/mechanisms/_exact_base/tronAdapter.js +48 -0
- package/dist/mechanisms/_exact_base/tronAdapter.js.map +1 -0
- package/dist/mechanisms/{nativeExact.d.ts → _exact_base/types.d.ts} +4 -11
- package/dist/mechanisms/_exact_base/types.d.ts.map +1 -0
- package/dist/mechanisms/{nativeExact.js → _exact_base/types.js} +1 -1
- package/dist/mechanisms/_exact_base/types.js.map +1 -0
- package/dist/mechanisms/_exact_permit_base/facilitator.d.ts +44 -0
- package/dist/mechanisms/_exact_permit_base/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/_exact_permit_base/facilitator.js +65 -0
- package/dist/mechanisms/_exact_permit_base/facilitator.js.map +1 -0
- package/dist/mechanisms/_exact_permit_base/index.d.ts +8 -0
- package/dist/mechanisms/_exact_permit_base/index.d.ts.map +1 -0
- package/dist/mechanisms/_exact_permit_base/index.js +7 -0
- package/dist/mechanisms/_exact_permit_base/index.js.map +1 -0
- package/dist/mechanisms/_exact_permit_base/server.d.ts +31 -0
- package/dist/mechanisms/_exact_permit_base/server.d.ts.map +1 -0
- package/dist/mechanisms/_exact_permit_base/server.js +53 -0
- package/dist/mechanisms/_exact_permit_base/server.js.map +1 -0
- package/dist/mechanisms/{nativeExactEvm.d.ts → evm/exact/client.d.ts} +2 -2
- package/dist/mechanisms/evm/exact/client.d.ts.map +1 -0
- package/dist/mechanisms/{nativeExactEvm.js → evm/exact/client.js} +6 -6
- package/dist/mechanisms/evm/exact/client.js.map +1 -0
- package/dist/mechanisms/evm/exact/facilitator.d.ts +20 -0
- package/dist/mechanisms/evm/exact/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact/facilitator.js +28 -0
- package/dist/mechanisms/evm/exact/facilitator.js.map +1 -0
- package/dist/mechanisms/evm/exact/index.d.ts +4 -0
- package/dist/mechanisms/evm/exact/index.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact/index.js +4 -0
- package/dist/mechanisms/evm/exact/index.js.map +1 -0
- package/dist/mechanisms/evm/exact/server.d.ts +19 -0
- package/dist/mechanisms/evm/exact/server.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact/server.js +23 -0
- package/dist/mechanisms/evm/exact/server.js.map +1 -0
- package/dist/mechanisms/{exactEvm.d.ts → evm/exact_permit/client.d.ts} +2 -2
- package/dist/mechanisms/evm/exact_permit/client.d.ts.map +1 -0
- package/dist/mechanisms/{exactEvm.js → evm/exact_permit/client.js} +2 -2
- package/dist/mechanisms/evm/exact_permit/client.js.map +1 -0
- package/dist/mechanisms/evm/exact_permit/facilitator.d.ts +18 -0
- package/dist/mechanisms/evm/exact_permit/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact_permit/facilitator.js +25 -0
- package/dist/mechanisms/evm/exact_permit/facilitator.js.map +1 -0
- package/dist/mechanisms/evm/exact_permit/index.d.ts +4 -0
- package/dist/mechanisms/evm/exact_permit/index.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact_permit/index.js +4 -0
- package/dist/mechanisms/evm/exact_permit/index.js.map +1 -0
- package/dist/mechanisms/evm/exact_permit/server.d.ts +11 -0
- package/dist/mechanisms/evm/exact_permit/server.d.ts.map +1 -0
- package/dist/mechanisms/evm/exact_permit/server.js +16 -0
- package/dist/mechanisms/evm/exact_permit/server.js.map +1 -0
- package/dist/mechanisms/index.d.ts +27 -8
- package/dist/mechanisms/index.d.ts.map +1 -1
- package/dist/mechanisms/index.js +27 -11
- package/dist/mechanisms/index.js.map +1 -1
- package/dist/mechanisms/{nativeExactTron.d.ts → tron/exact/client.d.ts} +2 -2
- package/dist/mechanisms/tron/exact/client.d.ts.map +1 -0
- package/dist/mechanisms/{nativeExactTron.js → tron/exact/client.js} +6 -6
- package/dist/mechanisms/tron/exact/client.js.map +1 -0
- package/dist/mechanisms/tron/exact/facilitator.d.ts +17 -0
- package/dist/mechanisms/tron/exact/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact/facilitator.js +25 -0
- package/dist/mechanisms/tron/exact/facilitator.js.map +1 -0
- package/dist/mechanisms/tron/exact/index.d.ts +4 -0
- package/dist/mechanisms/tron/exact/index.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact/index.js +4 -0
- package/dist/mechanisms/tron/exact/index.js.map +1 -0
- package/dist/mechanisms/tron/exact/server.d.ts +18 -0
- package/dist/mechanisms/tron/exact/server.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact/server.js +22 -0
- package/dist/mechanisms/tron/exact/server.js.map +1 -0
- package/dist/mechanisms/{exactGasfree.d.ts → tron/exact_gasfree/client.d.ts} +3 -3
- package/dist/mechanisms/tron/exact_gasfree/client.d.ts.map +1 -0
- package/dist/mechanisms/{exactGasfree.js → tron/exact_gasfree/client.js} +4 -4
- package/dist/mechanisms/tron/exact_gasfree/client.js.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/facilitator.d.ts +38 -0
- package/dist/mechanisms/tron/exact_gasfree/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/facilitator.js +71 -0
- package/dist/mechanisms/tron/exact_gasfree/facilitator.js.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/index.d.ts +5 -0
- package/dist/mechanisms/tron/exact_gasfree/index.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/index.js +4 -0
- package/dist/mechanisms/tron/exact_gasfree/index.js.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/server.d.ts +23 -0
- package/dist/mechanisms/tron/exact_gasfree/server.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_gasfree/server.js +43 -0
- package/dist/mechanisms/tron/exact_gasfree/server.js.map +1 -0
- package/dist/mechanisms/{exact.d.ts → tron/exact_permit/client.d.ts} +2 -2
- package/dist/mechanisms/tron/exact_permit/client.d.ts.map +1 -0
- package/dist/mechanisms/{exact.js → tron/exact_permit/client.js} +2 -2
- package/dist/mechanisms/tron/exact_permit/client.js.map +1 -0
- package/dist/mechanisms/tron/exact_permit/facilitator.d.ts +17 -0
- package/dist/mechanisms/tron/exact_permit/facilitator.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_permit/facilitator.js +24 -0
- package/dist/mechanisms/tron/exact_permit/facilitator.js.map +1 -0
- package/dist/mechanisms/tron/exact_permit/index.d.ts +4 -0
- package/dist/mechanisms/tron/exact_permit/index.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_permit/index.js +4 -0
- package/dist/mechanisms/tron/exact_permit/index.js.map +1 -0
- package/dist/mechanisms/tron/exact_permit/server.d.ts +11 -0
- package/dist/mechanisms/tron/exact_permit/server.d.ts.map +1 -0
- package/dist/mechanisms/tron/exact_permit/server.js +22 -0
- package/dist/mechanisms/tron/exact_permit/server.js.map +1 -0
- package/dist/middleware/core.d.ts +75 -0
- package/dist/middleware/core.d.ts.map +1 -0
- package/dist/middleware/core.js +132 -0
- package/dist/middleware/core.js.map +1 -0
- package/dist/middleware/express.d.ts +40 -0
- package/dist/middleware/express.d.ts.map +1 -0
- package/dist/middleware/express.js +83 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.d.ts +43 -0
- package/dist/middleware/hono.d.ts.map +1 -0
- package/dist/middleware/hono.js +72 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.d.ts +12 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +11 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/server/index.d.ts +12 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +10 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/types.d.ts +78 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/types.js +12 -0
- package/dist/server/types.js.map +1 -0
- package/dist/server/x402Server.d.ts +115 -0
- package/dist/server/x402Server.d.ts.map +1 -0
- package/dist/server/x402Server.js +318 -0
- package/dist/server/x402Server.js.map +1 -0
- package/dist/tokens.d.ts +30 -0
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +45 -0
- package/dist/tokens.js.map +1 -1
- package/dist/types/payment.d.ts +12 -1
- package/dist/types/payment.d.ts.map +1 -1
- package/dist/utils/encoding.d.ts +7 -0
- package/dist/utils/encoding.d.ts.map +1 -1
- package/dist/utils/encoding.js +15 -0
- package/dist/utils/encoding.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/tx_verification.d.ts +113 -0
- package/dist/utils/tx_verification.d.ts.map +1 -0
- package/dist/utils/tx_verification.js +98 -0
- package/dist/utils/tx_verification.js.map +1 -0
- package/package.json +23 -10
- package/dist/mechanisms/exact.d.ts.map +0 -1
- package/dist/mechanisms/exact.js.map +0 -1
- package/dist/mechanisms/exactEvm.d.ts.map +0 -1
- package/dist/mechanisms/exactEvm.js.map +0 -1
- package/dist/mechanisms/exactGasfree.d.ts.map +0 -1
- package/dist/mechanisms/exactGasfree.js.map +0 -1
- package/dist/mechanisms/nativeExact.d.ts.map +0 -1
- package/dist/mechanisms/nativeExact.js.map +0 -1
- package/dist/mechanisms/nativeExactEvm.d.ts.map +0 -1
- package/dist/mechanisms/nativeExactEvm.js.map +0 -1
- package/dist/mechanisms/nativeExactTron.d.ts.map +0 -1
- package/dist/mechanisms/nativeExactTron.js.map +0 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* X402Facilitator — TypeScript port of `bankofai.x402.facilitator.X402Facilitator`.
|
|
3
|
+
*
|
|
4
|
+
* The core in-process payment processor for an x402 facilitator service.
|
|
5
|
+
* Manages a registry of {@link FacilitatorMechanism}s keyed by `(network, scheme)`
|
|
6
|
+
* and routes verify / settle / feeQuote calls to the right implementation.
|
|
7
|
+
*
|
|
8
|
+
* This is the *engine* — wrap it with an HTTP server (Hono / Express / Fastify)
|
|
9
|
+
* to expose `/verify`, `/settle`, `/supported`, `/fee/quote` endpoints.
|
|
10
|
+
*/
|
|
11
|
+
import { getAddress, isAddress } from 'viem';
|
|
12
|
+
const consoleLogger = {
|
|
13
|
+
warn: (msg, meta) => console.warn(`[X402Facilitator] ${msg}`, meta ?? ''),
|
|
14
|
+
error: (msg, meta) => console.error(`[X402Facilitator] ${msg}`, meta ?? ''),
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* In-process payment processor. Mirrors the Python `X402Facilitator` 1:1.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const facilitator = new X402Facilitator()
|
|
22
|
+
* .register(['tron:nile'], tronExactMechanism)
|
|
23
|
+
* .register(['eip155:97'], bscExactPermitMechanism);
|
|
24
|
+
*
|
|
25
|
+
* const verifyResult = await facilitator.verify(payload, requirements);
|
|
26
|
+
* if (verifyResult.isValid) {
|
|
27
|
+
* const settleResult = await facilitator.settle(payload, requirements);
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class X402Facilitator {
|
|
32
|
+
/** network → scheme → mechanism */
|
|
33
|
+
mechanisms = new Map();
|
|
34
|
+
logger;
|
|
35
|
+
constructor(opts) {
|
|
36
|
+
this.logger = opts?.logger ?? consoleLogger;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Register a mechanism for a list of networks. Returns `this` for chaining.
|
|
40
|
+
*/
|
|
41
|
+
register(networks, mechanism) {
|
|
42
|
+
const scheme = mechanism.scheme();
|
|
43
|
+
for (const network of networks) {
|
|
44
|
+
let bucket = this.mechanisms.get(network);
|
|
45
|
+
if (!bucket) {
|
|
46
|
+
bucket = new Map();
|
|
47
|
+
this.mechanisms.set(network, bucket);
|
|
48
|
+
}
|
|
49
|
+
bucket.set(scheme, mechanism);
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/** Snapshot of registered `(x402Version, scheme, network)` triples. */
|
|
54
|
+
supported() {
|
|
55
|
+
const kinds = [];
|
|
56
|
+
for (const [network, schemes] of this.mechanisms) {
|
|
57
|
+
for (const scheme of schemes.keys()) {
|
|
58
|
+
kinds.push({ x402Version: 2, scheme, network });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { kinds };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fee quotes for a list of requirements. Unsupported `(network, scheme)` /
|
|
65
|
+
* mechanisms returning `null` are silently skipped — the result list may be
|
|
66
|
+
* shorter than the input. Per-mechanism exceptions surface as a thrown
|
|
67
|
+
* `Error` (caller decides whether to 4xx or 5xx the HTTP request).
|
|
68
|
+
*/
|
|
69
|
+
async feeQuote(accepts, context) {
|
|
70
|
+
const results = [];
|
|
71
|
+
for (const accept of accepts) {
|
|
72
|
+
const normalized = this.tryNormalizeEvmRequirements(accept);
|
|
73
|
+
const mechanism = this.findMechanism(normalized.network, normalized.scheme);
|
|
74
|
+
if (!mechanism) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const quote = await mechanism.feeQuote(normalized, context);
|
|
79
|
+
if (quote !== null) {
|
|
80
|
+
results.push(quote);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
throw new Error(`Fee quote failed for ${normalized.network}/${normalized.scheme}: ${err.message}`, { cause: err });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return results;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Verify a payment payload off-chain. Always returns a {@link VerifyResponse};
|
|
91
|
+
* mechanism exceptions become `isValid: false` with the message as
|
|
92
|
+
* `invalidReason` and a logger.error entry.
|
|
93
|
+
*/
|
|
94
|
+
async verify(payload, requirements) {
|
|
95
|
+
const mechanism = this.findMechanism(requirements.network, requirements.scheme);
|
|
96
|
+
if (!mechanism) {
|
|
97
|
+
return {
|
|
98
|
+
isValid: false,
|
|
99
|
+
invalidReason: `unsupported_network_scheme: ${requirements.network}/${requirements.scheme}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
let normalized;
|
|
103
|
+
try {
|
|
104
|
+
normalized = this.normalizeEvmRequirements(requirements);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
return { isValid: false, invalidReason: err.message };
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return await mechanism.verify(payload, normalized);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
this.logger.error('verify failed', {
|
|
114
|
+
network: normalized.network,
|
|
115
|
+
scheme: normalized.scheme,
|
|
116
|
+
error: err.message,
|
|
117
|
+
});
|
|
118
|
+
return { isValid: false, invalidReason: err.message };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Execute on-chain settlement. Always returns a {@link SettleResponse};
|
|
123
|
+
* exceptions become `success: false` with `errorReason` set.
|
|
124
|
+
*/
|
|
125
|
+
async settle(payload, requirements) {
|
|
126
|
+
const mechanism = this.findMechanism(requirements.network, requirements.scheme);
|
|
127
|
+
if (!mechanism) {
|
|
128
|
+
return {
|
|
129
|
+
success: false,
|
|
130
|
+
network: requirements.network,
|
|
131
|
+
errorReason: `unsupported_network_scheme: ${requirements.network}/${requirements.scheme}`,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
let normalized;
|
|
135
|
+
try {
|
|
136
|
+
normalized = this.normalizeEvmRequirements(requirements);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
return {
|
|
140
|
+
success: false,
|
|
141
|
+
network: requirements.network,
|
|
142
|
+
errorReason: err.message,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
return await mechanism.settle(payload, normalized);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
this.logger.error('settle failed', {
|
|
150
|
+
network: normalized.network,
|
|
151
|
+
scheme: normalized.scheme,
|
|
152
|
+
error: err.message,
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
network: normalized.network,
|
|
157
|
+
errorReason: err.message,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
findMechanism(network, scheme) {
|
|
162
|
+
return this.mechanisms.get(network)?.get(scheme) ?? null;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Strict variant — throws if any EVM address fails checksum. Used in verify /
|
|
166
|
+
* settle so the failure message ends up in the response.
|
|
167
|
+
*/
|
|
168
|
+
normalizeEvmRequirements(reqs) {
|
|
169
|
+
if (!reqs.network.startsWith('eip155:')) {
|
|
170
|
+
return reqs;
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
...reqs,
|
|
174
|
+
asset: this.checksumAddressStrict(reqs.asset, 'asset'),
|
|
175
|
+
payTo: this.checksumAddressStrict(reqs.payTo, 'payTo'),
|
|
176
|
+
extra: reqs.extra
|
|
177
|
+
? {
|
|
178
|
+
...reqs.extra,
|
|
179
|
+
fee: reqs.extra.fee
|
|
180
|
+
? {
|
|
181
|
+
...reqs.extra.fee,
|
|
182
|
+
feeTo: this.checksumAddressStrict(reqs.extra.fee.feeTo, 'extra.fee.feeTo'),
|
|
183
|
+
caller: reqs.extra.fee.caller
|
|
184
|
+
? this.checksumAddressStrict(reqs.extra.fee.caller, 'extra.fee.caller')
|
|
185
|
+
: reqs.extra.fee.caller,
|
|
186
|
+
}
|
|
187
|
+
: reqs.extra.fee,
|
|
188
|
+
}
|
|
189
|
+
: reqs.extra,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Lenient variant for `feeQuote` — invalid-EVM-input requirements are simply
|
|
194
|
+
* skipped (returning the original so they can be filtered out by mechanism
|
|
195
|
+
* lookup miss).
|
|
196
|
+
*/
|
|
197
|
+
tryNormalizeEvmRequirements(reqs) {
|
|
198
|
+
try {
|
|
199
|
+
return this.normalizeEvmRequirements(reqs);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return reqs;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
checksumAddressStrict(address, fieldName) {
|
|
206
|
+
if (!isAddress(address, { strict: false })) {
|
|
207
|
+
throw new Error(`Invalid EVM address for ${fieldName}: ${address}`);
|
|
208
|
+
}
|
|
209
|
+
return getAddress(address);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=x402Facilitator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402Facilitator.js","sourceRoot":"","sources":["../../src/facilitator/x402Facilitator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AA+C7C,MAAM,aAAa,GAAsB;IACvC,IAAI,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,qBAAqB,GAAG,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC;IACzE,KAAK,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,qBAAqB,GA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B,CAAC;4BAC1E,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM;gCAC3B,CAAC,CAAC,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC;gCACvE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM;yBAC1B;wBACH,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG;iBACnB;gBACH,CAAC,CAAC,IAAI,CAAC,KAAK;SACf,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACK,2BAA2B,CAAC,IAAyB;QAC3D,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;QAC7C,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAEO,qBAAqB,CAAC,OAAe,EAAE,SAAiB;QAC9D,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,2BAA2B,SAAS,KAAK,OAAO,EAAE,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
package/dist/http/client.d.ts
CHANGED
|
@@ -1,33 +1,82 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* X402FetchClient
|
|
2
|
+
* X402FetchClient — Fetch-based HTTP client with automatic 402 payment handling.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `bankofai.x402.clients.X402HttpClient` on the Python side. Wraps the
|
|
5
|
+
* global `fetch` (configurable for tests) so agent code can call any URL and
|
|
6
|
+
* have 402 challenges silently handled — challenge → sign → retry → return
|
|
7
|
+
* the final 200 response, with the settled payment receipt parseable via
|
|
8
|
+
* {@link parsePaymentResponseHeader}.
|
|
3
9
|
*/
|
|
4
|
-
import {
|
|
10
|
+
import { type PaymentRequirementsSelector, type SettleResponse, X402Client } from '../index.js';
|
|
11
|
+
/** Wire-format header names. */
|
|
12
|
+
export declare const PAYMENT_SIGNATURE_HEADER = "PAYMENT-SIGNATURE";
|
|
13
|
+
export declare const PAYMENT_REQUIRED_HEADER = "PAYMENT-REQUIRED";
|
|
14
|
+
export declare const PAYMENT_RESPONSE_HEADER = "PAYMENT-RESPONSE";
|
|
15
|
+
/** Constructor options for {@link X402FetchClient}. */
|
|
16
|
+
export interface X402FetchClientOptions {
|
|
17
|
+
/** Custom payment requirements selector (defaults to library default). */
|
|
18
|
+
selector?: PaymentRequirementsSelector;
|
|
19
|
+
/** Custom fetch implementation (defaults to global `fetch`). Useful for tests. */
|
|
20
|
+
fetchImpl?: typeof fetch;
|
|
21
|
+
}
|
|
5
22
|
/**
|
|
6
|
-
* Fetch-based HTTP client with automatic 402 payment handling
|
|
23
|
+
* Fetch-based HTTP client with automatic 402 payment handling.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const x402Client = new X402Client(...).register(mechanism);
|
|
28
|
+
* const httpClient = new X402FetchClient(x402Client);
|
|
29
|
+
*
|
|
30
|
+
* const res = await httpClient.get('https://api.example.com/llm');
|
|
31
|
+
* if (res.ok) {
|
|
32
|
+
* const settled = parsePaymentResponseHeader(res);
|
|
33
|
+
* console.log('paid via tx', settled?.transaction);
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
7
36
|
*/
|
|
8
37
|
export declare class X402FetchClient {
|
|
9
|
-
private x402Client;
|
|
10
|
-
private selector?;
|
|
11
|
-
|
|
38
|
+
private readonly x402Client;
|
|
39
|
+
private readonly selector?;
|
|
40
|
+
private readonly fetchImpl;
|
|
41
|
+
constructor(x402Client: X402Client, selectorOrOptions?: PaymentRequirementsSelector | X402FetchClientOptions);
|
|
12
42
|
/**
|
|
13
|
-
*
|
|
43
|
+
* Issue a request with automatic 402 → pay → retry handling.
|
|
44
|
+
*
|
|
45
|
+
* If the server returns a non-402 status, the response is passed through.
|
|
46
|
+
* If the 402 cannot be parsed (no PAYMENT-REQUIRED header and no parseable
|
|
47
|
+
* body), the original 402 is returned to the caller for inspection.
|
|
14
48
|
*/
|
|
15
49
|
request(url: string, init?: RequestInit): Promise<Response>;
|
|
16
|
-
/**
|
|
17
|
-
* GET request with payment handling
|
|
18
|
-
*/
|
|
50
|
+
/** GET shorthand. */
|
|
19
51
|
get(url: string, init?: RequestInit): Promise<Response>;
|
|
20
|
-
/**
|
|
21
|
-
* POST request with payment handling
|
|
22
|
-
*/
|
|
52
|
+
/** POST shorthand. */
|
|
23
53
|
post(url: string, body?: RequestInit['body'], init?: RequestInit): Promise<Response>;
|
|
54
|
+
/** PUT shorthand. */
|
|
55
|
+
put(url: string, body?: RequestInit['body'], init?: RequestInit): Promise<Response>;
|
|
56
|
+
/** PATCH shorthand. */
|
|
57
|
+
patch(url: string, body?: RequestInit['body'], init?: RequestInit): Promise<Response>;
|
|
58
|
+
/** DELETE shorthand. */
|
|
59
|
+
delete(url: string, init?: RequestInit): Promise<Response>;
|
|
24
60
|
/**
|
|
25
|
-
*
|
|
61
|
+
* Try to parse a `PaymentRequired` from the 402 response.
|
|
62
|
+
*
|
|
63
|
+
* Order:
|
|
64
|
+
* 1. `PAYMENT-REQUIRED` header (base64 JSON).
|
|
65
|
+
* 2. JSON body fallback when header is missing or undecodable.
|
|
66
|
+
*
|
|
67
|
+
* Returns `null` if neither path yields a recognizable shape.
|
|
26
68
|
*/
|
|
27
69
|
private parsePaymentRequired;
|
|
28
|
-
/**
|
|
29
|
-
* Retry request with payment payload
|
|
30
|
-
*/
|
|
70
|
+
/** Re-issue the original request with the encoded payment payload header. */
|
|
31
71
|
private retryWithPayment;
|
|
32
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Pull the {@link SettleResponse} out of a 200 response's PAYMENT-RESPONSE header.
|
|
75
|
+
*
|
|
76
|
+
* Servers attach this on success so the client can record the settlement tx
|
|
77
|
+
* hash / network without making a separate facilitator call.
|
|
78
|
+
*
|
|
79
|
+
* @returns The decoded settle response, or `null` if the header is absent or malformed.
|
|
80
|
+
*/
|
|
81
|
+
export declare function parsePaymentResponseHeader(response: Response): SettleResponse | null;
|
|
33
82
|
//# sourceMappingURL=client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/http/client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/http/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAGL,KAAK,2BAA2B,EAChC,KAAK,cAAc,EACnB,UAAU,EAGX,MAAM,aAAa,CAAC;AAErB,gCAAgC;AAChC,eAAO,MAAM,wBAAwB,sBAAsB,CAAC;AAC5D,eAAO,MAAM,uBAAuB,qBAAqB,CAAC;AAC1D,eAAO,MAAM,uBAAuB,qBAAqB,CAAC;AAE1D,uDAAuD;AACvD,MAAM,WAAW,sBAAsB;IACrC,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,2BAA2B,CAAC;IACvC,kFAAkF;IAClF,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAC1B;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAA8B;IACxD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAe;gBAGvC,UAAU,EAAE,UAAU,EACtB,iBAAiB,CAAC,EAAE,2BAA2B,GAAG,sBAAsB;IAY1E;;;;;;OAMG;IACG,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;IAqBjE,qBAAqB;IACf,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;IAI7D,sBAAsB;IAChB,IAAI,CACR,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,EAC1B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,QAAQ,CAAC;IAIpB,qBAAqB;IACf,GAAG,CACP,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,EAC1B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,QAAQ,CAAC;IAIpB,uBAAuB;IACjB,KAAK,CACT,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,EAC1B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,QAAQ,CAAC;IAIpB,wBAAwB;IAClB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;IAIhE;;;;;;;;OAQG;YACW,oBAAoB;IAsBlC,6EAA6E;YAC/D,gBAAgB;CAU/B;AAED;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,QAAQ,GAAG,cAAc,GAAG,IAAI,CAUpF"}
|
package/dist/http/client.js
CHANGED
|
@@ -1,26 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* X402FetchClient
|
|
2
|
+
* X402FetchClient — Fetch-based HTTP client with automatic 402 payment handling.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `bankofai.x402.clients.X402HttpClient` on the Python side. Wraps the
|
|
5
|
+
* global `fetch` (configurable for tests) so agent code can call any URL and
|
|
6
|
+
* have 402 challenges silently handled — challenge → sign → retry → return
|
|
7
|
+
* the final 200 response, with the settled payment receipt parseable via
|
|
8
|
+
* {@link parsePaymentResponseHeader}.
|
|
3
9
|
*/
|
|
4
|
-
import {
|
|
5
|
-
/**
|
|
6
|
-
const PAYMENT_SIGNATURE_HEADER = 'PAYMENT-SIGNATURE';
|
|
7
|
-
const PAYMENT_REQUIRED_HEADER = 'PAYMENT-REQUIRED';
|
|
8
|
-
const PAYMENT_RESPONSE_HEADER = 'PAYMENT-RESPONSE';
|
|
10
|
+
import { decodePaymentPayload, encodePaymentPayload, } from '../index.js';
|
|
11
|
+
/** Wire-format header names. */
|
|
12
|
+
export const PAYMENT_SIGNATURE_HEADER = 'PAYMENT-SIGNATURE';
|
|
13
|
+
export const PAYMENT_REQUIRED_HEADER = 'PAYMENT-REQUIRED';
|
|
14
|
+
export const PAYMENT_RESPONSE_HEADER = 'PAYMENT-RESPONSE';
|
|
9
15
|
/**
|
|
10
|
-
* Fetch-based HTTP client with automatic 402 payment handling
|
|
16
|
+
* Fetch-based HTTP client with automatic 402 payment handling.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const x402Client = new X402Client(...).register(mechanism);
|
|
21
|
+
* const httpClient = new X402FetchClient(x402Client);
|
|
22
|
+
*
|
|
23
|
+
* const res = await httpClient.get('https://api.example.com/llm');
|
|
24
|
+
* if (res.ok) {
|
|
25
|
+
* const settled = parsePaymentResponseHeader(res);
|
|
26
|
+
* console.log('paid via tx', settled?.transaction);
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
11
29
|
*/
|
|
12
30
|
export class X402FetchClient {
|
|
13
31
|
x402Client;
|
|
14
32
|
selector;
|
|
15
|
-
|
|
33
|
+
fetchImpl;
|
|
34
|
+
constructor(x402Client, selectorOrOptions) {
|
|
16
35
|
this.x402Client = x402Client;
|
|
17
|
-
|
|
36
|
+
if (typeof selectorOrOptions === 'function') {
|
|
37
|
+
this.selector = selectorOrOptions;
|
|
38
|
+
this.fetchImpl = fetch;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.selector = selectorOrOptions?.selector;
|
|
42
|
+
this.fetchImpl = selectorOrOptions?.fetchImpl ?? fetch;
|
|
43
|
+
}
|
|
18
44
|
}
|
|
19
45
|
/**
|
|
20
|
-
*
|
|
46
|
+
* Issue a request with automatic 402 → pay → retry handling.
|
|
47
|
+
*
|
|
48
|
+
* If the server returns a non-402 status, the response is passed through.
|
|
49
|
+
* If the 402 cannot be parsed (no PAYMENT-REQUIRED header and no parseable
|
|
50
|
+
* body), the original 402 is returned to the caller for inspection.
|
|
21
51
|
*/
|
|
22
52
|
async request(url, init) {
|
|
23
|
-
const response = await
|
|
53
|
+
const response = await this.fetchImpl(url, init);
|
|
24
54
|
if (response.status !== 402) {
|
|
25
55
|
return response;
|
|
26
56
|
}
|
|
@@ -31,20 +61,34 @@ export class X402FetchClient {
|
|
|
31
61
|
const paymentPayload = await this.x402Client.handlePayment(paymentRequired.accepts, url, paymentRequired.extensions, this.selector);
|
|
32
62
|
return this.retryWithPayment(url, init, paymentPayload);
|
|
33
63
|
}
|
|
34
|
-
/**
|
|
35
|
-
* GET request with payment handling
|
|
36
|
-
*/
|
|
64
|
+
/** GET shorthand. */
|
|
37
65
|
async get(url, init) {
|
|
38
66
|
return this.request(url, { ...init, method: 'GET' });
|
|
39
67
|
}
|
|
40
|
-
/**
|
|
41
|
-
* POST request with payment handling
|
|
42
|
-
*/
|
|
68
|
+
/** POST shorthand. */
|
|
43
69
|
async post(url, body, init) {
|
|
44
70
|
return this.request(url, { ...init, method: 'POST', body });
|
|
45
71
|
}
|
|
72
|
+
/** PUT shorthand. */
|
|
73
|
+
async put(url, body, init) {
|
|
74
|
+
return this.request(url, { ...init, method: 'PUT', body });
|
|
75
|
+
}
|
|
76
|
+
/** PATCH shorthand. */
|
|
77
|
+
async patch(url, body, init) {
|
|
78
|
+
return this.request(url, { ...init, method: 'PATCH', body });
|
|
79
|
+
}
|
|
80
|
+
/** DELETE shorthand. */
|
|
81
|
+
async delete(url, init) {
|
|
82
|
+
return this.request(url, { ...init, method: 'DELETE' });
|
|
83
|
+
}
|
|
46
84
|
/**
|
|
47
|
-
*
|
|
85
|
+
* Try to parse a `PaymentRequired` from the 402 response.
|
|
86
|
+
*
|
|
87
|
+
* Order:
|
|
88
|
+
* 1. `PAYMENT-REQUIRED` header (base64 JSON).
|
|
89
|
+
* 2. JSON body fallback when header is missing or undecodable.
|
|
90
|
+
*
|
|
91
|
+
* Returns `null` if neither path yields a recognizable shape.
|
|
48
92
|
*/
|
|
49
93
|
async parsePaymentRequired(response) {
|
|
50
94
|
const headerValue = response.headers.get(PAYMENT_REQUIRED_HEADER);
|
|
@@ -53,31 +97,46 @@ export class X402FetchClient {
|
|
|
53
97
|
return decodePaymentPayload(headerValue);
|
|
54
98
|
}
|
|
55
99
|
catch {
|
|
56
|
-
//
|
|
100
|
+
// Header malformed — fall through to body.
|
|
57
101
|
}
|
|
58
102
|
}
|
|
59
103
|
try {
|
|
60
|
-
const body = await response.json();
|
|
61
|
-
if (
|
|
104
|
+
const body = (await response.clone().json());
|
|
105
|
+
if (Array.isArray(body.accepts)) {
|
|
62
106
|
return body;
|
|
63
107
|
}
|
|
64
108
|
}
|
|
65
109
|
catch {
|
|
66
|
-
//
|
|
110
|
+
// Body not parseable — give up.
|
|
67
111
|
}
|
|
68
112
|
return null;
|
|
69
113
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Retry request with payment payload
|
|
72
|
-
*/
|
|
114
|
+
/** Re-issue the original request with the encoded payment payload header. */
|
|
73
115
|
async retryWithPayment(url, init, paymentPayload) {
|
|
74
|
-
const
|
|
116
|
+
const encoded = encodePaymentPayload(paymentPayload);
|
|
75
117
|
const headers = new Headers(init?.headers);
|
|
76
|
-
headers.set(PAYMENT_SIGNATURE_HEADER,
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
118
|
+
headers.set(PAYMENT_SIGNATURE_HEADER, encoded);
|
|
119
|
+
return this.fetchImpl(url, { ...init, headers });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Pull the {@link SettleResponse} out of a 200 response's PAYMENT-RESPONSE header.
|
|
124
|
+
*
|
|
125
|
+
* Servers attach this on success so the client can record the settlement tx
|
|
126
|
+
* hash / network without making a separate facilitator call.
|
|
127
|
+
*
|
|
128
|
+
* @returns The decoded settle response, or `null` if the header is absent or malformed.
|
|
129
|
+
*/
|
|
130
|
+
export function parsePaymentResponseHeader(response) {
|
|
131
|
+
const headerValue = response.headers.get(PAYMENT_RESPONSE_HEADER);
|
|
132
|
+
if (!headerValue) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
return decodePaymentPayload(headerValue);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return null;
|
|
81
140
|
}
|
|
82
141
|
}
|
|
83
142
|
//# sourceMappingURL=client.js.map
|
package/dist/http/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/http/client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/http/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAML,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AAErB,gCAAgC;AAChC,MAAM,CAAC,MAAM,wBAAwB,GAAG,mBAAmB,CAAC;AAC5D,MAAM,CAAC,MAAM,uBAAuB,GAAG,kBAAkB,CAAC;AAC1D,MAAM,CAAC,MAAM,uBAAuB,GAAG,kBAAkB,CAAC;AAU1D;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,eAAe;IACT,UAAU,CAAa;IACvB,QAAQ,CAA+B;IACvC,SAAS,CAAe;IAEzC,YACE,UAAsB,EACtB,iBAAwE;QAExE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,OAAO,iBAAiB,KAAK,UAAU,EAAE,CAAC;YAC5C,IAAI,CAAC,QAAQ,GAAG,iBAAiB,CAAC;YAClC,IAAI,CAAC,SAAS,GAAG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C,gBAAgB,CAC5B,GAAW,EACX,IAA6B,EAC7B,cAA8B;QAE9B,MAAM,OAAO,GAAG,oBAAoB,CAAC,cAAc,CAAC,CAAC;QACrD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC3C,OAAO,CAAC,GAAG,CAAC,wBAAwB,EAAE,OAAO,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IACnD,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,0BAA0B,CAAC,QAAkB;IAC3D,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAClE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAAC;QACH,OAAO,oBAAoB,CAAiB,WAAW,CAAC,CAAC;IAC3D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,8 +12,11 @@ export * from './errors.js';
|
|
|
12
12
|
export * from './tokens.js';
|
|
13
13
|
export * from './address.js';
|
|
14
14
|
export * from './http/client.js';
|
|
15
|
+
export * from './facilitator/index.js';
|
|
16
|
+
export * from './server/index.js';
|
|
17
|
+
export * from './middleware/index.js';
|
|
15
18
|
export * from './mechanisms/index.js';
|
|
16
|
-
export * from './mechanisms/
|
|
19
|
+
export * from './mechanisms/evm/exact_permit/index.js';
|
|
17
20
|
export * from './signers/signer.js';
|
|
18
21
|
export * from './signers/evmSigner.js';
|
|
19
22
|
export type { TronWeb, TypedDataDomain, TypedDataField, TronNetwork, TRON_CHAIN_IDS } from './signers/types.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAG7B,cAAc,kBAAkB,CAAC;AAGjC,cAAc,uBAAuB,CAAC;AAGtC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAG7B,cAAc,kBAAkB,CAAC;AAGjC,cAAc,wBAAwB,CAAC;AAGvC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,uBAAuB,CAAC;AAGtC,cAAc,uBAAuB,CAAC;AAGtC,cAAc,wCAAwC,CAAC;AAGvD,cAAc,qBAAqB,CAAC;AAEpC,cAAc,wBAAwB,CAAC;AACvC,YAAY,EAAE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -14,10 +14,16 @@ export * from './tokens.js';
|
|
|
14
14
|
export * from './address.js';
|
|
15
15
|
// HTTP Client
|
|
16
16
|
export * from './http/client.js';
|
|
17
|
+
// Facilitator client (talks to remote /verify, /settle, /supported, /fee/quote)
|
|
18
|
+
export * from './facilitator/index.js';
|
|
19
|
+
// High-level server API (X402Server, ResourceConfig, ServerMechanism)
|
|
20
|
+
export * from './server/index.js';
|
|
21
|
+
// Server middleware (Hono / Express adapters + framework-agnostic core)
|
|
22
|
+
export * from './middleware/index.js';
|
|
17
23
|
// Mechanisms
|
|
18
24
|
export * from './mechanisms/index.js';
|
|
19
25
|
// EVM ExactPermit Mechanism
|
|
20
|
-
export * from './mechanisms/
|
|
26
|
+
export * from './mechanisms/evm/exact_permit/index.js';
|
|
21
27
|
// TRON Signer
|
|
22
28
|
export * from './signers/signer.js';
|
|
23
29
|
// EVM Signer
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO;AACP,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAE7B,cAAc;AACd,cAAc,kBAAkB,CAAC;AAEjC,aAAa;AACb,cAAc,uBAAuB,CAAC;AAEtC,4BAA4B;AAC5B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO;AACP,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAE7B,cAAc;AACd,cAAc,kBAAkB,CAAC;AAEjC,gFAAgF;AAChF,cAAc,wBAAwB,CAAC;AAEvC,sEAAsE;AACtE,cAAc,mBAAmB,CAAC;AAElC,wEAAwE;AACxE,cAAc,uBAAuB,CAAC;AAEtC,aAAa;AACb,cAAc,uBAAuB,CAAC;AAEtC,4BAA4B;AAC5B,cAAc,wCAAwC,CAAC;AAEvD,cAAc;AACd,cAAc,qBAAqB,CAAC;AACpC,aAAa;AACb,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base mechanism interfaces — mirrors Python `bankofai.x402.mechanisms._base`.
|
|
3
|
+
*
|
|
4
|
+
* The three role interfaces (`ClientMechanism`, `ServerMechanism`,
|
|
5
|
+
* `FacilitatorMechanism`) currently live close to where they're consumed:
|
|
6
|
+
*
|
|
7
|
+
* - `ClientMechanism` → `src/client/x402Client.ts`
|
|
8
|
+
* - `ServerMechanism` → `src/server/types.ts`
|
|
9
|
+
* - `FacilitatorMechanism` → `src/facilitator/x402Facilitator.ts`
|
|
10
|
+
*
|
|
11
|
+
* This file re-exports them under one mechanism-rooted location so that
|
|
12
|
+
* `mechanisms/<chain>/<scheme>/{client,server,facilitator}.ts` modules import
|
|
13
|
+
* their role contract from a single canonical path. Mirrors how Python's
|
|
14
|
+
* `_base/{client,server,facilitator}.py` are imported by concrete mechanisms.
|
|
15
|
+
*/
|
|
16
|
+
export type { ClientMechanism, ClientSigner } from '../../client/x402Client.js';
|
|
17
|
+
export type { ServerMechanism } from '../../server/types.js';
|
|
18
|
+
export type { FacilitatorMechanism, FacilitatorLogger, } from '../../facilitator/x402Facilitator.js';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mechanisms/_base/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAChF,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,YAAY,EACV,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,sCAAsC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base mechanism interfaces — mirrors Python `bankofai.x402.mechanisms._base`.
|
|
3
|
+
*
|
|
4
|
+
* The three role interfaces (`ClientMechanism`, `ServerMechanism`,
|
|
5
|
+
* `FacilitatorMechanism`) currently live close to where they're consumed:
|
|
6
|
+
*
|
|
7
|
+
* - `ClientMechanism` → `src/client/x402Client.ts`
|
|
8
|
+
* - `ServerMechanism` → `src/server/types.ts`
|
|
9
|
+
* - `FacilitatorMechanism` → `src/facilitator/x402Facilitator.ts`
|
|
10
|
+
*
|
|
11
|
+
* This file re-exports them under one mechanism-rooted location so that
|
|
12
|
+
* `mechanisms/<chain>/<scheme>/{client,server,facilitator}.ts` modules import
|
|
13
|
+
* their role contract from a single canonical path. Mirrors how Python's
|
|
14
|
+
* `_base/{client,server,facilitator}.py` are imported by concrete mechanisms.
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/mechanisms/_base/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChainAdapter — pluggable per-chain primitives for the `exact` scheme.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors Python `mechanisms._exact_base.base.ChainAdapter`. Subclasses
|
|
5
|
+
* provide chain-specific parsing / validation / address normalization so
|
|
6
|
+
* `ExactBase{Client,Server,Facilitator}Mechanism` can stay chain-agnostic.
|
|
7
|
+
*/
|
|
8
|
+
export interface ChainAdapter {
|
|
9
|
+
/** Parse chain id from CAIP-2 network (e.g. `"eip155:97"` → `97`). */
|
|
10
|
+
parseChainId(network: string): number;
|
|
11
|
+
/** Check the CAIP-2 prefix is valid for this adapter (`"eip155:"` / `"tron:"`). */
|
|
12
|
+
validateNetwork(network: string): boolean;
|
|
13
|
+
/** Format check (no on-chain lookup). */
|
|
14
|
+
validateAddress(address: string): boolean;
|
|
15
|
+
/** Canonicalize for storage / comparison (lowercase EVM, Base58 TRON, ...). */
|
|
16
|
+
normalizeAddress(address: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Convert to the address representation used inside EIP-712 / TIP-712 typed
|
|
19
|
+
* data (0x-prefixed hex on both chains). Used by signing path.
|
|
20
|
+
*/
|
|
21
|
+
toSigningAddress(address: string): string;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../src/mechanisms/_exact_base/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,YAAY;IAC3B,sEAAsE;IACtE,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,mFAAmF;IACnF,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IAC1C,yCAAyC;IACzC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IAC1C,+EAA+E;IAC/E,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1C;;;OAGG;IACH,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3C"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChainAdapter — pluggable per-chain primitives for the `exact` scheme.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors Python `mechanisms._exact_base.base.ChainAdapter`. Subclasses
|
|
5
|
+
* provide chain-specific parsing / validation / address normalization so
|
|
6
|
+
* `ExactBase{Client,Server,Facilitator}Mechanism` can stay chain-agnostic.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../../src/mechanisms/_exact_base/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|