@dcentralab/d402-client 0.1.0 → 0.1.2
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 +37 -68
- package/dist/index.d.mts +13 -13
- package/dist/index.d.ts +13 -13
- package/dist/index.js +17 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +17 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @dcentralab/d402-client
|
|
2
2
|
|
|
3
3
|
**D402 Payment Protocol Client** for TypeScript/JavaScript
|
|
4
4
|
|
|
@@ -9,11 +9,14 @@ Port of Python IATP package to TypeScript with 1:1 API compatibility.
|
|
|
9
9
|
## 🚀 Installation
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
npm install @
|
|
12
|
+
npm install @dcentralab/d402-client viem wagmi
|
|
13
|
+
|
|
13
14
|
# or
|
|
14
|
-
pnpm add @
|
|
15
|
+
pnpm add @dcentralab/d402-client viem wagmi
|
|
16
|
+
|
|
15
17
|
# or
|
|
16
|
-
yarn add @
|
|
18
|
+
yarn add @dcentralab/d402-client viem wagmi
|
|
19
|
+
|
|
17
20
|
```
|
|
18
21
|
|
|
19
22
|
**Requirements:**
|
|
@@ -24,46 +27,14 @@ yarn add @iatp/d402-client viem wagmi
|
|
|
24
27
|
|
|
25
28
|
## 📖 Quick Start (React/Next.js)
|
|
26
29
|
|
|
27
|
-
###
|
|
28
|
-
|
|
29
|
-
**File:** `next.config.js`
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
module.exports = {
|
|
33
|
-
webpack: (config, { isServer }) => {
|
|
34
|
-
if (!isServer) {
|
|
35
|
-
config.resolve.fallback = {
|
|
36
|
-
...config.resolve.fallback,
|
|
37
|
-
buffer: require.resolve('buffer/'),
|
|
38
|
-
crypto: require.resolve('crypto-browserify'),
|
|
39
|
-
stream: require.resolve('stream-browserify'),
|
|
40
|
-
process: require.resolve('process/browser')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const webpack = require('webpack')
|
|
44
|
-
config.plugins.push(
|
|
45
|
-
new webpack.ProvidePlugin({
|
|
46
|
-
Buffer: ['buffer', 'Buffer'],
|
|
47
|
-
process: 'process/browser'
|
|
48
|
-
})
|
|
49
|
-
)
|
|
50
|
-
}
|
|
51
|
-
return config
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
**Install polyfills:**
|
|
57
|
-
```bash
|
|
58
|
-
pnpm add buffer process crypto-browserify stream-browserify
|
|
59
|
-
```
|
|
30
|
+
### Usage in React Component
|
|
60
31
|
|
|
61
|
-
|
|
32
|
+
**No special configuration needed!** ✨ Uses native browser APIs (TextEncoder, TextDecoder, Web Crypto).
|
|
62
33
|
|
|
63
34
|
```typescript
|
|
64
35
|
'use client'
|
|
65
36
|
|
|
66
|
-
import { D402Client } from '@
|
|
37
|
+
import { D402Client } from '@dcentralab/d402-client'
|
|
67
38
|
import { useWalletClient } from 'wagmi'
|
|
68
39
|
import { useState } from 'react'
|
|
69
40
|
|
|
@@ -84,7 +55,7 @@ export default function AnalyzeButton() {
|
|
|
84
55
|
// Create D402 client with user's wallet
|
|
85
56
|
const client = new D402Client({
|
|
86
57
|
operatorAccount: walletClient.account,
|
|
87
|
-
|
|
58
|
+
iatpWalletAddress: '0xUserIATPWallet...', // User's IATPWallet
|
|
88
59
|
maxValue: 1000000n // 1 USDC max
|
|
89
60
|
})
|
|
90
61
|
|
|
@@ -125,7 +96,7 @@ export default function AnalyzeButton() {
|
|
|
125
96
|
6. API returns data
|
|
126
97
|
7. User sees result
|
|
127
98
|
|
|
128
|
-
**All in browser!** No backend needed.
|
|
99
|
+
**All in browser!** No backend or polyfills needed.
|
|
129
100
|
|
|
130
101
|
## 🏗️ Features
|
|
131
102
|
|
|
@@ -134,7 +105,7 @@ export default function AnalyzeButton() {
|
|
|
134
105
|
Create on-chain wallet with owner/operator separation:
|
|
135
106
|
|
|
136
107
|
```typescript
|
|
137
|
-
import { createIATPWallet } from '@
|
|
108
|
+
import { createIATPWallet } from '@dcentralab/d402-client'
|
|
138
109
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
139
110
|
|
|
140
111
|
// Owner creates wallet (one-time setup)
|
|
@@ -146,12 +117,12 @@ const result = await createIATPWallet({
|
|
|
146
117
|
rpcUrl: 'https://ethereum-sepolia-rpc.publicnode.com'
|
|
147
118
|
})
|
|
148
119
|
|
|
149
|
-
console.log('Wallet:', result.
|
|
120
|
+
console.log('Wallet:', result.iatpWalletAddress)
|
|
150
121
|
console.log('Operator key:', result.operatorPrivateKey) // Store securely!
|
|
151
122
|
```
|
|
152
123
|
|
|
153
124
|
**Returns:**
|
|
154
|
-
- `
|
|
125
|
+
- `iatpWalletAddress` - IATPWallet contract address
|
|
155
126
|
- `operatorAddress` - Operator's address
|
|
156
127
|
- `operatorPrivateKey` - Operator's private key (store securely!)
|
|
157
128
|
- `transactionHash` - Creation transaction
|
|
@@ -160,11 +131,11 @@ console.log('Operator key:', result.operatorPrivateKey) // Store securely!
|
|
|
160
131
|
### 2. Automatic 402 Payment Handling
|
|
161
132
|
|
|
162
133
|
```typescript
|
|
163
|
-
import { D402Client } from '@
|
|
134
|
+
import { D402Client } from '@dcentralab/d402-client'
|
|
164
135
|
|
|
165
136
|
const client = new D402Client({
|
|
166
137
|
operatorAccount,
|
|
167
|
-
|
|
138
|
+
iatpWalletAddress: '0xYourWallet...',
|
|
168
139
|
maxValue: 1000000n,
|
|
169
140
|
networkFilter: 'base-mainnet', // Only Base network
|
|
170
141
|
schemeFilter: 'exact' // Only exact payments
|
|
@@ -185,7 +156,7 @@ import {
|
|
|
185
156
|
parsePaymentRequirement,
|
|
186
157
|
signD402Payment,
|
|
187
158
|
encodePayment
|
|
188
|
-
} from '@
|
|
159
|
+
} from '@dcentralab/d402-client'
|
|
189
160
|
|
|
190
161
|
// 1. Make request
|
|
191
162
|
const response = await fetch('https://api.example.com')
|
|
@@ -198,7 +169,7 @@ if (response.status === 402) {
|
|
|
198
169
|
const signedPayment = await signD402Payment({
|
|
199
170
|
operatorAccount,
|
|
200
171
|
paymentRequirement: requirement,
|
|
201
|
-
|
|
172
|
+
iatpWalletAddress: '0xYourWallet...'
|
|
202
173
|
})
|
|
203
174
|
|
|
204
175
|
// 4. Encode for header
|
|
@@ -228,7 +199,7 @@ new D402Client(config: D402ClientConfig)
|
|
|
228
199
|
| Option | Type | Required | Description |
|
|
229
200
|
|--------|------|----------|-------------|
|
|
230
201
|
| `operatorAccount` | `Account` | ✅ | Viem account for signing payments (EOA) |
|
|
231
|
-
| `
|
|
202
|
+
| `iatpWalletAddress` | `0x${string}` | ❌ | IATPWallet contract address (uses operator address if not provided) |
|
|
232
203
|
| `maxValue` | `bigint` | ❌ | Max payment in base units (wei). Safety limit to prevent overpaying. |
|
|
233
204
|
| `networkFilter` | `string` | ❌ | Only select requirements matching this network |
|
|
234
205
|
| `schemeFilter` | `string` | ❌ | Payment scheme filter (default: "exact") |
|
|
@@ -337,7 +308,7 @@ Get chain ID for network name.
|
|
|
337
308
|
```typescript
|
|
338
309
|
'use client'
|
|
339
310
|
|
|
340
|
-
import { D402Client } from '@
|
|
311
|
+
import { D402Client } from '@dcentralab/d402-client'
|
|
341
312
|
import { useWalletClient } from 'wagmi'
|
|
342
313
|
|
|
343
314
|
export default function PaymentComponent() {
|
|
@@ -349,7 +320,7 @@ export default function PaymentComponent() {
|
|
|
349
320
|
// User's wallet signs payments
|
|
350
321
|
const client = new D402Client({
|
|
351
322
|
operatorAccount: walletClient.account,
|
|
352
|
-
|
|
323
|
+
iatpWalletAddress: '0xUserIATPWallet...',
|
|
353
324
|
maxValue: 1000000n
|
|
354
325
|
})
|
|
355
326
|
|
|
@@ -373,7 +344,6 @@ export default function PaymentComponent() {
|
|
|
373
344
|
|
|
374
345
|
**Cons:**
|
|
375
346
|
- ⚠️ Requires wallet connection
|
|
376
|
-
- ⚠️ Needs Next.js polyfill config
|
|
377
347
|
- ⚠️ User approves each payment
|
|
378
348
|
|
|
379
349
|
### Pattern 2: Backend Proxy (Alternative)
|
|
@@ -382,7 +352,7 @@ export default function PaymentComponent() {
|
|
|
382
352
|
|
|
383
353
|
**Backend (Node.js):**
|
|
384
354
|
```typescript
|
|
385
|
-
import { D402Client } from '@
|
|
355
|
+
import { D402Client } from '@dcentralab/d402-client'
|
|
386
356
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
387
357
|
|
|
388
358
|
const operatorAccount = privateKeyToAccount(process.env.OPERATOR_KEY!)
|
|
@@ -390,7 +360,7 @@ const operatorAccount = privateKeyToAccount(process.env.OPERATOR_KEY!)
|
|
|
390
360
|
app.post('/api/analyze', async (req, res) => {
|
|
391
361
|
const client = new D402Client({
|
|
392
362
|
operatorAccount,
|
|
393
|
-
|
|
363
|
+
iatpWalletAddress: req.user.iatpWallet,
|
|
394
364
|
maxValue: 1000000n
|
|
395
365
|
})
|
|
396
366
|
|
|
@@ -416,7 +386,6 @@ const response = await fetch('/api/analyze', {
|
|
|
416
386
|
|
|
417
387
|
**Pros:**
|
|
418
388
|
- ✅ Simpler frontend (no wallet needed)
|
|
419
|
-
- ✅ No polyfills needed in frontend
|
|
420
389
|
- ✅ Smooth UX (no wallet popups)
|
|
421
390
|
|
|
422
391
|
**Cons:**
|
|
@@ -439,10 +408,10 @@ const response = await fetch('/api/analyze', {
|
|
|
439
408
|
|
|
440
409
|
```typescript
|
|
441
410
|
// Main client
|
|
442
|
-
export { D402Client } from '@
|
|
411
|
+
export { D402Client } from '@dcentralab/d402-client'
|
|
443
412
|
|
|
444
413
|
// Wallet management
|
|
445
|
-
export { createIATPWallet } from '@
|
|
414
|
+
export { createIATPWallet } from '@dcentralab/d402-client'
|
|
446
415
|
|
|
447
416
|
// Payment functions
|
|
448
417
|
export {
|
|
@@ -451,7 +420,7 @@ export {
|
|
|
451
420
|
signD402Payment,
|
|
452
421
|
encodePayment,
|
|
453
422
|
decodePayment
|
|
454
|
-
} from '@
|
|
423
|
+
} from '@dcentralab/d402-client'
|
|
455
424
|
|
|
456
425
|
// Contract access
|
|
457
426
|
export {
|
|
@@ -459,7 +428,7 @@ export {
|
|
|
459
428
|
getContractAddress,
|
|
460
429
|
getContractAbi,
|
|
461
430
|
getContractConfig
|
|
462
|
-
} from '@
|
|
431
|
+
} from '@dcentralab/d402-client'
|
|
463
432
|
|
|
464
433
|
// Utilities
|
|
465
434
|
export {
|
|
@@ -467,7 +436,7 @@ export {
|
|
|
467
436
|
usdToUsdc,
|
|
468
437
|
generateNonce,
|
|
469
438
|
getChainId
|
|
470
|
-
} from '@
|
|
439
|
+
} from '@dcentralab/d402-client'
|
|
471
440
|
|
|
472
441
|
// Error classes
|
|
473
442
|
export {
|
|
@@ -475,7 +444,7 @@ export {
|
|
|
475
444
|
PaymentAmountExceededError,
|
|
476
445
|
UnsupportedSchemeError,
|
|
477
446
|
Invalid402ResponseError
|
|
478
|
-
} from '@
|
|
447
|
+
} from '@dcentralab/d402-client'
|
|
479
448
|
|
|
480
449
|
// Types
|
|
481
450
|
export type {
|
|
@@ -483,7 +452,7 @@ export type {
|
|
|
483
452
|
PaymentRequirement,
|
|
484
453
|
SignedPayment,
|
|
485
454
|
WalletCreationResult
|
|
486
|
-
} from '@
|
|
455
|
+
} from '@dcentralab/d402-client'
|
|
487
456
|
```
|
|
488
457
|
|
|
489
458
|
## 🧪 Testing
|
|
@@ -569,9 +538,9 @@ pnpm typecheck
|
|
|
569
538
|
## 📝 Related Packages
|
|
570
539
|
|
|
571
540
|
Part of the IATP-JS monorepo:
|
|
572
|
-
- `@
|
|
573
|
-
- `@
|
|
574
|
-
- `@
|
|
541
|
+
- `@dcentralab/d402-client` - D402 payment protocol (this package)
|
|
542
|
+
- `@dcentralab/mcp-client` - MCP integration (planned)
|
|
543
|
+
- `@dcentralab/a2a-client` - A2A protocol (planned)
|
|
575
544
|
|
|
576
545
|
## 🔗 Related Projects
|
|
577
546
|
|
|
@@ -585,10 +554,10 @@ MIT
|
|
|
585
554
|
|
|
586
555
|
## 💬 Support
|
|
587
556
|
|
|
588
|
-
- GitHub Issues: [iatp-js/issues](https://github.com/
|
|
589
|
-
- Email: support@
|
|
557
|
+
- GitHub Issues: [iatp-js/issues](https://github.com/DcentraLab/iatp-js/issues)
|
|
558
|
+
- Email: dev.support@d402.net
|
|
590
559
|
|
|
591
560
|
---
|
|
592
561
|
|
|
593
|
-
**Made with ❤️ by the
|
|
562
|
+
**Made with ❤️ by the DcentraLab Team**
|
|
594
563
|
|
package/dist/index.d.mts
CHANGED
|
@@ -189,10 +189,10 @@ interface D402ClientConfig {
|
|
|
189
189
|
/** Operator account with private key for signing payments (EOA) */
|
|
190
190
|
operatorAccount: Account;
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
193
|
-
* If not provided, uses
|
|
192
|
+
* IATPWallet contract address.
|
|
193
|
+
* If not provided, uses operatorAccount.address for testing.
|
|
194
194
|
*/
|
|
195
|
-
|
|
195
|
+
iatpWalletAddress?: `0x${string}`;
|
|
196
196
|
/**
|
|
197
197
|
* Optional safety limit for maximum payment amount per request in base units.
|
|
198
198
|
*
|
|
@@ -238,14 +238,14 @@ interface D402ClientConfig {
|
|
|
238
238
|
*
|
|
239
239
|
* @example
|
|
240
240
|
* ```ts
|
|
241
|
-
* import { D402Client } from '@
|
|
241
|
+
* import { D402Client } from '@dcentralab/d402-client'
|
|
242
242
|
* import { privateKeyToAccount } from 'viem/accounts'
|
|
243
243
|
*
|
|
244
244
|
* const operatorAccount = privateKeyToAccount('0x...')
|
|
245
245
|
*
|
|
246
246
|
* const client = new D402Client({
|
|
247
247
|
* operatorAccount,
|
|
248
|
-
|
|
248
|
+
* iatpWalletAddress: '0xUserIATPWallet...',
|
|
249
249
|
* maxValue: 1000000n // 1 USDC max
|
|
250
250
|
* })
|
|
251
251
|
*
|
|
@@ -259,7 +259,7 @@ interface D402ClientConfig {
|
|
|
259
259
|
*/
|
|
260
260
|
declare class D402Client {
|
|
261
261
|
private readonly operatorAccount;
|
|
262
|
-
private readonly
|
|
262
|
+
private readonly iatpWalletAddress;
|
|
263
263
|
private readonly maxValue?;
|
|
264
264
|
private readonly paymentRequirementsSelector;
|
|
265
265
|
private readonly networkFilter?;
|
|
@@ -282,11 +282,11 @@ declare class D402Client {
|
|
|
282
282
|
*/
|
|
283
283
|
selectPaymentRequirement(requirements: PaymentRequirement[]): PaymentRequirement;
|
|
284
284
|
/**
|
|
285
|
-
* Get the wallet address used for payments.
|
|
285
|
+
* Get the IATP wallet address used for payments.
|
|
286
286
|
*
|
|
287
287
|
* @returns IATPWallet contract address or operator EOA address
|
|
288
288
|
*/
|
|
289
|
-
|
|
289
|
+
getIATPWalletAddress(): `0x${string}`;
|
|
290
290
|
/**
|
|
291
291
|
* Get the operator account used for signing.
|
|
292
292
|
*
|
|
@@ -318,7 +318,7 @@ declare class D402Client {
|
|
|
318
318
|
*
|
|
319
319
|
* @example
|
|
320
320
|
* ```ts
|
|
321
|
-
* const client = new D402Client({ operatorAccount,
|
|
321
|
+
* const client = new D402Client({ operatorAccount, iatpWalletAddress })
|
|
322
322
|
*
|
|
323
323
|
* const response = await client.fetch('http://api.example.com/analyze', {
|
|
324
324
|
* method: 'POST',
|
|
@@ -349,7 +349,7 @@ declare class D402Client {
|
|
|
349
349
|
* @param params - Signing parameters
|
|
350
350
|
* @param params.operatorAccount - Operator account with private key for signing (EOA)
|
|
351
351
|
* @param params.paymentRequirement - Payment requirements from 402 response
|
|
352
|
-
* @param params.
|
|
352
|
+
* @param params.iatpWalletAddress - IATPWallet contract address (optional, uses operator address if not provided)
|
|
353
353
|
* @param params.requestPath - API request path (optional, uses payment_requirements.resource if not provided)
|
|
354
354
|
* @returns Signed payment ready to encode and send
|
|
355
355
|
*
|
|
@@ -359,7 +359,7 @@ declare class D402Client {
|
|
|
359
359
|
* const signedPayment = await signD402Payment({
|
|
360
360
|
* operatorAccount: account,
|
|
361
361
|
* paymentRequirement: requirement,
|
|
362
|
-
*
|
|
362
|
+
* iatpWalletAddress: '0xUserWallet...'
|
|
363
363
|
* })
|
|
364
364
|
* const encoded = encodePayment(signedPayment)
|
|
365
365
|
* ```
|
|
@@ -367,7 +367,7 @@ declare class D402Client {
|
|
|
367
367
|
declare function signD402Payment(params: {
|
|
368
368
|
operatorAccount: Account;
|
|
369
369
|
paymentRequirement: PaymentRequirement;
|
|
370
|
-
|
|
370
|
+
iatpWalletAddress?: `0x${string}`;
|
|
371
371
|
requestPath?: string;
|
|
372
372
|
d402Version?: number;
|
|
373
373
|
}): Promise<SignedPayment>;
|
|
@@ -835,7 +835,7 @@ interface WalletCreationResult {
|
|
|
835
835
|
*
|
|
836
836
|
* @example
|
|
837
837
|
* ```ts
|
|
838
|
-
* import { createIATPWallet } from '@
|
|
838
|
+
* import { createIATPWallet } from '@dcentralab/d402-client'
|
|
839
839
|
* import { privateKeyToAccount } from 'viem/accounts'
|
|
840
840
|
*
|
|
841
841
|
* const ownerAccount = privateKeyToAccount('0x...')
|
package/dist/index.d.ts
CHANGED
|
@@ -189,10 +189,10 @@ interface D402ClientConfig {
|
|
|
189
189
|
/** Operator account with private key for signing payments (EOA) */
|
|
190
190
|
operatorAccount: Account;
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
193
|
-
* If not provided, uses
|
|
192
|
+
* IATPWallet contract address.
|
|
193
|
+
* If not provided, uses operatorAccount.address for testing.
|
|
194
194
|
*/
|
|
195
|
-
|
|
195
|
+
iatpWalletAddress?: `0x${string}`;
|
|
196
196
|
/**
|
|
197
197
|
* Optional safety limit for maximum payment amount per request in base units.
|
|
198
198
|
*
|
|
@@ -238,14 +238,14 @@ interface D402ClientConfig {
|
|
|
238
238
|
*
|
|
239
239
|
* @example
|
|
240
240
|
* ```ts
|
|
241
|
-
* import { D402Client } from '@
|
|
241
|
+
* import { D402Client } from '@dcentralab/d402-client'
|
|
242
242
|
* import { privateKeyToAccount } from 'viem/accounts'
|
|
243
243
|
*
|
|
244
244
|
* const operatorAccount = privateKeyToAccount('0x...')
|
|
245
245
|
*
|
|
246
246
|
* const client = new D402Client({
|
|
247
247
|
* operatorAccount,
|
|
248
|
-
|
|
248
|
+
* iatpWalletAddress: '0xUserIATPWallet...',
|
|
249
249
|
* maxValue: 1000000n // 1 USDC max
|
|
250
250
|
* })
|
|
251
251
|
*
|
|
@@ -259,7 +259,7 @@ interface D402ClientConfig {
|
|
|
259
259
|
*/
|
|
260
260
|
declare class D402Client {
|
|
261
261
|
private readonly operatorAccount;
|
|
262
|
-
private readonly
|
|
262
|
+
private readonly iatpWalletAddress;
|
|
263
263
|
private readonly maxValue?;
|
|
264
264
|
private readonly paymentRequirementsSelector;
|
|
265
265
|
private readonly networkFilter?;
|
|
@@ -282,11 +282,11 @@ declare class D402Client {
|
|
|
282
282
|
*/
|
|
283
283
|
selectPaymentRequirement(requirements: PaymentRequirement[]): PaymentRequirement;
|
|
284
284
|
/**
|
|
285
|
-
* Get the wallet address used for payments.
|
|
285
|
+
* Get the IATP wallet address used for payments.
|
|
286
286
|
*
|
|
287
287
|
* @returns IATPWallet contract address or operator EOA address
|
|
288
288
|
*/
|
|
289
|
-
|
|
289
|
+
getIATPWalletAddress(): `0x${string}`;
|
|
290
290
|
/**
|
|
291
291
|
* Get the operator account used for signing.
|
|
292
292
|
*
|
|
@@ -318,7 +318,7 @@ declare class D402Client {
|
|
|
318
318
|
*
|
|
319
319
|
* @example
|
|
320
320
|
* ```ts
|
|
321
|
-
* const client = new D402Client({ operatorAccount,
|
|
321
|
+
* const client = new D402Client({ operatorAccount, iatpWalletAddress })
|
|
322
322
|
*
|
|
323
323
|
* const response = await client.fetch('http://api.example.com/analyze', {
|
|
324
324
|
* method: 'POST',
|
|
@@ -349,7 +349,7 @@ declare class D402Client {
|
|
|
349
349
|
* @param params - Signing parameters
|
|
350
350
|
* @param params.operatorAccount - Operator account with private key for signing (EOA)
|
|
351
351
|
* @param params.paymentRequirement - Payment requirements from 402 response
|
|
352
|
-
* @param params.
|
|
352
|
+
* @param params.iatpWalletAddress - IATPWallet contract address (optional, uses operator address if not provided)
|
|
353
353
|
* @param params.requestPath - API request path (optional, uses payment_requirements.resource if not provided)
|
|
354
354
|
* @returns Signed payment ready to encode and send
|
|
355
355
|
*
|
|
@@ -359,7 +359,7 @@ declare class D402Client {
|
|
|
359
359
|
* const signedPayment = await signD402Payment({
|
|
360
360
|
* operatorAccount: account,
|
|
361
361
|
* paymentRequirement: requirement,
|
|
362
|
-
*
|
|
362
|
+
* iatpWalletAddress: '0xUserWallet...'
|
|
363
363
|
* })
|
|
364
364
|
* const encoded = encodePayment(signedPayment)
|
|
365
365
|
* ```
|
|
@@ -367,7 +367,7 @@ declare class D402Client {
|
|
|
367
367
|
declare function signD402Payment(params: {
|
|
368
368
|
operatorAccount: Account;
|
|
369
369
|
paymentRequirement: PaymentRequirement;
|
|
370
|
-
|
|
370
|
+
iatpWalletAddress?: `0x${string}`;
|
|
371
371
|
requestPath?: string;
|
|
372
372
|
d402Version?: number;
|
|
373
373
|
}): Promise<SignedPayment>;
|
|
@@ -835,7 +835,7 @@ interface WalletCreationResult {
|
|
|
835
835
|
*
|
|
836
836
|
* @example
|
|
837
837
|
* ```ts
|
|
838
|
-
* import { createIATPWallet } from '@
|
|
838
|
+
* import { createIATPWallet } from '@dcentralab/d402-client'
|
|
839
839
|
* import { privateKeyToAccount } from 'viem/accounts'
|
|
840
840
|
*
|
|
841
841
|
* const ownerAccount = privateKeyToAccount('0x...')
|
package/dist/index.js
CHANGED
|
@@ -327,7 +327,9 @@ function normalizeAddress(address) {
|
|
|
327
327
|
}
|
|
328
328
|
function decodePaymentResponse(header) {
|
|
329
329
|
try {
|
|
330
|
-
const
|
|
330
|
+
const binString = atob(header);
|
|
331
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
332
|
+
const decoded = new TextDecoder().decode(bytes);
|
|
331
333
|
return JSON.parse(decoded);
|
|
332
334
|
} catch (error) {
|
|
333
335
|
throw new Error(`Failed to decode payment response: ${error}`);
|
|
@@ -349,11 +351,11 @@ async function signD402Payment(params) {
|
|
|
349
351
|
const {
|
|
350
352
|
operatorAccount,
|
|
351
353
|
paymentRequirement,
|
|
352
|
-
|
|
354
|
+
iatpWalletAddress,
|
|
353
355
|
requestPath,
|
|
354
356
|
d402Version = 1
|
|
355
357
|
} = params;
|
|
356
|
-
const consumerWallet =
|
|
358
|
+
const consumerWallet = iatpWalletAddress || operatorAccount.address;
|
|
357
359
|
let finalRequestPath = requestPath || paymentRequirement.resource || "/mcp";
|
|
358
360
|
if (!finalRequestPath || finalRequestPath.trim() === "") {
|
|
359
361
|
finalRequestPath = "/mcp";
|
|
@@ -427,11 +429,14 @@ __export(encoder_exports, {
|
|
|
427
429
|
safeBase64Encode: () => safeBase64Encode
|
|
428
430
|
});
|
|
429
431
|
function safeBase64Encode(data) {
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
+
const bytes = new TextEncoder().encode(data);
|
|
433
|
+
const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join("");
|
|
434
|
+
return btoa(binString);
|
|
432
435
|
}
|
|
433
436
|
function safeBase64Decode(data) {
|
|
434
|
-
|
|
437
|
+
const binString = atob(data);
|
|
438
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
439
|
+
return new TextDecoder().decode(bytes);
|
|
435
440
|
}
|
|
436
441
|
function encodePayment(payment) {
|
|
437
442
|
const jsonString = JSON.stringify(payment);
|
|
@@ -540,7 +545,7 @@ var D402Client = class {
|
|
|
540
545
|
*/
|
|
541
546
|
constructor(config) {
|
|
542
547
|
this.operatorAccount = config.operatorAccount;
|
|
543
|
-
this.
|
|
548
|
+
this.iatpWalletAddress = config.iatpWalletAddress || config.operatorAccount.address;
|
|
544
549
|
this.maxValue = config.maxValue;
|
|
545
550
|
this.networkFilter = config.networkFilter;
|
|
546
551
|
this.schemeFilter = config.schemeFilter || "exact";
|
|
@@ -562,12 +567,12 @@ var D402Client = class {
|
|
|
562
567
|
});
|
|
563
568
|
}
|
|
564
569
|
/**
|
|
565
|
-
* Get the wallet address used for payments.
|
|
570
|
+
* Get the IATP wallet address used for payments.
|
|
566
571
|
*
|
|
567
572
|
* @returns IATPWallet contract address or operator EOA address
|
|
568
573
|
*/
|
|
569
|
-
|
|
570
|
-
return this.
|
|
574
|
+
getIATPWalletAddress() {
|
|
575
|
+
return this.iatpWalletAddress;
|
|
571
576
|
}
|
|
572
577
|
/**
|
|
573
578
|
* Get the operator account used for signing.
|
|
@@ -604,7 +609,7 @@ var D402Client = class {
|
|
|
604
609
|
*
|
|
605
610
|
* @example
|
|
606
611
|
* ```ts
|
|
607
|
-
* const client = new D402Client({ operatorAccount,
|
|
612
|
+
* const client = new D402Client({ operatorAccount, iatpWalletAddress })
|
|
608
613
|
*
|
|
609
614
|
* const response = await client.fetch('http://api.example.com/analyze', {
|
|
610
615
|
* method: 'POST',
|
|
@@ -629,7 +634,7 @@ var D402Client = class {
|
|
|
629
634
|
const signedPayment = await signD402Payment2({
|
|
630
635
|
operatorAccount: this.operatorAccount,
|
|
631
636
|
paymentRequirement: selectedRequirement,
|
|
632
|
-
|
|
637
|
+
iatpWalletAddress: this.iatpWalletAddress
|
|
633
638
|
});
|
|
634
639
|
const paymentHeader = encodePayment2(signedPayment);
|
|
635
640
|
response = await fetch(url, {
|