tronwb 6.0.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 (318) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/dist/TronWeb.js +3 -0
  4. package/dist/TronWeb.js.LICENSE.txt +12 -0
  5. package/dist/TronWeb.js.map +1 -0
  6. package/dist/TronWeb.node.js +2 -0
  7. package/dist/TronWeb.node.js.map +1 -0
  8. package/kj9btptx.cjs +1 -0
  9. package/lib/commonjs/index.d.ts +64 -0
  10. package/lib/commonjs/index.js +37 -0
  11. package/lib/commonjs/index.js.map +1 -0
  12. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
  13. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js +1771 -0
  14. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
  15. package/lib/commonjs/lib/TransactionBuilder/helper.d.ts +28 -0
  16. package/lib/commonjs/lib/TransactionBuilder/helper.js +122 -0
  17. package/lib/commonjs/lib/TransactionBuilder/helper.js.map +1 -0
  18. package/lib/commonjs/lib/contract/index.d.ts +29 -0
  19. package/lib/commonjs/lib/contract/index.js +124 -0
  20. package/lib/commonjs/lib/contract/index.js.map +1 -0
  21. package/lib/commonjs/lib/contract/method.d.ts +50 -0
  22. package/lib/commonjs/lib/contract/method.js +238 -0
  23. package/lib/commonjs/lib/contract/method.js.map +1 -0
  24. package/lib/commonjs/lib/event.d.ts +85 -0
  25. package/lib/commonjs/lib/event.js +137 -0
  26. package/lib/commonjs/lib/event.js.map +1 -0
  27. package/lib/commonjs/lib/plugin.d.ts +28 -0
  28. package/lib/commonjs/lib/plugin.js +84 -0
  29. package/lib/commonjs/lib/plugin.js.map +1 -0
  30. package/lib/commonjs/lib/providers/HttpProvider.d.ts +15 -0
  31. package/lib/commonjs/lib/providers/HttpProvider.js +63 -0
  32. package/lib/commonjs/lib/providers/HttpProvider.js.map +1 -0
  33. package/lib/commonjs/lib/providers/index.d.ts +6 -0
  34. package/lib/commonjs/lib/providers/index.js +8 -0
  35. package/lib/commonjs/lib/providers/index.js.map +1 -0
  36. package/lib/commonjs/lib/trx.d.ts +315 -0
  37. package/lib/commonjs/lib/trx.js +965 -0
  38. package/lib/commonjs/lib/trx.js.map +1 -0
  39. package/lib/commonjs/package.json +1 -0
  40. package/lib/commonjs/paramValidator/index.d.ts +19 -0
  41. package/lib/commonjs/paramValidator/index.js +110 -0
  42. package/lib/commonjs/paramValidator/index.js.map +1 -0
  43. package/lib/commonjs/protocol/core/Tron_pb.cjs +7524 -0
  44. package/lib/commonjs/protocol/core/contract/account_contract_pb.cjs +961 -0
  45. package/lib/commonjs/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  46. package/lib/commonjs/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  47. package/lib/commonjs/protocol/core/contract/common_pb.cjs +34 -0
  48. package/lib/commonjs/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  49. package/lib/commonjs/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  50. package/lib/commonjs/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  51. package/lib/commonjs/protocol/core/contract/storage_contract_pb.cjs +848 -0
  52. package/lib/commonjs/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  53. package/lib/commonjs/protocol/core/contract/witness_contract_pb.cjs +956 -0
  54. package/lib/commonjs/tronweb.d.ts +111 -0
  55. package/lib/commonjs/tronweb.js +365 -0
  56. package/lib/commonjs/tronweb.js.map +1 -0
  57. package/lib/commonjs/types/ABI.d.ts +59 -0
  58. package/lib/commonjs/types/ABI.js +3 -0
  59. package/lib/commonjs/types/ABI.js.map +1 -0
  60. package/lib/commonjs/types/APIResponse.d.ts +31 -0
  61. package/lib/commonjs/types/APIResponse.js +3 -0
  62. package/lib/commonjs/types/APIResponse.js.map +1 -0
  63. package/lib/commonjs/types/Contract.d.ts +287 -0
  64. package/lib/commonjs/types/Contract.js +63 -0
  65. package/lib/commonjs/types/Contract.js.map +1 -0
  66. package/lib/commonjs/types/Providers.d.ts +8 -0
  67. package/lib/commonjs/types/Providers.js +3 -0
  68. package/lib/commonjs/types/Providers.js.map +1 -0
  69. package/lib/commonjs/types/Transaction.d.ts +65 -0
  70. package/lib/commonjs/types/Transaction.js +3 -0
  71. package/lib/commonjs/types/Transaction.js.map +1 -0
  72. package/lib/commonjs/types/TransactionBuilder.d.ts +270 -0
  73. package/lib/commonjs/types/TransactionBuilder.js +3 -0
  74. package/lib/commonjs/types/TransactionBuilder.js.map +1 -0
  75. package/lib/commonjs/types/TronWeb.d.ts +20 -0
  76. package/lib/commonjs/types/TronWeb.js +3 -0
  77. package/lib/commonjs/types/TronWeb.js.map +1 -0
  78. package/lib/commonjs/types/Trx.d.ts +263 -0
  79. package/lib/commonjs/types/Trx.js +42 -0
  80. package/lib/commonjs/types/Trx.js.map +1 -0
  81. package/lib/commonjs/types/UtilsTypes.d.ts +23 -0
  82. package/lib/commonjs/types/UtilsTypes.js +3 -0
  83. package/lib/commonjs/types/UtilsTypes.js.map +1 -0
  84. package/lib/commonjs/types/index.d.ts +9 -0
  85. package/lib/commonjs/types/index.js +13 -0
  86. package/lib/commonjs/types/index.js.map +1 -0
  87. package/lib/commonjs/utils/abi.d.ts +5 -0
  88. package/lib/commonjs/utils/abi.js +251 -0
  89. package/lib/commonjs/utils/abi.js.map +1 -0
  90. package/lib/commonjs/utils/accounts.d.ts +22 -0
  91. package/lib/commonjs/utils/accounts.js +51 -0
  92. package/lib/commonjs/utils/accounts.js.map +1 -0
  93. package/lib/commonjs/utils/address.d.ts +12 -0
  94. package/lib/commonjs/utils/address.js +91 -0
  95. package/lib/commonjs/utils/address.js.map +1 -0
  96. package/lib/commonjs/utils/base58.d.ts +3 -0
  97. package/lib/commonjs/utils/base58.js +68 -0
  98. package/lib/commonjs/utils/base58.js.map +1 -0
  99. package/lib/commonjs/utils/base64.d.ts +10 -0
  100. package/lib/commonjs/utils/base64.js +173 -0
  101. package/lib/commonjs/utils/base64.js.map +1 -0
  102. package/lib/commonjs/utils/bytes.d.ts +7 -0
  103. package/lib/commonjs/utils/bytes.js +61 -0
  104. package/lib/commonjs/utils/bytes.js.map +1 -0
  105. package/lib/commonjs/utils/code.d.ts +12 -0
  106. package/lib/commonjs/utils/code.js +166 -0
  107. package/lib/commonjs/utils/code.js.map +1 -0
  108. package/lib/commonjs/utils/crypto.d.ts +25 -0
  109. package/lib/commonjs/utils/crypto.js +222 -0
  110. package/lib/commonjs/utils/crypto.js.map +1 -0
  111. package/lib/commonjs/utils/ethersUtils.d.ts +14 -0
  112. package/lib/commonjs/utils/ethersUtils.js +39 -0
  113. package/lib/commonjs/utils/ethersUtils.js.map +1 -0
  114. package/lib/commonjs/utils/fragments.d.ts +465 -0
  115. package/lib/commonjs/utils/fragments.js +1342 -0
  116. package/lib/commonjs/utils/fragments.js.map +1 -0
  117. package/lib/commonjs/utils/help.d.ts +8 -0
  118. package/lib/commonjs/utils/help.js +61 -0
  119. package/lib/commonjs/utils/help.js.map +1 -0
  120. package/lib/commonjs/utils/index.d.ts +56 -0
  121. package/lib/commonjs/utils/index.js +43 -0
  122. package/lib/commonjs/utils/index.js.map +1 -0
  123. package/lib/commonjs/utils/interface.d.ts +381 -0
  124. package/lib/commonjs/utils/interface.js +1121 -0
  125. package/lib/commonjs/utils/interface.js.map +1 -0
  126. package/lib/commonjs/utils/message.d.ts +4 -0
  127. package/lib/commonjs/utils/message.js +38 -0
  128. package/lib/commonjs/utils/message.js.map +1 -0
  129. package/lib/commonjs/utils/transaction.d.ts +17 -0
  130. package/lib/commonjs/utils/transaction.js +678 -0
  131. package/lib/commonjs/utils/transaction.js.map +1 -0
  132. package/lib/commonjs/utils/typedData.d.ts +148 -0
  133. package/lib/commonjs/utils/typedData.js +498 -0
  134. package/lib/commonjs/utils/typedData.js.map +1 -0
  135. package/lib/commonjs/utils/validations.d.ts +22 -0
  136. package/lib/commonjs/utils/validations.js +128 -0
  137. package/lib/commonjs/utils/validations.js.map +1 -0
  138. package/lib/esm/index.d.ts +64 -0
  139. package/lib/esm/index.js +33 -0
  140. package/lib/esm/index.js.map +1 -0
  141. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
  142. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js +1767 -0
  143. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
  144. package/lib/esm/lib/TransactionBuilder/helper.d.ts +28 -0
  145. package/lib/esm/lib/TransactionBuilder/helper.js +112 -0
  146. package/lib/esm/lib/TransactionBuilder/helper.js.map +1 -0
  147. package/lib/esm/lib/contract/index.d.ts +29 -0
  148. package/lib/esm/lib/contract/index.js +118 -0
  149. package/lib/esm/lib/contract/index.js.map +1 -0
  150. package/lib/esm/lib/contract/method.d.ts +50 -0
  151. package/lib/esm/lib/contract/method.js +233 -0
  152. package/lib/esm/lib/contract/method.js.map +1 -0
  153. package/lib/esm/lib/event.d.ts +85 -0
  154. package/lib/esm/lib/event.js +132 -0
  155. package/lib/esm/lib/event.js.map +1 -0
  156. package/lib/esm/lib/plugin.d.ts +28 -0
  157. package/lib/esm/lib/plugin.js +79 -0
  158. package/lib/esm/lib/plugin.js.map +1 -0
  159. package/lib/esm/lib/providers/HttpProvider.d.ts +15 -0
  160. package/lib/esm/lib/providers/HttpProvider.js +59 -0
  161. package/lib/esm/lib/providers/HttpProvider.js.map +1 -0
  162. package/lib/esm/lib/providers/index.d.ts +6 -0
  163. package/lib/esm/lib/providers/index.js +4 -0
  164. package/lib/esm/lib/providers/index.js.map +1 -0
  165. package/lib/esm/lib/trx.d.ts +315 -0
  166. package/lib/esm/lib/trx.js +960 -0
  167. package/lib/esm/lib/trx.js.map +1 -0
  168. package/lib/esm/package.json +1 -0
  169. package/lib/esm/paramValidator/index.d.ts +19 -0
  170. package/lib/esm/paramValidator/index.js +106 -0
  171. package/lib/esm/paramValidator/index.js.map +1 -0
  172. package/lib/esm/protocol/core/Tron_pb.cjs +7524 -0
  173. package/lib/esm/protocol/core/contract/account_contract_pb.cjs +961 -0
  174. package/lib/esm/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  175. package/lib/esm/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  176. package/lib/esm/protocol/core/contract/common_pb.cjs +34 -0
  177. package/lib/esm/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  178. package/lib/esm/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  179. package/lib/esm/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  180. package/lib/esm/protocol/core/contract/storage_contract_pb.cjs +848 -0
  181. package/lib/esm/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  182. package/lib/esm/protocol/core/contract/witness_contract_pb.cjs +956 -0
  183. package/lib/esm/tronweb.d.ts +111 -0
  184. package/lib/esm/tronweb.js +360 -0
  185. package/lib/esm/tronweb.js.map +1 -0
  186. package/lib/esm/types/ABI.d.ts +59 -0
  187. package/lib/esm/types/ABI.js +2 -0
  188. package/lib/esm/types/ABI.js.map +1 -0
  189. package/lib/esm/types/APIResponse.d.ts +31 -0
  190. package/lib/esm/types/APIResponse.js +2 -0
  191. package/lib/esm/types/APIResponse.js.map +1 -0
  192. package/lib/esm/types/Contract.d.ts +287 -0
  193. package/lib/esm/types/Contract.js +60 -0
  194. package/lib/esm/types/Contract.js.map +1 -0
  195. package/lib/esm/types/Providers.d.ts +8 -0
  196. package/lib/esm/types/Providers.js +2 -0
  197. package/lib/esm/types/Providers.js.map +1 -0
  198. package/lib/esm/types/Transaction.d.ts +65 -0
  199. package/lib/esm/types/Transaction.js +2 -0
  200. package/lib/esm/types/Transaction.js.map +1 -0
  201. package/lib/esm/types/TransactionBuilder.d.ts +270 -0
  202. package/lib/esm/types/TransactionBuilder.js +2 -0
  203. package/lib/esm/types/TransactionBuilder.js.map +1 -0
  204. package/lib/esm/types/TronWeb.d.ts +20 -0
  205. package/lib/esm/types/TronWeb.js +2 -0
  206. package/lib/esm/types/TronWeb.js.map +1 -0
  207. package/lib/esm/types/Trx.d.ts +263 -0
  208. package/lib/esm/types/Trx.js +39 -0
  209. package/lib/esm/types/Trx.js.map +1 -0
  210. package/lib/esm/types/UtilsTypes.d.ts +23 -0
  211. package/lib/esm/types/UtilsTypes.js +2 -0
  212. package/lib/esm/types/UtilsTypes.js.map +1 -0
  213. package/lib/esm/types/index.d.ts +9 -0
  214. package/lib/esm/types/index.js +10 -0
  215. package/lib/esm/types/index.js.map +1 -0
  216. package/lib/esm/utils/abi.d.ts +5 -0
  217. package/lib/esm/utils/abi.js +245 -0
  218. package/lib/esm/utils/abi.js.map +1 -0
  219. package/lib/esm/utils/accounts.d.ts +22 -0
  220. package/lib/esm/utils/accounts.js +46 -0
  221. package/lib/esm/utils/accounts.js.map +1 -0
  222. package/lib/esm/utils/address.d.ts +12 -0
  223. package/lib/esm/utils/address.js +82 -0
  224. package/lib/esm/utils/address.js.map +1 -0
  225. package/lib/esm/utils/base58.d.ts +3 -0
  226. package/lib/esm/utils/base58.js +64 -0
  227. package/lib/esm/utils/base58.js.map +1 -0
  228. package/lib/esm/utils/base64.d.ts +10 -0
  229. package/lib/esm/utils/base64.js +169 -0
  230. package/lib/esm/utils/base64.js.map +1 -0
  231. package/lib/esm/utils/bytes.d.ts +7 -0
  232. package/lib/esm/utils/bytes.js +53 -0
  233. package/lib/esm/utils/bytes.js.map +1 -0
  234. package/lib/esm/utils/code.d.ts +12 -0
  235. package/lib/esm/utils/code.js +149 -0
  236. package/lib/esm/utils/code.js.map +1 -0
  237. package/lib/esm/utils/crypto.d.ts +25 -0
  238. package/lib/esm/utils/crypto.js +198 -0
  239. package/lib/esm/utils/crypto.js.map +1 -0
  240. package/lib/esm/utils/ethersUtils.d.ts +14 -0
  241. package/lib/esm/utils/ethersUtils.js +17 -0
  242. package/lib/esm/utils/ethersUtils.js.map +1 -0
  243. package/lib/esm/utils/fragments.d.ts +465 -0
  244. package/lib/esm/utils/fragments.js +1330 -0
  245. package/lib/esm/utils/fragments.js.map +1 -0
  246. package/lib/esm/utils/help.d.ts +8 -0
  247. package/lib/esm/utils/help.js +51 -0
  248. package/lib/esm/utils/help.js.map +1 -0
  249. package/lib/esm/utils/index.d.ts +56 -0
  250. package/lib/esm/utils/index.js +40 -0
  251. package/lib/esm/utils/index.js.map +1 -0
  252. package/lib/esm/utils/interface.d.ts +381 -0
  253. package/lib/esm/utils/interface.js +1112 -0
  254. package/lib/esm/utils/interface.js.map +1 -0
  255. package/lib/esm/utils/message.d.ts +4 -0
  256. package/lib/esm/utils/message.js +32 -0
  257. package/lib/esm/utils/message.js.map +1 -0
  258. package/lib/esm/utils/transaction.d.ts +17 -0
  259. package/lib/esm/utils/transaction.js +669 -0
  260. package/lib/esm/utils/transaction.js.map +1 -0
  261. package/lib/esm/utils/typedData.d.ts +148 -0
  262. package/lib/esm/utils/typedData.js +493 -0
  263. package/lib/esm/utils/typedData.js.map +1 -0
  264. package/lib/esm/utils/validations.d.ts +22 -0
  265. package/lib/esm/utils/validations.js +108 -0
  266. package/lib/esm/utils/validations.js.map +1 -0
  267. package/package.json +110 -0
  268. package/src/index.ts +42 -0
  269. package/src/lib/TransactionBuilder/TransactionBuilder.ts +2272 -0
  270. package/src/lib/TransactionBuilder/helper.ts +139 -0
  271. package/src/lib/contract/index.ts +151 -0
  272. package/src/lib/contract/method.ts +336 -0
  273. package/src/lib/event.ts +248 -0
  274. package/src/lib/plugin.ts +102 -0
  275. package/src/lib/providers/HttpProvider.ts +66 -0
  276. package/src/lib/providers/index.ts +6 -0
  277. package/src/lib/trx.ts +1463 -0
  278. package/src/paramValidator/index.ts +136 -0
  279. package/src/protocol/core/Tron_pb.cjs +7524 -0
  280. package/src/protocol/core/contract/account_contract_pb.cjs +961 -0
  281. package/src/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  282. package/src/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  283. package/src/protocol/core/contract/common_pb.cjs +34 -0
  284. package/src/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  285. package/src/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  286. package/src/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  287. package/src/protocol/core/contract/storage_contract_pb.cjs +848 -0
  288. package/src/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  289. package/src/protocol/core/contract/witness_contract_pb.cjs +956 -0
  290. package/src/tronweb.ts +437 -0
  291. package/src/types/ABI.ts +82 -0
  292. package/src/types/APIResponse.ts +34 -0
  293. package/src/types/Contract.ts +352 -0
  294. package/src/types/Providers.ts +7 -0
  295. package/src/types/Transaction.ts +70 -0
  296. package/src/types/TransactionBuilder.ts +281 -0
  297. package/src/types/TronWeb.ts +23 -0
  298. package/src/types/Trx.ts +285 -0
  299. package/src/types/UtilsTypes.ts +25 -0
  300. package/src/types/index.ts +9 -0
  301. package/src/types/interface.d.ts +1 -0
  302. package/src/utils/abi.ts +257 -0
  303. package/src/utils/accounts.ts +60 -0
  304. package/src/utils/address.ts +89 -0
  305. package/src/utils/base58.ts +78 -0
  306. package/src/utils/base64.ts +200 -0
  307. package/src/utils/bytes.ts +68 -0
  308. package/src/utils/code.ts +172 -0
  309. package/src/utils/crypto.ts +264 -0
  310. package/src/utils/ethersUtils.ts +60 -0
  311. package/src/utils/fragments.ts +1738 -0
  312. package/src/utils/help.ts +59 -0
  313. package/src/utils/index.ts +42 -0
  314. package/src/utils/interface.ts +1362 -0
  315. package/src/utils/message.ts +38 -0
  316. package/src/utils/transaction.ts +991 -0
  317. package/src/utils/typedData.ts +688 -0
  318. package/src/utils/validations.ts +130 -0
