@bankofai/x402 0.3.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 +368 -0
- package/dist/abi.d.ts +112 -0
- package/dist/abi.d.ts.map +1 -0
- package/dist/abi.js +73 -0
- package/dist/abi.js.map +1 -0
- package/dist/address.d.ts +50 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +154 -0
- package/dist/address.js.map +1 -0
- package/dist/client/index.d.ts +9 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/policies.d.ts +23 -0
- package/dist/client/policies.d.ts.map +1 -0
- package/dist/client/policies.js +54 -0
- package/dist/client/policies.js.map +1 -0
- package/dist/client/tokenSelection.d.ts +22 -0
- package/dist/client/tokenSelection.d.ts.map +1 -0
- package/dist/client/tokenSelection.js +40 -0
- package/dist/client/tokenSelection.js.map +1 -0
- package/dist/client/x402Client.d.ts +128 -0
- package/dist/client/x402Client.d.ts.map +1 -0
- package/dist/client/x402Client.js +143 -0
- package/dist/client/x402Client.js.map +1 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +92 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +41 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +69 -0
- package/dist/errors.js.map +1 -0
- package/dist/http/client.d.ts +33 -0
- package/dist/http/client.d.ts.map +1 -0
- package/dist/http/client.js +83 -0
- package/dist/http/client.js.map +1 -0
- package/dist/http/index.d.ts +5 -0
- package/dist/http/index.d.ts.map +1 -0
- package/dist/http/index.js +5 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/mechanisms/exact.d.ts +20 -0
- package/dist/mechanisms/exact.d.ts.map +1 -0
- package/dist/mechanisms/exact.js +110 -0
- package/dist/mechanisms/exact.js.map +1 -0
- package/dist/mechanisms/exactEvm.d.ts +18 -0
- package/dist/mechanisms/exactEvm.d.ts.map +1 -0
- package/dist/mechanisms/exactEvm.js +91 -0
- package/dist/mechanisms/exactEvm.js.map +1 -0
- package/dist/mechanisms/index.d.ts +10 -0
- package/dist/mechanisms/index.d.ts.map +1 -0
- package/dist/mechanisms/index.js +12 -0
- package/dist/mechanisms/index.js.map +1 -0
- package/dist/mechanisms/nativeExact.d.ts +64 -0
- package/dist/mechanisms/nativeExact.d.ts.map +1 -0
- package/dist/mechanisms/nativeExact.js +66 -0
- package/dist/mechanisms/nativeExact.js.map +1 -0
- package/dist/mechanisms/nativeExactEvm.d.ts +19 -0
- package/dist/mechanisms/nativeExactEvm.d.ts.map +1 -0
- package/dist/mechanisms/nativeExactEvm.js +61 -0
- package/dist/mechanisms/nativeExactEvm.js.map +1 -0
- package/dist/mechanisms/nativeExactTron.d.ts +19 -0
- package/dist/mechanisms/nativeExactTron.d.ts.map +1 -0
- package/dist/mechanisms/nativeExactTron.js +61 -0
- package/dist/mechanisms/nativeExactTron.js.map +1 -0
- package/dist/signers/evmSigner.d.ts +23 -0
- package/dist/signers/evmSigner.d.ts.map +1 -0
- package/dist/signers/evmSigner.js +159 -0
- package/dist/signers/evmSigner.js.map +1 -0
- package/dist/signers/index.d.ts +8 -0
- package/dist/signers/index.d.ts.map +1 -0
- package/dist/signers/index.js +7 -0
- package/dist/signers/index.js.map +1 -0
- package/dist/signers/signer.d.ts +37 -0
- package/dist/signers/signer.d.ts.map +1 -0
- package/dist/signers/signer.js +179 -0
- package/dist/signers/signer.js.map +1 -0
- package/dist/signers/types.d.ts +70 -0
- package/dist/signers/types.d.ts.map +1 -0
- package/dist/signers/types.js +10 -0
- package/dist/signers/types.js.map +1 -0
- package/dist/tokens.d.ts +18 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +105 -0
- package/dist/tokens.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/payment.d.ts +111 -0
- package/dist/types/payment.d.ts.map +1 -0
- package/dist/types/payment.js +5 -0
- package/dist/types/payment.js.map +1 -0
- package/dist/types/responses.d.ts +76 -0
- package/dist/types/responses.d.ts.map +1 -0
- package/dist/types/responses.js +5 -0
- package/dist/types/responses.js.map +1 -0
- package/dist/utils/encoding.d.ts +48 -0
- package/dist/utils/encoding.d.ts.map +1 -0
- package/dist/utils/encoding.js +105 -0
- package/dist/utils/encoding.js.map +1 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
# @bankofai/x402
|
|
2
|
+
|
|
3
|
+
TypeScript client SDK for the x402 payment protocol on TRON blockchain. Enable pay-per-request APIs with automatic HTTP 402 payment handling.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **Automatic Payment Handling** - Transparently handles HTTP 402 Payment Required responses
|
|
8
|
+
- ⛓️ **TRON Native** - Built for TRON Mainnet, Shasta, and Nile testnets
|
|
9
|
+
- 🔑 **TIP-712 Signing** - Secure cryptographic signatures using TRON's EIP-712 implementation
|
|
10
|
+
- 💰 **Token Approval Management** - Automatic ERC20 token allowance handling
|
|
11
|
+
- 🚀 **Simple Integration** - Just 3 lines of code to get started
|
|
12
|
+
- 📦 **Zero Dependencies** - Only requires `tronweb` as peer dependency
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm i @bankofai/x402 tronweb
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import TronWeb from 'tronweb';
|
|
24
|
+
import {
|
|
25
|
+
X402Client,
|
|
26
|
+
X402FetchClient,
|
|
27
|
+
ExactTronClientMechanism,
|
|
28
|
+
TronClientSigner,
|
|
29
|
+
} from '@bankofai/x402';
|
|
30
|
+
|
|
31
|
+
// 1. Initialize TronWeb
|
|
32
|
+
const tronWeb = new TronWeb({
|
|
33
|
+
fullHost: 'https://nile.trongrid.io',
|
|
34
|
+
privateKey: 'your_private_key_here',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// 2. Create signer and register payment mechanism
|
|
38
|
+
const signer = TronClientSigner.withPrivateKey(tronWeb, 'your_private_key_here', 'nile');
|
|
39
|
+
const x402Client = new X402Client().register('tron:*', new ExactTronClientMechanism(signer));
|
|
40
|
+
|
|
41
|
+
// 3. Create HTTP client with automatic 402 handling
|
|
42
|
+
const client = new X402FetchClient(x402Client);
|
|
43
|
+
|
|
44
|
+
// 4. Make requests - payments are handled automatically!
|
|
45
|
+
const response = await client.get('https://api.example.com/premium-data');
|
|
46
|
+
const data = await response.json();
|
|
47
|
+
console.log(data);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Core Concepts
|
|
51
|
+
|
|
52
|
+
### Payment Flow
|
|
53
|
+
|
|
54
|
+
When you make a request to a protected resource:
|
|
55
|
+
|
|
56
|
+
1. **First Request** - Server responds with `402 Payment Required` and payment requirements
|
|
57
|
+
2. **Payment Creation** - SDK automatically creates and signs a payment permit
|
|
58
|
+
3. **Token Approval** - SDK ensures sufficient token allowance (if needed)
|
|
59
|
+
4. **Retry with Payment** - Request is retried with payment signature
|
|
60
|
+
5. **Success** - Server validates payment and returns the protected resource
|
|
61
|
+
|
|
62
|
+
### Components
|
|
63
|
+
|
|
64
|
+
- **`X402Client`** - Core payment client that manages payment mechanisms
|
|
65
|
+
- **`X402FetchClient`** - HTTP client wrapper with automatic 402 handling
|
|
66
|
+
- **`TronClientSigner`** - Signs payment permits using TIP-712
|
|
67
|
+
- **`ExactTronClientMechanism`** - Implements the "exact" payment scheme for TRON
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
### X402Client
|
|
72
|
+
|
|
73
|
+
Core client for managing payment mechanisms.
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const x402Client = new X402Client();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### Methods
|
|
80
|
+
|
|
81
|
+
##### `register(networkPattern: string, mechanism: ClientMechanism): X402Client`
|
|
82
|
+
|
|
83
|
+
Register a payment mechanism for a network pattern.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
x402Client.register('tron:*', new ExactTronClientMechanism(signer));
|
|
87
|
+
x402Client.register('tron:nile', new ExactTronClientMechanism(nileSigner));
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
##### `selectPaymentRequirements(accepts: PaymentRequirements[], filters?: PaymentRequirementsFilter): PaymentRequirements`
|
|
91
|
+
|
|
92
|
+
Select payment requirements from available options.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const selected = x402Client.selectPaymentRequirements(accepts, {
|
|
96
|
+
network: 'tron:nile',
|
|
97
|
+
maxAmount: '1000000',
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
##### `createPaymentPayload(requirements: PaymentRequirements, resource: string, extensions?: object): Promise<PaymentPayload>`
|
|
102
|
+
|
|
103
|
+
Create a payment payload for the given requirements.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const payload = await x402Client.createPaymentPayload(requirements, '/api/data', extensions);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### X402FetchClient
|
|
110
|
+
|
|
111
|
+
HTTP client with automatic 402 payment handling.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const client = new X402FetchClient(x402Client, selector?);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Methods
|
|
118
|
+
|
|
119
|
+
##### `get(url: string, init?: RequestInit): Promise<Response>`
|
|
120
|
+
|
|
121
|
+
Make a GET request with automatic payment handling.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const response = await client.get('https://api.example.com/data');
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
##### `post(url: string, body?: RequestInit['body'], init?: RequestInit): Promise<Response>`
|
|
128
|
+
|
|
129
|
+
Make a POST request with automatic payment handling.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const response = await client.post('https://api.example.com/data', JSON.stringify({ key: 'value' }));
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
##### `request(url: string, init?: RequestInit): Promise<Response>`
|
|
136
|
+
|
|
137
|
+
Make any HTTP request with automatic payment handling.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const response = await client.request('https://api.example.com/data', {
|
|
141
|
+
method: 'PUT',
|
|
142
|
+
body: JSON.stringify({ key: 'value' }),
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### TronClientSigner
|
|
147
|
+
|
|
148
|
+
Signer for creating payment permits using TIP-712.
|
|
149
|
+
|
|
150
|
+
#### Static Methods
|
|
151
|
+
|
|
152
|
+
##### `withPrivateKey(tronWeb: TronWeb, privateKey: string, network?: TronNetwork): TronClientSigner`
|
|
153
|
+
|
|
154
|
+
Create a signer with an explicit private key.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const signer = TronClientSigner.withPrivateKey(tronWeb, '0x...', 'nile');
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
##### `fromTronWeb(tronWeb: TronWeb, network?: TronNetwork): TronClientSigner`
|
|
161
|
+
|
|
162
|
+
Create a signer from a TronWeb instance (uses default private key).
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
const signer = TronClientSigner.fromTronWeb(tronWeb, 'mainnet');
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Instance Methods
|
|
169
|
+
|
|
170
|
+
##### `getAddress(): string`
|
|
171
|
+
|
|
172
|
+
Get the signer's TRON address (Base58 format).
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
const address = signer.getAddress(); // "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
##### `checkAllowance(token: string, amount: bigint, network: string): Promise<bigint>`
|
|
179
|
+
|
|
180
|
+
Check current token allowance for the payment permit contract.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const allowance = await signer.checkAllowance(tokenAddress, BigInt(1000000), 'tron:nile');
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
##### `ensureAllowance(token: string, amount: bigint, network: string, mode?: 'auto' | 'interactive' | 'skip'): Promise<boolean>`
|
|
187
|
+
|
|
188
|
+
Ensure sufficient token allowance, approving if necessary.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
await signer.ensureAllowance(tokenAddress, BigInt(1000000), 'tron:nile', 'auto');
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### ExactTronClientMechanism
|
|
195
|
+
|
|
196
|
+
Payment mechanism implementing the "exact" scheme for TRON.
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
const mechanism = new ExactTronClientMechanism(signer);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Usage Examples
|
|
203
|
+
|
|
204
|
+
### Basic Usage with Automatic Payment
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import TronWeb from 'tronweb';
|
|
208
|
+
import { X402Client, X402FetchClient, ExactTronClientMechanism, TronClientSigner } from '@bankofai/x402';
|
|
209
|
+
|
|
210
|
+
const tronWeb = new TronWeb({
|
|
211
|
+
fullHost: 'https://nile.trongrid.io',
|
|
212
|
+
privateKey: process.env.TRON_PRIVATE_KEY,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const signer = TronClientSigner.withPrivateKey(tronWeb, process.env.TRON_PRIVATE_KEY, 'nile');
|
|
216
|
+
const x402Client = new X402Client().register('tron:*', new ExactTronClientMechanism(signer));
|
|
217
|
+
const client = new X402FetchClient(x402Client);
|
|
218
|
+
|
|
219
|
+
// Automatic payment handling
|
|
220
|
+
const response = await client.get('https://api.example.com/weather');
|
|
221
|
+
const weather = await response.json();
|
|
222
|
+
console.log(weather);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Manual Payment Handling
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import TronWeb from 'tronweb';
|
|
229
|
+
import { X402Client, ExactTronClientMechanism, TronClientSigner, encodePaymentPayload } from '@bankofai/x402';
|
|
230
|
+
|
|
231
|
+
const tronWeb = new TronWeb({ fullHost: 'https://nile.trongrid.io', privateKey: process.env.TRON_PRIVATE_KEY });
|
|
232
|
+
const signer = TronClientSigner.withPrivateKey(tronWeb, process.env.TRON_PRIVATE_KEY, 'nile');
|
|
233
|
+
const x402Client = new X402Client().register('tron:*', new ExactTronClientMechanism(signer));
|
|
234
|
+
|
|
235
|
+
// First request
|
|
236
|
+
const response = await fetch('https://api.example.com/data');
|
|
237
|
+
|
|
238
|
+
if (response.status === 402) {
|
|
239
|
+
// Parse payment requirements
|
|
240
|
+
const paymentRequired = await response.json();
|
|
241
|
+
|
|
242
|
+
// Create payment payload
|
|
243
|
+
const payload = await x402Client.createPaymentPayload(
|
|
244
|
+
paymentRequired.accepts[0],
|
|
245
|
+
'/data',
|
|
246
|
+
paymentRequired.extensions
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// Retry with payment
|
|
250
|
+
const paidResponse = await fetch('https://api.example.com/data', {
|
|
251
|
+
headers: {
|
|
252
|
+
'PAYMENT-SIGNATURE': encodePaymentPayload(payload),
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const data = await paidResponse.json();
|
|
257
|
+
console.log(data);
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Custom Payment Selection
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { X402Client, X402FetchClient, ExactTronClientMechanism, TronClientSigner } from '@bankofai/x402';
|
|
265
|
+
|
|
266
|
+
// Custom selector function
|
|
267
|
+
const selector = (requirements) => {
|
|
268
|
+
// Choose the cheapest option
|
|
269
|
+
return requirements.sort((a, b) => BigInt(a.amount) - BigInt(b.amount))[0];
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const client = new X402FetchClient(x402Client, selector);
|
|
273
|
+
const response = await client.get('https://api.example.com/data');
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Browser Integration
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
// In browser with TronLink wallet
|
|
280
|
+
import { X402Client, X402FetchClient, ExactTronClientMechanism, TronClientSigner } from '@bankofai/x402';
|
|
281
|
+
|
|
282
|
+
// Wait for TronLink
|
|
283
|
+
const tronWeb = window.tronWeb;
|
|
284
|
+
if (!tronWeb) {
|
|
285
|
+
throw new Error('TronLink not found');
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const signer = TronClientSigner.fromTronWeb(tronWeb, 'mainnet');
|
|
289
|
+
const x402Client = new X402Client().register('tron:*', new ExactTronClientMechanism(signer));
|
|
290
|
+
const client = new X402FetchClient(x402Client);
|
|
291
|
+
|
|
292
|
+
// Make paid requests
|
|
293
|
+
const response = await client.get('https://api.example.com/premium-content');
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Multiple Networks
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
// Support multiple TRON networks
|
|
300
|
+
const nileClient = new X402Client()
|
|
301
|
+
.register('tron:nile', new ExactTronClientMechanism(nileSigner))
|
|
302
|
+
.register('tron:shasta', new ExactTronClientMechanism(shastaSigner))
|
|
303
|
+
.register('tron:mainnet', new ExactTronClientMechanism(mainnetSigner));
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Supported Networks
|
|
307
|
+
|
|
308
|
+
- **TRON Mainnet** - `tron:mainnet`
|
|
309
|
+
- **TRON Shasta Testnet** - `tron:shasta`
|
|
310
|
+
- **TRON Nile Testnet** - `tron:nile`
|
|
311
|
+
|
|
312
|
+
## Payment Schemes
|
|
313
|
+
|
|
314
|
+
### Exact Scheme
|
|
315
|
+
|
|
316
|
+
The `exact` scheme allows payments for a specified exact amount. Useful for:
|
|
317
|
+
|
|
318
|
+
- Pay-per-use APIs (e.g., LLM token generation)
|
|
319
|
+
- Fixed-price resources
|
|
320
|
+
- Predictable pricing for API calls
|
|
321
|
+
|
|
322
|
+
The server charges the exact amount specified in the payment permit.
|
|
323
|
+
|
|
324
|
+
## Security
|
|
325
|
+
|
|
326
|
+
- **Never commit private keys** - Use environment variables
|
|
327
|
+
- **TIP-712 signatures** - Cryptographically secure payment permits
|
|
328
|
+
- **Token allowances** - Explicit approval required before payments
|
|
329
|
+
- **Trust-minimizing** - Facilitator cannot move funds outside client authorization
|
|
330
|
+
|
|
331
|
+
## Troubleshooting
|
|
332
|
+
|
|
333
|
+
### "TronWeb does not support signTypedData"
|
|
334
|
+
|
|
335
|
+
Upgrade to TronWeb >= 5.0:
|
|
336
|
+
|
|
337
|
+
```bash
|
|
338
|
+
npm i tronweb@latest
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### "No mechanism registered for network"
|
|
342
|
+
|
|
343
|
+
Ensure you've registered a mechanism for the network:
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
x402Client.register('tron:nile', new ExactTronClientMechanism(signer));
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### "Insufficient allowance"
|
|
350
|
+
|
|
351
|
+
The SDK automatically handles token approvals in `auto` mode. If you're using `skip` mode, manually approve tokens:
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
await signer.ensureAllowance(tokenAddress, amount, network, 'auto');
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## Links
|
|
358
|
+
|
|
359
|
+
- **Repository**: https://github.com/bankofai/x402
|
|
360
|
+
- **Issues**: https://github.com/bankofai/x402/issues
|
|
361
|
+
- **Contributing**: https://github.com/bankofai/CONTRIBUTING.md
|
|
362
|
+
- **Documentation**: https://github.com/bankofai/x402#readme
|
|
363
|
+
- **TRON Documentation**: https://developers.tron.network/
|
|
364
|
+
- **TIP-712 Specification**: https://github.com/tronprotocol/tips/blob/master/tip-712.md
|
|
365
|
+
|
|
366
|
+
## License
|
|
367
|
+
|
|
368
|
+
MIT
|
package/dist/abi.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ABI and EIP-712 type definitions for x402 protocol
|
|
3
|
+
* Shared across all mechanisms
|
|
4
|
+
*/
|
|
5
|
+
import type { DeliveryKind } from './types/payment.js';
|
|
6
|
+
/** EIP-712 Primary Type for PaymentPermit */
|
|
7
|
+
export declare const PAYMENT_PERMIT_PRIMARY_TYPE = "PaymentPermitDetails";
|
|
8
|
+
/**
|
|
9
|
+
* EIP-712 Domain Type
|
|
10
|
+
* Based on contract: keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)")
|
|
11
|
+
* Note: NO version field!
|
|
12
|
+
*/
|
|
13
|
+
export declare const EIP712_DOMAIN_TYPE: readonly [{
|
|
14
|
+
readonly name: "name";
|
|
15
|
+
readonly type: "string";
|
|
16
|
+
}, {
|
|
17
|
+
readonly name: "chainId";
|
|
18
|
+
readonly type: "uint256";
|
|
19
|
+
}, {
|
|
20
|
+
readonly name: "verifyingContract";
|
|
21
|
+
readonly type: "address";
|
|
22
|
+
}];
|
|
23
|
+
/**
|
|
24
|
+
* EIP-712 type definitions for PaymentPermit
|
|
25
|
+
* Based on PermitHash.sol from the contract
|
|
26
|
+
*/
|
|
27
|
+
export declare const PAYMENT_PERMIT_TYPES: {
|
|
28
|
+
readonly PermitMeta: readonly [{
|
|
29
|
+
readonly name: "kind";
|
|
30
|
+
readonly type: "uint8";
|
|
31
|
+
}, {
|
|
32
|
+
readonly name: "paymentId";
|
|
33
|
+
readonly type: "bytes16";
|
|
34
|
+
}, {
|
|
35
|
+
readonly name: "nonce";
|
|
36
|
+
readonly type: "uint256";
|
|
37
|
+
}, {
|
|
38
|
+
readonly name: "validAfter";
|
|
39
|
+
readonly type: "uint256";
|
|
40
|
+
}, {
|
|
41
|
+
readonly name: "validBefore";
|
|
42
|
+
readonly type: "uint256";
|
|
43
|
+
}];
|
|
44
|
+
readonly Payment: readonly [{
|
|
45
|
+
readonly name: "payToken";
|
|
46
|
+
readonly type: "address";
|
|
47
|
+
}, {
|
|
48
|
+
readonly name: "payAmount";
|
|
49
|
+
readonly type: "uint256";
|
|
50
|
+
}, {
|
|
51
|
+
readonly name: "payTo";
|
|
52
|
+
readonly type: "address";
|
|
53
|
+
}];
|
|
54
|
+
readonly Fee: readonly [{
|
|
55
|
+
readonly name: "feeTo";
|
|
56
|
+
readonly type: "address";
|
|
57
|
+
}, {
|
|
58
|
+
readonly name: "feeAmount";
|
|
59
|
+
readonly type: "uint256";
|
|
60
|
+
}];
|
|
61
|
+
readonly PaymentPermitDetails: readonly [{
|
|
62
|
+
readonly name: "meta";
|
|
63
|
+
readonly type: "PermitMeta";
|
|
64
|
+
}, {
|
|
65
|
+
readonly name: "buyer";
|
|
66
|
+
readonly type: "address";
|
|
67
|
+
}, {
|
|
68
|
+
readonly name: "caller";
|
|
69
|
+
readonly type: "address";
|
|
70
|
+
}, {
|
|
71
|
+
readonly name: "payment";
|
|
72
|
+
readonly type: "Payment";
|
|
73
|
+
}, {
|
|
74
|
+
readonly name: "fee";
|
|
75
|
+
readonly type: "Fee";
|
|
76
|
+
}];
|
|
77
|
+
};
|
|
78
|
+
/** Kind mapping for EIP-712 (string to numeric) */
|
|
79
|
+
export declare const KIND_MAP: Record<DeliveryKind, number>;
|
|
80
|
+
/** ERC20 ABI for allowance/approve calls */
|
|
81
|
+
export declare const ERC20_ABI: readonly [{
|
|
82
|
+
readonly name: "allowance";
|
|
83
|
+
readonly type: "function";
|
|
84
|
+
readonly stateMutability: "view";
|
|
85
|
+
readonly inputs: readonly [{
|
|
86
|
+
readonly name: "owner";
|
|
87
|
+
readonly type: "address";
|
|
88
|
+
}, {
|
|
89
|
+
readonly name: "spender";
|
|
90
|
+
readonly type: "address";
|
|
91
|
+
}];
|
|
92
|
+
readonly outputs: readonly [{
|
|
93
|
+
readonly name: "";
|
|
94
|
+
readonly type: "uint256";
|
|
95
|
+
}];
|
|
96
|
+
}, {
|
|
97
|
+
readonly name: "approve";
|
|
98
|
+
readonly type: "function";
|
|
99
|
+
readonly stateMutability: "nonpayable";
|
|
100
|
+
readonly inputs: readonly [{
|
|
101
|
+
readonly name: "spender";
|
|
102
|
+
readonly type: "address";
|
|
103
|
+
}, {
|
|
104
|
+
readonly name: "amount";
|
|
105
|
+
readonly type: "uint256";
|
|
106
|
+
}];
|
|
107
|
+
readonly outputs: readonly [{
|
|
108
|
+
readonly name: "";
|
|
109
|
+
readonly type: "bool";
|
|
110
|
+
}];
|
|
111
|
+
}];
|
|
112
|
+
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../src/abi.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,6CAA6C;AAC7C,eAAO,MAAM,2BAA2B,yBAAyB,CAAC;AAElE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB;;;;;;;;;EAIrB,CAAC;AAEX;;;GAGG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwBvB,CAAC;AAEX,mDAAmD;AACnD,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAEjD,CAAC;AAEF,4CAA4C;AAC5C,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqBZ,CAAC"}
|
package/dist/abi.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ABI and EIP-712 type definitions for x402 protocol
|
|
3
|
+
* Shared across all mechanisms
|
|
4
|
+
*/
|
|
5
|
+
/** EIP-712 Primary Type for PaymentPermit */
|
|
6
|
+
export const PAYMENT_PERMIT_PRIMARY_TYPE = 'PaymentPermitDetails';
|
|
7
|
+
/**
|
|
8
|
+
* EIP-712 Domain Type
|
|
9
|
+
* Based on contract: keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)")
|
|
10
|
+
* Note: NO version field!
|
|
11
|
+
*/
|
|
12
|
+
export const EIP712_DOMAIN_TYPE = [
|
|
13
|
+
{ name: 'name', type: 'string' },
|
|
14
|
+
{ name: 'chainId', type: 'uint256' },
|
|
15
|
+
{ name: 'verifyingContract', type: 'address' },
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* EIP-712 type definitions for PaymentPermit
|
|
19
|
+
* Based on PermitHash.sol from the contract
|
|
20
|
+
*/
|
|
21
|
+
export const PAYMENT_PERMIT_TYPES = {
|
|
22
|
+
PermitMeta: [
|
|
23
|
+
{ name: 'kind', type: 'uint8' },
|
|
24
|
+
{ name: 'paymentId', type: 'bytes16' },
|
|
25
|
+
{ name: 'nonce', type: 'uint256' },
|
|
26
|
+
{ name: 'validAfter', type: 'uint256' },
|
|
27
|
+
{ name: 'validBefore', type: 'uint256' },
|
|
28
|
+
],
|
|
29
|
+
Payment: [
|
|
30
|
+
{ name: 'payToken', type: 'address' },
|
|
31
|
+
{ name: 'payAmount', type: 'uint256' },
|
|
32
|
+
{ name: 'payTo', type: 'address' },
|
|
33
|
+
],
|
|
34
|
+
Fee: [
|
|
35
|
+
{ name: 'feeTo', type: 'address' },
|
|
36
|
+
{ name: 'feeAmount', type: 'uint256' },
|
|
37
|
+
],
|
|
38
|
+
PaymentPermitDetails: [
|
|
39
|
+
{ name: 'meta', type: 'PermitMeta' },
|
|
40
|
+
{ name: 'buyer', type: 'address' },
|
|
41
|
+
{ name: 'caller', type: 'address' },
|
|
42
|
+
{ name: 'payment', type: 'Payment' },
|
|
43
|
+
{ name: 'fee', type: 'Fee' },
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
/** Kind mapping for EIP-712 (string to numeric) */
|
|
47
|
+
export const KIND_MAP = {
|
|
48
|
+
PAYMENT_ONLY: 0,
|
|
49
|
+
};
|
|
50
|
+
/** ERC20 ABI for allowance/approve calls */
|
|
51
|
+
export const ERC20_ABI = [
|
|
52
|
+
{
|
|
53
|
+
name: 'allowance',
|
|
54
|
+
type: 'function',
|
|
55
|
+
stateMutability: 'view',
|
|
56
|
+
inputs: [
|
|
57
|
+
{ name: 'owner', type: 'address' },
|
|
58
|
+
{ name: 'spender', type: 'address' },
|
|
59
|
+
],
|
|
60
|
+
outputs: [{ name: '', type: 'uint256' }],
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'approve',
|
|
64
|
+
type: 'function',
|
|
65
|
+
stateMutability: 'nonpayable',
|
|
66
|
+
inputs: [
|
|
67
|
+
{ name: 'spender', type: 'address' },
|
|
68
|
+
{ name: 'amount', type: 'uint256' },
|
|
69
|
+
],
|
|
70
|
+
outputs: [{ name: '', type: 'bool' }],
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
//# sourceMappingURL=abi.js.map
|
package/dist/abi.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.js","sourceRoot":"","sources":["../src/abi.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,6CAA6C;AAC7C,MAAM,CAAC,MAAM,2BAA2B,GAAG,sBAAsB,CAAC;AAElE;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;IAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;IACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;CACtC,CAAC;AAEX;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE;QAC/B,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE;QACvC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;KACzC;IACD,OAAO,EAAE;QACP,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;KACnC;IACD,GAAG,EAAE;QACH,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;KACvC;IACD,oBAAoB,EAAE;QACpB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE;QACpC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE;QACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE;KAC7B;CACO,CAAC;AAEX,mDAAmD;AACnD,MAAM,CAAC,MAAM,QAAQ,GAAiC;IACpD,YAAY,EAAE,CAAC;CAChB,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB;QACE,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,UAAU;QAChB,eAAe,EAAE,MAAM;QACvB,MAAM,EAAE;YACN,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;YAClC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;SACrC;QACD,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;KACzC;IACD;QACE,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,UAAU;QAChB,eAAe,EAAE,YAAY;QAC7B,MAAM,EAAE;YACN,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;YACpC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE;SACpC;QACD,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;KACtC;CACO,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address conversion utilities for x402 protocol
|
|
3
|
+
* Handles conversion between TRON Base58 and EVM hex formats
|
|
4
|
+
*/
|
|
5
|
+
/** Hex address type */
|
|
6
|
+
export type Hex = `0x${string}`;
|
|
7
|
+
/** Zero address in EVM hex format */
|
|
8
|
+
export declare const ZERO_ADDRESS_HEX: Hex;
|
|
9
|
+
/**
|
|
10
|
+
* Address converter interface
|
|
11
|
+
*/
|
|
12
|
+
export interface AddressConverter {
|
|
13
|
+
/** Normalize address to canonical format */
|
|
14
|
+
normalize(address: string): string;
|
|
15
|
+
/** Convert to EVM hex format (0x...) */
|
|
16
|
+
toEvmFormat(address: string): Hex;
|
|
17
|
+
/** Get zero address for this network type */
|
|
18
|
+
getZeroAddress(): string;
|
|
19
|
+
/** Convert all addresses in a message object */
|
|
20
|
+
convertMessageAddresses(message: Record<string, unknown>): Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* EVM address converter (passthrough)
|
|
24
|
+
*/
|
|
25
|
+
export declare class EvmAddressConverter implements AddressConverter {
|
|
26
|
+
normalize(address: string): string;
|
|
27
|
+
toEvmFormat(address: string): Hex;
|
|
28
|
+
getZeroAddress(): string;
|
|
29
|
+
convertMessageAddresses(message: Record<string, unknown>): Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* TRON address converter
|
|
33
|
+
*/
|
|
34
|
+
export declare class TronAddressConverter implements AddressConverter {
|
|
35
|
+
normalize(address: string): string;
|
|
36
|
+
toEvmFormat(address: string): Hex;
|
|
37
|
+
getZeroAddress(): string;
|
|
38
|
+
convertMessageAddresses(message: Record<string, unknown>): Record<string, unknown>;
|
|
39
|
+
private isAddress;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Convert TRON address to EVM hex format (0x...)
|
|
43
|
+
* Handles: Base58 (T...), hex with 41 prefix, and 0x format
|
|
44
|
+
*/
|
|
45
|
+
export declare function toEvmHex(addr: string): Hex;
|
|
46
|
+
/**
|
|
47
|
+
* Convert EVM hex address to TRON Base58 format
|
|
48
|
+
*/
|
|
49
|
+
export declare function toBase58(addr: string): string;
|
|
50
|
+
//# sourceMappingURL=address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,uBAAuB;AACvB,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAKhC,qCAAqC;AACrC,eAAO,MAAM,gBAAgB,EAAE,GAAkD,CAAC;AAElF;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,4CAA4C;IAC5C,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACnC,wCAAwC;IACxC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IAClC,6CAA6C;IAC7C,cAAc,IAAI,MAAM,CAAC;IACzB,gDAAgD;IAChD,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpF;AAED;;GAEG;AACH,qBAAa,mBAAoB,YAAW,gBAAgB;IAC1D,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAIlC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;IAOjC,cAAc,IAAI,MAAM;IAIxB,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAInF;AAED;;GAEG;AACH,qBAAa,oBAAqB,YAAW,gBAAgB;IAC3D,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAKlC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;IAIjC,cAAc,IAAI,MAAM;IAIxB,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAgBlF,OAAO,CAAC,SAAS;CASlB;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAoC1C;AAgCD;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAoB7C"}
|