abi.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodeParams = decodeParams;
  4. exports.encodeParams = encodeParams;
  5. exports.encodeParamsV2ByABI = encodeParamsV2ByABI;
  6. exports.decodeParamsV2ByABI = decodeParamsV2ByABI;
  7. const ethersUtils_js_1 = require("./ethersUtils.js");
  8. const tronweb_js_1 = require("../tronweb.js");
  9. const address_js_1 = require("./address.js");
  10. const abiCoder = new ethersUtils_js_1.AbiCoder();
  11. function _addressToHex(value) {
  12. return tronweb_js_1.TronWeb.address.toHex(value).replace(address_js_1.ADDRESS_PREFIX_REGEX, '0x');
  13. }
  14. function decodeParams(names, types, output, ignoreMethodHash = false) {
  15. if (ignoreMethodHash && output.replace(/^0x/, '').length % 64 === 8)
  16. output = '0x' + output.replace(/^0x/, '').substring(8);
  17. if (output.replace(/^0x/, '').length % 64) {
  18. throw new Error('The encoded string is not valid. Its length must be a multiple of 64.');
  19. }
  20. // workaround for unsupported trcToken type
  21. types = types.map((type) => {
  22. if (/trcToken/.test(type)) {
  23. type = type.replace(/trcToken/, 'uint256');
  24. }
  25. return type;
  26. });
  27. return abiCoder.decode(types, output).reduce((obj, arg, index) => {
  28. if (types[index] == 'address') {
  29. arg = address_js_1.ADDRESS_PREFIX + arg.substr(2).toLowerCase();
  30. }
  31. if (names.length) {
  32. obj[names[index]] = arg;
  33. }
  34. else {
  35. obj.push(arg);
  36. }
  37. return obj;
  38. }, names.length ? {} : []);
  39. }
  40. function encodeParams(types, values) {
  41. for (let i = 0; i < types.length; i++) {
  42. if (types[i] === 'address') {
  43. values[i] = tronweb_js_1.TronWeb.address.toHex(values[i]).replace(address_js_1.ADDRESS_PREFIX_REGEX, '0x');
  44. }
  45. }
  46. return abiCoder.encode(types, values);
  47. }
  48. function extractSize(type) {
  49. const size = type.match(/([a-zA-Z0-9])(\[.*\])/);
  50. return size ? size[2] : '';
  51. }
  52. function extractArrayDim(type) {
  53. const size = extractSize(type);
  54. return (size.match(/\]\[/g) || []).length + 1;
  55. }
  56. function encodeParamsV2ByABI(funABI, args) {
  57. const types = [];
  58. const buildFullTypeDefinition = (typeDef) => {
  59. if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) {
  60. const innerTypes = typeDef.components.map((innerType) => {
  61. return buildFullTypeDefinition(innerType);
  62. });
  63. return `tuple(${innerTypes.join(',')})${extractSize(typeDef.type)}`;
  64. }
  65. if (/trcToken/.test(typeDef.type))
  66. return typeDef.type.replace(/trcToken/, 'uint256');
  67. return typeDef.type;
  68. };
  69. const convertTypes = (types) => {
  70. for (let i = 0; i < types.length; i++) {
  71. const type = types[i];
  72. if (/trcToken/.test(type))
  73. types[i] = type.replace(/trcToken/, 'uint256');
  74. }
  75. };
  76. const convertAddresses = (addrArr) => {
  77. if (Array.isArray(addrArr)) {
  78. addrArr.forEach((addrs, i) => {
  79. addrArr[i] = convertAddresses(addrs);
  80. });
  81. return addrArr;
  82. }
  83. else {
  84. return _addressToHex(addrArr);
  85. }
  86. };
  87. const mapTuple = (components, args, dimension) => {
  88. if (dimension > 1) {
  89. if (args.length) {
  90. args.forEach((arg) => {
  91. mapTuple(components, arg, dimension - 1);
  92. });
  93. }
  94. }
  95. else {
  96. if (args.length && dimension) {
  97. args.forEach((arg) => {
  98. encodeArgs(components, arg);
  99. });
  100. }
  101. }
  102. };
  103. const encodeArgs = (inputs = [], args) => {
  104. if (inputs.length)
  105. inputs.forEach((input, i) => {
  106. const type = input.type;
  107. if (args[i])
  108. if (type === 'address')
  109. args[i] = _addressToHex(args[i]);
  110. else if (type.match(/^([^\x5b]*)(\x5b|$)/)[0] === 'address[')
  111. convertAddresses(args[i]);
  112. else if (type.indexOf('tuple') === 0)
  113. if (extractSize(type)) {
  114. const dimension = extractArrayDim(type);
  115. mapTuple(input.components, args[i], dimension);
  116. }
  117. else
  118. encodeArgs(input.components, args[i]);
  119. });
  120. };
  121. if (funABI.inputs && funABI.inputs.length) {
  122. for (let i = 0; i < funABI.inputs.length; i++) {
  123. const type = funABI.inputs[i].type;
  124. // "false" will be converting to `false` and "true" will be working
  125. // fine as abiCoder assume anything in quotes as `true`
  126. if (type === 'bool' && args[i] === 'false') {
  127. args[i] = false;
  128. }
  129. types.push(type.indexOf('tuple') === 0 ? buildFullTypeDefinition(funABI.inputs[i]) : type);
  130. if (args.length < types.length) {
  131. args.push('');
  132. }
  133. }
  134. }
  135. encodeArgs(funABI.inputs, args);
  136. convertTypes(types);
  137. return abiCoder.encode(types, args);
  138. }
  139. function decodeParamsV2ByABI(funABI, data) {
  140. const convertTypeNames = (types) => {
  141. for (let i = 0; i < types.length; i++) {
  142. const type = types[i];
  143. if (/^trcToken/.test(type))
  144. types[i] = type.replace(/^trcToken/, 'uint256');
  145. }
  146. };
  147. const convertAddresses = (addrArr) => {
  148. if (Array.isArray(addrArr)) {
  149. addrArr.forEach((addrs, i) => {
  150. addrArr[i] = convertAddresses(addrs);
  151. });
  152. return addrArr;
  153. }
  154. else {
  155. return tronweb_js_1.TronWeb.address.toHex(addrArr);
  156. }
  157. };
  158. const mapTuple = (components, args, dimension) => {
  159. if (dimension > 1) {
  160. if (args.length) {
  161. args.forEach((arg) => {
  162. mapTuple(components, arg, dimension - 1);
  163. });
  164. }
  165. }
  166. else {
  167. if (args.length && dimension) {
  168. args.forEach((arg) => {
  169. decodeResult(components, arg);
  170. });
  171. }
  172. }
  173. };
  174. const buildFullTypeNameDefinition = (typeDef) => {
  175. const name = typeDef.name ? ` ${typeDef.name}` : '';
  176. if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) {
  177. const innerTypes = typeDef.components.map((innerType) => {
  178. return buildFullTypeNameDefinition(innerType);
  179. });
  180. return `tuple(${innerTypes.join(',')})${extractSize(typeDef.type)}${name}`;
  181. }
  182. if (/trcToken/.test(typeDef.type))
  183. return typeDef.type.replace(/trcToken/, 'uint256') + name;
  184. return typeDef.type + name;
  185. };
  186. const decodeResult = (outputs, result) => {
  187. if (outputs.length)
  188. outputs.forEach((output, i) => {
  189. const { type, name } = output;
  190. if (result[i]) {
  191. if (type === 'address') {
  192. result[i] = tronweb_js_1.TronWeb.address.toHex(result[i]);
  193. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  194. //@ts-ignore
  195. if (name)
  196. result[name] = tronweb_js_1.TronWeb.address.toHex(result[i]);
  197. }
  198. else if (type.match(/^([^\x5b]*)(\x5b|$)/)[0] === 'address[') {
  199. convertAddresses(result[i]);
  200. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  201. //@ts-ignore
  202. if (name)
  203. result[name] = convertAddresses(result[i]);
  204. }
  205. else if (type.indexOf('tuple') === 0) {
  206. if (extractSize(type)) {
  207. const dimension = extractArrayDim(type);
  208. mapTuple(output.components, result[i], dimension);
  209. }
  210. else
  211. decodeResult(output.components, result[i]);
  212. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  213. //@ts-ignore
  214. if (name)
  215. result[name] = result[i];
  216. }
  217. else {
  218. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  219. //@ts-ignore
  220. if (name)
  221. result[name] = result[i];
  222. }
  223. }
  224. else {
  225. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  226. //@ts-ignore
  227. if (name)
  228. result[name] = result[i];
  229. }
  230. });
  231. };
  232. // Only decode if there supposed to be fields
  233. if ('outputs' in funABI && funABI.outputs && funABI.outputs.length > 0) {
  234. const outputTypes = [];
  235. for (let i = 0; i < funABI.outputs.length; i++) {
  236. const type = funABI.outputs[i].type;
  237. const name = funABI.outputs[i].name ? ` ${funABI.outputs[i].name}` : '';
  238. outputTypes.push(type.indexOf('tuple') === 0 ? buildFullTypeNameDefinition(funABI.outputs[i]) : type + name);
  239. }
  240. convertTypeNames(outputTypes);
  241. if (!data || !data.length)
  242. data = new Uint8Array(32 * funABI.outputs.length); // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data
  243. // decode data
  244. const decodeRes = abiCoder.decode(outputTypes, data);
  245. const decodeResCopy = decodeRes.toArray(true);
  246. decodeResult(funABI.outputs, decodeResCopy);
  247. return decodeResCopy;
  248. }
  249. return [];
  250. }
  251. //# sourceMappingURL=abi.js.map