@t402/wdk 2.4.0 → 2.6.0

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.
Files changed (61) hide show
  1. package/dist/cjs/adapters/index.d.ts +198 -1
  2. package/dist/cjs/adapters/index.js +255 -0
  3. package/dist/cjs/adapters/index.js.map +1 -1
  4. package/dist/cjs/adapters/svm-adapter.d.ts +146 -2
  5. package/dist/cjs/adapters/svm-adapter.js +255 -2
  6. package/dist/cjs/adapters/svm-adapter.js.map +1 -1
  7. package/dist/cjs/adapters/ton-adapter.d.ts +57 -2
  8. package/dist/cjs/adapters/ton-adapter.js +75 -2
  9. package/dist/cjs/adapters/ton-adapter.js.map +1 -1
  10. package/dist/cjs/adapters/tron-adapter.d.ts +57 -2
  11. package/dist/cjs/adapters/tron-adapter.js +101 -0
  12. package/dist/cjs/adapters/tron-adapter.js.map +1 -1
  13. package/dist/cjs/index-DnEI5M6d.d.ts +1798 -0
  14. package/dist/cjs/index.d.ts +702 -1118
  15. package/dist/cjs/index.js +3905 -246
  16. package/dist/cjs/index.js.map +1 -1
  17. package/dist/cjs/integrations/index.d.ts +9 -0
  18. package/dist/cjs/integrations/index.js +249 -0
  19. package/dist/cjs/integrations/index.js.map +1 -0
  20. package/dist/cjs/testing/index.d.ts +62 -0
  21. package/dist/cjs/testing/index.js +129 -0
  22. package/dist/cjs/testing/index.js.map +1 -0
  23. package/dist/cjs/types-BwK8Xgvg.d.ts +967 -0
  24. package/dist/esm/adapters/index.d.mts +198 -1
  25. package/dist/esm/adapters/index.mjs +14 -3
  26. package/dist/esm/adapters/svm-adapter.d.mts +146 -2
  27. package/dist/esm/adapters/svm-adapter.mjs +18 -3
  28. package/dist/esm/adapters/ton-adapter.d.mts +57 -2
  29. package/dist/esm/adapters/ton-adapter.mjs +8 -3
  30. package/dist/esm/adapters/tron-adapter.d.mts +57 -2
  31. package/dist/esm/adapters/tron-adapter.mjs +2 -1
  32. package/dist/esm/chunk-2KWVW77U.mjs +353 -0
  33. package/dist/esm/chunk-2KWVW77U.mjs.map +1 -0
  34. package/dist/esm/chunk-7CG77QAN.mjs +153 -0
  35. package/dist/esm/chunk-7CG77QAN.mjs.map +1 -0
  36. package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
  37. package/dist/esm/chunk-BJTO5JO5.mjs.map +1 -0
  38. package/dist/esm/{chunk-YWBJJV5M.mjs → chunk-KWX6CJIH.mjs} +72 -1
  39. package/dist/esm/chunk-KWX6CJIH.mjs.map +1 -0
  40. package/dist/esm/{chunk-HB2DGKQ3.mjs → chunk-QZKUU2O6.mjs} +102 -1
  41. package/dist/esm/chunk-QZKUU2O6.mjs.map +1 -0
  42. package/dist/esm/chunk-TVSNUSFZ.mjs +219 -0
  43. package/dist/esm/chunk-TVSNUSFZ.mjs.map +1 -0
  44. package/dist/esm/index-D5kvtDfm.d.mts +1798 -0
  45. package/dist/esm/index.d.mts +702 -1118
  46. package/dist/esm/index.mjs +2934 -70
  47. package/dist/esm/index.mjs.map +1 -1
  48. package/dist/esm/integrations/index.d.mts +9 -0
  49. package/dist/esm/integrations/index.mjs +16 -0
  50. package/dist/esm/integrations/index.mjs.map +1 -0
  51. package/dist/esm/testing/index.d.mts +62 -0
  52. package/dist/esm/testing/index.mjs +101 -0
  53. package/dist/esm/testing/index.mjs.map +1 -0
  54. package/dist/esm/types-BwK8Xgvg.d.mts +967 -0
  55. package/package.json +69 -20
  56. package/dist/cjs/types-V7c-qhn6.d.ts +0 -489
  57. package/dist/esm/chunk-HB2DGKQ3.mjs.map +0 -1
  58. package/dist/esm/chunk-MCFHZSF7.mjs +0 -107
  59. package/dist/esm/chunk-MCFHZSF7.mjs.map +0 -1
  60. package/dist/esm/chunk-YWBJJV5M.mjs.map +0 -1
  61. package/dist/esm/types-V7c-qhn6.d.mts +0 -489
