step by step encoding

This commit is contained in:
debris 2015-07-28 00:32:46 +02:00
parent d319ee8d92
commit 2559b958a6
2 changed files with 19 additions and 4 deletions

View File

@ -100,7 +100,7 @@ SolidityType.prototype.encode = function (value, name) {
var nestedName = this.nestedName(name); var nestedName = this.nestedName(name);
var result = []; var result = [];
result.push(f.formatInputInt(length)); result.push(f.formatInputInt(length)).encode();
var self = this; var self = this;
value.forEach(function (v) { value.forEach(function (v) {
@ -235,9 +235,23 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
console.log(encoded); 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 * 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; return name.match(/address(\[([0-9]*)\])?/)[2] || 1;
}; };
SolidityTypeAddress.prototype.nestedName = function (name) { SolidityTypeAddress.prototype.nestedName = function (name) {
// removes first [] in name // removes first [] in name
return name.replace(/\[([0-9])*\]/, ''); return name.replace(/\[([0-9])*\]/, '');

View File

@ -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: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); //test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); //test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});