@dcentralab/d402-client 0.1.0 โ 0.1.1
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 +29 -60
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -13
- package/LICENSE +0 -22
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
|
|
|
@@ -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)
|
|
@@ -160,7 +131,7 @@ 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,
|
|
@@ -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')
|
|
@@ -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() {
|
|
@@ -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!)
|
|
@@ -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
|
@@ -238,7 +238,7 @@ 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...')
|
|
@@ -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
|
@@ -238,7 +238,7 @@ 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...')
|
|
@@ -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}`);
|
|
@@ -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);
|