From 2559b958a684b9648623686101e8c29ee8b9d989 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 28 Jul 2015 00:32:46 +0200 Subject: [PATCH] step by step encoding --- lib/solidity/coder.js | 20 ++++++++++++++++---- test/coder.encodeParam.js | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/solidity/coder.js b/lib/solidity/coder.js index 6e7ec0d..0658500 100644 --- a/lib/solidity/coder.js +++ b/lib/solidity/coder.js @@ -100,7 +100,7 @@ SolidityType.prototype.encode = function (value, name) { var nestedName = this.nestedName(name); var result = []; - result.push(f.formatInputInt(length)); + result.push(f.formatInputInt(length)).encode(); var self = this; value.forEach(function (v) { @@ -235,9 +235,23 @@ SolidityCoder.prototype.encodeParams = function (types, params) { console.log(encoded); - return encoded[0]; + var totalOffset = solidityTypes.reduce(function (acc, solidityType, index) { + return acc + solidityType.staticPartLength(types[index]); + }, 0); + + var result = solidityTypes.reduce(function (acc, solidityType, index) { + if (solidityType.isDynamicArray(types[index])) { + return acc + f.formatInputInt(offsets[index]).encode(); + } else { + return acc + encoded[index]; + } + }, ""); + + return result; }; + + /** * Should be used to decode bytes to plain param * @@ -320,8 +334,6 @@ SolidityTypeAddress.prototype.staticArrayLength = function (name) { return name.match(/address(\[([0-9]*)\])?/)[2] || 1; }; - - SolidityTypeAddress.prototype.nestedName = function (name) { // removes first [] in name return name.replace(/\[([0-9])*\]/, ''); diff --git a/test/coder.encodeParam.js b/test/coder.encodeParam.js index e7e0a50..43ff681 100644 --- a/test/coder.encodeParam.js +++ b/test/coder.encodeParam.js @@ -117,6 +117,9 @@ describe('lib/solidity/coder', function () { }; + test({ types: ['address', 'address'], values: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'], + expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3'}); //test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); //test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); //test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});