tronweb.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TronWeb = void 0;
  4. const tslib_1 = require("tslib");
  5. const index_js_1 = require("./lib/providers/index.js");
  6. const index_js_2 = tslib_1.__importDefault(require("./utils/index.js"));
  7. const bignumber_js_1 = require("bignumber.js");
  8. const eventemitter3_1 = tslib_1.__importDefault(require("eventemitter3"));
  9. const semver_1 = tslib_1.__importDefault(require("semver"));
  10. const TransactionBuilder_js_1 = require("./lib/TransactionBuilder/TransactionBuilder.js");
  11. const trx_js_1 = require("./lib/trx.js");
  12. const index_js_3 = require("./lib/contract/index.js");
  13. const plugin_js_1 = require("./lib/plugin.js");
  14. const event_js_1 = require("./lib/event.js");
  15. const ethersUtils_js_1 = require("./utils/ethersUtils.js");
  16. const address_js_1 = require("./utils/address.js");
  17. const validations_js_1 = require("./utils/validations.js");
  18. const DEFAULT_VERSION = '4.7.1';
  19. const FEE_LIMIT = 150000000;
  20. const version = '6.0.3';
  21. function isValidOptions(options) {
  22. return (!!options &&
  23. typeof options === 'object' &&
  24. (!!options.fullNode || !!options.fullHost));
  25. }
  26. class TronWeb extends eventemitter3_1.default {
  27. providers;
  28. BigNumber;
  29. transactionBuilder;
  30. trx;
  31. plugin;
  32. event;
  33. version;
  34. static version = version;
  35. utils;
  36. defaultBlock;
  37. defaultPrivateKey;
  38. defaultAddress;
  39. fullnodeVersion;
  40. feeLimit;
  41. fullNode;
  42. solidityNode;
  43. eventServer;
  44. constructor(options, solidityNode = '', eventServer, privateKey = '') {
  45. super();
  46. let fullNode;
  47. let headers = false;
  48. let eventHeaders = false;
  49. if (isValidOptions(options)) {
  50. fullNode = options.fullNode || options.fullHost;
  51. solidityNode = (options.solidityNode || options.fullHost);
  52. eventServer = (options.eventServer || options.fullHost);
  53. headers = options.headers || false;
  54. eventHeaders = options.eventHeaders || headers;
  55. privateKey = options.privateKey;
  56. }
  57. else {
  58. fullNode = options;
  59. }
  60. if (index_js_2.default.isString(fullNode))
  61. fullNode = new index_js_1.providers.HttpProvider(fullNode);
  62. if (index_js_2.default.isString(solidityNode))
  63. solidityNode = new index_js_1.providers.HttpProvider(solidityNode);
  64. if (index_js_2.default.isString(eventServer))
  65. eventServer = new index_js_1.providers.HttpProvider(eventServer);
  66. this.event = new event_js_1.Event(this);
  67. this.transactionBuilder = new TransactionBuilder_js_1.TransactionBuilder(this);
  68. this.trx = new trx_js_1.Trx(this);
  69. this.plugin = new plugin_js_1.Plugin(this, {
  70. disablePlugins: isValidOptions(options) ? options.disablePlugins : false,
  71. });
  72. this.utils = index_js_2.default;
  73. this.setFullNode(fullNode);
  74. this.setSolidityNode(solidityNode);
  75. this.setEventServer(eventServer);
  76. this.providers = index_js_1.providers;
  77. this.BigNumber = bignumber_js_1.BigNumber;
  78. this.defaultBlock = false;
  79. this.defaultPrivateKey = false;
  80. this.defaultAddress = {
  81. hex: false,
  82. base58: false,
  83. };
  84. this.version = TronWeb.version;
  85. this.sha3 = TronWeb.sha3;
  86. this.fromUtf8 = TronWeb.fromUtf8;
  87. this.address = TronWeb.address;
  88. this.toAscii = TronWeb.toAscii;
  89. this.toUtf8 = TronWeb.toUtf8;
  90. this.isAddress = TronWeb.isAddress;
  91. this.fromAscii = TronWeb.fromAscii;
  92. this.toHex = TronWeb.toHex;
  93. this.toBigNumber = TronWeb.toBigNumber;
  94. this.toDecimal = TronWeb.toDecimal;
  95. this.fromDecimal = TronWeb.fromDecimal;
  96. this.toSun = TronWeb.toSun;
  97. this.fromSun = TronWeb.fromSun;
  98. this.createAccount = TronWeb.createAccount;
  99. this.createRandom = TronWeb.createRandom;
  100. this.fromMnemonic = TronWeb.fromMnemonic;
  101. if (privateKey)
  102. this.setPrivateKey(privateKey);
  103. this.fullnodeVersion = DEFAULT_VERSION;
  104. this.feeLimit = FEE_LIMIT;
  105. if (headers) {
  106. this.setFullNodeHeader(headers);
  107. }
  108. if (eventHeaders) {
  109. this.setEventHeader(eventHeaders);
  110. }
  111. }
  112. async getFullnodeVersion() {
  113. try {
  114. const nodeInfo = await this.trx.getNodeInfo();
  115. this.fullnodeVersion = nodeInfo.configNodeInfo.codeVersion;
  116. if (this.fullnodeVersion.split('.').length === 2) {
  117. this.fullnodeVersion += '.0';
  118. }
  119. }
  120. catch (err) {
  121. this.fullnodeVersion = DEFAULT_VERSION;
  122. }
  123. }
  124. setDefaultBlock(blockID = false) {
  125. if ([false, 'latest', 'earliest', 0].includes(blockID)) {
  126. return (this.defaultBlock = blockID);
  127. }
  128. if (!index_js_2.default.isInteger(blockID) || !blockID)
  129. throw new Error('Invalid block ID provided');
  130. return (this.defaultBlock = Math.abs(blockID));
  131. }
  132. setPrivateKey(privateKey) {
  133. try {
  134. this.setAddress(TronWeb.address.fromPrivateKey(privateKey));
  135. }
  136. catch {
  137. throw new Error('Invalid private key provided');
  138. }
  139. this.defaultPrivateKey = privateKey;
  140. this.emit('privateKeyChanged', privateKey);
  141. }
  142. setAddress(address) {
  143. if (!TronWeb.isAddress(address))
  144. throw new Error('Invalid address provided');
  145. const hex = TronWeb.address.toHex(address);
  146. const base58 = TronWeb.address.fromHex(address);
  147. if (this.defaultPrivateKey && TronWeb.address.fromPrivateKey(this.defaultPrivateKey) !== base58)
  148. this.defaultPrivateKey = false;
  149. this.defaultAddress = {
  150. hex,
  151. base58,
  152. };
  153. this.emit('addressChanged', { hex, base58 });
  154. }
  155. fullnodeSatisfies(version) {
  156. return semver_1.default.satisfies(this.fullnodeVersion, version);
  157. }
  158. isValidProvider(provider) {
  159. return Object.values(index_js_1.providers).some((knownProvider) => provider instanceof knownProvider);
  160. }
  161. setFullNode(fullNode) {
  162. if ((0, validations_js_1.isString)(fullNode))
  163. fullNode = new index_js_1.providers.HttpProvider(fullNode);
  164. if (!this.isValidProvider(fullNode))
  165. throw new Error('Invalid full node provided');
  166. this.fullNode = fullNode;
  167. this.fullNode.setStatusPage('wallet/getnowblock');
  168. }
  169. setSolidityNode(solidityNode) {
  170. if (index_js_2.default.isString(solidityNode))
  171. solidityNode = new index_js_1.providers.HttpProvider(solidityNode);
  172. if (!this.isValidProvider(solidityNode))
  173. throw new Error('Invalid solidity node provided');
  174. this.solidityNode = solidityNode;
  175. this.solidityNode.setStatusPage('walletsolidity/getnowblock');
  176. }
  177. setEventServer(eventServer, healthcheck) {
  178. this.event.setServer(eventServer, healthcheck);
  179. }
  180. setHeader(headers = {}) {
  181. const fullNode = new index_js_1.providers.HttpProvider(this.fullNode.host, 30000, '', '', headers);
  182. const solidityNode = new index_js_1.providers.HttpProvider(this.solidityNode.host, 30000, '', '', headers);
  183. const eventServer = new index_js_1.providers.HttpProvider(this.eventServer.host, 30000, '', '', headers);
  184. this.setFullNode(fullNode);
  185. this.setSolidityNode(solidityNode);
  186. this.setEventServer(eventServer);
  187. }
  188. setFullNodeHeader(headers = {}) {
  189. const fullNode = new index_js_1.providers.HttpProvider(this.fullNode.host, 30000, '', '', headers);
  190. const solidityNode = new index_js_1.providers.HttpProvider(this.solidityNode.host, 30000, '', '', headers);
  191. this.setFullNode(fullNode);
  192. this.setSolidityNode(solidityNode);
  193. }
  194. setEventHeader(headers = {}) {
  195. const eventServer = new index_js_1.providers.HttpProvider(this.eventServer.host, 30000, '', '', headers);
  196. this.setEventServer(eventServer);
  197. }
  198. currentProviders() {
  199. return {
  200. fullNode: this.fullNode,
  201. solidityNode: this.solidityNode,
  202. eventServer: this.eventServer,
  203. };
  204. }
  205. currentProvider() {
  206. return this.currentProviders();
  207. }
  208. getEventResult(...params) {
  209. return this.event.getEventsByContractAddress(...params);
  210. }
  211. getEventByTransactionID(...params) {
  212. return this.event.getEventsByTransactionID(...params);
  213. }
  214. contract(abi = [], address) {
  215. return new index_js_3.Contract(this, abi, address);
  216. }
  217. address;
  218. static get address() {
  219. return {
  220. fromHex(address) {
  221. return (0, address_js_1.fromHex)(address);
  222. },
  223. toHex(address) {
  224. return (0, address_js_1.toHex)(address);
  225. },
  226. toChecksumAddress(address) {
  227. return (0, address_js_1.toChecksumAddress)(address);
  228. },
  229. isChecksumAddress(address) {
  230. return (0, address_js_1.isChecksumAddress)(address);
  231. },
  232. fromPrivateKey(privateKey, strict = false) {
  233. return (0, address_js_1.fromPrivateKey)(privateKey, strict);
  234. },
  235. };
  236. }
  237. sha3;
  238. static sha3(string, prefix = true) {
  239. return (prefix ? '0x' : '') + (0, ethersUtils_js_1.keccak256)(Buffer.from(string, 'utf-8')).toString().substring(2);
  240. }
  241. toHex;
  242. static toHex(val) {
  243. if (index_js_2.default.isBoolean(val))
  244. return TronWeb.fromDecimal(+val);
  245. if (index_js_2.default.isBigNumber(val))
  246. return TronWeb.fromDecimal(val);
  247. if (typeof val === 'object')
  248. return TronWeb.fromUtf8(JSON.stringify(val));
  249. if (index_js_2.default.isString(val)) {
  250. if (/^(-|)0x/.test(val))
  251. return val;
  252. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  253. // @ts-ignore
  254. if (!isFinite(val) || /^\s*$/.test(val))
  255. return TronWeb.fromUtf8(val);
  256. }
  257. const result = TronWeb.fromDecimal(val);
  258. if (result === '0xNaN') {
  259. throw new Error('The passed value is not convertible to a hex string');
  260. }
  261. else {
  262. return result;
  263. }
  264. }
  265. toUtf8;
  266. static toUtf8(hex) {
  267. if (index_js_2.default.isHex(hex)) {
  268. hex = hex.replace(/^0x/, '');
  269. return Buffer.from(hex, 'hex').toString('utf8');
  270. }
  271. else {
  272. throw new Error('The passed value is not a valid hex string');
  273. }
  274. }
  275. fromUtf8;
  276. static fromUtf8(string) {
  277. if (!index_js_2.default.isString(string)) {
  278. throw new Error('The passed value is not a valid utf-8 string');
  279. }
  280. return '0x' + Buffer.from(string, 'utf8').toString('hex');
  281. }
  282. toAscii;
  283. static toAscii(hex) {
  284. if (index_js_2.default.isHex(hex)) {
  285. let str = '';
  286. let i = 0;
  287. const l = hex.length;
  288. if (hex.substring(0, 2) === '0x') {
  289. i = 2;
  290. }
  291. for (; i < l; i += 2) {
  292. const code = parseInt(hex.substr(i, 2), 16);
  293. str += String.fromCharCode(code);
  294. }
  295. return str;
  296. }
  297. else {
  298. throw new Error('The passed value is not a valid hex string');
  299. }
  300. }
  301. fromAscii;
  302. static fromAscii(string, padding) {
  303. if (!index_js_2.default.isString(string)) {
  304. throw new Error('The passed value is not a valid utf-8 string');
  305. }
  306. return '0x' + Buffer.from(string, 'ascii').toString('hex').padEnd(padding, '0');
  307. }
  308. toDecimal;
  309. static toDecimal(value) {
  310. return TronWeb.toBigNumber(value).toNumber();
  311. }
  312. fromDecimal;
  313. static fromDecimal(value) {
  314. const number = TronWeb.toBigNumber(value);
  315. const result = number.toString(16);
  316. return number.isLessThan(0) ? '-0x' + result.substr(1) : '0x' + result;
  317. }
  318. fromSun;
  319. static fromSun(sun) {
  320. const trx = TronWeb.toBigNumber(sun).div(1_000_000);
  321. return index_js_2.default.isBigNumber(sun) ? trx : trx.toString(10);
  322. }
  323. toSun;
  324. static toSun(trx) {
  325. const sun = TronWeb.toBigNumber(trx).times(1_000_000);
  326. return index_js_2.default.isBigNumber(trx) ? sun : sun.toString(10);
  327. }
  328. toBigNumber;
  329. static toBigNumber(amount = 0) {
  330. if (index_js_2.default.isBigNumber(amount))
  331. return amount;
  332. if (index_js_2.default.isString(amount) && /^(-|)0x/.test(amount))
  333. return new bignumber_js_1.BigNumber(amount.replace('0x', ''), 16);
  334. return new bignumber_js_1.BigNumber(amount.toString(10), 10);
  335. }
  336. isAddress;
  337. static isAddress(address = '') {
  338. return (0, address_js_1.isAddress)(address);
  339. }
  340. createAccount;
  341. static async createAccount() {
  342. const account = index_js_2.default.accounts.generateAccount();
  343. return account;
  344. }
  345. createRandom;
  346. static createRandom(...params) {
  347. const account = index_js_2.default.accounts.generateRandom(...params);
  348. return account;
  349. }
  350. fromMnemonic;
  351. static fromMnemonic(...params) {
  352. const account = index_js_2.default.accounts.generateAccountWithMnemonic(...params);
  353. return account;
  354. }
  355. async isConnected() {
  356. return {
  357. fullNode: await this.fullNode.isConnected(),
  358. solidityNode: await this.solidityNode.isConnected(),
  359. eventServer: this.eventServer && (await this.eventServer.isConnected()),
  360. };
  361. }
  362. }
  363. exports.TronWeb = TronWeb;
  364. exports.default = TronWeb;
  365. //# sourceMappingURL=tronweb.js.map