x402-core-mantle 2.1.1-mantle
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/client/index.d.ts +243 -0
- package/dist/cjs/client/index.js +413 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +192 -0
- package/dist/cjs/facilitator/index.js +398 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +52 -0
- package/dist/cjs/http/index.js +827 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mechanisms-CzuGzYsS.d.ts +270 -0
- package/dist/cjs/server/index.d.ts +2 -0
- package/dist/cjs/server/index.js +1305 -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 +66 -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-D1YtlH_r.d.ts +719 -0
- package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
- package/dist/esm/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs +86 -0
- package/dist/esm/chunk-TDLQZ6MP.mjs.map +1 -0
- package/dist/esm/chunk-VE37GDG2.mjs +7 -0
- package/dist/esm/chunk-VE37GDG2.mjs.map +1 -0
- package/dist/esm/chunk-X4W4S5RB.mjs +39 -0
- package/dist/esm/chunk-X4W4S5RB.mjs.map +1 -0
- package/dist/esm/chunk-Z4QX3O5V.mjs +748 -0
- package/dist/esm/chunk-Z4QX3O5V.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +243 -0
- package/dist/esm/client/index.mjs +260 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +192 -0
- package/dist/esm/facilitator/index.mjs +373 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +52 -0
- package/dist/esm/http/index.mjs +29 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +3 -0
- package/dist/esm/index.mjs +8 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/mechanisms-CzuGzYsS.d.mts +270 -0
- package/dist/esm/server/index.d.mts +2 -0
- package/dist/esm/server/index.mjs +563 -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-BIfIK5HS.d.mts +719 -0
- package/package.json +129 -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).
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { c as PaymentRequired, a as PaymentRequirements, P as PaymentPayload, N as Network, d as SchemeNetworkClient, S as SettleResponse } from '../mechanisms-CzuGzYsS.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client Hook Context Interfaces
|
|
5
|
+
*/
|
|
6
|
+
interface PaymentCreationContext {
|
|
7
|
+
paymentRequired: PaymentRequired;
|
|
8
|
+
selectedRequirements: PaymentRequirements;
|
|
9
|
+
}
|
|
10
|
+
interface PaymentCreatedContext extends PaymentCreationContext {
|
|
11
|
+
paymentPayload: PaymentPayload;
|
|
12
|
+
}
|
|
13
|
+
interface PaymentCreationFailureContext extends PaymentCreationContext {
|
|
14
|
+
error: Error;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Client Hook Type Definitions
|
|
18
|
+
*/
|
|
19
|
+
type BeforePaymentCreationHook = (context: PaymentCreationContext) => Promise<void | {
|
|
20
|
+
abort: true;
|
|
21
|
+
reason: string;
|
|
22
|
+
}>;
|
|
23
|
+
type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;
|
|
24
|
+
type OnPaymentCreationFailureHook = (context: PaymentCreationFailureContext) => Promise<void | {
|
|
25
|
+
recovered: true;
|
|
26
|
+
payload: PaymentPayload;
|
|
27
|
+
}>;
|
|
28
|
+
type SelectPaymentRequirements = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;
|
|
29
|
+
/**
|
|
30
|
+
* A policy function that filters or transforms payment requirements.
|
|
31
|
+
* Policies are applied in order before the selector chooses the final option.
|
|
32
|
+
*
|
|
33
|
+
* @param x402Version - The x402 protocol version
|
|
34
|
+
* @param paymentRequirements - Array of payment requirements to filter/transform
|
|
35
|
+
* @returns Filtered array of payment requirements
|
|
36
|
+
*/
|
|
37
|
+
type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];
|
|
38
|
+
/**
|
|
39
|
+
* Configuration for registering a payment scheme with a specific network
|
|
40
|
+
*/
|
|
41
|
+
interface SchemeRegistration {
|
|
42
|
+
/**
|
|
43
|
+
* The network identifier (e.g., 'eip155:8453', 'solana:mainnet')
|
|
44
|
+
*/
|
|
45
|
+
network: Network;
|
|
46
|
+
/**
|
|
47
|
+
* The scheme client implementation for this network
|
|
48
|
+
*/
|
|
49
|
+
client: SchemeNetworkClient;
|
|
50
|
+
/**
|
|
51
|
+
* The x402 protocol version to use for this scheme
|
|
52
|
+
*
|
|
53
|
+
* @default 2
|
|
54
|
+
*/
|
|
55
|
+
x402Version?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Configuration options for the fetch wrapper
|
|
59
|
+
*/
|
|
60
|
+
interface x402ClientConfig {
|
|
61
|
+
/**
|
|
62
|
+
* Array of scheme registrations defining which payment methods are supported
|
|
63
|
+
*/
|
|
64
|
+
schemes: SchemeRegistration[];
|
|
65
|
+
/**
|
|
66
|
+
* Policies to apply to the client
|
|
67
|
+
*/
|
|
68
|
+
policies?: PaymentPolicy[];
|
|
69
|
+
/**
|
|
70
|
+
* Custom payment requirements selector function
|
|
71
|
+
* If not provided, uses the default selector (first available option)
|
|
72
|
+
*/
|
|
73
|
+
paymentRequirementsSelector?: SelectPaymentRequirements;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Core client for managing x402 payment schemes and creating payment payloads.
|
|
77
|
+
*
|
|
78
|
+
* Handles registration of payment schemes, policy-based filtering of payment requirements,
|
|
79
|
+
* and creation of payment payloads based on server requirements.
|
|
80
|
+
*/
|
|
81
|
+
declare class x402Client {
|
|
82
|
+
private readonly paymentRequirementsSelector;
|
|
83
|
+
private readonly registeredClientSchemes;
|
|
84
|
+
private readonly policies;
|
|
85
|
+
private beforePaymentCreationHooks;
|
|
86
|
+
private afterPaymentCreationHooks;
|
|
87
|
+
private onPaymentCreationFailureHooks;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new x402Client instance.
|
|
90
|
+
*
|
|
91
|
+
* @param paymentRequirementsSelector - Function to select payment requirements from available options
|
|
92
|
+
*/
|
|
93
|
+
constructor(paymentRequirementsSelector?: SelectPaymentRequirements);
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new x402Client instance from a configuration object.
|
|
96
|
+
*
|
|
97
|
+
* @param config - The client configuration including schemes, policies, and payment requirements selector
|
|
98
|
+
* @returns A configured x402Client instance
|
|
99
|
+
*/
|
|
100
|
+
static fromConfig(config: x402ClientConfig): x402Client;
|
|
101
|
+
/**
|
|
102
|
+
* Registers a scheme client for the current x402 version.
|
|
103
|
+
*
|
|
104
|
+
* @param network - The network to register the client for
|
|
105
|
+
* @param client - The scheme network client to register
|
|
106
|
+
* @returns The x402Client instance for chaining
|
|
107
|
+
*/
|
|
108
|
+
register(network: Network, client: SchemeNetworkClient): x402Client;
|
|
109
|
+
/**
|
|
110
|
+
* Registers a scheme client for x402 version 1.
|
|
111
|
+
*
|
|
112
|
+
* @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')
|
|
113
|
+
* @param client - The scheme network client to register
|
|
114
|
+
* @returns The x402Client instance for chaining
|
|
115
|
+
*/
|
|
116
|
+
registerV1(network: string, client: SchemeNetworkClient): x402Client;
|
|
117
|
+
/**
|
|
118
|
+
* Registers a policy to filter or transform payment requirements.
|
|
119
|
+
*
|
|
120
|
+
* Policies are applied in order after filtering by registered schemes
|
|
121
|
+
* and before the selector chooses the final payment requirement.
|
|
122
|
+
*
|
|
123
|
+
* @param policy - Function to filter/transform payment requirements
|
|
124
|
+
* @returns The x402Client instance for chaining
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* // Prefer cheaper options
|
|
129
|
+
* client.registerPolicy((version, reqs) =>
|
|
130
|
+
* reqs.filter(r => BigInt(r.value) < BigInt('1000000'))
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* // Prefer specific networks
|
|
134
|
+
* client.registerPolicy((version, reqs) =>
|
|
135
|
+
* reqs.filter(r => r.network.startsWith('eip155:'))
|
|
136
|
+
* );
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
registerPolicy(policy: PaymentPolicy): x402Client;
|
|
140
|
+
/**
|
|
141
|
+
* Register a hook to execute before payment payload creation.
|
|
142
|
+
* Can abort creation by returning { abort: true, reason: string }
|
|
143
|
+
*
|
|
144
|
+
* @param hook - The hook function to register
|
|
145
|
+
* @returns The x402Client instance for chaining
|
|
146
|
+
*/
|
|
147
|
+
onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client;
|
|
148
|
+
/**
|
|
149
|
+
* Register a hook to execute after successful payment payload creation.
|
|
150
|
+
*
|
|
151
|
+
* @param hook - The hook function to register
|
|
152
|
+
* @returns The x402Client instance for chaining
|
|
153
|
+
*/
|
|
154
|
+
onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client;
|
|
155
|
+
/**
|
|
156
|
+
* Register a hook to execute when payment payload creation fails.
|
|
157
|
+
* Can recover from failure by returning { recovered: true, payload: PaymentPayload }
|
|
158
|
+
*
|
|
159
|
+
* @param hook - The hook function to register
|
|
160
|
+
* @returns The x402Client instance for chaining
|
|
161
|
+
*/
|
|
162
|
+
onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client;
|
|
163
|
+
/**
|
|
164
|
+
* Creates a payment payload based on a PaymentRequired response.
|
|
165
|
+
*
|
|
166
|
+
* Automatically extracts x402Version, resource, and extensions from the PaymentRequired
|
|
167
|
+
* response and constructs a complete PaymentPayload with the accepted requirements.
|
|
168
|
+
*
|
|
169
|
+
* @param paymentRequired - The PaymentRequired response from the server
|
|
170
|
+
* @returns Promise resolving to the complete payment payload
|
|
171
|
+
*/
|
|
172
|
+
createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload>;
|
|
173
|
+
/**
|
|
174
|
+
* Selects appropriate payment requirements based on registered clients and policies.
|
|
175
|
+
*
|
|
176
|
+
* Selection process:
|
|
177
|
+
* 1. Filter by registered schemes (network + scheme support)
|
|
178
|
+
* 2. Apply all registered policies in order
|
|
179
|
+
* 3. Use selector to choose final requirement
|
|
180
|
+
*
|
|
181
|
+
* @param x402Version - The x402 protocol version
|
|
182
|
+
* @param paymentRequirements - Array of available payment requirements
|
|
183
|
+
* @returns The selected payment requirements
|
|
184
|
+
*/
|
|
185
|
+
private selectPaymentRequirements;
|
|
186
|
+
/**
|
|
187
|
+
* Internal method to register a scheme client.
|
|
188
|
+
*
|
|
189
|
+
* @param x402Version - The x402 protocol version
|
|
190
|
+
* @param network - The network to register the client for
|
|
191
|
+
* @param client - The scheme network client to register
|
|
192
|
+
* @returns The x402Client instance for chaining
|
|
193
|
+
*/
|
|
194
|
+
private _registerScheme;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* HTTP-specific client for handling x402 payment protocol over HTTP.
|
|
199
|
+
*
|
|
200
|
+
* Wraps a x402Client to provide HTTP-specific encoding/decoding functionality
|
|
201
|
+
* for payment headers and responses while maintaining the builder pattern.
|
|
202
|
+
*/
|
|
203
|
+
declare class x402HTTPClient {
|
|
204
|
+
private readonly client;
|
|
205
|
+
/**
|
|
206
|
+
* Creates a new x402HTTPClient instance.
|
|
207
|
+
*
|
|
208
|
+
* @param client - The underlying x402Client for payment logic
|
|
209
|
+
*/
|
|
210
|
+
constructor(client: x402Client);
|
|
211
|
+
/**
|
|
212
|
+
* Encodes a payment payload into appropriate HTTP headers based on version.
|
|
213
|
+
*
|
|
214
|
+
* @param paymentPayload - The payment payload to encode
|
|
215
|
+
* @returns HTTP headers containing the encoded payment signature
|
|
216
|
+
*/
|
|
217
|
+
encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string>;
|
|
218
|
+
/**
|
|
219
|
+
* Extracts payment required information from HTTP response.
|
|
220
|
+
*
|
|
221
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
222
|
+
* @param body - Optional response body for v1 compatibility
|
|
223
|
+
* @returns The payment required object
|
|
224
|
+
*/
|
|
225
|
+
getPaymentRequiredResponse(getHeader: (name: string) => string | null | undefined, body?: unknown): PaymentRequired;
|
|
226
|
+
/**
|
|
227
|
+
* Extracts payment settlement response from HTTP headers.
|
|
228
|
+
*
|
|
229
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
230
|
+
* @returns The settlement response object
|
|
231
|
+
*/
|
|
232
|
+
getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse;
|
|
233
|
+
/**
|
|
234
|
+
* Creates a payment payload for the given payment requirements.
|
|
235
|
+
* Delegates to the underlying x402Client.
|
|
236
|
+
*
|
|
237
|
+
* @param paymentRequired - The payment required response from the server
|
|
238
|
+
* @returns Promise resolving to the payment payload
|
|
239
|
+
*/
|
|
240
|
+
createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { type AfterPaymentCreationHook, type BeforePaymentCreationHook, type OnPaymentCreationFailureHook, type PaymentCreatedContext, type PaymentCreationContext, type PaymentCreationFailureContext, type PaymentPolicy, type SchemeRegistration, type SelectPaymentRequirements, x402Client, type x402ClientConfig, x402HTTPClient };
|