helper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fromUtf8 = fromUtf8;
  4. exports.deepCopyJson = deepCopyJson;
  5. exports.resultManager = resultManager;
  6. exports.resultManagerTriggerSmartContract = resultManagerTriggerSmartContract;
  7. exports.genContractAddress = genContractAddress;
  8. exports.getHeaderInfo = getHeaderInfo;
  9. exports.createTransaction = createTransaction;
  10. exports.getTransactionOptions = getTransactionOptions;
  11. const tronweb_js_1 = require("../../tronweb.js");
  12. const transaction_js_1 = require("../../utils/transaction.js");
  13. const ethersUtils_js_1 = require("../../utils/ethersUtils.js");
  14. function fromUtf8(value) {
  15. return tronweb_js_1.TronWeb.fromUtf8(value).replace(/^0x/, '');
  16. }
  17. function deepCopyJson(json) {
  18. return JSON.parse(JSON.stringify(json));
  19. }
  20. function resultManager(transaction, data, options) {
  21. if (transaction.Error)
  22. throw new Error(transaction.Error);
  23. if (transaction.result && transaction.result.message) {
  24. throw new Error(tronweb_js_1.TronWeb.toUtf8(transaction.result.message));
  25. }
  26. const authResult = (0, transaction_js_1.txCheckWithArgs)(transaction, data, options);
  27. if (authResult) {
  28. return transaction;
  29. }
  30. throw new Error('Invalid transaction');
  31. }
  32. function resultManagerTriggerSmartContract(transaction, data, options) {
  33. if (transaction.Error)
  34. throw new Error(transaction.Error);
  35. if (transaction.result && transaction.result.message) {
  36. throw new Error(tronweb_js_1.TronWeb.toUtf8(transaction.result.message));
  37. }
  38. if (!(options._isConstant || options.estimateEnergy)) {
  39. const authResult = (0, transaction_js_1.txCheckWithArgs)(transaction.transaction, data, options);
  40. if (authResult) {
  41. return transaction;
  42. }
  43. throw new Error('Invalid transaction');
  44. }
  45. return transaction;
  46. }
  47. function genContractAddress(ownerAddress, txID) {
  48. return ('41' +
  49. (0, ethersUtils_js_1.keccak256)(Buffer.from(txID + ownerAddress, 'hex'))
  50. .toString()
  51. .substring(2)
  52. .slice(24));
  53. }
  54. function getHeaderInfo(node) {
  55. return node.request('wallet/getblock', { detail: false }, 'post').then((data) => {
  56. return {
  57. ref_block_bytes: data.block_header.raw_data.number.toString(16).slice(-4).padStart(4, '0'),
  58. ref_block_hash: data.blockID.slice(16, 32),
  59. expiration: data.block_header.raw_data.timestamp + 60 * 1000,
  60. timestamp: data.block_header.raw_data.timestamp,
  61. };
  62. });
  63. }
  64. function checkBlockHeader(options = {}) {
  65. if (typeof options['ref_block_bytes'] === 'undefined' &&
  66. typeof options['ref_block_hash'] === 'undefined' &&
  67. typeof options['expiration'] === 'undefined' &&
  68. typeof options['timestamp'] === 'undefined') {
  69. return false;
  70. }
  71. if (typeof options['ref_block_bytes'] !== 'string') {
  72. throw new Error('Invalid ref_block_bytes provided.');
  73. }
  74. if (typeof options['ref_block_hash'] !== 'string') {
  75. throw new Error('Invalid ref_block_hash provided.');
  76. }
  77. if (typeof options['expiration'] !== 'number') {
  78. throw new Error('Invalid expiration provided.');
  79. }
  80. if (typeof options['timestamp'] !== 'number') {
  81. throw new Error('Invalid timestamp provided.');
  82. }
  83. return true;
  84. }
  85. async function createTransaction(tronWeb, type, value, Permission_id, options = {}) {
  86. const tx = {
  87. visible: false,
  88. txID: '',
  89. raw_data_hex: '',
  90. raw_data: {
  91. contract: [
  92. {
  93. parameter: {
  94. value,
  95. type_url: `type.googleapis.com/protocol.${type}`,
  96. },
  97. type,
  98. },
  99. ],
  100. ...(checkBlockHeader(options) ? {} : await getHeaderInfo(tronWeb.fullNode)),
  101. ...options,
  102. },
  103. };
  104. if (Permission_id) {
  105. tx.raw_data.contract[0].Permission_id = Permission_id;
  106. }
  107. const pb = (0, transaction_js_1.txJsonToPb)(tx);
  108. tx.txID = (0, transaction_js_1.txPbToTxID)(pb).replace(/^0x/, '');
  109. tx.raw_data_hex = (0, transaction_js_1.txPbToRawDataHex)(pb).toLowerCase();
  110. return tx;
  111. }
  112. function getTransactionOptions(options = {}) {
  113. const ret = {};
  114. if (checkBlockHeader(options.blockHeader)) {
  115. ret['ref_block_bytes'] = options.blockHeader['ref_block_bytes'];
  116. ret['ref_block_hash'] = options.blockHeader['ref_block_hash'];
  117. ret['expiration'] = options.blockHeader['expiration'];
  118. ret['timestamp'] = options.blockHeader['timestamp'];
  119. }
  120. return ret;
  121. }
  122. //# sourceMappingURL=helper.js.map