abi.js 9.5 KB

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