abi.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. This file is part of ethereum.js.
  3. ethereum.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethereum.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file abi.js
  15. * @authors:
  16. * Marek Kotewicz <marek@ethdev.com>
  17. * Gav Wood <g@ethdev.com>
  18. * @date 2014
  19. */
  20. // TODO: make these be actually accurate instead of falling back onto JS's doubles.
  21. var hexToDec = function (hex) {
  22. return parseInt(hex, 16).toString();
  23. };
  24. var decToHex = function (dec) {
  25. return parseInt(dec).toString(16);
  26. };
  27. var findIndex = function (array, callback) {
  28. var end = false;
  29. var i = 0;
  30. for (; i < array.length && !end; i++) {
  31. end = callback(array[i]);
  32. }
  33. return end ? i - 1 : -1;
  34. };
  35. var findMethodIndex = function (json, methodName) {
  36. return findIndex(json, function (method) {
  37. return method.name === methodName;
  38. });
  39. };
  40. var padLeft = function (string, chars) {
  41. return Array(chars - string.length + 1).join("0") + string;
  42. };
  43. var setupInputTypes = function () {
  44. var prefixedType = function (prefix) {
  45. return function (type, value) {
  46. var expected = prefix;
  47. if (type.indexOf(expected) !== 0) {
  48. return false;
  49. }
  50. var padding = parseInt(type.slice(expected.length)) / 8;
  51. if (typeof value === "number")
  52. value = value.toString(16);
  53. else if (value.indexOf('0x') === 0)
  54. value = value.substr(2);
  55. else
  56. value = (+value).toString(16);
  57. return padLeft(value, padding * 2);
  58. };
  59. };
  60. var namedType = function (name, padding, formatter) {
  61. return function (type, value) {
  62. if (type !== name) {
  63. return false;
  64. }
  65. return padLeft(formatter ? formatter(value) : value, padding * 2);
  66. };
  67. };
  68. var formatBool = function (value) {
  69. return value ? '0x1' : '0x0';
  70. };
  71. return [
  72. prefixedType('uint'),
  73. prefixedType('int'),
  74. prefixedType('hash'),
  75. namedType('address', 20),
  76. namedType('bool', 1, formatBool),
  77. ];
  78. };
  79. var inputTypes = setupInputTypes();
  80. var toAbiInput = function (json, methodName, params) {
  81. var bytes = "";
  82. var index = findMethodIndex(json, methodName);
  83. if (index === -1) {
  84. return;
  85. }
  86. bytes = "0x" + padLeft(index.toString(16), 2);
  87. var method = json[index];
  88. for (var i = 0; i < method.inputs.length; i++) {
  89. var found = false;
  90. for (var j = 0; j < inputTypes.length && !found; j++) {
  91. found = inputTypes[j](method.inputs[i].type, params[i]);
  92. }
  93. if (!found) {
  94. console.error('unsupported json type: ' + method.inputs[i].type);
  95. }
  96. bytes += found;
  97. }
  98. return bytes;
  99. };
  100. var setupOutputTypes = function () {
  101. var prefixedType = function (prefix) {
  102. return function (type) {
  103. var expected = prefix;
  104. if (type.indexOf(expected) !== 0) {
  105. return -1;
  106. }
  107. var padding = parseInt(type.slice(expected.length)) / 8;
  108. return padding * 2;
  109. };
  110. };
  111. var namedType = function (name, padding) {
  112. return function (type) {
  113. return name === type ? padding * 2 : -1;
  114. };
  115. };
  116. var formatInt = function (value) {
  117. return value.length <= 8 ? +parseInt(value, 16) : hexToDec(value);
  118. };
  119. var formatHash = function (value) {
  120. return "0x" + value;
  121. };
  122. var formatBool = function (value) {
  123. return value === '1' ? true : false;
  124. };
  125. return [
  126. { padding: prefixedType('uint'), format: formatInt },
  127. { padding: prefixedType('int'), format: formatInt },
  128. { padding: prefixedType('hash'), format: formatHash },
  129. { padding: namedType('address', 20) },
  130. { padding: namedType('bool', 1), format: formatBool }
  131. ];
  132. };
  133. var outputTypes = setupOutputTypes();
  134. var fromAbiOutput = function (json, methodName, output) {
  135. var index = findMethodIndex(json, methodName);
  136. if (index === -1) {
  137. return;
  138. }
  139. output = output.slice(2);
  140. var result = [];
  141. var method = json[index];
  142. for (var i = 0; i < method.outputs.length; i++) {
  143. var padding = -1;
  144. for (var j = 0; j < outputTypes.length && padding === -1; j++) {
  145. padding = outputTypes[j].padding(method.outputs[i].type);
  146. }
  147. if (padding === -1) {
  148. // not found output parsing
  149. continue;
  150. }
  151. var res = output.slice(0, padding);
  152. var formatter = outputTypes[j - 1].format;
  153. result.push(formatter ? formatter(res) : ("0x" + res));
  154. output = output.slice(padding);
  155. }
  156. return result;
  157. };
  158. var inputParser = function (json) {
  159. var parser = {};
  160. json.forEach(function (method) {
  161. parser[method.name] = function () {
  162. var params = Array.prototype.slice.call(arguments);
  163. return toAbiInput(json, method.name, params);
  164. };
  165. });
  166. return parser;
  167. };
  168. var outputParser = function (json) {
  169. var parser = {};
  170. json.forEach(function (method) {
  171. parser[method.name] = function (output) {
  172. return fromAbiOutput(json, method.name, output);
  173. };
  174. });
  175. return parser;
  176. };
  177. module.exports = {
  178. inputParser: inputParser,
  179. outputParser: outputParser
  180. };