@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/dist/index.js ADDED
@@ -0,0 +1,1011 @@
1
+ // src/client.ts
2
+ import { createPublicClient, http } from "viem";
3
+ import { base } from "viem/chains";
4
+
5
+ // src/modules/admin.ts
6
+ import {
7
+ DJEON402_ABI,
8
+ validateAddress
9
+ } from "@bbuilders/djeon402-core";
10
+ var AdminModule = class {
11
+ constructor(sdk) {
12
+ this.sdk = sdk;
13
+ }
14
+ /**
15
+ * Get role hashes
16
+ */
17
+ async getRoles() {
18
+ const [minter, burner, pauser, blacklister, admin] = await Promise.all([
19
+ this.sdk.publicClient.readContract({
20
+ address: this.sdk.contractAddress,
21
+ abi: DJEON402_ABI,
22
+ functionName: "MINTER_ROLE"
23
+ }),
24
+ this.sdk.publicClient.readContract({
25
+ address: this.sdk.contractAddress,
26
+ abi: DJEON402_ABI,
27
+ functionName: "BURNER_ROLE"
28
+ }),
29
+ this.sdk.publicClient.readContract({
30
+ address: this.sdk.contractAddress,
31
+ abi: DJEON402_ABI,
32
+ functionName: "PAUSER_ROLE"
33
+ }),
34
+ this.sdk.publicClient.readContract({
35
+ address: this.sdk.contractAddress,
36
+ abi: DJEON402_ABI,
37
+ functionName: "BLACKLISTER_ROLE"
38
+ }),
39
+ this.sdk.publicClient.readContract({
40
+ address: this.sdk.contractAddress,
41
+ abi: DJEON402_ABI,
42
+ functionName: "DEFAULT_ADMIN_ROLE"
43
+ })
44
+ ]);
45
+ return {
46
+ MINTER_ROLE: minter,
47
+ BURNER_ROLE: burner,
48
+ PAUSER_ROLE: pauser,
49
+ BLACKLISTER_ROLE: blacklister,
50
+ DEFAULT_ADMIN_ROLE: admin
51
+ };
52
+ }
53
+ /**
54
+ * Check if address has role
55
+ */
56
+ async hasRole(role, account) {
57
+ validateAddress(account);
58
+ const result = await this.sdk.publicClient.readContract({
59
+ address: this.sdk.contractAddress,
60
+ abi: DJEON402_ABI,
61
+ functionName: "hasRole",
62
+ args: [role, account]
63
+ });
64
+ return {
65
+ role,
66
+ address: account,
67
+ hasRole: result
68
+ };
69
+ }
70
+ /**
71
+ * Check if address is blacklisted
72
+ */
73
+ async isBlacklisted(account) {
74
+ validateAddress(account);
75
+ const isBlacklisted = await this.sdk.publicClient.readContract({
76
+ address: this.sdk.contractAddress,
77
+ abi: DJEON402_ABI,
78
+ functionName: "isBlacklisted",
79
+ args: [account]
80
+ });
81
+ return {
82
+ isBlacklisted,
83
+ address: account
84
+ };
85
+ }
86
+ /**
87
+ * Blacklist an address
88
+ */
89
+ async blacklist(params) {
90
+ const { walletClient, account } = params;
91
+ if (!walletClient.account) {
92
+ throw new Error("Wallet client must have an account");
93
+ }
94
+ validateAddress(account);
95
+ const hash = await walletClient.writeContract({
96
+ address: this.sdk.contractAddress,
97
+ abi: DJEON402_ABI,
98
+ functionName: "blacklist",
99
+ args: [account],
100
+ account: walletClient.account,
101
+ chain: walletClient.chain
102
+ });
103
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
104
+ return {
105
+ success: receipt.status === "success",
106
+ hash,
107
+ blockNumber: receipt.blockNumber.toString()
108
+ };
109
+ }
110
+ /**
111
+ * Remove from blacklist
112
+ */
113
+ async unBlacklist(params) {
114
+ const { walletClient, account } = params;
115
+ if (!walletClient.account) {
116
+ throw new Error("Wallet client must have an account");
117
+ }
118
+ validateAddress(account);
119
+ const hash = await walletClient.writeContract({
120
+ address: this.sdk.contractAddress,
121
+ abi: DJEON402_ABI,
122
+ functionName: "unBlacklist",
123
+ args: [account],
124
+ account: walletClient.account,
125
+ chain: walletClient.chain
126
+ });
127
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
128
+ return {
129
+ success: receipt.status === "success",
130
+ hash,
131
+ blockNumber: receipt.blockNumber.toString()
132
+ };
133
+ }
134
+ /**
135
+ * Pause contract
136
+ */
137
+ async pause(params) {
138
+ const { walletClient } = params;
139
+ if (!walletClient.account) {
140
+ throw new Error("Wallet client must have an account");
141
+ }
142
+ const hash = await walletClient.writeContract({
143
+ address: this.sdk.contractAddress,
144
+ abi: DJEON402_ABI,
145
+ functionName: "pause",
146
+ account: walletClient.account,
147
+ chain: walletClient.chain
148
+ });
149
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
150
+ return {
151
+ success: receipt.status === "success",
152
+ hash,
153
+ blockNumber: receipt.blockNumber.toString()
154
+ };
155
+ }
156
+ /**
157
+ * Unpause contract
158
+ */
159
+ async unpause(params) {
160
+ const { walletClient } = params;
161
+ if (!walletClient.account) {
162
+ throw new Error("Wallet client must have an account");
163
+ }
164
+ const hash = await walletClient.writeContract({
165
+ address: this.sdk.contractAddress,
166
+ abi: DJEON402_ABI,
167
+ functionName: "unpause",
168
+ account: walletClient.account,
169
+ chain: walletClient.chain
170
+ });
171
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
172
+ return {
173
+ success: receipt.status === "success",
174
+ hash,
175
+ blockNumber: receipt.blockNumber.toString()
176
+ };
177
+ }
178
+ };
179
+
180
+ // src/modules/kyc.ts
181
+ import {
182
+ KYC_REGISTRY_ABI,
183
+ validateAddress as validateAddress2
184
+ } from "@bbuilders/djeon402-core";
185
+ var KYCModule = class {
186
+ constructor(sdk) {
187
+ this.sdk = sdk;
188
+ }
189
+ /**
190
+ * Get KYC data for an address
191
+ */
192
+ async getData(address) {
193
+ if (!this.sdk.kycRegistryAddress) {
194
+ throw new Error("KYC Registry address not configured");
195
+ }
196
+ validateAddress2(address);
197
+ const kycData = await this.sdk.publicClient.readContract({
198
+ address: this.sdk.kycRegistryAddress,
199
+ abi: KYC_REGISTRY_ABI,
200
+ functionName: "getKYCData",
201
+ args: [address]
202
+ });
203
+ const result = kycData;
204
+ const [level, expiryDate, kycHash, isActive, dailyLimit, dailySpent] = result;
205
+ const levelNames = ["None", "Tier1", "Tier2", "Tier3"];
206
+ const expiryDateReadable = new Date(Number(expiryDate) * 1e3).toISOString();
207
+ return {
208
+ level,
209
+ levelName: levelNames[level],
210
+ expiryDate: Number(expiryDate),
211
+ expiryDateReadable,
212
+ kycHash,
213
+ isActive,
214
+ dailyLimit: dailyLimit.toString(),
215
+ dailyLimitRaw: dailyLimit.toString(),
216
+ dailySpent: dailySpent.toString(),
217
+ dailySpentRaw: dailySpent.toString(),
218
+ userAddress: address
219
+ };
220
+ }
221
+ /**
222
+ * Get remaining daily limit
223
+ */
224
+ async getRemainingLimit(address) {
225
+ if (!this.sdk.kycRegistryAddress) {
226
+ throw new Error("KYC Registry address not configured");
227
+ }
228
+ validateAddress2(address);
229
+ const remaining = await this.sdk.publicClient.readContract({
230
+ address: this.sdk.kycRegistryAddress,
231
+ abi: KYC_REGISTRY_ABI,
232
+ functionName: "getRemainingDailyLimit",
233
+ args: [address]
234
+ });
235
+ return {
236
+ address,
237
+ remainingLimit: remaining.toString()
238
+ };
239
+ }
240
+ /**
241
+ * Verify KYC (admin only)
242
+ */
243
+ async verify(params) {
244
+ if (!this.sdk.kycRegistryAddress) {
245
+ throw new Error("KYC Registry address not configured");
246
+ }
247
+ const { walletClient, userAddress, level, expiryDate, documentHash } = params;
248
+ if (!walletClient.account) {
249
+ throw new Error("Wallet client must have an account");
250
+ }
251
+ validateAddress2(userAddress);
252
+ if (level < 0 || level > 3) {
253
+ throw new Error("Invalid KYC level. Must be 0-3");
254
+ }
255
+ const hash = await walletClient.writeContract({
256
+ address: this.sdk.kycRegistryAddress,
257
+ abi: KYC_REGISTRY_ABI,
258
+ functionName: "verifyKYC",
259
+ args: [userAddress, level, BigInt(expiryDate), documentHash],
260
+ account: walletClient.account,
261
+ chain: walletClient.chain
262
+ });
263
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
264
+ return {
265
+ success: receipt.status === "success",
266
+ hash,
267
+ blockNumber: receipt.blockNumber.toString()
268
+ };
269
+ }
270
+ /**
271
+ * Update KYC level (admin only)
272
+ */
273
+ async updateKYC(params) {
274
+ if (!this.sdk.kycRegistryAddress) {
275
+ throw new Error("KYC Registry address not configured");
276
+ }
277
+ const { walletClient, userAddress, level, expiryDate = 0 } = params;
278
+ if (!walletClient.account) {
279
+ throw new Error("Wallet client must have an account");
280
+ }
281
+ validateAddress2(userAddress);
282
+ const hash = await walletClient.writeContract({
283
+ address: this.sdk.kycRegistryAddress,
284
+ abi: KYC_REGISTRY_ABI,
285
+ functionName: "updateKYC",
286
+ args: [userAddress, level, BigInt(expiryDate)],
287
+ account: walletClient.account,
288
+ chain: walletClient.chain
289
+ });
290
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
291
+ return {
292
+ success: receipt.status === "success",
293
+ hash,
294
+ blockNumber: receipt.blockNumber.toString()
295
+ };
296
+ }
297
+ /**
298
+ * Revoke KYC (admin only)
299
+ */
300
+ async revokeKYC(params) {
301
+ if (!this.sdk.kycRegistryAddress) {
302
+ throw new Error("KYC Registry address not configured");
303
+ }
304
+ const { walletClient, userAddress } = params;
305
+ if (!walletClient.account) {
306
+ throw new Error("Wallet client must have an account");
307
+ }
308
+ validateAddress2(userAddress);
309
+ const hash = await walletClient.writeContract({
310
+ address: this.sdk.kycRegistryAddress,
311
+ abi: KYC_REGISTRY_ABI,
312
+ functionName: "revokeKYC",
313
+ args: [userAddress],
314
+ account: walletClient.account,
315
+ chain: walletClient.chain
316
+ });
317
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
318
+ return {
319
+ success: receipt.status === "success",
320
+ hash,
321
+ blockNumber: receipt.blockNumber.toString()
322
+ };
323
+ }
324
+ /**
325
+ * Set daily limit for a user (admin only)
326
+ */
327
+ async setDailyLimit(params) {
328
+ if (!this.sdk.kycRegistryAddress) {
329
+ throw new Error("KYC Registry address not configured");
330
+ }
331
+ const { walletClient, userAddress, limitUSD } = params;
332
+ if (!walletClient.account) {
333
+ throw new Error("Wallet client must have an account");
334
+ }
335
+ validateAddress2(userAddress);
336
+ const limitInSmallestUnit = BigInt(parseFloat(limitUSD) * 1e6);
337
+ const hash = await walletClient.writeContract({
338
+ address: this.sdk.kycRegistryAddress,
339
+ abi: KYC_REGISTRY_ABI,
340
+ functionName: "setDailyLimit",
341
+ args: [userAddress, limitInSmallestUnit],
342
+ account: walletClient.account,
343
+ chain: walletClient.chain
344
+ });
345
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
346
+ return {
347
+ success: receipt.status === "success",
348
+ hash,
349
+ blockNumber: receipt.blockNumber.toString()
350
+ };
351
+ }
352
+ /**
353
+ * Check if a transaction amount is within the user's daily limit
354
+ */
355
+ async checkDailyLimit(params) {
356
+ if (!this.sdk.kycRegistryAddress) {
357
+ throw new Error("KYC Registry address not configured");
358
+ }
359
+ const { walletClient, userAddress, amountUSD } = params;
360
+ if (!walletClient.account) {
361
+ throw new Error("Wallet client must have an account");
362
+ }
363
+ validateAddress2(userAddress);
364
+ const amountInSmallestUnit = BigInt(parseFloat(amountUSD) * 1e6);
365
+ const result = await walletClient.writeContract({
366
+ address: this.sdk.kycRegistryAddress,
367
+ abi: KYC_REGISTRY_ABI,
368
+ functionName: "checkDailyLimit",
369
+ args: [userAddress, amountInSmallestUnit],
370
+ account: walletClient.account,
371
+ chain: walletClient.chain
372
+ });
373
+ return result;
374
+ }
375
+ /**
376
+ * Check if a user's KYC is valid (view)
377
+ */
378
+ async isKYCValid(userAddress) {
379
+ if (!this.sdk.kycRegistryAddress) {
380
+ throw new Error("KYC Registry address not configured");
381
+ }
382
+ validateAddress2(userAddress);
383
+ const result = await this.sdk.publicClient.readContract({
384
+ address: this.sdk.kycRegistryAddress,
385
+ abi: KYC_REGISTRY_ABI,
386
+ functionName: "isKYCValid",
387
+ args: [userAddress]
388
+ });
389
+ return result;
390
+ }
391
+ };
392
+
393
+ // src/modules/token.ts
394
+ import {
395
+ DJEON402_ABI as DJEON402_ABI2,
396
+ formatTokenAmount,
397
+ parseTokenAmount,
398
+ validateAddress as validateAddress3,
399
+ validateAmount
400
+ } from "@bbuilders/djeon402-core";
401
+ var TokenModule = class {
402
+ constructor(sdk) {
403
+ this.sdk = sdk;
404
+ }
405
+ /**
406
+ * Get token information
407
+ */
408
+ async getInfo() {
409
+ const [name, symbol, decimals, totalSupply, paused] = await Promise.all([
410
+ this.sdk.publicClient.readContract({
411
+ address: this.sdk.contractAddress,
412
+ abi: DJEON402_ABI2,
413
+ functionName: "name"
414
+ }),
415
+ this.sdk.publicClient.readContract({
416
+ address: this.sdk.contractAddress,
417
+ abi: DJEON402_ABI2,
418
+ functionName: "symbol"
419
+ }),
420
+ this.sdk.publicClient.readContract({
421
+ address: this.sdk.contractAddress,
422
+ abi: DJEON402_ABI2,
423
+ functionName: "decimals"
424
+ }),
425
+ this.sdk.publicClient.readContract({
426
+ address: this.sdk.contractAddress,
427
+ abi: DJEON402_ABI2,
428
+ functionName: "totalSupply"
429
+ }),
430
+ this.sdk.publicClient.readContract({
431
+ address: this.sdk.contractAddress,
432
+ abi: DJEON402_ABI2,
433
+ functionName: "paused"
434
+ })
435
+ ]);
436
+ return {
437
+ name,
438
+ symbol,
439
+ decimals,
440
+ totalSupply: formatTokenAmount(totalSupply, decimals),
441
+ totalSupplyRaw: totalSupply.toString(),
442
+ paused,
443
+ contractAddress: this.sdk.contractAddress
444
+ };
445
+ }
446
+ /**
447
+ * Get balance of an address
448
+ */
449
+ async getBalance(address) {
450
+ validateAddress3(address);
451
+ const [balance, decimals] = await Promise.all([
452
+ this.sdk.publicClient.readContract({
453
+ address: this.sdk.contractAddress,
454
+ abi: DJEON402_ABI2,
455
+ functionName: "balanceOf",
456
+ args: [address]
457
+ }),
458
+ this.sdk.publicClient.readContract({
459
+ address: this.sdk.contractAddress,
460
+ abi: DJEON402_ABI2,
461
+ functionName: "decimals"
462
+ })
463
+ ]);
464
+ return {
465
+ address,
466
+ balance: formatTokenAmount(balance, decimals),
467
+ balanceRaw: balance.toString()
468
+ };
469
+ }
470
+ /**
471
+ * Get allowance
472
+ */
473
+ async getAllowance(owner, spender) {
474
+ validateAddress3(owner);
475
+ validateAddress3(spender);
476
+ const [allowance, decimals] = await Promise.all([
477
+ this.sdk.publicClient.readContract({
478
+ address: this.sdk.contractAddress,
479
+ abi: DJEON402_ABI2,
480
+ functionName: "allowance",
481
+ args: [owner, spender]
482
+ }),
483
+ this.sdk.publicClient.readContract({
484
+ address: this.sdk.contractAddress,
485
+ abi: DJEON402_ABI2,
486
+ functionName: "decimals"
487
+ })
488
+ ]);
489
+ return {
490
+ owner,
491
+ spender,
492
+ allowance: formatTokenAmount(allowance, decimals),
493
+ allowanceRaw: allowance.toString()
494
+ };
495
+ }
496
+ /**
497
+ * Transfer tokens using wallet client
498
+ */
499
+ async transfer(params) {
500
+ const { walletClient, to, amount } = params;
501
+ if (!walletClient.account) {
502
+ throw new Error("Wallet client must have an account");
503
+ }
504
+ validateAddress3(to);
505
+ validateAmount(amount);
506
+ const decimals = await this.sdk.publicClient.readContract({
507
+ address: this.sdk.contractAddress,
508
+ abi: DJEON402_ABI2,
509
+ functionName: "decimals"
510
+ });
511
+ const amountBigInt = parseTokenAmount(amount, decimals);
512
+ const hash = await walletClient.writeContract({
513
+ address: this.sdk.contractAddress,
514
+ abi: DJEON402_ABI2,
515
+ functionName: "transfer",
516
+ args: [to, amountBigInt],
517
+ account: walletClient.account,
518
+ chain: walletClient.chain
519
+ });
520
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({
521
+ hash
522
+ });
523
+ return {
524
+ success: receipt.status === "success",
525
+ hash,
526
+ from: walletClient.account.address,
527
+ to,
528
+ amount,
529
+ amountRaw: amountBigInt.toString(),
530
+ blockNumber: receipt.blockNumber.toString(),
531
+ status: receipt.status
532
+ };
533
+ }
534
+ /**
535
+ * Approve spending
536
+ */
537
+ async approve(params) {
538
+ const { walletClient, spender, amount } = params;
539
+ if (!walletClient.account) {
540
+ throw new Error("Wallet client must have an account");
541
+ }
542
+ validateAddress3(spender);
543
+ validateAmount(amount);
544
+ const decimals = await this.sdk.publicClient.readContract({
545
+ address: this.sdk.contractAddress,
546
+ abi: DJEON402_ABI2,
547
+ functionName: "decimals"
548
+ });
549
+ const amountBigInt = parseTokenAmount(amount, decimals);
550
+ const hash = await walletClient.writeContract({
551
+ address: this.sdk.contractAddress,
552
+ abi: DJEON402_ABI2,
553
+ functionName: "approve",
554
+ args: [spender, amountBigInt],
555
+ account: walletClient.account,
556
+ chain: walletClient.chain
557
+ });
558
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
559
+ return {
560
+ success: receipt.status === "success",
561
+ hash,
562
+ blockNumber: receipt.blockNumber.toString()
563
+ };
564
+ }
565
+ /**
566
+ * Mint tokens (requires MINTER_ROLE)
567
+ */
568
+ async mint(params) {
569
+ const { walletClient, to, amount } = params;
570
+ if (!walletClient.account) {
571
+ throw new Error("Wallet client must have an account");
572
+ }
573
+ validateAddress3(to);
574
+ validateAmount(amount);
575
+ const decimals = await this.sdk.publicClient.readContract({
576
+ address: this.sdk.contractAddress,
577
+ abi: DJEON402_ABI2,
578
+ functionName: "decimals"
579
+ });
580
+ const amountBigInt = parseTokenAmount(amount, decimals);
581
+ const hash = await walletClient.writeContract({
582
+ address: this.sdk.contractAddress,
583
+ abi: DJEON402_ABI2,
584
+ functionName: "mint",
585
+ args: [to, amountBigInt],
586
+ account: walletClient.account,
587
+ chain: walletClient.chain
588
+ });
589
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
590
+ return {
591
+ success: receipt.status === "success",
592
+ hash,
593
+ blockNumber: receipt.blockNumber.toString()
594
+ };
595
+ }
596
+ /**
597
+ * Burn tokens (requires BURNER_ROLE)
598
+ */
599
+ async burn(params) {
600
+ const { walletClient, amount } = params;
601
+ if (!walletClient.account) {
602
+ throw new Error("Wallet client must have an account");
603
+ }
604
+ validateAmount(amount);
605
+ const decimals = await this.sdk.publicClient.readContract({
606
+ address: this.sdk.contractAddress,
607
+ abi: DJEON402_ABI2,
608
+ functionName: "decimals"
609
+ });
610
+ const amountBigInt = parseTokenAmount(amount, decimals);
611
+ const hash = await walletClient.writeContract({
612
+ address: this.sdk.contractAddress,
613
+ abi: DJEON402_ABI2,
614
+ functionName: "burn",
615
+ args: [amountBigInt],
616
+ account: walletClient.account,
617
+ chain: walletClient.chain
618
+ });
619
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
620
+ return {
621
+ success: receipt.status === "success",
622
+ hash,
623
+ blockNumber: receipt.blockNumber.toString()
624
+ };
625
+ }
626
+ };
627
+
628
+ // src/modules/x402.ts
629
+ import {
630
+ DJEON402_ABI as DJEON402_ABI3,
631
+ parseTokenAmount as parseTokenAmount2,
632
+ validateAddress as validateAddress4,
633
+ validateAmount as validateAmount2
634
+ } from "@bbuilders/djeon402-core";
635
+ var X402Module = class {
636
+ constructor(sdk) {
637
+ this.sdk = sdk;
638
+ }
639
+ /**
640
+ * Generate a random nonce
641
+ */
642
+ generateNonce() {
643
+ const randomBytes = new Uint8Array(32);
644
+ crypto.getRandomValues(randomBytes);
645
+ return `0x${Array.from(randomBytes).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
646
+ }
647
+ /**
648
+ * Get EIP-712 domain separator
649
+ */
650
+ async getDomainSeparator() {
651
+ const domain = await this.sdk.publicClient.readContract({
652
+ address: this.sdk.contractAddress,
653
+ abi: DJEON402_ABI3,
654
+ functionName: "DOMAIN_SEPARATOR"
655
+ });
656
+ return domain;
657
+ }
658
+ /**
659
+ * Get authorization state (whether a nonce has been used)
660
+ */
661
+ async getAuthorizationState(authorizer, nonce) {
662
+ return await this.sdk.publicClient.readContract({
663
+ address: this.sdk.contractAddress,
664
+ abi: DJEON402_ABI3,
665
+ functionName: "authorizationState",
666
+ args: [authorizer, nonce]
667
+ });
668
+ }
669
+ /**
670
+ * Sign transfer authorization using wallet
671
+ */
672
+ async signTransferWithWallet(params) {
673
+ const { walletClient, to, amount, validAfter = 0n, validBefore, nonce } = params;
674
+ if (!walletClient.account) {
675
+ throw new Error("Wallet client must have an account");
676
+ }
677
+ validateAddress4(to);
678
+ validateAmount2(amount);
679
+ const from = walletClient.account.address;
680
+ const decimals = await this.sdk.publicClient.readContract({
681
+ address: this.sdk.contractAddress,
682
+ abi: DJEON402_ABI3,
683
+ functionName: "decimals"
684
+ });
685
+ const value = parseTokenAmount2(amount, decimals);
686
+ const transferNonce = nonce || this.generateNonce();
687
+ const validBeforeTimestamp = validBefore ?? BigInt(Math.floor(Date.now() / 1e3) + 3600);
688
+ const name = await this.sdk.publicClient.readContract({
689
+ address: this.sdk.contractAddress,
690
+ abi: DJEON402_ABI3,
691
+ functionName: "name"
692
+ });
693
+ const domain = {
694
+ name,
695
+ version: "1",
696
+ chainId: this.sdk.chainId,
697
+ verifyingContract: this.sdk.contractAddress
698
+ };
699
+ const types = {
700
+ TransferWithAuthorization: [
701
+ { name: "from", type: "address" },
702
+ { name: "to", type: "address" },
703
+ { name: "value", type: "uint256" },
704
+ { name: "validAfter", type: "uint256" },
705
+ { name: "validBefore", type: "uint256" },
706
+ { name: "nonce", type: "bytes32" }
707
+ ]
708
+ };
709
+ const message = {
710
+ from,
711
+ to,
712
+ value,
713
+ validAfter,
714
+ validBefore: validBeforeTimestamp,
715
+ nonce: transferNonce
716
+ };
717
+ const signature = await walletClient.signTypedData({
718
+ account: walletClient.account,
719
+ domain,
720
+ types,
721
+ primaryType: "TransferWithAuthorization",
722
+ message
723
+ });
724
+ const v = parseInt(signature.slice(130, 132), 16);
725
+ const r = `0x${signature.slice(2, 66)}`;
726
+ const s = `0x${signature.slice(66, 130)}`;
727
+ return {
728
+ from,
729
+ to,
730
+ value,
731
+ validAfter,
732
+ validBefore: validBeforeTimestamp,
733
+ nonce: transferNonce,
734
+ v,
735
+ r,
736
+ s
737
+ };
738
+ }
739
+ /**
740
+ * Sign receive authorization using wallet (receiver signs)
741
+ */
742
+ async signReceiveWithWallet(params) {
743
+ const { walletClient, from, amount, validAfter = 0n, validBefore, nonce } = params;
744
+ if (!walletClient.account) {
745
+ throw new Error("Wallet client must have an account");
746
+ }
747
+ validateAddress4(from);
748
+ validateAmount2(amount);
749
+ const to = walletClient.account.address;
750
+ const decimals = await this.sdk.publicClient.readContract({
751
+ address: this.sdk.contractAddress,
752
+ abi: DJEON402_ABI3,
753
+ functionName: "decimals"
754
+ });
755
+ const value = parseTokenAmount2(amount, decimals);
756
+ const receiveNonce = nonce || this.generateNonce();
757
+ const validBeforeTimestamp = validBefore ?? BigInt(Math.floor(Date.now() / 1e3) + 3600);
758
+ const name = await this.sdk.publicClient.readContract({
759
+ address: this.sdk.contractAddress,
760
+ abi: DJEON402_ABI3,
761
+ functionName: "name"
762
+ });
763
+ const domain = {
764
+ name,
765
+ version: "1",
766
+ chainId: this.sdk.chainId,
767
+ verifyingContract: this.sdk.contractAddress
768
+ };
769
+ const types = {
770
+ ReceiveWithAuthorization: [
771
+ { name: "from", type: "address" },
772
+ { name: "to", type: "address" },
773
+ { name: "value", type: "uint256" },
774
+ { name: "validAfter", type: "uint256" },
775
+ { name: "validBefore", type: "uint256" },
776
+ { name: "nonce", type: "bytes32" }
777
+ ]
778
+ };
779
+ const message = {
780
+ from,
781
+ to,
782
+ value,
783
+ validAfter,
784
+ validBefore: validBeforeTimestamp,
785
+ nonce: receiveNonce
786
+ };
787
+ const signature = await walletClient.signTypedData({
788
+ account: walletClient.account,
789
+ domain,
790
+ types,
791
+ primaryType: "ReceiveWithAuthorization",
792
+ message
793
+ });
794
+ const v = parseInt(signature.slice(130, 132), 16);
795
+ const r = `0x${signature.slice(2, 66)}`;
796
+ const s = `0x${signature.slice(66, 130)}`;
797
+ return {
798
+ from,
799
+ to,
800
+ value,
801
+ validAfter,
802
+ validBefore: validBeforeTimestamp,
803
+ nonce: receiveNonce,
804
+ v,
805
+ r,
806
+ s
807
+ };
808
+ }
809
+ /**
810
+ * Execute receive with authorization (caller must be the receiver)
811
+ */
812
+ async executeReceive(params) {
813
+ const { walletClient, authorization } = params;
814
+ if (!walletClient.account) {
815
+ throw new Error("Wallet client must have an account");
816
+ }
817
+ const { from, to, value, validAfter, validBefore, nonce, v, r, s } = authorization;
818
+ const hash = await walletClient.writeContract({
819
+ address: this.sdk.contractAddress,
820
+ abi: DJEON402_ABI3,
821
+ functionName: "receiveWithAuthorization",
822
+ args: [from, to, value, validAfter, validBefore, nonce, v, r, s],
823
+ account: walletClient.account,
824
+ chain: walletClient.chain
825
+ });
826
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
827
+ return {
828
+ success: receipt.status === "success",
829
+ hash,
830
+ blockNumber: receipt.blockNumber.toString()
831
+ };
832
+ }
833
+ /**
834
+ * Sign cancel authorization using wallet
835
+ */
836
+ async signCancelWithWallet(params) {
837
+ const { walletClient, authorizer, nonce: authNonce } = params;
838
+ if (!walletClient.account) {
839
+ throw new Error("Wallet client must have an account");
840
+ }
841
+ const name = await this.sdk.publicClient.readContract({
842
+ address: this.sdk.contractAddress,
843
+ abi: DJEON402_ABI3,
844
+ functionName: "name"
845
+ });
846
+ const domain = {
847
+ name,
848
+ version: "1",
849
+ chainId: this.sdk.chainId,
850
+ verifyingContract: this.sdk.contractAddress
851
+ };
852
+ const types = {
853
+ CancelAuthorization: [
854
+ { name: "authorizer", type: "address" },
855
+ { name: "nonce", type: "bytes32" }
856
+ ]
857
+ };
858
+ const message = { authorizer, nonce: authNonce };
859
+ const signature = await walletClient.signTypedData({
860
+ account: walletClient.account,
861
+ domain,
862
+ types,
863
+ primaryType: "CancelAuthorization",
864
+ message
865
+ });
866
+ const v = parseInt(signature.slice(130, 132), 16);
867
+ const r = `0x${signature.slice(2, 66)}`;
868
+ const s = `0x${signature.slice(66, 130)}`;
869
+ return { authorizer, nonce: authNonce, v, r, s };
870
+ }
871
+ /**
872
+ * Cancel an authorization (marks nonce as used)
873
+ */
874
+ async cancelAuthorization(params) {
875
+ const { walletClient } = params;
876
+ const { authorizer, nonce, v, r, s } = await this.signCancelWithWallet(params);
877
+ const hash = await walletClient.writeContract({
878
+ address: this.sdk.contractAddress,
879
+ abi: DJEON402_ABI3,
880
+ functionName: "cancelAuthorization",
881
+ args: [authorizer, nonce, v, r, s],
882
+ account: walletClient.account,
883
+ chain: walletClient.chain
884
+ });
885
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
886
+ return {
887
+ success: receipt.status === "success",
888
+ hash,
889
+ blockNumber: receipt.blockNumber.toString()
890
+ };
891
+ }
892
+ /**
893
+ * Execute transfer with authorization (relayer function)
894
+ */
895
+ async executeTransfer(params) {
896
+ const { walletClient, authorization } = params;
897
+ if (!walletClient.account) {
898
+ throw new Error("Wallet client must have an account");
899
+ }
900
+ const { from, to, value, validAfter, validBefore, nonce, v, r, s } = authorization;
901
+ const hash = await walletClient.writeContract({
902
+ address: this.sdk.contractAddress,
903
+ abi: DJEON402_ABI3,
904
+ functionName: "transferWithAuthorization",
905
+ args: [from, to, value, validAfter, validBefore, nonce, v, r, s],
906
+ account: walletClient.account,
907
+ chain: walletClient.chain
908
+ });
909
+ const receipt = await this.sdk.publicClient.waitForTransactionReceipt({ hash });
910
+ return {
911
+ success: receipt.status === "success",
912
+ hash,
913
+ blockNumber: receipt.blockNumber.toString()
914
+ };
915
+ }
916
+ /**
917
+ * Fetch wrapper that auto-handles 402 Payment Required responses.
918
+ * On 402, decodes PAYMENT-REQUIRED header, signs authorization, retries with PAYMENT-SIGNATURE.
919
+ */
920
+ async fetchWithPayment(walletClient, url, options = {}) {
921
+ const response = await fetch(url, options);
922
+ if (response.status !== 402) return response;
923
+ const paymentRequiredHeader = response.headers.get("PAYMENT-REQUIRED");
924
+ if (!paymentRequiredHeader) {
925
+ throw new Error("402 response missing PAYMENT-REQUIRED header");
926
+ }
927
+ const paymentRequired = JSON.parse(atob(paymentRequiredHeader));
928
+ const accepted = paymentRequired.accepts?.[0];
929
+ if (!accepted) {
930
+ throw new Error("No accepted payment methods in 402 response");
931
+ }
932
+ const authData = await this.signTransferWithWallet({
933
+ walletClient,
934
+ to: accepted.payTo,
935
+ amount: accepted.amount
936
+ });
937
+ const paymentPayload = {
938
+ x402Version: paymentRequired.x402Version || 2,
939
+ resource: paymentRequired.resource,
940
+ accepted,
941
+ payload: {
942
+ signature: `0x${authData.r.slice(2)}${authData.s.slice(2)}${authData.v.toString(16).padStart(2, "0")}`,
943
+ authorization: {
944
+ from: authData.from,
945
+ to: authData.to,
946
+ value: authData.value.toString(),
947
+ validAfter: authData.validAfter.toString(),
948
+ validBefore: authData.validBefore.toString(),
949
+ nonce: authData.nonce
950
+ }
951
+ }
952
+ };
953
+ const encoded = btoa(JSON.stringify(paymentPayload));
954
+ return fetch(url, {
955
+ ...options,
956
+ headers: {
957
+ ...Object.fromEntries(new Headers(options.headers).entries()),
958
+ "PAYMENT-SIGNATURE": encoded
959
+ }
960
+ });
961
+ }
962
+ };
963
+
964
+ // src/client.ts
965
+ var Djeon402ClientSDK = class {
966
+ constructor(config) {
967
+ const { rpcUrl, contractAddress, kycRegistryAddress, chainId } = config;
968
+ this.contractAddress = contractAddress;
969
+ this.kycRegistryAddress = kycRegistryAddress;
970
+ this.chainId = chainId;
971
+ const chain = chainId === 8453 ? base : {
972
+ id: chainId,
973
+ name: "Custom Chain",
974
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
975
+ rpcUrls: {
976
+ default: { http: [rpcUrl || ""] },
977
+ public: { http: [rpcUrl || ""] }
978
+ }
979
+ };
980
+ this.publicClient = createPublicClient({
981
+ chain,
982
+ transport: http(rpcUrl)
983
+ });
984
+ this.token = new TokenModule(this);
985
+ this.admin = new AdminModule(this);
986
+ this.kyc = new KYCModule(this);
987
+ this.x402 = new X402Module(this);
988
+ }
989
+ /**
990
+ * Get chain configuration
991
+ */
992
+ getChain() {
993
+ return this.chainId === 8453 ? base : {
994
+ id: this.chainId,
995
+ name: "Custom Chain",
996
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
997
+ rpcUrls: {
998
+ default: { http: [this.publicClient.transport.url || ""] },
999
+ public: { http: [this.publicClient.transport.url || ""] }
1000
+ }
1001
+ };
1002
+ }
1003
+ };
1004
+ export {
1005
+ AdminModule,
1006
+ Djeon402ClientSDK,
1007
+ KYCModule,
1008
+ TokenModule,
1009
+ X402Module
1010
+ };
1011
+ //# sourceMappingURL=index.js.map