@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,4 +1,4 @@
1
- import { b as WDKTronAccount } from '../types-V7c-qhn6.js';
1
+ import { b as WDKTronAccount } from '../types-BwK8Xgvg.js';
2
2
  import 'viem';
3
3
 
4
4
  /**
@@ -6,6 +6,11 @@ import 'viem';
6
6
  *
7
7
  * Wraps a Tether WDK TRON account to implement T402's ClientTronSigner interface.
8
8
  * This allows WDK-managed TRON wallets to be used for T402 payments.
9
+ *
10
+ * Includes support for:
11
+ * - Energy estimation for dynamic fee limits
12
+ * - External energy delegation providers
13
+ * - Dynamic fee limit calculation with 20% margin
9
14
  */
10
15
 
11
16
  /**
@@ -42,6 +47,22 @@ interface ClientTronSigner {
42
47
  signTransaction(params: SignTransactionParams): Promise<string>;
43
48
  getBlockInfo(): Promise<BlockInfo>;
44
49
  }
50
+ /**
51
+ * Energy estimation result
52
+ */
53
+ interface EnergyEstimate {
54
+ energyRequired: number;
55
+ energyAvailable: number;
56
+ trxCostIfNoEnergy: bigint;
57
+ bandwidthRequired: number;
58
+ }
59
+ /**
60
+ * Energy provider interface for external delegation services
61
+ */
62
+ interface EnergyProvider {
63
+ delegateEnergy(to: string, amount: number): Promise<string>;
64
+ getPrice(amount: number): Promise<bigint>;
65
+ }
45
66
  /**
46
67
  * WDKTronSignerAdapter - Adapts a WDK TRON account to T402's ClientTronSigner
47
68
  *
@@ -60,6 +81,7 @@ declare class WDKTronSignerAdapter implements ClientTronSigner {
60
81
  private _address;
61
82
  private _initialized;
62
83
  private _rpcUrl;
84
+ private _energyProvider;
63
85
  constructor(account: WDKTronAccount, rpcUrl?: string);
64
86
  /**
65
87
  * Get the wallet address (T-prefix base58check)
@@ -92,6 +114,39 @@ declare class WDKTronSignerAdapter implements ClientTronSigner {
92
114
  * This is required for TRON's replay protection mechanism
93
115
  */
94
116
  getBlockInfo(): Promise<BlockInfo>;
117
+ /**
118
+ * Estimate the energy required for a TRC20 transfer.
119
+ *
120
+ * Uses the `wallet/triggerconstantcontract` API to simulate the transfer
121
+ * and return the energy/bandwidth requirements.
122
+ *
123
+ * @param params - Transaction parameters to simulate
124
+ * @returns Energy estimation result
125
+ */
126
+ estimateEnergy(params: SignTransactionParams): Promise<EnergyEstimate>;
127
+ /**
128
+ * Sign a TRC20 transfer with dynamic fee limit estimation.
129
+ *
130
+ * Instead of using a hardcoded 100 TRX fee limit, estimates the actual
131
+ * energy cost and adds a 20% margin.
132
+ *
133
+ * @param params - Transaction parameters
134
+ * @returns Hex-encoded signed transaction
135
+ */
136
+ signTransactionWithEstimation(params: SignTransactionParams): Promise<string>;
137
+ /**
138
+ * Register an external energy delegation provider.
139
+ *
140
+ * Energy providers can delegate bandwidth and energy to this account
141
+ * to reduce TRX costs for TRC20 transfers.
142
+ *
143
+ * @param provider - Energy delegation provider
144
+ */
145
+ registerEnergyProvider(provider: EnergyProvider): void;
146
+ /**
147
+ * Get the registered energy provider, if any
148
+ */
149
+ getEnergyProvider(): EnergyProvider | null;
95
150
  /**
96
151
  * Build a TRC20 transfer transaction
97
152
  */
