aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarek Kotewicz <marek.kotewicz@gmail.com>2014-11-13 01:59:29 +0800
committerMarek Kotewicz <marek.kotewicz@gmail.com>2014-11-13 01:59:29 +0800
commit05290d554749c7b3507da4af11796112c67be718 (patch)
tree2b6b24bc486d7b8b6613f9d20e7f6d42136a328e /lib
parentdc100f85b3719f38a92223c39eb36d8d4ff24cdb (diff)
downloaddexon-05290d554749c7b3507da4af11796112c67be718.tar
dexon-05290d554749c7b3507da4af11796112c67be718.tar.gz
dexon-05290d554749c7b3507da4af11796112c67be718.tar.bz2
dexon-05290d554749c7b3507da4af11796112c67be718.tar.lz
dexon-05290d554749c7b3507da4af11796112c67be718.tar.xz
dexon-05290d554749c7b3507da4af11796112c67be718.tar.zst
dexon-05290d554749c7b3507da4af11796112c67be718.zip
abi, the beginning
Diffstat (limited to 'lib')
-rw-r--r--lib/abi.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/abi.js b/lib/abi.js
new file mode 100644
index 000000000..3082b8232
--- /dev/null
+++ b/lib/abi.js
@@ -0,0 +1,77 @@
+
+var findIndex = function (array, callback) {
+ var end = false;
+ var i = 0;
+ for (; i < array.length && !end; i++) {
+ end = callback(array[i]);
+ }
+ return end ? i - 1 : -1;
+};
+
+var padLeft = function (number, n) {
+ return (new Array(n - number.toString.length + 1)).join("0") + number;
+};
+
+var setupTypes = function () {
+ var prefixedType = function (prefix) {
+ return function (type, value) {
+ var expected = prefix;
+ if (type.indexOf(expected) !== 0) {
+ return false;
+ }
+
+ var padding = parseInt(type.slice(expected.length)) / 8;
+ return padLeft(value, padding);
+ };
+ };
+
+ var namedType = function (name, padding) {
+ return function (type, value) {
+ if (type !== name) {
+ return false;
+ }
+
+ return padLeft(value, padding);
+ };
+ };
+
+ return [
+ prefixedType('uint'),
+ prefixedType('int'),
+ namedType('address', 20),
+ namedType('bool', 1),
+ ];
+};
+
+var types = setupTypes();
+
+var toBytes = function (json, methodName, params) {
+ var bytes = "";
+ var index = findIndex(json, function (method) {
+ return method.name === methodName;
+ });
+
+ if (index === -1) {
+ return;
+ }
+
+ bytes = bytes + index + 'x';
+ var method = json[index];
+
+ for (var i = 0; i < method.inputs.length; i++) {
+ var found = false;
+ for (var j = 0; j < types.length && !found; j++) {
+ found = types[j](method.inputs[i].type, params[i]);
+ }
+ if (!found) {
+ console.error('unsupported json type: ' + method.inputs[i].type);
+ }
+ bytes += found;
+ }
+ return bytes;
+};
+
+module.exports = {
+ toBytes: toBytes
+};
+