@@ -1,5 +1,202 @@
1
1
  export { WDKTonSignerAdapter, createWDKTonSigner } from './ton-adapter.js';
2
2
  export { WDKSvmSignerAdapter, createWDKSvmSigner } from './svm-adapter.js';
3
3
  export { WDKTronSignerAdapter, createWDKTronSigner } from './tron-adapter.js';
4
- import '../types-V7c-qhn6.js';
4
+ import '../types-BwK8Xgvg.js';
5
5
  import 'viem';
6
+
7
+ /**
8
+ * Spark (Bitcoin L2) Signer Adapter for WDK
9
+ *
10
+ * Wraps a Spark wallet account to implement T402's payment signing interface.
11
+ * Spark is a Bitcoin L2 built on the Spark SDK (@buildonspark/spark-sdk).
12
+ */
13
+ /**
14
+ * SparkWalletAccount interface (matches @buildonspark/spark-sdk)
15
+ */
16
+ interface SparkWalletAccount {
17
+ getAddress(): Promise<string>;
18
+ getBalance(): Promise<bigint>;
19
+ sendTransaction(params: {
20
+ to: string;
21
+ amount: bigint;
22
+ }): Promise<{
23
+ hash: string;
24
+ }>;
25
+ signMessage(message: string | Uint8Array): Promise<string>;
26
+ }
27
+ /**
28
+ * WDKSparkSignerAdapter - Adapts a Spark wallet account for T402 payments
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const adapter = await createWDKSparkSigner(sparkAccount);
33
+ *
34
+ * // Use with T402 client
35
+ * const client = createT402HTTPClient({
36
+ * signers: [{ scheme: 'exact', network: 'spark:mainnet', signer: adapter }]
37
+ * });
38
+ * ```
39
+ */
40
+ declare class WDKSparkSignerAdapter {
41
+ private _account;
42
+ private _address;
43
+ private _initialized;
44
+ constructor(account: SparkWalletAccount);
45
+ /**
46
+ * Get the wallet address
47
+ * @throws Error if not initialized
48
+ */
49
+ get address(): string;
50
+ /**
51
+ * Check if the adapter is initialized
52
+ */
53
+ get isInitialized(): boolean;
54
+ /**
55
+ * Initialize the adapter by fetching the address
56
+ * Must be called before using the signer
57
+ */
58
+ initialize(): Promise<void>;
59
+ /**
60
+ * Sign a message using the Spark wallet
61
+ * @param message - Message to sign (string or bytes)
62
+ * @returns Signature string
63
+ */
64
+ signMessage(message: string | Uint8Array): Promise<string>;
65
+ /**
66
+ * Get the wallet balance in satoshis
67
+ */
68
+ getBalance(): Promise<bigint>;
69
+ /**
70
+ * Send a transaction via the Spark network
71
+ * @param params - Transaction parameters
72
+ * @returns Transaction result with hash
73
+ */
74
+ sendTransaction(params: {
75
+ to: string;
76
+ amount: bigint;
77
+ }): Promise<{
78
+ hash: string;
79
+ }>;
80
+ }
81
+ /**
82
+ * Create an initialized WDK Spark signer
83
+ *
84
+ * @param account - Spark wallet account from @buildonspark/spark-sdk
85
+ * @returns Initialized WDKSparkSignerAdapter
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * import { T402WDK } from '@t402/wdk';
90
+ *
91
+ * const wallet = new T402WDK(seedPhrase, config);
92
+ * const sparkSigner = await wallet.getSparkSigner();
93
+ *
94
+ * // Use with T402 client
95
+ * const client = createT402HTTPClient({
96
+ * signers: [{ scheme: 'exact', network: 'spark:mainnet', signer: sparkSigner }]
97
+ * });
98
+ * ```
99
+ */
100
+ declare function createWDKSparkSigner(account: SparkWalletAccount): Promise<WDKSparkSignerAdapter>;
101
+
102
+ /**
103
+ * Bitcoin (BTC) On-Chain Signer Adapter for WDK
104
+ *
105
+ * Wraps a Tether WDK Bitcoin account to implement T402's payment signing interface.
106
+ * This allows WDK-managed Bitcoin wallets to be used for T402 payments.
107
+ */
108
+ /**
109
+ * WDKBtcAccount interface (matches @tetherto/wdk-wallet-btc)
110
+ */
111
+ interface WDKBtcAccount {
112
+ getAddress(): Promise<string>;
113
+ getBalance(): Promise<bigint>;
114
+ sendTransaction(params: {
115
+ to: string;
116
+ amount: bigint;
117
+ fee?: bigint;
118
+ }): Promise<string>;
119
+ signMessage(message: string): Promise<string>;
120
+ signPsbt(psbt: Uint8Array): Promise<Uint8Array>;
121
+ }
122
+ /**
123
+ * WDKBtcSignerAdapter - Adapts a WDK Bitcoin account for T402 payments
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * const adapter = await createWDKBtcSigner(wdkBtcAccount);
128
+ *
129
+ * // Use with T402 client
130
+ * const client = createT402HTTPClient({
131
+ * signers: [{ scheme: 'exact', network: 'bip122:000000000019d6689c085ae165831e93', signer: adapter }]
132
+ * });
133
+ * ```
134
+ */
135
+ declare class WDKBtcSignerAdapter {
136
+ private _account;
137
+ private _address;
138
+ private _initialized;
139
+ constructor(account: WDKBtcAccount);
140
+ /**
141
+ * Get the wallet address
142
+ * @throws Error if not initialized
143
+ */
144
+ get address(): string;
145
+ /**
146
+ * Check if the adapter is initialized
147
+ */
148
+ get isInitialized(): boolean;
149
+ /**
150
+ * Initialize the adapter by fetching the address
151
+ * Must be called before using the signer
152
+ */
153
+ initialize(): Promise<void>;
154
+ /**
155
+ * Sign a message using the Bitcoin wallet
156
+ * @param message - Message string to sign
157
+ * @returns Signature string
158
+ */
159
+ signMessage(message: string): Promise<string>;
160
+ /**
161
+ * Sign a Partially Signed Bitcoin Transaction (PSBT)
162
+ * @param psbt - PSBT bytes to sign
163
+ * @returns Signed PSBT bytes
164
+ */
165
+ signPsbt(psbt: Uint8Array): Promise<Uint8Array>;
166
+ /**
167
+ * Get the wallet balance in satoshis
168
+ */
169
+ getBalance(): Promise<bigint>;
170
+ /**
171
+ * Send a Bitcoin transaction
172
+ * @param params - Transaction parameters
173
+ * @returns Transaction hash
174
+ */
175
+ sendTransaction(params: {
176
+ to: string;
177
+ amount: bigint;
178
+ fee?: bigint;
179
+ }): Promise<string>;
180
+ }
181
+ /**
182
+ * Create an initialized WDK Bitcoin signer
183
+ *
184
+ * @param account - WDK Bitcoin account from @tetherto/wdk-wallet-btc
185
+ * @returns Initialized WDKBtcSignerAdapter
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * import { T402WDK } from '@t402/wdk';
190
+ *
191
+ * const wallet = new T402WDK(seedPhrase, config);
192
+ * const btcSigner = await wallet.getBtcSigner();
193
+ *
194
+ * // Use with T402 client
195
+ * const client = createT402HTTPClient({
196
+ * signers: [{ scheme: 'exact', network: 'bip122:000000000019d6689c085ae165831e93', signer: btcSigner }]
197
+ * });
198
+ * ```
199
+ */
200
+ declare function createWDKBtcSigner(account: WDKBtcAccount): Promise<WDKBtcSignerAdapter>;
201
+
202
+ export { WDKBtcSignerAdapter, WDKSparkSignerAdapter, createWDKBtcSigner, createWDKSparkSigner };
@@ -30,9 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/adapters/index.ts
31
31
  var adapters_exports = {};
