@x402-crosschain/sdk 1.0.2 → 2.1.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 +143 -356
- package/dist/client/browser-client.d.ts +2 -2
- package/dist/client/browser-client.d.ts.map +1 -1
- package/dist/client/browser-client.js +139 -89
- package/dist/client/browser-client.js.map +1 -1
- package/dist/client/payment-client.d.ts +3 -2
- package/dist/client/payment-client.d.ts.map +1 -1
- package/dist/client/payment-client.js +244 -111
- package/dist/client/payment-client.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/middleware/payment-middleware.d.ts +5 -1
- package/dist/middleware/payment-middleware.d.ts.map +1 -1
- package/dist/middleware/payment-middleware.js +42 -10
- package/dist/middleware/payment-middleware.js.map +1 -1
- package/dist/types.d.ts +63 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +45 -1
- package/dist/types.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
# @x402-crosschain/sdk
|
|
2
2
|
|
|
3
|
-
> **
|
|
3
|
+
> **x402-compliant cross-chain payment SDK with Relay Network**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Accept payments in any token on any chain. Customers pay with ETH, WETH, USDC, or any ERC-20 token. Merchants always receive USDC on Base. Gasless for customers (ERC-20 tokens).
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@x402-crosschain/sdk)
|
|
8
8
|
[](LICENSE)
|
|
9
|
+
|
|
9
10
|
---
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
## **Key Features**
|
|
12
13
|
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **Any Token** - Accept
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
- **
|
|
14
|
+
- **x402 Protocol Compliant** - Works with standard x402 clients
|
|
15
|
+
- **Gasless Payments** - Customers sign permits, no gas needed (ERC-20)
|
|
16
|
+
- **Any Token** - Accept ETH, WETH, USDC, DAI, or any ERC-2612 token
|
|
17
|
+
- **Any Chain** - 10+ chains including Ethereum, Arbitrum, Base, Polygon, BNB Chain
|
|
18
|
+
- **Instant Settlement** - 2-3 second bridging via Relay Network
|
|
19
|
+
- **Simple Integration** - Express middleware for merchants
|
|
19
20
|
|
|
20
21
|
---
|
|
21
22
|
|
|
@@ -24,8 +25,6 @@ Enable your application to accept payments in any token on any chain, with insta
|
|
|
24
25
|
```bash
|
|
25
26
|
npm install @x402-crosschain/sdk
|
|
26
27
|
# or
|
|
27
|
-
yarn add @x402-crosschain/sdk
|
|
28
|
-
# or
|
|
29
28
|
pnpm add @x402-crosschain/sdk
|
|
30
29
|
```
|
|
31
30
|
|
|
@@ -35,455 +34,243 @@ pnpm add @x402-crosschain/sdk
|
|
|
35
34
|
|
|
36
35
|
### **For Merchants (Backend)**
|
|
37
36
|
|
|
38
|
-
Protect your API endpoints with payment requirements:
|
|
39
|
-
|
|
40
37
|
```typescript
|
|
41
38
|
import express from 'express';
|
|
42
39
|
import { paymentMiddleware } from '@x402-crosschain/sdk';
|
|
43
40
|
|
|
44
41
|
const app = express();
|
|
45
42
|
|
|
46
|
-
// Protect endpoint with payment requirement
|
|
47
43
|
app.use(
|
|
48
44
|
'/premium-content',
|
|
49
45
|
paymentMiddleware({
|
|
50
|
-
|
|
51
|
-
price: '1.00',
|
|
46
|
+
payTo: '0xYourWalletAddress',
|
|
47
|
+
price: '$1.00',
|
|
52
48
|
network: 'base',
|
|
53
|
-
facilitatorUrl: '
|
|
49
|
+
facilitatorUrl: 'http://localhost:3001',
|
|
54
50
|
})
|
|
55
51
|
);
|
|
56
52
|
|
|
57
53
|
app.get('/premium-content', (req, res) => {
|
|
58
|
-
res.json({
|
|
54
|
+
res.json({
|
|
55
|
+
content: 'Premium content unlocked!',
|
|
56
|
+
paidBy: (req as any).payment?.payer
|
|
57
|
+
});
|
|
59
58
|
});
|
|
60
59
|
|
|
61
|
-
app.listen(3000
|
|
60
|
+
app.listen(3000);
|
|
62
61
|
```
|
|
63
62
|
|
|
64
|
-
### **For Customers (
|
|
65
|
-
|
|
66
|
-
Make cross-chain payments easily:
|
|
63
|
+
### **For Customers (Node.js)**
|
|
67
64
|
|
|
68
65
|
```typescript
|
|
69
|
-
import {
|
|
70
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
71
|
-
|
|
72
|
-
// Create wallet/signer
|
|
73
|
-
const account = privateKeyToAccount('0xYourPrivateKey');
|
|
74
|
-
|
|
75
|
-
// Make API request
|
|
76
|
-
const response = await fetch('https://api.example.com/premium-content');
|
|
77
|
-
|
|
78
|
-
// If payment required (402 status)
|
|
79
|
-
if (response.status === 402) {
|
|
80
|
-
// Pay with any token on any chain
|
|
81
|
-
const result = await payX402(response, account, {
|
|
82
|
-
fromChainId: 42161, // Arbitrum
|
|
83
|
-
fromToken: '0x0000000000000000000000000000000000000000', // Native ETH
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
console.log('Payment completed:', result.txHash);
|
|
87
|
-
|
|
88
|
-
// Retry request - now authenticated
|
|
89
|
-
const paidResponse = await fetch('https://api.example.com/premium-content', {
|
|
90
|
-
headers: {
|
|
91
|
-
'X-Payment-Signature': result.signature,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const content = await paidResponse.json();
|
|
96
|
-
console.log('Content:', content);
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
---
|
|
66
|
+
import { createPaymentClient } from '@x402-crosschain/sdk';
|
|
101
67
|
|
|
102
|
-
|
|
68
|
+
// Create payment client with preferred token
|
|
69
|
+
const client = createPaymentClient('0xYourPrivateKey', {
|
|
70
|
+
preferredChainId: 42161, // Arbitrum
|
|
71
|
+
preferredToken: '0x0000000000000000000000000000000000000000', // Native ETH
|
|
72
|
+
preferredNetwork: 'arbitrum',
|
|
73
|
+
});
|
|
103
74
|
|
|
75
|
+
// Make request - SDK handles 402 payment automatically
|
|
76
|
+
const response = await client.get('http://localhost:3000/premium-content');
|
|
77
|
+
console.log(response.data);
|
|
104
78
|
```
|
|
105
|
-
┌─────────────────────────────────────────────────────────┐
|
|
106
|
-
│ Your Application │
|
|
107
|
-
├─────────────────────────────────────────────────────────┤
|
|
108
|
-
│ │
|
|
109
|
-
│ Merchant Backend Customer Frontend │
|
|
110
|
-
│ (Express + SDK) (Browser + SDK) │
|
|
111
|
-
│ │ │ │
|
|
112
|
-
│ └──────────┬───────────────┘ │
|
|
113
|
-
│ │ │
|
|
114
|
-
└────────────────────┼────────────────────────────────────┘
|
|
115
|
-
│
|
|
116
|
-
▼
|
|
117
|
-
┌────────────────────────┐
|
|
118
|
-
│ x402 Facilitator │
|
|
119
|
-
│ (our facilitator or │
|
|
120
|
-
│ hosted service) │
|
|
121
|
-
└────────────────────────┘
|
|
122
|
-
│
|
|
123
|
-
▼
|
|
124
|
-
┌────────────────────────┐
|
|
125
|
-
│ Relay Network │
|
|
126
|
-
│ (Instant bridging via │
|
|
127
|
-
│ liquidity pools) │
|
|
128
|
-
└────────────────────────┘
|
|
129
|
-
│
|
|
130
|
-
┌────────────┴────────────┐
|
|
131
|
-
▼ ▼
|
|
132
|
-
Arbitrum Base
|
|
133
|
-
(Customer pays) (Merchant receives)
|
|
134
|
-
ETH, USDC, etc. USDC
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
---
|
|
138
|
-
|
|
139
|
-
## **API Reference**
|
|
140
|
-
|
|
141
|
-
### **Merchant Middleware**
|
|
142
|
-
|
|
143
|
-
#### `paymentMiddleware(config: PaymentConfig)`
|
|
144
79
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
**Parameters:**
|
|
80
|
+
### **For Customers (Browser)**
|
|
148
81
|
|
|
149
82
|
```typescript
|
|
150
|
-
|
|
151
|
-
recipient: string; // Wallet address to receive payments
|
|
152
|
-
price: string; // Price in USD (e.g., "1.00")
|
|
153
|
-
network: 'base' | 'base-sepolia'; // Settlement network
|
|
154
|
-
facilitatorUrl: string; // Facilitator endpoint URL
|
|
155
|
-
}
|
|
156
|
-
```
|
|
83
|
+
import { createBrowserPaymentClient } from '@x402-crosschain/sdk';
|
|
157
84
|
|
|
158
|
-
|
|
85
|
+
// With MetaMask/Coinbase Wallet
|
|
86
|
+
const client = createBrowserPaymentClient(walletClient, {
|
|
87
|
+
preferredChainId: 8453,
|
|
88
|
+
preferredToken: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
|
|
89
|
+
});
|
|
159
90
|
|
|
160
|
-
|
|
161
|
-
app.use(
|
|
162
|
-
'/api/premium',
|
|
163
|
-
paymentMiddleware({
|
|
164
|
-
recipient: '0x742d35Cc6634C0532925a3b8D9d4DB0a2D7DD5B3',
|
|
165
|
-
price: '5.00',
|
|
166
|
-
network: 'base',
|
|
167
|
-
facilitatorUrl: 'https://facilitator.yourdomain.com',
|
|
168
|
-
})
|
|
169
|
-
);
|
|
91
|
+
const response = await client.get('https://api.example.com/premium');
|
|
170
92
|
```
|
|
171
93
|
|
|
172
94
|
---
|
|
173
95
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
#### `payX402(response, signer, userPreference)`
|
|
96
|
+
## **How It Works**
|
|
177
97
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
**Parameters:**
|
|
181
|
-
|
|
182
|
-
```typescript
|
|
183
|
-
interface UserPaymentPreference {
|
|
184
|
-
fromChainId: number; // Source chain ID (e.g., 42161 for Arbitrum)
|
|
185
|
-
fromToken: string; // Token address (0x0000... for native)
|
|
186
|
-
}
|
|
98
|
+
### **ERC-20 Tokens (Gasless for Customer)**
|
|
187
99
|
|
|
188
|
-
// Signer can be:
|
|
189
|
-
// - viem Account: privateKeyToAccount('0x...')
|
|
190
|
-
// - ethers Signer: new Wallet('0x...', provider)
|
|
191
100
|
```
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
signature: string; // Payment proof for subsequent requests
|
|
201
|
-
}
|
|
101
|
+
1. Customer requests protected resource
|
|
102
|
+
2. Server returns 402 with payment requirements
|
|
103
|
+
3. Customer signs ERC-2612 permit or EIP-3009 authorization (NO GAS)
|
|
104
|
+
4. Customer retries with X-PAYMENT header
|
|
105
|
+
5. Facilitator verifies signature
|
|
106
|
+
6. Facilitator takes tokens via permit
|
|
107
|
+
7. Facilitator swaps + bridges via Relay → USDC on Base
|
|
108
|
+
8. Merchant receives USDC
|
|
202
109
|
```
|
|
203
110
|
|
|
204
|
-
**
|
|
205
|
-
|
|
206
|
-
```typescript
|
|
207
|
-
const result = await payX402(response, signer, {
|
|
208
|
-
fromChainId: 42161, // Arbitrum
|
|
209
|
-
fromToken: '0x0000000000000000000000000000000000000000', // ETH
|
|
210
|
-
});
|
|
111
|
+
### **Native ETH/BNB (Customer Pays Gas)**
|
|
211
112
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
113
|
+
```
|
|
114
|
+
1. Customer requests protected resource
|
|
115
|
+
2. Server returns 402 with payment requirements
|
|
116
|
+
3. Customer sends ETH tx to Relay
|
|
117
|
+
4. Relay swaps ETH → USDC + bridges to Base
|
|
118
|
+
5. Merchant receives USDC
|
|
216
119
|
```
|
|
217
120
|
|
|
218
121
|
---
|
|
219
122
|
|
|
220
|
-
##
|
|
221
|
-
|
|
222
|
-
The SDK supports 69+ chains via Relay Network:
|
|
223
|
-
|
|
224
|
-
### **Major EVM Chains**
|
|
225
|
-
- Ethereum (1)
|
|
226
|
-
- Base (8453)
|
|
227
|
-
- Arbitrum (42161)
|
|
228
|
-
- Optimism (10)
|
|
229
|
-
- Polygon (137)
|
|
230
|
-
- BNB Chain (56)
|
|
231
|
-
- Avalanche (43114)
|
|
232
|
-
- And 60+ more...
|
|
123
|
+
## **Supported Tokens**
|
|
233
124
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
- ✅ Tron
|
|
240
|
-
|
|
241
|
-
See full list: https://docs.relay.link/resources/supported-chains
|
|
125
|
+
| Token Type | Gasless? | How It Works |
|
|
126
|
+
|------------|----------|--------------|
|
|
127
|
+
| **USDC** | ✅ Yes | EIP-3009 TransferWithAuthorization |
|
|
128
|
+
| **WETH, DAI, etc.** | ✅ Yes | ERC-2612 Permit |
|
|
129
|
+
| **Native ETH/BNB/MATIC** | ❌ No | Customer sends tx to Relay |
|
|
242
130
|
|
|
243
131
|
---
|
|
244
132
|
|
|
245
|
-
##
|
|
246
|
-
|
|
247
|
-
### **Source Chains (What customers can pay with)**
|
|
248
|
-
- Native tokens (ETH, MATIC, AVAX, etc.)
|
|
249
|
-
- USDC
|
|
250
|
-
- USDT
|
|
251
|
-
- WETH
|
|
252
|
-
- DAI
|
|
253
|
-
- And more...
|
|
133
|
+
## **Supported Chains**
|
|
254
134
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
135
|
+
| Chain | ID | Native Token |
|
|
136
|
+
|-------|-----|--------------|
|
|
137
|
+
| Ethereum | 1 | ETH |
|
|
138
|
+
| Base | 8453 | ETH |
|
|
139
|
+
| Arbitrum | 42161 | ETH |
|
|
140
|
+
| Optimism | 10 | ETH |
|
|
141
|
+
| Polygon | 137 | MATIC |
|
|
142
|
+
| BNB Chain | 56 | BNB |
|
|
143
|
+
| Avalanche | 43114 | AVAX |
|
|
144
|
+
| zkSync | 324 | ETH |
|
|
145
|
+
| Linea | 59144 | ETH |
|
|
259
146
|
|
|
260
147
|
---
|
|
261
148
|
|
|
262
|
-
##
|
|
263
|
-
|
|
264
|
-
### **Dynamic Pricing**
|
|
265
|
-
|
|
266
|
-
```typescript
|
|
267
|
-
app.use(
|
|
268
|
-
'/api/content/:id',
|
|
269
|
-
paymentMiddleware({
|
|
270
|
-
recipient: '0xYourAddress',
|
|
271
|
-
price: async (req) => {
|
|
272
|
-
// Fetch dynamic pricing from database
|
|
273
|
-
const content = await db.getContent(req.params.id);
|
|
274
|
-
return content.price.toString();
|
|
275
|
-
},
|
|
276
|
-
network: 'base',
|
|
277
|
-
facilitatorUrl: process.env.FACILITATOR_URL,
|
|
278
|
-
})
|
|
279
|
-
);
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
### **Custom Token Selection**
|
|
149
|
+
## **API Reference**
|
|
283
150
|
|
|
284
|
-
|
|
285
|
-
// Customer chooses preferred payment token
|
|
286
|
-
const result = await payX402(response, signer, {
|
|
287
|
-
fromChainId: 137, // Polygon
|
|
288
|
-
fromToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC on Polygon
|
|
289
|
-
});
|
|
290
|
-
```
|
|
151
|
+
### **paymentMiddleware(config)**
|
|
291
152
|
|
|
292
|
-
|
|
153
|
+
Express middleware for protecting routes with payment requirements.
|
|
293
154
|
|
|
294
155
|
```typescript
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
} catch (error) {
|
|
302
|
-
if (error.message.includes('insufficient funds')) {
|
|
303
|
-
console.error('User does not have enough tokens');
|
|
304
|
-
} else if (error.message.includes('user rejected')) {
|
|
305
|
-
console.error('User cancelled transaction');
|
|
306
|
-
} else {
|
|
307
|
-
console.error('Payment error:', error);
|
|
308
|
-
}
|
|
156
|
+
interface MiddlewareConfig {
|
|
157
|
+
payTo: string; // Merchant wallet address
|
|
158
|
+
price: string; // Price (e.g., '$0.01', '$10.00')
|
|
159
|
+
network: string; // Settlement network ('base')
|
|
160
|
+
facilitatorUrl: string; // Facilitator URL
|
|
161
|
+
description?: string; // Optional description
|
|
309
162
|
}
|
|
310
163
|
```
|
|
311
164
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
##**Self-Hosting the Facilitator**
|
|
165
|
+
### **createPaymentClient(privateKey, preferences)**
|
|
315
166
|
|
|
316
|
-
|
|
167
|
+
Create a payment client for Node.js applications.
|
|
317
168
|
|
|
318
169
|
```typescript
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
paymentSettlementAddress: '0xYourContractAddress',
|
|
325
|
-
settlerPrivateKey: process.env.SETTLER_PRIVATE_KEY,
|
|
326
|
-
});
|
|
170
|
+
interface PaymentPreferences {
|
|
171
|
+
preferredChainId?: number; // Source chain ID
|
|
172
|
+
preferredToken?: string; // Source token address
|
|
173
|
+
preferredNetwork?: string; // Source network name
|
|
174
|
+
}
|
|
327
175
|
```
|
|
328
176
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
---
|
|
332
|
-
|
|
333
|
-
## **Fee Structure**
|
|
334
|
-
|
|
335
|
-
### **Cross-Chain Payment Costs**
|
|
177
|
+
### **createBrowserPaymentClient(walletClient, preferences)**
|
|
336
178
|
|
|
337
|
-
|
|
338
|
-
|-----------|------|----------|
|
|
339
|
-
| **Bridge Fee** | 0.1-0.3% | Customer |
|
|
340
|
-
| **Gas (Source Chain)** | $0.01-0.10 | Customer |
|
|
341
|
-
| **Gas (Destination)** | Included | Relay |
|
|
342
|
-
| **Settlement Gas** | $0.001 | Facilitator |
|
|
343
|
-
|
|
344
|
-
### **Recommended Minimum Payment**
|
|
345
|
-
|
|
346
|
-
- **Minimum**: $1.00 (15% total fee)
|
|
347
|
-
- **Optimal**: $10+ (2-3% total fee)
|
|
348
|
-
- **Not recommended**: < $0.50 (fees dominate)
|
|
349
|
-
|
|
350
|
-
**Example:**
|
|
351
|
-
```
|
|
352
|
-
Payment: $10.00
|
|
353
|
-
Bridge fee: $0.02 (0.2%)
|
|
354
|
-
Gas costs: $0.03
|
|
355
|
-
Total cost: $10.05
|
|
356
|
-
Effective fee: 0.5%
|
|
357
|
-
```
|
|
179
|
+
Create a payment client for browser applications with MetaMask/Coinbase Wallet.
|
|
358
180
|
|
|
359
181
|
---
|
|
360
182
|
|
|
361
|
-
##
|
|
362
|
-
|
|
363
|
-
### **Payment Verification**
|
|
364
|
-
- All payments verified on-chain
|
|
365
|
-
- Settlement contract ensures atomicity
|
|
366
|
-
- Non-custodial (no funds held by facilitator)
|
|
183
|
+
## **Token Addresses**
|
|
367
184
|
|
|
368
|
-
### **Best Practices**
|
|
369
185
|
```typescript
|
|
370
|
-
//
|
|
371
|
-
|
|
186
|
+
// Native token (ETH, BNB, MATIC, etc.)
|
|
187
|
+
const NATIVE = '0x0000000000000000000000000000000000000000';
|
|
372
188
|
|
|
373
|
-
//
|
|
374
|
-
const
|
|
189
|
+
// USDC
|
|
190
|
+
const USDC_BASE = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
|
|
191
|
+
const USDC_ARBITRUM = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831';
|
|
375
192
|
|
|
376
|
-
//
|
|
377
|
-
const
|
|
193
|
+
// WETH
|
|
194
|
+
const WETH_ARBITRUM = '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1';
|
|
195
|
+
const WETH_BASE = '0x4200000000000000000000000000000000000006';
|
|
378
196
|
```
|
|
379
197
|
|
|
380
198
|
---
|
|
381
199
|
|
|
382
|
-
##
|
|
200
|
+
## **Examples**
|
|
383
201
|
|
|
384
|
-
### **
|
|
202
|
+
### **Pay with Native ETH on Arbitrum**
|
|
385
203
|
|
|
386
204
|
```typescript
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
network: 'base-sepolia',
|
|
392
|
-
facilitatorUrl: 'https://testnet-facilitator.yourdomain.com',
|
|
205
|
+
const client = createPaymentClient(privateKey, {
|
|
206
|
+
preferredChainId: 42161,
|
|
207
|
+
preferredToken: '0x0000000000000000000000000000000000000000',
|
|
208
|
+
preferredNetwork: 'arbitrum',
|
|
393
209
|
});
|
|
394
210
|
```
|
|
395
211
|
|
|
396
|
-
### **
|
|
397
|
-
|
|
398
|
-
- **Base Sepolia ETH**: https://www.alchemy.com/faucets/base-sepolia
|
|
399
|
-
- **Arbitrum Sepolia ETH**: https://faucets.chain.link/arbitrum-sepolia
|
|
400
|
-
|
|
401
|
-
---
|
|
402
|
-
|
|
403
|
-
## **Examples**
|
|
404
|
-
|
|
405
|
-
### **E-commerce API**
|
|
212
|
+
### **Pay with USDC on Base (Same Chain)**
|
|
406
213
|
|
|
407
214
|
```typescript
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
// Product catalog
|
|
414
|
-
const products = {
|
|
415
|
-
'prod_1': { name: 'Premium Article', price: '2.00' },
|
|
416
|
-
'prod_2': { name: 'Video Course', price: '49.00' },
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
// Dynamic payment per product
|
|
420
|
-
app.use(
|
|
421
|
-
'/api/product/:id',
|
|
422
|
-
paymentMiddleware({
|
|
423
|
-
recipient: process.env.MERCHANT_WALLET,
|
|
424
|
-
price: (req) => products[req.params.id].price,
|
|
425
|
-
network: 'base',
|
|
426
|
-
facilitatorUrl: process.env.FACILITATOR_URL,
|
|
427
|
-
})
|
|
428
|
-
);
|
|
429
|
-
|
|
430
|
-
app.get('/api/product/:id', (req, res) => {
|
|
431
|
-
const product = products[req.params.id];
|
|
432
|
-
res.json({ product: product.name, delivered: true });
|
|
215
|
+
const client = createPaymentClient(privateKey, {
|
|
216
|
+
preferredChainId: 8453,
|
|
217
|
+
preferredToken: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
218
|
+
preferredNetwork: 'base',
|
|
433
219
|
});
|
|
434
220
|
```
|
|
435
221
|
|
|
436
|
-
### **
|
|
222
|
+
### **Pay with WETH on Arbitrum**
|
|
437
223
|
|
|
438
224
|
```typescript
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
'
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
// Get creator's wallet from database
|
|
445
|
-
const creator = await db.getCreator(req.params.creatorId);
|
|
446
|
-
return creator.walletAddress;
|
|
447
|
-
},
|
|
448
|
-
price: async (req) => {
|
|
449
|
-
const content = await db.getContent(req.params.contentId);
|
|
450
|
-
return content.price;
|
|
451
|
-
},
|
|
452
|
-
network: 'base',
|
|
453
|
-
facilitatorUrl: process.env.FACILITATOR_URL,
|
|
454
|
-
})
|
|
455
|
-
);
|
|
225
|
+
const client = createPaymentClient(privateKey, {
|
|
226
|
+
preferredChainId: 42161,
|
|
227
|
+
preferredToken: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
|
|
228
|
+
preferredNetwork: 'arbitrum',
|
|
229
|
+
});
|
|
456
230
|
```
|
|
457
231
|
|
|
458
232
|
---
|
|
459
233
|
|
|
460
|
-
## **
|
|
234
|
+
## **Testing**
|
|
461
235
|
|
|
462
|
-
|
|
463
|
-
|
|
236
|
+
```bash
|
|
237
|
+
# Start facilitator
|
|
238
|
+
cd packages/facilitator && pnpm dev
|
|
464
239
|
|
|
240
|
+
# Start merchant server
|
|
241
|
+
cd examples/merchant-hosted && pnpm dev
|
|
242
|
+
|
|
243
|
+
# Run customer client
|
|
244
|
+
cd examples/customer-client && pnpm dev
|
|
245
|
+
```
|
|
465
246
|
|
|
466
247
|
---
|
|
467
248
|
|
|
468
|
-
##
|
|
249
|
+
## **License**
|
|
469
250
|
|
|
470
251
|
MIT License
|
|
471
252
|
|
|
472
253
|
---
|
|
473
254
|
|
|
255
|
+
## **Support**
|
|
256
|
+
|
|
257
|
+
- **GitHub Issues**: https://github.com/divi2806/x402-cross-bridge-sdk/issues
|
|
258
|
+
- **Email**: divyansh2824@gmail.com
|
|
259
|
+
|
|
474
260
|
---
|
|
475
261
|
|
|
476
|
-
##
|
|
262
|
+
## **Changelog**
|
|
263
|
+
|
|
264
|
+
### v2.0.0 (December 2025)
|
|
265
|
+
- **x402 Protocol Compliance** - Full compatibility with x402 standard
|
|
266
|
+
- **Gasless Payments** - EIP-3009 and ERC-2612 signature support
|
|
267
|
+
- **Any Token Support** - Accept any ERC-20 token with permit support
|
|
268
|
+
- **Native Token Support** - Accept ETH, BNB, MATIC via Relay
|
|
269
|
+
- **Multi-Chain** - Added BNB Chain, Avalanche, zkSync, Linea
|
|
270
|
+
- **Simplified API** - New `createPaymentClient` and `createBrowserPaymentClient`
|
|
477
271
|
|
|
478
272
|
### v1.0.0
|
|
479
273
|
- Initial release
|
|
480
|
-
-
|
|
481
|
-
- Express middleware for merchants
|
|
482
|
-
- Browser client for customers
|
|
483
|
-
- Self-hosted facilitator option
|
|
484
|
-
- Comprehensive documentation
|
|
485
|
-
|
|
486
|
-
## **Reference**
|
|
487
|
-
- Refer to facilitator on how to integrate/use the cross chain facilitator - https://www.npmjs.com/package/@x402-crosschain/facilitator
|
|
488
|
-
---
|
|
274
|
+
- Basic cross-chain support via Relay
|
|
489
275
|
|
|
276
|
+
---
|
|
@@ -2,8 +2,8 @@ import { AxiosInstance } from 'axios';
|
|
|
2
2
|
import type { WalletClient } from 'viem';
|
|
3
3
|
import type { PaymentPreferences } from '../types.js';
|
|
4
4
|
/**
|
|
5
|
-
* Create an x402 payment client for BROWSER use with MetaMask/Coinbase Wallet
|
|
6
|
-
*
|
|
5
|
+
* Create an x402-compatible payment client for BROWSER use with MetaMask/Coinbase Wallet
|
|
6
|
+
* Signs Permit2 transfers or EIP-3009 authorizations (gasless for customer)
|
|
7
7
|
*/
|
|
8
8
|
export declare function createBrowserPaymentClient(walletClient: WalletClient, preferences?: PaymentPreferences): AxiosInstance;
|
|
9
9
|
//# sourceMappingURL=browser-client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser-client.d.ts","sourceRoot":"","sources":["../../src/client/browser-client.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAc,MAAM,OAAO,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"browser-client.d.ts","sourceRoot":"","sources":["../../src/client/browser-client.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAc,MAAM,OAAO,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAO,MAAM,MAAM,CAAC;AAE9C,OAAO,KAAK,EAAuC,kBAAkB,EAAgB,MAAM,aAAa,CAAC;AA8BzG;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,YAAY,EAC1B,WAAW,CAAC,EAAE,kBAAkB,GAC/B,aAAa,CAqGf"}
|