@bbuilders/djeon402-sdk-client 1.0.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 +355 -0
- package/dist/index.cjs +1025 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +261 -0
- package/dist/index.d.ts +261 -0
- package/dist/index.js +1011 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +1230 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +339 -0
- package/dist/react/index.d.ts +339 -0
- package/dist/react/index.js +1211 -0
- package/dist/react/index.js.map +1 -0
- package/dist/vue/index.cjs +1333 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.cts +346 -0
- package/dist/vue/index.d.ts +346 -0
- package/dist/vue/index.js +1314 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +105 -0
package/README.md
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# @bbuilders/djeon402/sdk-client
|
|
2
|
+
|
|
3
|
+
Browser SDK for DJEON402 token with React hooks and Vue composables support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **React Hooks** - Ready-to-use hooks with wagmi integration
|
|
8
|
+
- ✅ **Vue Composables** - Vue 3 Composition API support
|
|
9
|
+
- ✅ **TypeScript** - Full type safety
|
|
10
|
+
- ✅ **Wallet Integration** - MetaMask, WalletConnect, etc.
|
|
11
|
+
- ✅ **No Private Keys** - Secure wallet-based signing
|
|
12
|
+
- ✅ **x402 Gasless** - Sign gasless transfer authorizations
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# npm
|
|
18
|
+
npm install @bbuilders/djeon402/sdk-client viem wagmi
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @bbuilders/djeon402/sdk-client viem wagmi
|
|
22
|
+
|
|
23
|
+
# yarn
|
|
24
|
+
yarn add @bbuilders/djeon402/sdk-client viem wagmi
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### React + wagmi
|
|
30
|
+
|
|
31
|
+
#### 1. Setup Provider
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { Djeon402Provider } from '@bbuilders/djeon402/sdk-client/react';
|
|
35
|
+
import { WagmiProvider, createConfig, http } from 'wagmi';
|
|
36
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
37
|
+
|
|
38
|
+
// Configuration constants
|
|
39
|
+
const DJEON402_CONTRACT = '0x...';
|
|
40
|
+
const KYC_REGISTRY = '0x....';
|
|
41
|
+
const CHAIN_ID = 31337;
|
|
42
|
+
|
|
43
|
+
const config = createConfig({
|
|
44
|
+
chains: [/* your chains */],
|
|
45
|
+
transports: {
|
|
46
|
+
/* your transports */
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const queryClient = new QueryClient();
|
|
51
|
+
|
|
52
|
+
function App() {
|
|
53
|
+
return (
|
|
54
|
+
<WagmiProvider config={config}>
|
|
55
|
+
<QueryClientProvider client={queryClient}>
|
|
56
|
+
<Djeon402Provider
|
|
57
|
+
config={{
|
|
58
|
+
contractAddress: DJEON402_CONTRACT,
|
|
59
|
+
kycRegistryAddress: KYC_REGISTRY,
|
|
60
|
+
chainId: CHAIN_ID,
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
<YourApp />
|
|
64
|
+
</Djeon402Provider>
|
|
65
|
+
</QueryClientProvider>
|
|
66
|
+
</WagmiProvider>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### 2. Use Hooks
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { useAccount } from 'wagmi';
|
|
75
|
+
import { useBalance, useTransfer } from '@bbuilders/djeon402/sdk-client/react';
|
|
76
|
+
|
|
77
|
+
// Test account addresses
|
|
78
|
+
const RECIPIENT_ADDRESS = '0x...';
|
|
79
|
+
|
|
80
|
+
// Transfer amount constant
|
|
81
|
+
const TRANSFER_AMOUNT = '100';
|
|
82
|
+
|
|
83
|
+
function TokenDashboard() {
|
|
84
|
+
const { address } = useAccount();
|
|
85
|
+
const { data: balance, isLoading } = useBalance(address);
|
|
86
|
+
const { transfer, isLoading: isTransferring } = useTransfer();
|
|
87
|
+
|
|
88
|
+
const handleTransfer = async () => {
|
|
89
|
+
await transfer({
|
|
90
|
+
to: RECIPIENT_ADDRESS,
|
|
91
|
+
amount: TRANSFER_AMOUNT,
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<div>
|
|
97
|
+
<p>Balance: {balance?.balance}</p>
|
|
98
|
+
<button onClick={handleTransfer} disabled={isTransferring}>
|
|
99
|
+
Send {TRANSFER_AMOUNT} Tokens
|
|
100
|
+
</button>
|
|
101
|
+
</div>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Vue 3
|
|
107
|
+
|
|
108
|
+
#### 1. Setup Provider
|
|
109
|
+
|
|
110
|
+
```vue
|
|
111
|
+
<script setup lang="ts">
|
|
112
|
+
import { provideDjeon402 } from '@bbuilders/djeon402/sdk-client/vue';
|
|
113
|
+
|
|
114
|
+
// Configuration constants
|
|
115
|
+
const DJEON402_CONTRACT = '0x....';
|
|
116
|
+
const KYC_REGISTRY = '0x....';
|
|
117
|
+
const RPC_URL = 'http://localhost:8545';
|
|
118
|
+
const CHAIN_ID = 31337;
|
|
119
|
+
|
|
120
|
+
provideDjeon402({
|
|
121
|
+
contractAddress: DJEON402_CONTRACT,
|
|
122
|
+
kycRegistryAddress: KYC_REGISTRY,
|
|
123
|
+
chainId: CHAIN_ID,
|
|
124
|
+
rpcUrl: RPC_URL,
|
|
125
|
+
});
|
|
126
|
+
</script>
|
|
127
|
+
|
|
128
|
+
<template>
|
|
129
|
+
<YourApp />
|
|
130
|
+
</template>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### 2. Use Composables
|
|
134
|
+
|
|
135
|
+
```vue
|
|
136
|
+
<script setup lang="ts">
|
|
137
|
+
import { ref, computed } from 'vue';
|
|
138
|
+
import { useBalance, useTransfer } from '@bbuilders/djeon402/sdk-client/vue';
|
|
139
|
+
import { useWalletClient } from '@wagmi/vue';
|
|
140
|
+
|
|
141
|
+
// Test account addresses
|
|
142
|
+
const RECIPIENT_ADDRESS = '0x.....';
|
|
143
|
+
|
|
144
|
+
// Transfer amount constant
|
|
145
|
+
const TRANSFER_AMOUNT = '100';
|
|
146
|
+
|
|
147
|
+
const { data: walletClient } = useWalletClient();
|
|
148
|
+
const address = computed(() => walletClient.value?.account?.address);
|
|
149
|
+
|
|
150
|
+
const { data: balance, isLoading } = useBalance(address);
|
|
151
|
+
const { transfer, isLoading: isTransferring } = useTransfer(walletClient);
|
|
152
|
+
|
|
153
|
+
const handleTransfer = async () => {
|
|
154
|
+
await transfer(RECIPIENT_ADDRESS, TRANSFER_AMOUNT);
|
|
155
|
+
};
|
|
156
|
+
</script>
|
|
157
|
+
|
|
158
|
+
<template>
|
|
159
|
+
<div>
|
|
160
|
+
<p>Balance: {{ balance?.balance }}</p>
|
|
161
|
+
<button @click="handleTransfer" :disabled="isTransferring">
|
|
162
|
+
Send {{ TRANSFER_AMOUNT }} Tokens
|
|
163
|
+
</button>
|
|
164
|
+
</div>
|
|
165
|
+
</template>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Vanilla JavaScript
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { Djeon402ClientSDK } from '@bbuilders/djeon402/sdk-client';
|
|
172
|
+
import { createWalletClient, custom } from 'viem';
|
|
173
|
+
|
|
174
|
+
// Configuration constants
|
|
175
|
+
const DJEON402_CONTRACT = '0x....';
|
|
176
|
+
const RECIPIENT_ADDRESS = '0x....';
|
|
177
|
+
const RPC_URL = 'http://localhost:8545';
|
|
178
|
+
const CHAIN_ID = 31337;
|
|
179
|
+
const TRANSFER_AMOUNT = '100';
|
|
180
|
+
|
|
181
|
+
// Create SDK instance
|
|
182
|
+
const sdk = new Djeon402ClientSDK({
|
|
183
|
+
contractAddress: DJEON402_CONTRACT,
|
|
184
|
+
chainId: CHAIN_ID,
|
|
185
|
+
rpcUrl: RPC_URL,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Get token info
|
|
189
|
+
const info = await sdk.token.getInfo();
|
|
190
|
+
console.log(info);
|
|
191
|
+
|
|
192
|
+
// Create wallet client from browser wallet (MetaMask)
|
|
193
|
+
const walletClient = createWalletClient({
|
|
194
|
+
chain: sdk.getChain(),
|
|
195
|
+
transport: custom(window.ethereum),
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Transfer tokens
|
|
199
|
+
const result = await sdk.token.transfer({
|
|
200
|
+
walletClient,
|
|
201
|
+
to: RECIPIENT_ADDRESS,
|
|
202
|
+
amount: TRANSFER_AMOUNT,
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## API Reference
|
|
207
|
+
|
|
208
|
+
### React Hooks
|
|
209
|
+
|
|
210
|
+
All hooks require `Djeon402Provider` to be set up in your app.
|
|
211
|
+
|
|
212
|
+
- `useTokenInfo()` - Get token information
|
|
213
|
+
- `useBalance(address)` - Get balance of an address
|
|
214
|
+
- `useTransfer()` - Transfer tokens
|
|
215
|
+
- `useApprove()` - Approve spending
|
|
216
|
+
- `useX402Transfer()` - Sign gasless transfer authorization
|
|
217
|
+
- `useX402Receive()` - Sign gasless receive authorization
|
|
218
|
+
- `useKYCData(address)` - Get KYC data
|
|
219
|
+
- `useIsBlacklisted(address)` - Check blacklist status
|
|
220
|
+
|
|
221
|
+
### Vue Composables
|
|
222
|
+
|
|
223
|
+
Same API as React hooks, compatible with Vue 3 reactivity.
|
|
224
|
+
|
|
225
|
+
- `useTokenInfo()`, `useBalance()`, `useTransfer()`, `useApprove()`
|
|
226
|
+
- `useX402Transfer(walletClient)` - Sign gasless transfer
|
|
227
|
+
- `useX402Receive(walletClient)` - Sign gasless receive
|
|
228
|
+
- `useKYCData()`, `useIsBlacklisted()`
|
|
229
|
+
|
|
230
|
+
### Core SDK Modules
|
|
231
|
+
|
|
232
|
+
- `sdk.token` - ERC-20 operations
|
|
233
|
+
- `sdk.admin` - Admin functions
|
|
234
|
+
- `sdk.kyc` - KYC management
|
|
235
|
+
- `sdk.x402` - Gasless transfers (sign, execute, receive, cancel, fetchWithPayment)
|
|
236
|
+
|
|
237
|
+
## Examples
|
|
238
|
+
|
|
239
|
+
### Gasless Payment (x402)
|
|
240
|
+
|
|
241
|
+
```tsx
|
|
242
|
+
import { useX402Transfer } from '@bbuilders/djeon402/sdk-client/react';
|
|
243
|
+
|
|
244
|
+
// Merchant configuration
|
|
245
|
+
const MERCHANT_ADDRESS = '0xMerchantAddress...';
|
|
246
|
+
const PAYMENT_AMOUNT = '99.99';
|
|
247
|
+
|
|
248
|
+
function CheckoutButton() {
|
|
249
|
+
const { signTransfer } = useX402Transfer();
|
|
250
|
+
|
|
251
|
+
const handlePayment = async () => {
|
|
252
|
+
// User signs authorization (no gas needed!)
|
|
253
|
+
const authorization = await signTransfer({
|
|
254
|
+
to: MERCHANT_ADDRESS,
|
|
255
|
+
amount: PAYMENT_AMOUNT,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// Send to backend for execution
|
|
259
|
+
await fetch('/api/relay-payment', {
|
|
260
|
+
method: 'POST',
|
|
261
|
+
body: JSON.stringify({ authorization }),
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
return <button onClick={handlePayment}>Pay ${PAYMENT_AMOUNT}</button>;
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Receive Authorization (Payee-Initiated)
|
|
270
|
+
|
|
271
|
+
```tsx
|
|
272
|
+
import { useX402Receive } from '@bbuilders/djeon402/sdk-client/react';
|
|
273
|
+
|
|
274
|
+
function ReceiveButton() {
|
|
275
|
+
const { signReceive } = useX402Receive();
|
|
276
|
+
|
|
277
|
+
const handleReceive = async () => {
|
|
278
|
+
// Receiver signs to pull funds from sender
|
|
279
|
+
const authorization = await signReceive({
|
|
280
|
+
from: '0xSender...',
|
|
281
|
+
amount: '50',
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Send to backend for execution
|
|
285
|
+
await fetch('/api/relay/execute-receive', {
|
|
286
|
+
method: 'POST',
|
|
287
|
+
body: JSON.stringify({ authorization }),
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
return <button onClick={handleReceive}>Receive 50 Tokens</button>;
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Auto-Handle 402 Payment Required
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
import { Djeon402ClientSDK } from '@bbuilders/djeon402/sdk-client';
|
|
299
|
+
|
|
300
|
+
const sdk = new Djeon402ClientSDK({ contractAddress: '0x...', chainId: 31337 });
|
|
301
|
+
|
|
302
|
+
// Automatically handles HTTP 402 responses:
|
|
303
|
+
// 1. Detects 402 + PAYMENT-REQUIRED header
|
|
304
|
+
// 2. Signs transfer authorization with wallet
|
|
305
|
+
// 3. Retries with PAYMENT-SIGNATURE header
|
|
306
|
+
const response = await sdk.x402.fetchWithPayment(walletClient, '/api/content/article-1');
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Check KYC Before Transfer
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
import { useAccount } from 'wagmi';
|
|
313
|
+
import { useKYCData, useTransfer } from '@bbuilders/djeon402/sdk-client/react';
|
|
314
|
+
|
|
315
|
+
// Transfer configuration
|
|
316
|
+
const RECIPIENT_ADDRESS = '0xRecipientAddress...';
|
|
317
|
+
const TRANSFER_AMOUNT = '1000';
|
|
318
|
+
|
|
319
|
+
function SecureTransfer() {
|
|
320
|
+
const { address } = useAccount();
|
|
321
|
+
const { data: kyc } = useKYCData(address);
|
|
322
|
+
const { transfer } = useTransfer();
|
|
323
|
+
|
|
324
|
+
const handleTransfer = async () => {
|
|
325
|
+
if (!kyc?.isVerified) {
|
|
326
|
+
alert('Please complete KYC verification first');
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
await transfer({
|
|
331
|
+
to: RECIPIENT_ADDRESS,
|
|
332
|
+
amount: TRANSFER_AMOUNT
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
return (
|
|
337
|
+
<div>
|
|
338
|
+
<p>KYC Status: {kyc?.levelName}</p>
|
|
339
|
+
<button onClick={handleTransfer}>Transfer</button>
|
|
340
|
+
</div>
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## Requirements
|
|
346
|
+
|
|
347
|
+
- React >= 18.0.0 (for React hooks)
|
|
348
|
+
- Vue >= 3.0.0 (for Vue composables)
|
|
349
|
+
- wagmi ^2.0.0
|
|
350
|
+
- viem ^2.0.0
|
|
351
|
+
- @tanstack/react-query ^5.0.0 (for React)
|
|
352
|
+
|
|
353
|
+
## License
|
|
354
|
+
|
|
355
|
+
MIT
|