@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
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# @x402/core
|
|
2
|
+
|
|
3
|
+
Core implementation of the x402 payment protocol for TypeScript/JavaScript applications. Provides transport-agnostic client, server and facilitator components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install @x402/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### Client Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { x402Client } from '@x402/core/client';
|
|
17
|
+
import { x402HTTPClient } from '@x402/core/http';
|
|
18
|
+
import { ExactEvmScheme } from '@x402/evm/exact/client';
|
|
19
|
+
|
|
20
|
+
// Create core client and register payment schemes
|
|
21
|
+
const coreClient = new x402Client()
|
|
22
|
+
.register('eip155:*', new ExactEvmScheme(evmSigner));
|
|
23
|
+
|
|
24
|
+
// Wrap with HTTP client for header encoding/decoding
|
|
25
|
+
const client = new x402HTTPClient(coreClient);
|
|
26
|
+
|
|
27
|
+
// Make a request
|
|
28
|
+
const response = await fetch('https://api.example.com/protected');
|
|
29
|
+
|
|
30
|
+
if (response.status === 402) {
|
|
31
|
+
// Extract payment requirements from response
|
|
32
|
+
const paymentRequired = client.getPaymentRequiredResponse(
|
|
33
|
+
(name) => response.headers.get(name),
|
|
34
|
+
await response.json()
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Create and send payment
|
|
38
|
+
const paymentPayload = await client.createPaymentPayload(paymentRequired);
|
|
39
|
+
|
|
40
|
+
const paidResponse = await fetch('https://api.example.com/protected', {
|
|
41
|
+
headers: client.encodePaymentSignatureHeader(paymentPayload),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Get settlement confirmation
|
|
45
|
+
const settlement = client.getPaymentSettleResponse(
|
|
46
|
+
(name) => paidResponse.headers.get(name)
|
|
47
|
+
);
|
|
48
|
+
console.log('Transaction:', settlement.transaction);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Server Usage
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { x402ResourceServer, HTTPFacilitatorClient } from '@x402/core/server';
|
|
56
|
+
import { x402HTTPResourceServer } from '@x402/core/http';
|
|
57
|
+
import { ExactEvmScheme } from '@x402/evm/exact/server';
|
|
58
|
+
|
|
59
|
+
// Connect to facilitator
|
|
60
|
+
const facilitatorClient = new HTTPFacilitatorClient({
|
|
61
|
+
url: 'https://x402.org/facilitator',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Create resource server with payment schemes
|
|
65
|
+
const resourceServer = new x402ResourceServer(facilitatorClient)
|
|
66
|
+
.register('eip155:*', new ExactEvmScheme());
|
|
67
|
+
|
|
68
|
+
// Initialize (fetches supported kinds from facilitator)
|
|
69
|
+
await resourceServer.initialize();
|
|
70
|
+
|
|
71
|
+
// Configure routes with payment requirements
|
|
72
|
+
const routes = {
|
|
73
|
+
'GET /api/data': {
|
|
74
|
+
accepts: {
|
|
75
|
+
scheme: 'exact',
|
|
76
|
+
network: 'eip155:8453',
|
|
77
|
+
payTo: '0xYourAddress',
|
|
78
|
+
price: '$0.01',
|
|
79
|
+
},
|
|
80
|
+
description: 'Premium data access',
|
|
81
|
+
mimeType: 'application/json',
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Create HTTP server wrapper
|
|
86
|
+
const httpServer = new x402HTTPResourceServer(resourceServer, routes);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Facilitator Usage
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { x402Facilitator } from '@x402/core/facilitator';
|
|
93
|
+
import { registerExactEvmScheme } from '@x402/evm/exact/facilitator';
|
|
94
|
+
|
|
95
|
+
const facilitator = new x402Facilitator();
|
|
96
|
+
|
|
97
|
+
// Register scheme implementations using helper
|
|
98
|
+
registerExactEvmScheme(facilitator, {
|
|
99
|
+
signer: evmSigner,
|
|
100
|
+
networks: 'eip155:84532',
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Verify payment
|
|
104
|
+
const verifyResult = await facilitator.verify(paymentPayload, paymentRequirements);
|
|
105
|
+
|
|
106
|
+
if (verifyResult.isValid) {
|
|
107
|
+
// Settle payment
|
|
108
|
+
const settleResult = await facilitator.settle(paymentPayload, paymentRequirements);
|
|
109
|
+
console.log('Transaction:', settleResult.transaction);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Route Configuration
|
|
114
|
+
|
|
115
|
+
Routes use the `accepts` field to define payment options:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const routes = {
|
|
119
|
+
// Single payment option
|
|
120
|
+
'GET /api/data': {
|
|
121
|
+
accepts: {
|
|
122
|
+
scheme: 'exact',
|
|
123
|
+
network: 'eip155:8453',
|
|
124
|
+
payTo: '0xAddress',
|
|
125
|
+
price: '$0.01',
|
|
126
|
+
},
|
|
127
|
+
description: 'Data endpoint',
|
|
128
|
+
mimeType: 'application/json',
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
// Multiple payment options (EVM + SVM)
|
|
132
|
+
'POST /api/*': {
|
|
133
|
+
accepts: [
|
|
134
|
+
{
|
|
135
|
+
scheme: 'exact',
|
|
136
|
+
network: 'eip155:8453',
|
|
137
|
+
payTo: evmAddress,
|
|
138
|
+
price: '$0.05',
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
scheme: 'exact',
|
|
142
|
+
network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
143
|
+
payTo: svmAddress,
|
|
144
|
+
price: '$0.05',
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Client Configuration
|
|
152
|
+
|
|
153
|
+
Use `fromConfig()` for declarative setup:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const client = x402Client.fromConfig({
|
|
157
|
+
schemes: [
|
|
158
|
+
{ network: 'eip155:8453', client: new ExactEvmScheme(evmSigner) },
|
|
159
|
+
{ network: 'solana:mainnet', client: new ExactSvmScheme(svmSigner) },
|
|
160
|
+
],
|
|
161
|
+
policies: [
|
|
162
|
+
// Filter by max price
|
|
163
|
+
(version, reqs) => reqs.filter(r => BigInt(r.amount) < BigInt('1000000')),
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Lifecycle Hooks
|
|
169
|
+
|
|
170
|
+
### Client Hooks
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
client
|
|
174
|
+
.onBeforePaymentCreation(async (ctx) => {
|
|
175
|
+
console.log('Creating payment for:', ctx.selectedRequirements.network);
|
|
176
|
+
// Return { abort: true, reason: '...' } to cancel
|
|
177
|
+
})
|
|
178
|
+
.onAfterPaymentCreation(async (ctx) => {
|
|
179
|
+
console.log('Payment created:', ctx.paymentPayload);
|
|
180
|
+
})
|
|
181
|
+
.onPaymentCreationFailure(async (ctx) => {
|
|
182
|
+
console.error('Payment failed:', ctx.error);
|
|
183
|
+
// Return { recovered: true, payload: ... } to recover
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Server Hooks
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
resourceServer
|
|
191
|
+
.onBeforeVerify(async (ctx) => { /* ... */ })
|
|
192
|
+
.onAfterVerify(async (ctx) => { /* ... */ })
|
|
193
|
+
.onBeforeSettle(async (ctx) => { /* ... */ })
|
|
194
|
+
.onAfterSettle(async (ctx) => { /* ... */ });
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Facilitator Hooks
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
facilitator
|
|
201
|
+
.onBeforeVerify(async (ctx) => { console.log('Before verify', ctx); })
|
|
202
|
+
.onAfterVerify(async (ctx) => { console.log('After verify', ctx); })
|
|
203
|
+
.onVerifyFailure(async (ctx) => { console.log('Verify failure', ctx); })
|
|
204
|
+
.onBeforeSettle(async (ctx) => { console.log('Before settle', ctx); })
|
|
205
|
+
.onAfterSettle(async (ctx) => { console.log('After settle', ctx); })
|
|
206
|
+
.onSettleFailure(async (ctx) => { console.log('Settle failure', ctx); });
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## HTTP Headers
|
|
210
|
+
|
|
211
|
+
### v2 Protocol (Current)
|
|
212
|
+
|
|
213
|
+
| Header | Description |
|
|
214
|
+
|--------|-------------|
|
|
215
|
+
| `PAYMENT-SIGNATURE` | Base64-encoded payment payload |
|
|
216
|
+
| `PAYMENT-REQUIRED` | Base64-encoded payment requirements |
|
|
217
|
+
| `PAYMENT-RESPONSE` | Base64-encoded settlement response |
|
|
218
|
+
|
|
219
|
+
### v1 Protocol (Legacy)
|
|
220
|
+
|
|
221
|
+
| Header | Description |
|
|
222
|
+
|--------|-------------|
|
|
223
|
+
| `X-PAYMENT` | Base64-encoded payment payload |
|
|
224
|
+
| `X-PAYMENT-RESPONSE` | Base64-encoded settlement response |
|
|
225
|
+
|
|
226
|
+
## Network Pattern Matching
|
|
227
|
+
|
|
228
|
+
Register handlers for network families using wildcards:
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
// All EVM networks
|
|
232
|
+
server.register('eip155:*', new ExactEvmScheme());
|
|
233
|
+
|
|
234
|
+
// Specific network takes precedence
|
|
235
|
+
server.register('eip155:8453', new ExactEvmScheme());
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Types
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
type Network = `${string}:${string}`; // e.g., "eip155:8453"
|
|
242
|
+
|
|
243
|
+
type PaymentRequirements = {
|
|
244
|
+
scheme: string;
|
|
245
|
+
network: Network;
|
|
246
|
+
asset: string;
|
|
247
|
+
amount: string;
|
|
248
|
+
payTo: string;
|
|
249
|
+
maxTimeoutSeconds: number;
|
|
250
|
+
extra: Record<string, unknown>;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
type PaymentPayload = {
|
|
254
|
+
x402Version: number;
|
|
255
|
+
resource: ResourceInfo;
|
|
256
|
+
accepted: PaymentRequirements;
|
|
257
|
+
payload: Record<string, unknown>;
|
|
258
|
+
extensions?: Record<string, unknown>;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
type PaymentRequired = {
|
|
262
|
+
x402Version: number;
|
|
263
|
+
error?: string;
|
|
264
|
+
resource: ResourceInfo;
|
|
265
|
+
accepts: PaymentRequirements[];
|
|
266
|
+
extensions?: Record<string, unknown>;
|
|
267
|
+
};
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Framework Integration
|
|
271
|
+
|
|
272
|
+
For framework-specific middleware, use:
|
|
273
|
+
|
|
274
|
+
- `@x402/express` - Express.js middleware
|
|
275
|
+
- `@x402/hono` - Hono middleware
|
|
276
|
+
- `@x402/next` - Next.js integration
|
|
277
|
+
- `@x402/axios` - Axios interceptor
|
|
278
|
+
- `@x402/fetch` - Fetch wrapper
|
|
279
|
+
|
|
280
|
+
## Implementation Packages
|
|
281
|
+
|
|
282
|
+
For blockchain-specific implementations:
|
|
283
|
+
|
|
284
|
+
- `@x402/evm` - Ethereum and EVM-compatible chains
|
|
285
|
+
- `@x402/svm` - Solana blockchain
|
|
286
|
+
|
|
287
|
+
## Examples
|
|
288
|
+
|
|
289
|
+
See the [examples directory](https://github.com/coinbase/x402/tree/main/examples/typescript) for complete examples.
|
|
290
|
+
|
|
291
|
+
## Contributing
|
|
292
|
+
|
|
293
|
+
Contributions welcome! See [Contributing Guide](https://github.com/coinbase/x402/blob/main/CONTRIBUTING.md).
|