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