@@ -136,4 +191,4 @@ declare class WDKTronSignerAdapter implements ClientTronSigner {
136
191
  */
137
192
  declare function createWDKTronSigner(account: WDKTronAccount, rpcUrl?: string): Promise<WDKTronSignerAdapter>;
138
193
 
139
- export { type BlockInfo, type ClientTronSigner, type SignTransactionParams, WDKTronSignerAdapter, createWDKTronSigner };
194
+ export { type BlockInfo, type ClientTronSigner, type EnergyEstimate, type EnergyProvider, type SignTransactionParams, WDKTronSignerAdapter, createWDKTronSigner };
@@ -29,6 +29,7 @@ var WDKTronSignerAdapter = class {
29
29
  _address = null;
30
30
  _initialized = false;
31
31
  _rpcUrl;
32
+ _energyProvider = null;
32
33
  constructor(account, rpcUrl = "https://api.trongrid.io") {
33
34
  if (!account) {
34
35
  throw new Error("WDK TRON account is required");
@@ -130,6 +131,106 @@ var WDKTronSignerAdapter = class {
130
131
  );
131
132
  }
132
133
  }
134
+ /**
135
+ * Estimate the energy required for a TRC20 transfer.
136
+ *
137
+ * Uses the `wallet/triggerconstantcontract` API to simulate the transfer
138
+ * and return the energy/bandwidth requirements.
139
+ *
140
+ * @param params - Transaction parameters to simulate
141
+ * @returns Energy estimation result
142
+ */
143
+ async estimateEnergy(params) {
144
+ if (!this._address) {
145
+ throw new Error("TRON signer not initialized. Call initialize() first.");
146
+ }
147
+ const functionSelector = "transfer(address,uint256)";
148
+ const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, "0");
149
+ const amountHex = BigInt(params.amount).toString(16).padStart(64, "0");
150
+ const parameter = toAddressHex + amountHex;
151
+ try {
152
+ const response = await fetch(`${this._rpcUrl}/wallet/triggerconstantcontract`, {
153
+ method: "POST",
154
+ headers: { "Content-Type": "application/json" },
155
+ body: JSON.stringify({
156
+ owner_address: this.addressToHex(this._address),
157
+ contract_address: this.addressToHex(params.contractAddress),
158
+ function_selector: functionSelector,
159
+ parameter
160
+ })
161
+ });
162
+ if (!response.ok) {
163
+ throw new Error(`Energy estimation failed: ${response.status}`);
164
+ }
165
+ const result = await response.json();
166
+ if (result.result?.code && result.result.code !== "SUCCESS") {
167
+ throw new Error(`Energy estimation failed: ${result.result.message ?? result.result.code}`);
168
+ }
169
+ const energyRequired = (result.energy_used ?? 0) + (result.energy_penalty ?? 0);
170
+ const resourceResponse = await fetch(`${this._rpcUrl}/wallet/getaccountresource`, {
171
+ method: "POST",
172
+ headers: { "Content-Type": "application/json" },
173
+ body: JSON.stringify({
174
+ address: this.addressToHex(this._address)
175
+ })
176
+ });
177
+ let energyAvailable = 0;
178
+ if (resourceResponse.ok) {
179
+ const resources = await resourceResponse.json();
180
+ energyAvailable = (resources.EnergyLimit ?? 0) - (resources.EnergyUsed ?? 0);
181
+ }
182
+ const bandwidthRequired = 350;
183
+ const trxCostIfNoEnergy = BigInt(Math.max(0, energyRequired - energyAvailable)) * 420n;
184
+ return {
185
+ energyRequired,
186
+ energyAvailable,
187
+ trxCostIfNoEnergy,
188
+ bandwidthRequired
189
+ };
190
+ } catch (error) {
191
+ throw new Error(
192
+ `Failed to estimate energy: ${error instanceof Error ? error.message : String(error)}`
193
+ );
194
+ }
195
+ }
196
+ /**
197
+ * Sign a TRC20 transfer with dynamic fee limit estimation.
198
+ *
199
+ * Instead of using a hardcoded 100 TRX fee limit, estimates the actual
200
+ * energy cost and adds a 20% margin.
201
+ *
202
+ * @param params - Transaction parameters
203
+ * @returns Hex-encoded signed transaction
204
+ */
205
+ async signTransactionWithEstimation(params) {
206
+ if (!params.feeLimit) {
207
+ const estimate = await this.estimateEnergy(params);
208
+ const estimatedFee = estimate.trxCostIfNoEnergy;
209
+ const feeWithMargin = estimatedFee + estimatedFee * 20n / 100n;
210
+ const feeLimitSun = Number(
211
+ feeWithMargin < 10000000n ? 10000000n : feeWithMargin > 150000000n ? 150000000n : feeWithMargin
212
+ );
213
+ return this.signTransaction({ ...params, feeLimit: feeLimitSun });
214
+ }
215
+ return this.signTransaction(params);
216
+ }
217
+ /**
218
+ * Register an external energy delegation provider.
219
+ *
220
+ * Energy providers can delegate bandwidth and energy to this account
221
+ * to reduce TRX costs for TRC20 transfers.
222
+ *
223
+ * @param provider - Energy delegation provider
224
+ */
225
+ registerEnergyProvider(provider) {
226
+ this._energyProvider = provider;
227
+ }
228
+ /**
229
+ * Get the registered energy provider, if any
230
+ */
231
+ getEnergyProvider() {
232
+ return this._energyProvider;
233
+ }
133
234
  /**
134
235
  * Build a TRC20 transfer transaction
135
236
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/adapters/tron-adapter.ts"],"sourcesContent":["/**\n * TRON Signer Adapter for WDK\n *\n * Wraps a Tether WDK TRON account to implement T402's ClientTronSigner interface.\n * This allows WDK-managed TRON wallets to be used for T402 payments.\n */\n\nimport type { WDKTronAccount } from '../types.js'\n\n/**\n * SignTransactionParams matching T402's @t402/tron interface\n */\nexport interface SignTransactionParams {\n /** TRC20 contract address */\n contractAddress: string\n /** Recipient address (T-prefix base58check) */\n to: string\n /** Amount to transfer (in smallest units) */\n amount: string\n /** Fee limit in SUN (optional, defaults to 100 TRX) */\n feeLimit?: number\n /** Transaction expiration time in milliseconds (optional) */\n expiration?: number\n}\n\n/**\n * Block info for transaction building\n */\nexport interface BlockInfo {\n /** Reference block bytes (hex) */\n refBlockBytes: string\n /** Reference block hash (hex) */\n refBlockHash: string\n /** Expiration timestamp in milliseconds */\n expiration: number\n}\n\n/**\n * ClientTronSigner interface matching T402's @t402/tron\n */\nexport interface ClientTronSigner {\n readonly address: string\n signTransaction(params: SignTransactionParams): Promise<string>\n getBlockInfo(): Promise<BlockInfo>\n}\n\n// TronWeb-compatible types\ninterface TronWebTransaction {\n txID: string\n raw_data: {\n contract: unknown[]\n ref_block_bytes: string\n ref_block_hash: string\n expiration: number\n timestamp: number\n }\n raw_data_hex: string\n signature?: string[]\n}\n\ninterface TronWebBlock {\n block_header: {\n raw_data: {\n number: number\n txTrieRoot: string\n witness_address: string\n parentHash: string\n version: number\n timestamp: number\n }\n witness_signature: string\n }\n blockID: string\n}\n\n/**\n * WDKTronSignerAdapter - Adapts a WDK TRON account to T402's ClientTronSigner\n *\n * @example\n * ```typescript\n * const adapter = await createWDKTronSigner(wdkTronAccount);\n * const signedTx = await adapter.signTransaction({\n * contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',\n * to: 'TRecipientAddress...',\n * amount: '1000000', // 1 USDT\n * });\n * ```\n */\nexport class WDKTronSignerAdapter implements ClientTronSigner {\n private _account: WDKTronAccount\n private _address: string | null = null\n private _initialized = false\n private _rpcUrl: string\n\n constructor(account: WDKTronAccount, rpcUrl = 'https://api.trongrid.io') {\n if (!account) {\n throw new Error('WDK TRON account is required')\n }\n this._account = account\n this._rpcUrl = rpcUrl\n }\n\n /**\n * Get the wallet address (T-prefix base58check)\n * @throws Error if not initialized\n */\n get address(): string {\n if (!this._address) {\n throw new Error(\n 'TRON signer not initialized. Call initialize() first or use createWDKTronSigner().',\n )\n }\n return this._address\n }\n\n /**\n * Check if the adapter is initialized\n */\n get isInitialized(): boolean {\n return this._initialized\n }\n\n /**\n * Initialize the adapter by fetching the address\n * Must be called before using the signer\n */\n async initialize(): Promise<void> {\n if (this._initialized) {\n return\n }\n\n this._address = await this._account.getAddress()\n this._initialized = true\n }\n\n /**\n * Sign a TRC20 transfer transaction\n *\n * This method:\n * 1. Builds a TRC20 transfer transaction\n * 2. Signs it using the WDK account\n * 3. Returns the hex-encoded signed transaction\n *\n * @param params - Transaction parameters\n * @returns Hex-encoded signed transaction\n */\n async signTransaction(params: SignTransactionParams): Promise<string> {\n if (!params.contractAddress) {\n throw new Error('contractAddress is required')\n }\n if (!params.to) {\n throw new Error('recipient address (to) is required')\n }\n if (!params.amount || BigInt(params.amount) <= 0n) {\n throw new Error('amount must be a positive value')\n }\n\n // Get block info for transaction\n const blockInfo = await this.getBlockInfo()\n\n // Default fee limit: 100 TRX = 100_000_000 SUN\n const feeLimit = params.feeLimit ?? 100_000_000\n\n // Build the TRC20 transfer transaction\n const transaction = await this.buildTrc20Transaction({\n contractAddress: params.contractAddress,\n to: params.to,\n amount: params.amount,\n feeLimit,\n refBlockBytes: blockInfo.refBlockBytes,\n refBlockHash: blockInfo.refBlockHash,\n expiration: params.expiration ?? blockInfo.expiration,\n })\n\n // Sign the transaction using WDK account\n const signedTx = await this._account.signTransaction(transaction)\n\n // Serialize to hex format\n return this.serializeTransaction(signedTx as TronWebTransaction)\n }\n\n /**\n * Get the current reference block info for transaction building\n * This is required for TRON's replay protection mechanism\n */\n async getBlockInfo(): Promise<BlockInfo> {\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/getnowblock`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({}),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to get block info: ${response.status}`)\n }\n\n const block = (await response.json()) as TronWebBlock\n\n // Extract reference block bytes (last 4 bytes of block number)\n const blockNum = block.block_header.raw_data.number\n const refBlockBytes = blockNum.toString(16).padStart(8, '0').slice(-4)\n\n // Reference block hash (first 8 bytes of block ID)\n const refBlockHash = block.blockID.slice(16, 32)\n\n // Expiration: block timestamp + 60 seconds\n const expiration = block.block_header.raw_data.timestamp + 60000\n\n return {\n refBlockBytes,\n refBlockHash,\n expiration,\n }\n } catch (error) {\n throw new Error(\n `Failed to get TRON block info: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Build a TRC20 transfer transaction\n */\n private async buildTrc20Transaction(params: {\n contractAddress: string\n to: string\n amount: string\n feeLimit: number\n refBlockBytes: string\n refBlockHash: string\n expiration: number\n }): Promise<TronWebTransaction> {\n // Build TRC20 transfer function call\n // transfer(address,uint256) = 0xa9059cbb\n const functionSelector = 'transfer(address,uint256)'\n\n // Encode parameters\n const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, '0')\n const amountHex = BigInt(params.amount).toString(16).padStart(64, '0')\n const parameter = toAddressHex + amountHex\n\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/triggersmartcontract`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n owner_address: this.addressToHex(this._address!),\n contract_address: this.addressToHex(params.contractAddress),\n function_selector: functionSelector,\n parameter,\n fee_limit: params.feeLimit,\n }),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to build transaction: ${response.status}`)\n }\n\n const result = await response.json()\n\n if (result.result?.code) {\n throw new Error(`Transaction build failed: ${result.result.message}`)\n }\n\n return result.transaction as TronWebTransaction\n } catch (error) {\n throw new Error(\n `Failed to build TRC20 transaction: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Serialize a signed transaction to hex format\n */\n private serializeTransaction(signedTx: TronWebTransaction): string {\n // Return the raw_data_hex with signature appended\n if (signedTx.signature && signedTx.signature.length > 0) {\n return JSON.stringify(signedTx)\n }\n return signedTx.raw_data_hex\n }\n\n /**\n * Convert TRON base58 address to hex format\n */\n private addressToHex(address: string): string {\n // If already hex, return as-is\n if (address.startsWith('41') || address.startsWith('0x')) {\n return address.startsWith('0x') ? '41' + address.slice(2) : address\n }\n\n // Convert base58 to hex using simple algorithm\n const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'\n let num = BigInt(0)\n for (const char of address) {\n num = num * BigInt(58) + BigInt(ALPHABET.indexOf(char))\n }\n\n // Convert to hex and take first 42 chars (21 bytes)\n let hex = num.toString(16)\n // Handle leading zeros\n let leadingZeros = 0\n for (const char of address) {\n if (char === '1') leadingZeros++\n else break\n }\n hex = '00'.repeat(leadingZeros) + hex\n\n // TRON address is 21 bytes = 42 hex chars\n return hex.slice(0, 42)\n }\n\n /**\n * Get TRX balance in SUN\n */\n async getBalance(): Promise<bigint> {\n return this._account.getBalance()\n }\n\n /**\n * Get TRC20 token balance\n * @param contractAddress - TRC20 contract address\n */\n async getTrc20Balance(contractAddress: string): Promise<bigint> {\n return this._account.getTrc20Balance(contractAddress)\n }\n}\n\n/**\n * Create an initialized WDK TRON signer\n *\n * @param account - WDK TRON account from @tetherto/wdk-wallet-tron\n * @param rpcUrl - Optional custom RPC URL (default: https://api.trongrid.io)\n * @returns Initialized ClientTronSigner\n *\n * @example\n * ```typescript\n * import { T402WDK } from '@t402/wdk';\n *\n * const wallet = new T402WDK(seedPhrase, config);\n * const tronSigner = await wallet.getTronSigner();\n *\n * // Use with T402 client\n * const client = createT402HTTPClient({\n * signers: [{ scheme: 'exact', network: 'tron:mainnet', signer: tronSigner }]\n * });\n * ```\n */\nexport async function createWDKTronSigner(\n account: WDKTronAccount,\n rpcUrl?: string,\n): Promise<WDKTronSignerAdapter> {\n const adapter = new WDKTronSignerAdapter(account, rpcUrl)\n await adapter.initialize()\n return adapter\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwFO,IAAM,uBAAN,MAAuD;AAAA,EACpD;AAAA,EACA,WAA0B;AAAA,EAC1B,eAAe;AAAA,EACf;AAAA,EAER,YAAY,SAAyB,SAAS,2BAA2B;AACvE,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,SAAK,WAAW;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA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W;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,gBAAgB,iBAA0C;AAC9D,WAAO,KAAK,SAAS,gBAAgB,eAAe;AAAA,EACtD;AACF;AAsBA,eAAsB,oBACpB,SACA,QAC+B;AAC/B,QAAM,UAAU,IAAI,qBAAqB,SAAS,MAAM;AACxD,QAAM,QAAQ,WAAW;AACzB,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../../src/adapters/tron-adapter.ts"],"sourcesContent":["/**\n * TRON Signer Adapter for WDK\n *\n * Wraps a Tether WDK TRON account to implement T402's ClientTronSigner interface.\n * This allows WDK-managed TRON wallets to be used for T402 payments.\n *\n * Includes support for:\n * - Energy estimation for dynamic fee limits\n * - External energy delegation providers\n * - Dynamic fee limit calculation with 20% margin\n */\n\nimport type { WDKTronAccount } from '../types.js'\n\n/**\n * SignTransactionParams matching T402's @t402/tron interface\n */\nexport interface SignTransactionParams {\n /** TRC20 contract address */\n contractAddress: string\n /** Recipient address (T-prefix base58check) */\n to: string\n /** Amount to transfer (in smallest units) */\n amount: string\n /** Fee limit in SUN (optional, defaults to 100 TRX) */\n feeLimit?: number\n /** Transaction expiration time in milliseconds (optional) */\n expiration?: number\n}\n\n/**\n * Block info for transaction building\n */\nexport interface BlockInfo {\n /** Reference block bytes (hex) */\n refBlockBytes: string\n /** Reference block hash (hex) */\n refBlockHash: string\n /** Expiration timestamp in milliseconds */\n expiration: number\n}\n\n/**\n * ClientTronSigner interface matching T402's @t402/tron\n */\nexport interface ClientTronSigner {\n readonly address: string\n signTransaction(params: SignTransactionParams): Promise<string>\n getBlockInfo(): Promise<BlockInfo>\n}\n\n/**\n * Energy estimation result\n */\nexport interface EnergyEstimate {\n energyRequired: number\n energyAvailable: number\n trxCostIfNoEnergy: bigint\n bandwidthRequired: number\n}\n\n/**\n * Energy provider interface for external delegation services\n */\nexport interface EnergyProvider {\n delegateEnergy(to: string, amount: number): Promise<string>\n getPrice(amount: number): Promise<bigint>\n}\n\n// TronWeb-compatible types\ninterface TronWebTransaction {\n txID: string\n raw_data: {\n contract: unknown[]\n ref_block_bytes: string\n ref_block_hash: string\n expiration: number\n timestamp: number\n }\n raw_data_hex: string\n signature?: string[]\n}\n\ninterface TronWebBlock {\n block_header: {\n raw_data: {\n number: number\n txTrieRoot: string\n witness_address: string\n parentHash: string\n version: number\n timestamp: number\n }\n witness_signature: string\n }\n blockID: string\n}\n\n/**\n * WDKTronSignerAdapter - Adapts a WDK TRON account to T402's ClientTronSigner\n *\n * @example\n * ```typescript\n * const adapter = await createWDKTronSigner(wdkTronAccount);\n * const signedTx = await adapter.signTransaction({\n * contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',\n * to: 'TRecipientAddress...',\n * amount: '1000000', // 1 USDT\n * });\n * ```\n */\nexport class WDKTronSignerAdapter implements ClientTronSigner {\n private _account: WDKTronAccount\n private _address: string | null = null\n private _initialized = false\n private _rpcUrl: string\n private _energyProvider: EnergyProvider | null = null\n\n constructor(account: WDKTronAccount, rpcUrl = 'https://api.trongrid.io') {\n if (!account) {\n throw new Error('WDK TRON account is required')\n }\n this._account = account\n this._rpcUrl = rpcUrl\n }\n\n /**\n * Get the wallet address (T-prefix base58check)\n * @throws Error if not initialized\n */\n get address(): string {\n if (!this._address) {\n throw new Error(\n 'TRON signer not initialized. Call initialize() first or use createWDKTronSigner().',\n )\n }\n return this._address\n }\n\n /**\n * Check if the adapter is initialized\n */\n get isInitialized(): boolean {\n return this._initialized\n }\n\n /**\n * Initialize the adapter by fetching the address\n * Must be called before using the signer\n */\n async initialize(): Promise<void> {\n if (this._initialized) {\n return\n }\n\n this._address = await this._account.getAddress()\n this._initialized = true\n }\n\n /**\n * Sign a TRC20 transfer transaction\n *\n * This method:\n * 1. Builds a TRC20 transfer transaction\n * 2. Signs it using the WDK account\n * 3. Returns the hex-encoded signed transaction\n *\n * @param params - Transaction parameters\n * @returns Hex-encoded signed transaction\n */\n async signTransaction(params: SignTransactionParams): Promise<string> {\n if (!params.contractAddress) {\n throw new Error('contractAddress is required')\n }\n if (!params.to) {\n throw new Error('recipient address (to) is required')\n }\n if (!params.amount || BigInt(params.amount) <= 0n) {\n throw new Error('amount must be a positive value')\n }\n\n // Get block info for transaction\n const blockInfo = await this.getBlockInfo()\n\n // Default fee limit: 100 TRX = 100_000_000 SUN\n const feeLimit = params.feeLimit ?? 100_000_000\n\n // Build the TRC20 transfer transaction\n const transaction = await this.buildTrc20Transaction({\n contractAddress: params.contractAddress,\n to: params.to,\n amount: params.amount,\n feeLimit,\n refBlockBytes: blockInfo.refBlockBytes,\n refBlockHash: blockInfo.refBlockHash,\n expiration: params.expiration ?? blockInfo.expiration,\n })\n\n // Sign the transaction using WDK account\n const signedTx = await this._account.signTransaction(transaction)\n\n // Serialize to hex format\n return this.serializeTransaction(signedTx as TronWebTransaction)\n }\n\n /**\n * Get the current reference block info for transaction building\n * This is required for TRON's replay protection mechanism\n */\n async getBlockInfo(): Promise<BlockInfo> {\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/getnowblock`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({}),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to get block info: ${response.status}`)\n }\n\n const block = (await response.json()) as TronWebBlock\n\n // Extract reference block bytes (last 4 bytes of block number)\n const blockNum = block.block_header.raw_data.number\n const refBlockBytes = blockNum.toString(16).padStart(8, '0').slice(-4)\n\n // Reference block hash (first 8 bytes of block ID)\n const refBlockHash = block.blockID.slice(16, 32)\n\n // Expiration: block timestamp + 60 seconds\n const expiration = block.block_header.raw_data.timestamp + 60000\n\n return {\n refBlockBytes,\n refBlockHash,\n expiration,\n }\n } catch (error) {\n throw new Error(\n `Failed to get TRON block info: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Estimate the energy required for a TRC20 transfer.\n *\n * Uses the `wallet/triggerconstantcontract` API to simulate the transfer\n * and return the energy/bandwidth requirements.\n *\n * @param params - Transaction parameters to simulate\n * @returns Energy estimation result\n */\n async estimateEnergy(params: SignTransactionParams): Promise<EnergyEstimate> {\n if (!this._address) {\n throw new Error('TRON signer not initialized. Call initialize() first.')\n }\n\n const functionSelector = 'transfer(address,uint256)'\n const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, '0')\n const amountHex = BigInt(params.amount).toString(16).padStart(64, '0')\n const parameter = toAddressHex + amountHex\n\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/triggerconstantcontract`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n owner_address: this.addressToHex(this._address),\n contract_address: this.addressToHex(params.contractAddress),\n function_selector: functionSelector,\n parameter,\n }),\n })\n\n if (!response.ok) {\n throw new Error(`Energy estimation failed: ${response.status}`)\n }\n\n const result = (await response.json()) as {\n energy_used?: number\n energy_penalty?: number\n result?: { code?: string; message?: string }\n }\n\n if (result.result?.code && result.result.code !== 'SUCCESS') {\n throw new Error(`Energy estimation failed: ${result.result.message ?? result.result.code}`)\n }\n\n const energyRequired = (result.energy_used ?? 0) + (result.energy_penalty ?? 0)\n\n // Get account resources to check available energy\n const resourceResponse = await fetch(`${this._rpcUrl}/wallet/getaccountresource`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n address: this.addressToHex(this._address),\n }),\n })\n\n let energyAvailable = 0\n if (resourceResponse.ok) {\n const resources = (await resourceResponse.json()) as {\n EnergyLimit?: number\n EnergyUsed?: number\n freeNetLimit?: number\n freeNetUsed?: number\n NetLimit?: number\n NetUsed?: number\n }\n energyAvailable = (resources.EnergyLimit ?? 0) - (resources.EnergyUsed ?? 0)\n }\n\n // TRC20 transfer typically requires ~350 bytes of bandwidth\n const bandwidthRequired = 350\n\n // Energy costs 420 SUN each (approximate, varies with market)\n const trxCostIfNoEnergy = BigInt(Math.max(0, energyRequired - energyAvailable)) * 420n\n\n return {\n energyRequired,\n energyAvailable,\n trxCostIfNoEnergy,\n bandwidthRequired,\n }\n } catch (error) {\n throw new Error(\n `Failed to estimate energy: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Sign a TRC20 transfer with dynamic fee limit estimation.\n *\n * Instead of using a hardcoded 100 TRX fee limit, estimates the actual\n * energy cost and adds a 20% margin.\n *\n * @param params - Transaction parameters\n * @returns Hex-encoded signed transaction\n */\n async signTransactionWithEstimation(params: SignTransactionParams): Promise<string> {\n if (!params.feeLimit) {\n const estimate = await this.estimateEnergy(params)\n // Convert energy cost to SUN with 20% margin\n const estimatedFee = estimate.trxCostIfNoEnergy\n const feeWithMargin = estimatedFee + (estimatedFee * 20n) / 100n\n // Minimum 10 TRX fee limit, max 150 TRX\n const feeLimitSun = Number(\n feeWithMargin < 10_000_000n\n ? 10_000_000n\n : feeWithMargin > 150_000_000n\n ? 150_000_000n\n : feeWithMargin,\n )\n return this.signTransaction({ ...params, feeLimit: feeLimitSun })\n }\n return this.signTransaction(params)\n }\n\n /**\n * Register an external energy delegation provider.\n *\n * Energy providers can delegate bandwidth and energy to this account\n * to reduce TRX costs for TRC20 transfers.\n *\n * @param provider - Energy delegation provider\n */\n registerEnergyProvider(provider: EnergyProvider): void {\n this._energyProvider = provider\n }\n\n /**\n * Get the registered energy provider, if any\n */\n getEnergyProvider(): EnergyProvider | null {\n return this._energyProvider\n }\n\n /**\n * Build a TRC20 transfer transaction\n */\n private async buildTrc20Transaction(params: {\n contractAddress: string\n to: string\n amount: string\n feeLimit: number\n refBlockBytes: string\n refBlockHash: string\n expiration: number\n }): Promise<TronWebTransaction> {\n // Build TRC20 transfer function call\n // transfer(address,uint256) = 0xa9059cbb\n const functionSelector = 'transfer(address,uint256)'\n\n // Encode parameters\n const toAddressHex = this.addressToHex(params.to).slice(2).padStart(64, '0')\n const amountHex = BigInt(params.amount).toString(16).padStart(64, '0')\n const parameter = toAddressHex + amountHex\n\n try {\n const response = await fetch(`${this._rpcUrl}/wallet/triggersmartcontract`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n owner_address: this.addressToHex(this._address!),\n contract_address: this.addressToHex(params.contractAddress),\n function_selector: functionSelector,\n parameter,\n fee_limit: params.feeLimit,\n }),\n })\n\n if (!response.ok) {\n throw new Error(`Failed to build transaction: ${response.status}`)\n }\n\n const result = await response.json()\n\n if (result.result?.code) {\n throw new Error(`Transaction build failed: ${result.result.message}`)\n }\n\n return result.transaction as TronWebTransaction\n } catch (error) {\n throw new Error(\n `Failed to build TRC20 transaction: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n\n /**\n * Serialize a signed transaction to hex format\n */\n private serializeTransaction(signedTx: TronWebTransaction): string {\n // Return the raw_data_hex with signature appended\n if (signedTx.signature && signedTx.signature.length > 0) {\n return JSON.stringify(signedTx)\n }\n return signedTx.raw_data_hex\n }\n\n /**\n * Convert TRON base58 address to hex format\n */\n private addressToHex(address: string): string {\n // If already hex, return as-is\n if (address.startsWith('41') || address.startsWith('0x')) {\n return address.startsWith('0x') ? '41' + address.slice(2) : address\n }\n\n // Convert base58 to hex using simple algorithm\n const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'\n let num = BigInt(0)\n for (const char of address) {\n num = num * BigInt(58) + BigInt(ALPHABET.indexOf(char))\n }\n\n // Convert to hex and take first 42 chars (21 bytes)\n let hex = num.toString(16)\n // Handle leading zeros\n let leadingZeros = 0\n for (const char of address) {\n if (char === '1') leadingZeros++\n else break\n }\n hex = '00'.repeat(leadingZeros) + hex\n\n // TRON address is 21 bytes = 42 hex chars\n return hex.slice(0, 42)\n }\n\n /**\n * Get TRX balance in SUN\n */\n async getBalance(): Promise<bigint> {\n return this._account.getBalance()\n }\n\n /**\n * Get TRC20 token balance\n * @param contractAddress - TRC20 contract address\n */\n async getTrc20Balance(contractAddress: string): Promise<bigint> {\n return this._account.getTrc20Balance(contractAddress)\n }\n}\n\n/**\n * Create an initialized WDK TRON signer\n *\n * @param account - WDK TRON account from @tetherto/wdk-wallet-tron\n * @param rpcUrl - Optional custom RPC URL (default: https://api.trongrid.io)\n * @returns Initialized ClientTronSigner\n *\n * @example\n * ```typescript\n * import { T402WDK } from '@t402/wdk';\n *\n * const wallet = new T402WDK(seedPhrase, config);\n * const tronSigner = await wallet.getTronSigner();\n *\n * // Use with T402 client\n * const client = createT402HTTPClient({\n * signers: [{ scheme: 'exact', network: 'tron:mainnet', signer: tronSigner }]\n * });\n * ```\n */\nexport async function createWDKTronSigner(\n account: WDKTronAccount,\n rpcUrl?: string,\n): Promise<WDKTronSignerAdapter> {\n const adapter = new WDKTronSignerAdapter(account, rpcUrl)\n await adapter.initialize()\n return adapter\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+GO,IAAM,uBAAN,MAAuD;AAAA,EACpD;AAAA,EACA,WAA0B;AAAA,EAC1B,eAAe;AAAA,EACf;AAAA,EACA,kBAAyC;AAAA,EAEjD,YAAY,SAAyB,SAAS,2BAA2B;AACvE,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,SAAK,WAAW;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,UAAkB;AACpB,QAAI,CAAC,KAAK,UAAU;AAClB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,gBAAyB;AAC3B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,aAA4B;AAChC,QAAI,KAAK,cAAc;AACrB;AAAA,IACF;AAEA,SAAK,WAAW,MAAM,KAAK,SAAS,WAAW;AAC/C,SAAK,eAAe;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,MAA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