32
32
  __export(adapters_exports, {
33
+ WDKBtcSignerAdapter: () => WDKBtcSignerAdapter,
34
+ WDKSparkSignerAdapter: () => WDKSparkSignerAdapter,
33
35
  WDKSvmSignerAdapter: () => WDKSvmSignerAdapter,
34
36
  WDKTonSignerAdapter: () => WDKTonSignerAdapter,
35
37
  WDKTronSignerAdapter: () => WDKTronSignerAdapter,
38
+ createWDKBtcSigner: () => createWDKBtcSigner,
39
+ createWDKSparkSigner: () => createWDKSparkSigner,
36
40
  createWDKSvmSigner: () => createWDKSvmSigner,
37
41
  createWDKTonSigner: () => createWDKTonSigner,
38
42
  createWDKTronSigner: () => createWDKTronSigner
@@ -259,6 +263,7 @@ var WDKTronSignerAdapter = class {
259
263
  _address = null;
260
264
  _initialized = false;
261
265
  _rpcUrl;
266
+ _energyProvider = null;
262
267
  constructor(account, rpcUrl = "https://api.trongrid.io") {
263
268
  if (!account) {
264
269
  throw new Error("WDK TRON account is required");
@@ -360,6 +365,106 @@ var WDKTronSignerAdapter = class {
360
365
  );
361
366
  }
362
367
  }
368
+ /**
369
+ * Estimate the energy required for a TRC20 transfer.
370
+ *
371
+ * Uses the `wallet/triggerconstantcontract` API to simulate the transfer
372
+ * and return the energy/bandwidth requirements.
373
+ *
374
+ * @param params - Transaction parameters to simulate
375
+ * @returns Energy estimation result
376
+ */
377
+ async estimateEnergy(params) {
378
+ if (!this._address) {
379
+ throw new Error("TRON signer not initialized. Call initialize() first.");
380
+ }
381
+ const functionSelector = "transfer(address,uint256)";
382
+ const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, "0");
383
+ const amountHex = BigInt(params.amount).toString(16).padStart(64, "0");
384
+ const parameter = toAddressHex + amountHex;
385
+ try {
386
+ const response = await fetch(`${this._rpcUrl}/wallet/triggerconstantcontract`, {
387
+ method: "POST",
388
+ headers: { "Content-Type": "application/json" },
389
+ body: JSON.stringify({
390
+ owner_address: this.addressToHex(this._address),
391
+ contract_address: this.addressToHex(params.contractAddress),
392
+ function_selector: functionSelector,
393
+ parameter
394
+ })
395
+ });
396
+ if (!response.ok) {
397
+ throw new Error(`Energy estimation failed: ${response.status}`);
398
+ }
399
+ const result = await response.json();
400
+ if (result.result?.code && result.result.code !== "SUCCESS") {
401
+ throw new Error(`Energy estimation failed: ${result.result.message ?? result.result.code}`);
402
+ }
403
+ const energyRequired = (result.energy_used ?? 0) + (result.energy_penalty ?? 0);
404
+ const resourceResponse = await fetch(`${this._rpcUrl}/wallet/getaccountresource`, {
405
+ method: "POST",
406
+ headers: { "Content-Type": "application/json" },
407
+ body: JSON.stringify({
408
+ address: this.addressToHex(this._address)
409
+ })
410
+ });
411
+ let energyAvailable = 0;
412
+ if (resourceResponse.ok) {
413
+ const resources = await resourceResponse.json();
414
+ energyAvailable = (resources.EnergyLimit ?? 0) - (resources.EnergyUsed ?? 0);
415
+ }
416
+ const bandwidthRequired = 350;
417
+ const trxCostIfNoEnergy = BigInt(Math.max(0, energyRequired - energyAvailable)) * 420n;
418
+ return {
419
+ energyRequired,
420
+ energyAvailable,
421
+ trxCostIfNoEnergy,
422
+ bandwidthRequired
423
+ };
424
+ } catch (error) {
425
+ throw new Error(
426
+ `Failed to estimate energy: ${error instanceof Error ? error.message : String(error)}`
427
+ );
428
+ }
429
+ }
430
+ /**
431
+ * Sign a TRC20 transfer with dynamic fee limit estimation.
432
+ *
433
+ * Instead of using a hardcoded 100 TRX fee limit, estimates the actual
434
+ * energy cost and adds a 20% margin.
435
+ *
436
+ * @param params - Transaction parameters
437
+ * @returns Hex-encoded signed transaction
438
+ */
439
+ async signTransactionWithEstimation(params) {
440
+ if (!params.feeLimit) {
441
+ const estimate = await this.estimateEnergy(params);
442
+ const estimatedFee = estimate.trxCostIfNoEnergy;
443
+ const feeWithMargin = estimatedFee + estimatedFee * 20n / 100n;
444
+ const feeLimitSun = Number(
445
+ feeWithMargin < 10000000n ? 10000000n : feeWithMargin > 150000000n ? 150000000n : feeWithMargin
446
+ );
447
+ return this.signTransaction({ ...params, feeLimit: feeLimitSun });
448
+ }
449
+ return this.signTransaction(params);
450
+ }
451
+ /**
452
+ * Register an external energy delegation provider.
453
+ *
454
+ * Energy providers can delegate bandwidth and energy to this account
455
+ * to reduce TRX costs for TRC20 transfers.
456
+ *
457
+ * @param provider - Energy delegation provider
458
+ */
459
+ registerEnergyProvider(provider) {
460
+ this._energyProvider = provider;
461
+ }
462
+ /**
463
+ * Get the registered energy provider, if any
464
+ */
465
+ getEnergyProvider() {
466
+ return this._energyProvider;
467
+ }
363
468
  /**
364
469
  * Build a TRC20 transfer transaction
365
470
  */
@@ -443,11 +548,161 @@ async function createWDKTronSigner(account, rpcUrl) {
443
548
  await adapter.initialize();
444
549
  return adapter;
445
550
  }
551
+
552
+ // src/adapters/spark-adapter.ts
553
+ var WDKSparkSignerAdapter = class {
554
+ _account;
555
+ _address = null;
556
+ _initialized = false;
557
+ constructor(account) {
558
+ if (!account) {
559
+ throw new Error("Spark wallet account is required");
560
+ }
561
+ this._account = account;
562
+ }
563
+ /**
564
+ * Get the wallet address
565
+ * @throws Error if not initialized
566
+ */
567
+ get address() {
568
+ if (!this._address) {
569
+ throw new Error(
570
+ "Spark signer not initialized. Call initialize() first or use createWDKSparkSigner()."
571
+ );
572
+ }
573
+ return this._address;
574
+ }
575
+ /**
576
+ * Check if the adapter is initialized
577
+ */
578
+ get isInitialized() {
579
+ return this._initialized;
580
+ }
581
+ /**
582
+ * Initialize the adapter by fetching the address
583
+ * Must be called before using the signer
584
+ */
585
+ async initialize() {
586
+ if (this._initialized) {
587
+ return;
588
+ }
589
+ this._address = await this._account.getAddress();
590
+ this._initialized = true;
591
+ }
592
+ /**
593
+ * Sign a message using the Spark wallet
594
+ * @param message - Message to sign (string or bytes)
595
+ * @returns Signature string
596
+ */
597
+ async signMessage(message) {
598
+ return this._account.signMessage(message);
599
+ }
600
+ /**
601
+ * Get the wallet balance in satoshis
602
+ */
603
+ async getBalance() {
604
+ return this._account.getBalance();
605
+ }
606
+ /**
607
+ * Send a transaction via the Spark network
608
+ * @param params - Transaction parameters
609
+ * @returns Transaction result with hash
610
+ */
611
+ async sendTransaction(params) {
612
+ return this._account.sendTransaction(params);
613
+ }
614
+ };
615
+ async function createWDKSparkSigner(account) {
616
+ const adapter = new WDKSparkSignerAdapter(account);
617
+ await adapter.initialize();
618
+ return adapter;
619
+ }
620
+
621
+ // src/adapters/btc-adapter.ts
622
+ var WDKBtcSignerAdapter = class {
623
+ _account;
624
+ _address = null;
625
+ _initialized = false;
626
+ constructor(account) {
627
+ if (!account) {
628
+ throw new Error("WDK Bitcoin account is required");
629
+ }
630
+ this._account = account;
631
+ }
632
+ /**
633
+ * Get the wallet address
634
+ * @throws Error if not initialized
635
+ */
636
+ get address() {
637
+ if (!this._address) {
638
+ throw new Error(
639
+ "Bitcoin signer not initialized. Call initialize() first or use createWDKBtcSigner()."
640
+ );
641
+ }
642
+ return this._address;
643
+ }
644
+ /**
645
+ * Check if the adapter is initialized
646
+ */
647
+ get isInitialized() {
648
+ return this._initialized;
649
+ }
650
+ /**
651
+ * Initialize the adapter by fetching the address
652
+ * Must be called before using the signer
653
+ */
654
+ async initialize() {
655
+ if (this._initialized) {
656
+ return;
657
+ }
658
+ this._address = await this._account.getAddress();
659
+ this._initialized = true;
660
+ }
661
+ /**
662
+ * Sign a message using the Bitcoin wallet
663
+ * @param message - Message string to sign
664
+ * @returns Signature string
665
+ */
666
+ async signMessage(message) {
667
+ return this._account.signMessage(message);
668
+ }
669
+ /**
670
+ * Sign a Partially Signed Bitcoin Transaction (PSBT)
671
+ * @param psbt - PSBT bytes to sign
672
+ * @returns Signed PSBT bytes
673
+ */
674
+ async signPsbt(psbt) {
675
+ return this._account.signPsbt(psbt);
676
+ }
677
+ /**
678
+ * Get the wallet balance in satoshis
679
+ */
680
+ async getBalance() {
681
+ return this._account.getBalance();
682
+ }
683
+ /**
684
+ * Send a Bitcoin transaction
685
+ * @param params - Transaction parameters
686
+ * @returns Transaction hash
687
+ */
688
+ async sendTransaction(params) {
689
+ return this._account.sendTransaction(params);
690
+ }
691
+ };
692
+ async function createWDKBtcSigner(account) {
693
+ const adapter = new WDKBtcSignerAdapter(account);
694
+ await adapter.initialize();
695
+ return adapter;
696
+ }
446
697
  // Annotate the CommonJS export names for ESM import in node:
447
698
  0 && (module.exports = {
699
+ WDKBtcSignerAdapter,
700
+ WDKSparkSignerAdapter,
448
701
  WDKSvmSignerAdapter,
449
702
  WDKTonSignerAdapter,
450
703
  WDKTronSignerAdapter,
704
+ createWDKBtcSigner,
705
+ createWDKSparkSigner,
451
706
  createWDKSvmSigner,
452
707
  createWDKTonSigner,
453
708
  createWDKTronSigner