@@ -0,0 +1,688 @@
1
+ import { TronWeb } from '../tronweb.js';
2
+ import {
3
+ keccak256,
4
+ recoverAddress,
5
+ concat,
6
+ defineProperties,
7
+ getBigInt,
8
+ getBytes,
9
+ hexlify,
10
+ isHexString,
11
+ mask,
12
+ toBeHex,
13
+ toQuantity,
14
+ toTwos,
15
+ zeroPadValue,
16
+ assertArgument,
17
+ id,
18
+ } from 'ethers';
19
+ import type { BigNumberish, BytesLike, SignatureLike } from 'ethers';
20
+
21
+ import { ADDRESS_PREFIX_REGEX } from './address.js';
22
+
23
+ function getAddress(address: string) {
24
+ return TronWeb.address.toHex(address).replace(ADDRESS_PREFIX_REGEX, '0x');
25
+ }
26
+ function getTronAddress(address: string) {
27
+ return TronWeb.address.toHex(address);
28
+ }
29
+
30
+ const padding = new Uint8Array(32);
31
+ padding.fill(0);
32
+
33
+ const BN__1 = BigInt(-1);
34
+ const BN_0 = BigInt(0);
35
+ const BN_1 = BigInt(1);
36
+ const BN_MAX_UINT256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
37
+
38
+ // @TODO: in v7, verifyingContract should be an AddressLike and use resolveAddress
39
+
40
+ /**
41
+ * The domain for an [[link-eip-712]] payload.
42
+ */
43
+ export interface TypedDataDomain {
44
+ /**
45
+ * The human-readable name of the signing domain.
46
+ */
47
+ name?: null | string;
48
+
49
+ /**
50
+ * The major version of the signing domain.
51
+ */
52
+ version?: null | string;
53
+
54
+ /**
55
+ * The chain ID of the signing domain.
56
+ */
57
+ chainId?: null | BigNumberish;
58
+
59
+ /**
60
+ * The the address of the contract that will verify the signature.
61
+ */
62
+ verifyingContract?: null | string;
63
+
64
+ /**
65
+ * A salt used for purposes decided by the specific domain.
66
+ */
67
+ salt?: null | BytesLike;
68
+ }
69
+
70
+ /**
71
+ * A specific field of a structured [[link-eip-712]] type.
72
+ */
73
+ export interface TypedDataField {
74
+ /**
75
+ * The field name.
76
+ */
77
+ name: string;
78
+
79
+ /**
80
+ * The type of the field.
81
+ */
82
+ type: string;
83
+ }
84
+
85
+ function hexPadRight(value: BytesLike): string {
86
+ const bytes = getBytes(value);
87
+ const padOffset = bytes.length % 32;
88
+ if (padOffset) {
89
+ return concat([bytes, padding.slice(padOffset)]);
90
+ }
91
+ return hexlify(bytes);
92
+ }
93
+
94
+ const hexTrue = toBeHex(BN_1, 32);
95
+ const hexFalse = toBeHex(BN_0, 32);
96
+
97
+ const domainFieldTypes: Record<string, string> = {
98
+ name: 'string',
99
+ version: 'string',
100
+ chainId: 'uint256',
101
+ verifyingContract: 'address',
102
+ salt: 'bytes32',
103
+ };
104
+
105
+ const domainFieldNames: Array<string> = ['name', 'version', 'chainId', 'verifyingContract', 'salt'];
106
+
107
+ function checkString(key: string): (value: any) => string {
108
+ return function (value: any) {
109
+ assertArgument(typeof value === 'string', `invalid domain value for ${JSON.stringify(key)}`, `domain.${key}`, value);
110
+ return value;
111
+ };
112
+ }
113
+
114
+ const domainChecks: Record<string, (value: any) => any> = {
115
+ name: checkString('name'),
116
+ version: checkString('version'),
117
+ chainId: function (_value: any) {
118
+ const value = getBigInt(_value, 'domain.chainId');
119
+ assertArgument(value >= 0, 'invalid chain ID', 'domain.chainId', _value);
120
+ if (Number.isSafeInteger(value)) {
121
+ return Number(value);
122
+ }
123
+ return toQuantity(value);
124
+ },
125
+ verifyingContract: function (value: any) {
126
+ try {
127
+ return getTronAddress(value).toLowerCase();
128
+ } catch (error) {
129
+ //
130
+ }
131
+ assertArgument(false, `invalid domain value "verifyingContract"`, 'domain.verifyingContract', value);
132
+ },
133
+ salt: function (value: any) {
134
+ const bytes = getBytes(value, 'domain.salt');
135
+ assertArgument(bytes.length === 32, `invalid domain value "salt"`, 'domain.salt', value);
136
+ return hexlify(bytes);
137
+ },
138
+ };
139
+
140
+ function getBaseEncoder(type: string): null | ((value: any) => string) {
141
+ // intXX and uintXX
142
+ {
143
+ const match = type.match(/^(u?)int(\d*)$/);
144
+ if (match) {
145
+ const signed = match[1] === '';
146
+
147
+ const width = parseInt(match[2] || '256');
148
+ assertArgument(
149
+ width % 8 === 0 && width !== 0 && width <= 256 && (match[2] == null || match[2] === String(width)),
150
+ 'invalid numeric width',
151
+ 'type',
152
+ type
153
+ );
154
+
155
+ const boundsUpper = mask(BN_MAX_UINT256, signed ? width - 1 : width);
156
+ const boundsLower = signed ? (boundsUpper + BN_1) * BN__1 : BN_0;
157
+
158
+ return function (_value: BigNumberish) {
159
+ const value = getBigInt(_value, 'value');
160
+
161
+ assertArgument(value >= boundsLower && value <= boundsUpper, `value out-of-bounds for ${type}`, 'value', value);
162
+
163
+ return toBeHex(signed ? toTwos(value, 256) : value, 32);
164
+ };
165
+ }
166
+ }
167
+
168
+ // bytesXX
169
+ {
170
+ const match = type.match(/^bytes(\d+)$/);
171
+ if (match) {
172
+ const width = parseInt(match[1]);
173
+ assertArgument(width !== 0 && width <= 32 && match[1] === String(width), 'invalid bytes width', 'type', type);
174
+
175
+ return function (value: BytesLike) {
176
+ const bytes = getBytes(value);
177
+ assertArgument(bytes.length === width, `invalid length for ${type}`, 'value', value);
178
+ return hexPadRight(value);
179
+ };
180
+ }
181
+ }
182
+
183
+ switch (type) {
184
+ case 'trcToken':
185
+ return getBaseEncoder('uint256');
186
+ case 'address':
187
+ return function (value: string) {
188
+ return zeroPadValue(getAddress(value), 32);
189
+ };
190
+ case 'bool':
191
+ return function (value: boolean) {
192
+ return !value ? hexFalse : hexTrue;
193
+ };
194
+ case 'bytes':
195
+ return function (value: BytesLike) {
196
+ return keccak256(value);
197
+ };
198
+ case 'string':
199
+ return function (value: string) {
200
+ return id(value);
201
+ };
202
+ }
203
+
204
+ return null;
205
+ }
206
+
207
+ function encodeType(name: string, fields: Array<TypedDataField>): string {
208
+ return `${name}(${fields.map(({ name, type }) => type + ' ' + name).join(',')})`;
209
+ }
210
+
211
+ /**
212
+ * A **TypedDataEncode** prepares and encodes [[link-eip-712]] payloads
213
+ * for signed typed data.
214
+ *
215
+ * This is useful for those that wish to compute various components of a
216
+ * typed data hash, primary types, or sub-components, but generally the
217
+ * higher level [[Signer-signTypedData]] is more useful.
218
+ */
219
+ export class TypedDataEncoder {
220
+ /**
221
+ * The primary type for the structured [[types]].
222
+ *
223
+ * This is derived automatically from the [[types]], since no
224
+ * recursion is possible, once the DAG for the types is consturcted
225
+ * internally, the primary type must be the only remaining type with
226
+ * no parent nodes.
227
+ */
228
+ readonly primaryType!: string;
229
+
230
+ readonly #types: string;
231
+
232
+ /**
233
+ * The types.
234
+ */
235
+ get types(): Record<string, Array<TypedDataField>> {
236
+ return JSON.parse(this.#types);
237
+ }
238
+
239
+ readonly #fullTypes: Map<string, string>;
240
+
241
+ readonly #encoderCache: Map<string, (value: any) => string>;
242
+
243
+ /**
244
+ * Create a new **TypedDataEncoder** for %%types%%.
245
+ *
246
+ * This performs all necessary checking that types are valid and
247
+ * do not violate the [[link-eip-712]] structural constraints as
248
+ * well as computes the [[primaryType]].
249
+ */
250
+ constructor(types: Record<string, Array<TypedDataField>>) {
251
+ this.#types = JSON.stringify(types);
252
+ this.#fullTypes = new Map();
253
+ this.#encoderCache = new Map();
254
+
255
+ // Link struct types to their direct child structs
256
+ const links: Map<string, Set<string>> = new Map();
257
+
258
+ // Link structs to structs which contain them as a child
259
+ const parents: Map<string, Array<string>> = new Map();
260
+
261
+ // Link all subtypes within a given struct
262
+ const subtypes: Map<string, Set<string>> = new Map();
263
+
264
+ Object.keys(types).forEach((type) => {
265
+ links.set(type, new Set());
266
+ parents.set(type, []);
267
+ subtypes.set(type, new Set());
268
+ });
269
+
270
+ for (const name in types) {
271
+ const uniqueNames: Set<string> = new Set();
272
+
273
+ for (const field of types[name]) {
274
+ // Check each field has a unique name
275
+ assertArgument(
276
+ !uniqueNames.has(field.name),
277
+ `duplicate variable name ${JSON.stringify(field.name)} in ${JSON.stringify(name)}`,
278
+ 'types',
279
+ types
280
+ );
281
+ uniqueNames.add(field.name);
282
+
283
+ // Get the base type (drop any array specifiers)
284
+ const baseType = (<any>field.type.match(/^([^\x5b]*)(\x5b|$)/))[1] || null;
285
+ assertArgument(baseType !== name, `circular type reference to ${JSON.stringify(baseType)}`, 'types', types);
286
+
287
+ // Is this a base encoding type?
288
+ const encoder = getBaseEncoder(baseType);
289
+ if (encoder) {
290
+ continue;
291
+ }
292
+
293
+ assertArgument(parents.has(baseType), `unknown type ${JSON.stringify(baseType)}`, 'types', types);
294
+
295
+ // Add linkage
296
+ (parents.get(baseType) as Array<string>).push(name);
297
+ (links.get(name) as Set<string>).add(baseType);
298
+ }
299
+ }
300
+
301
+ // Deduce the primary type
302
+ const primaryTypes = Array.from(parents.keys()).filter((n) => (parents.get(n) as Array<string>).length === 0);
303
+ assertArgument(primaryTypes.length !== 0, 'missing primary type', 'types', types);
304
+ assertArgument(
305
+ primaryTypes.length === 1,
306
+ `ambiguous primary types or unused types: ${primaryTypes.map((t) => JSON.stringify(t)).join(', ')}`,
307
+ 'types',
308
+ types
309
+ );
310
+
311
+ defineProperties<TypedDataEncoder>(this, { primaryType: primaryTypes[0] });
312
+
313
+ // Check for circular type references
314
+ function checkCircular(type: string, found: Set<string>) {
315
+ assertArgument(!found.has(type), `circular type reference to ${JSON.stringify(type)}`, 'types', types);
316
+
317
+ found.add(type);
318
+
319
+ for (const child of links.get(type) as Set<string>) {
320
+ if (!parents.has(child)) {
321
+ continue;
322
+ }
323
+
324
+ // Recursively check children
325
+ checkCircular(child, found);
326
+
327
+ // Mark all ancestors as having this decendant
328
+ for (const subtype of found) {
329
+ (subtypes.get(subtype) as Set<string>).add(child);
330
+ }
331
+ }
332
+
333
+ found.delete(type);
334
+ }
335
+ checkCircular(this.primaryType, new Set());
336
+
337
+ // Compute each fully describe type
338
+ for (const [name, set] of subtypes) {
339
+ const st = Array.from(set);
340
+ st.sort();
341
+ this.#fullTypes.set(name, encodeType(name, types[name]) + st.map((t) => encodeType(t, types[t])).join(''));
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Returnthe encoder for the specific %%type%%.
347
+ */
348
+ getEncoder(type: string): (value: any) => string {
349
+ let encoder = this.#encoderCache.get(type);
350
+ if (!encoder) {
351
+ encoder = this.#getEncoder(type);
352
+ this.#encoderCache.set(type, encoder);
353
+ }
354
+ return encoder;
355
+ }
356
+
357
+ #getEncoder(type: string): (value: any) => string {
358
+ // Basic encoder type (address, bool, uint256, etc)
359
+ {
360
+ const encoder = getBaseEncoder(type);
361
+ if (encoder) {
362
+ return encoder;
363
+ }
364
+ }
365
+
366
+ // Array
367
+ const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
368
+ if (match) {
369
+ const subtype = match[1];
370
+ const subEncoder = this.getEncoder(subtype);
371
+ return (value: Array<any>) => {
372
+ assertArgument(
373
+ !match[3] || parseInt(match[3]) === value.length,
374
+ `array length mismatch; expected length ${parseInt(match[3])}`,
375
+ 'value',
376
+ value
377
+ );
378
+
379
+ let result = value.map(subEncoder);
380
+ if (this.#fullTypes.has(subtype)) {
381
+ result = result.map(keccak256);
382
+ }
383
+
384
+ return keccak256(concat(result));
385
+ };
386
+ }
387
+
388
+ // Struct
389
+ const fields = this.types[type];
390
+ if (fields) {
391
+ const encodedType = id(this.#fullTypes.get(type) as string);
392
+ return (value: Record<string, any>) => {
393
+ const values = fields.map(({ name, type }) => {
394
+ const result = this.getEncoder(type)(value[name]);
395
+ if (this.#fullTypes.has(type)) {
396
+ return keccak256(result);
397
+ }
398
+ return result;
399
+ });
400
+ values.unshift(encodedType);
401
+ return concat(values);
402
+ };
403
+ }
404
+
405
+ assertArgument(false, `unknown type: ${type}`, 'type', type);
406
+ }
407
+
408
+ /**
409
+ * Return the full type for %%name%%.
410
+ */
411
+ encodeType(name: string): string {
412
+ const result = this.#fullTypes.get(name);
413
+ assertArgument(result, `unknown type: ${JSON.stringify(name)}`, 'name', name);
414
+ return result!;
415
+ }
416
+
417
+ /**
418
+ * Return the encoded %%value%% for the %%type%%.
419
+ */
420
+ encodeData(type: string, value: any): string {
421
+ return this.getEncoder(type)(value);
422
+ }
423
+
424
+ /**
425
+ * Returns the hash of %%value%% for the type of %%name%%.
426
+ */
427
+ hashStruct(name: string, value: Record<string, any>): string {
428
+ return keccak256(this.encodeData(name, value));
429
+ }
430
+
431
+ /**
432
+ * Return the fulled encoded %%value%% for the [[types]].
433
+ */
434
+ encode(value: Record<string, any>): string {
435
+ return this.encodeData(this.primaryType, value);
436
+ }
437
+
438
+ /**
439
+ * Return the hash of the fully encoded %%value%% for the [[types]].
440
+ */
441
+ hash(value: Record<string, any>): string {
442
+ return this.hashStruct(this.primaryType, value);
443
+ }
444
+
445
+ /**
446
+ * @_ignore:
447
+ */
448
+ _visit(type: string, value: any, callback: (type: string, data: any) => any): any {
449
+ // Basic encoder type (address, bool, uint256, etc)
450
+ {
451
+ const encoder = getBaseEncoder(type);
452
+ if (encoder) {
453
+ return callback(type, value);
454
+ }
455
+ }
456
+
457
+ // Array
458
+ const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
459
+ if (match) {
460
+ assertArgument(
461
+ !match[3] || parseInt(match[3]) === value.length,
462
+ `array length mismatch; expected length ${parseInt(match[3])}`,
463
+ 'value',
464
+ value
465
+ );
466
+ return value.map((v: any) => this._visit(match[1], v, callback));
467
+ }
468
+
469
+ // Struct
470
+ const fields = this.types[type];
471
+ if (fields) {
472
+ return fields.reduce((accum, { name, type }) => {
473
+ accum[name] = this._visit(type, value[name], callback);
474
+ return accum;
475
+ }, <Record<string, any>>{});
476
+ }
477
+
478
+ assertArgument(false, `unknown type: ${type}`, 'type', type);
479
+ }
480
+
481
+ /**
482
+ * Call %%calback%% for each value in %%value%%, passing the type and
483
+ * component within %%value%%.
484
+ *
485
+ * This is useful for replacing addresses or other transformation that
486
+ * may be desired on each component, based on its type.
487
+ */
488
+ visit(value: Record<string, any>, callback: (type: string, data: any) => any): any {
489
+ return this._visit(this.primaryType, value, callback);
490
+ }
491
+
492
+ /**
493
+ * Create a new **TypedDataEncoder** for %%types%%.
494
+ */
495
+ static from(types: Record<string, Array<TypedDataField>>): TypedDataEncoder {
496
+ return new TypedDataEncoder(types);
497
+ }
498
+
499
+ /**
500
+ * Return the primary type for %%types%%.
501
+ */
502
+ static getPrimaryType(types: Record<string, Array<TypedDataField>>): string {
503
+ return TypedDataEncoder.from(types).primaryType;
504
+ }
505
+
506
+ /**
507
+ * Return the hashed struct for %%value%% using %%types%% and %%name%%.
508
+ */
509
+ static hashStruct(name: string, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string {
510
+ return TypedDataEncoder.from(types).hashStruct(name, value);
511
+ }
512
+
513
+ /**
514
+ * Return the domain hash for %%domain%%.
515
+ */
516
+ static hashDomain(domain: TypedDataDomain): string {
517
+ const domainFields: Array<TypedDataField> = [];
518
+ for (const name in domain) {
519
+ if ((<Record<string, any>>domain)[name] == null) {
520
+ continue;
521
+ }
522
+ const type = domainFieldTypes[name];
523
+ assertArgument(type, `invalid typed-data domain key: ${JSON.stringify(name)}`, 'domain', domain);
524
+ domainFields.push({ name, type });
525
+ }
526
+
527
+ domainFields.sort((a, b) => {
528
+ return domainFieldNames.indexOf(a.name) - domainFieldNames.indexOf(b.name);
529
+ });
530
+
531
+ return TypedDataEncoder.hashStruct('EIP712Domain', { EIP712Domain: domainFields }, domain);
532
+ }
533
+
534
+ /**
535
+ * Return the fully encoded [[link-eip-712]] %%value%% for %%types%% with %%domain%%.
536
+ */
537
+ static encode(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string {
538
+ return concat(['0x1901', TypedDataEncoder.hashDomain(domain), TypedDataEncoder.from(types).hash(value)]);
539
+ }
540
+
541
+ /**
542
+ * Return the hash of the fully encoded [[link-eip-712]] %%value%% for %%types%% with %%domain%%.
543
+ */
544
+ static hash(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string {
545
+ return keccak256(TypedDataEncoder.encode(domain, types, value));
546
+ }
547
+
548
+ // Replaces all address types with ENS names with their looked up address
549
+ /**
550
+ * Resolves to the value from resolving all addresses in %%value%% for
551
+ * %%types%% and the %%domain%%.
552
+ */
553
+ static async resolveNames(
554
+ domain: TypedDataDomain,
555
+ types: Record<string, Array<TypedDataField>>,
556
+ value: Record<string, any>,
557
+ resolveName: (name: string) => Promise<string>
558
+ ): Promise<{ domain: TypedDataDomain; value: any }> {
559
+ // Make a copy to isolate it from the object passed in
560
+ domain = Object.assign({}, domain);
561
+
562
+ // Allow passing null to ignore value
563
+ for (const key in domain) {
564
+ if ((<Record<string, any>>domain)[key] == null) {
565
+ delete (<Record<string, any>>domain)[key];
566
+ }
567
+ }
568
+
569
+ // Look up all ENS names
570
+ const ensCache: Record<string, string> = {};
571
+
572
+ // Do we need to look up the domain's verifyingContract?
573
+ if (domain.verifyingContract && !isHexString(domain.verifyingContract, 20)) {
574
+ ensCache[domain.verifyingContract] = '0x';
575
+ }
576
+
577
+ // We are going to use the encoder to visit all the base values
578
+ const encoder = TypedDataEncoder.from(types);
579
+
580
+ // Get a list of all the addresses
581
+ encoder.visit(value, (type: string, value: any) => {
582
+ if (type === 'address' && !isHexString(value, 20)) {
583
+ ensCache[value] = '0x';
584
+ }
585
+ return value;
586
+ });
587
+
588
+ // Lookup each name
589
+ for (const name in ensCache) {
590
+ ensCache[name] = await resolveName(name);
591
+ }
592
+
593
+ // Replace the domain verifyingContract if needed
594
+ if (domain.verifyingContract && ensCache[domain.verifyingContract]) {
595
+ domain.verifyingContract = ensCache[domain.verifyingContract];
596
+ }
597
+
598
+ // Replace all ENS names with their address
599
+ value = encoder.visit(value, (type: string, value: any) => {
600
+ if (type === 'address' && ensCache[value]) {
601
+ return ensCache[value];
602
+ }
603
+ return value;
604
+ });
605
+
606
+ return { domain, value };
607
+ }
608
+
609
+ /**
610
+ * Returns the JSON-encoded payload expected by nodes which implement
611
+ * the JSON-RPC [[link-eip-712]] method.
612
+ */
613
+ static getPayload(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): any {
614
+ // Validate the domain fields
615
+ TypedDataEncoder.hashDomain(domain);
616
+
617
+ // Derive the EIP712Domain Struct reference type
618
+ const domainValues: Record<string, any> = {};
619
+ const domainTypes: Array<{ name: string; type: string }> = [];
620
+
621
+ domainFieldNames.forEach((name) => {
622
+ const value = (<any>domain)[name];
623
+ if (value == null) {
624
+ return;
625
+ }
626
+ domainValues[name] = domainChecks[name](value);
627
+ domainTypes.push({ name, type: domainFieldTypes[name] });
628
+ });
629
+
630
+ const encoder = TypedDataEncoder.from(types);
631
+
632
+ const typesWithDomain = Object.assign({}, types);
633
+ assertArgument(
634
+ typesWithDomain.EIP712Domain == null,
635
+ 'types must not contain EIP712Domain type',
636
+ 'types.EIP712Domain',
637
+ types
638
+ );
639
+
640
+ typesWithDomain.EIP712Domain = domainTypes;
641
+
642
+ // Validate the data structures and types
643
+ encoder.encode(value);
644
+
645
+ return {
646
+ types: typesWithDomain,
647
+ domain: domainValues,
648
+ primaryType: encoder.primaryType,
649
+ message: encoder.visit(value, (type: string, value: any) => {
650
+ // bytes
651
+ if (type.match(/^bytes(\d*)/)) {
652
+ return hexlify(getBytes(value));
653
+ }
654
+
655
+ // uint or int
656
+ if (type.match(/^u?int/)) {
657
+ return getBigInt(value).toString();
658
+ }
659
+
660
+ switch (type) {
661
+ case 'trcToken':
662
+ return getBigInt(value).toString();
663
+ case 'address':
664
+ return value.toLowerCase();
665
+ case 'bool':
666
+ return !!value;
667
+ case 'string':
668
+ assertArgument(typeof value === 'string', 'invalid string', 'value', value);
669
+ return value;
670
+ }
671
+
672
+ assertArgument(false, 'unsupported type', 'type', type);
673
+ }),
674
+ };
675
+ }
676
+ }
677
+
678
+ /**
679
+ * Compute the address used to sign the typed data for the %%signature%%.
680
+ */
681
+ export function verifyTypedData(
682
+ domain: TypedDataDomain,
683
+ types: Record<string, Array<TypedDataField>>,
684
+ value: Record<string, any>,
685
+ signature: SignatureLike
686
+ ): string {
687
+ return recoverAddress(TypedDataEncoder.hash(domain, types, value), signature);
688
+ }