abi.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 constants_js_1 = require("./constants.js");
  9. const address_js_1 = require("./address.js");
  10. const abiCoder = new ethersUtils_js_1.AbiCoder();
  11. function _addressToHex(value) {
  12. return (0, address_js_1.toHex)(value).replace(constants_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 = constants_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] = _addressToHex(values[i]);
  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 (0, address_js_1.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 setResultProp = (result, name, value) => {
  187. if (name && !['length'].includes(name)) {
  188. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  189. // @ts-ignore
  190. result[name] = value;
  191. }
  192. };
  193. const decodeResult = (outputs, result) => {
  194. if (outputs.length)
  195. outputs.forEach((output, i) => {
  196. const { type, name } = output;
  197. if (result[i]) {
  198. if (type === 'address') {
  199. result[i] = (0, address_js_1.toHex)(result[i]);
  200. setResultProp(result, name, (0, address_js_1.toHex)(result[i]));
  201. }
  202. else if (type.match(/^([^\x5b]*)(\x5b|$)/)[0] === 'address[') {
  203. convertAddresses(result[i]);
  204. setResultProp(result, name, convertAddresses(result[i]));
  205. }
  206. else if (type.indexOf('tuple') === 0) {
  207. if (extractSize(type)) {
  208. const dimension = extractArrayDim(type);
  209. mapTuple(output.components, result[i], dimension);
  210. }
  211. else
  212. decodeResult(output.components, result[i]);
  213. setResultProp(result, name, result[i]);
  214. }
  215. else {
  216. setResultProp(result, name, result[i]);
  217. }
  218. }
  219. else {
  220. setResultProp(result, name, result[i]);
  221. }
  222. });
  223. };
  224. // Only decode if there supposed to be fields
  225. if ('outputs' in funABI && funABI.outputs && funABI.outputs.length > 0) {
  226. const outputTypes = [];
  227. for (let i = 0; i < funABI.outputs.length; i++) {
  228. const type = funABI.outputs[i].type;
  229. const name = funABI.outputs[i].name ? ` ${funABI.outputs[i].name}` : '';
  230. outputTypes.push(type.indexOf('tuple') === 0 ? buildFullTypeNameDefinition(funABI.outputs[i]) : type + name);
  231. }
  232. convertTypeNames(outputTypes);
  233. if (!data || !data.length)
  234. 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
  235. // decode data
  236. const decodeRes = abiCoder.decode(outputTypes, data);
  237. const decodeResCopy = decodeRes.toArray(true);
  238. decodeResult(funABI.outputs, decodeResCopy);
  239. return decodeResCopy;
  240. }
  241. return [];
  242. }
  243. //# sourceMappingURL=abi.js.map