common fixed in encoding

This commit is contained in:
debris 2015-07-28 15:46:38 +02:00
parent 6d3542315f
commit 03f6a5b849
3 changed files with 100 additions and 73 deletions

View File

@ -98,20 +98,32 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) { SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) {
var result = ""; var result = "";
var isDynamic = function (i) {
return solidityTypes[i].isDynamicArray(types[i]) || solidityTypes[i].isDynamicType(types[i]);
}
for (var i = 0; i < types.length; i++) { for (var i = 0; i < types.length; i++) {
if (solidityTypes[i].isDynamicArray(types[i])) { if (isDynamic(i)) {
result += f.formatInputInt(dynamicOffset).encode(); result += f.formatInputInt(dynamicOffset).encode();
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2; dynamicOffset += e.length / 2;
} else {
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
//dynamicOffset += e.length / 2; // don't add this. it's already counted
result += e;
} }
// TODO: figure out nested arrays // TODO: figure out nested arrays
} }
for (var i = 0; i < types.length; i++) { for (var i = 0; i < types.length; i++) {
if (isDynamic(i)) {
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2; dynamicOffset += e.length / 2;
result += e; result += e;
} }
}
return result; return result;
}; };

View File

@ -67,7 +67,8 @@ var formatInputDynamicBytes = function (value) {
var length = result.length / 2; var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64); var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64); result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(length).value + result, 32); //return new SolidityParam(formatInputInt(length).value + result, 32);
return new SolidityParam(formatInputInt(length).value + result);
}; };
/** /**
@ -82,7 +83,8 @@ var formatInputString = function (value) {
var length = result.length / 2; var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64); var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64); result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(length).value + result, 32); //return new SolidityParam(formatInputInt(length).value + result, 32);
return new SolidityParam(formatInputInt(length).value + result);
}; };
/** /**

View File

@ -238,6 +238,14 @@ describe('lib/solidity/coder', function () {
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' + test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' + '0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'}); '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'string'], values: ['gavofyork', 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5], //test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' + //expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
@ -245,66 +253,71 @@ describe('lib/solidity/coder', function () {
//test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'], //test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
//expected: '0000000000000000000000000000000000000000000000000000000000000005' + //expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'}); //'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['string', 'int'], values: ['gavofyork', 5], test({ types: ['int', 'string'], values: [5, 'gavofyork'],
//expected: '0000000000000000000000000000000000000000000000000000000000000040' + expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000005' + '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000009' + '0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'}); '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['string', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]], test({ types: ['string', 'int'], values: ['gavofyork', 5],
//expected: '0000000000000000000000000000000000000000000000000000000000000060' + expected: '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000005' +
//'00000000000000000000000000000000000000000000000000000000000000a0' + '0000000000000000000000000000000000000000000000000000000000000009' +
//'0000000000000000000000000000000000000000000000000000000000000009' + '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//'6761766f66796f726b0000000000000000000000000000000000000000000000' + test({ types: ['string', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
//'0000000000000000000000000000000000000000000000000000000000000003' + expected: '0000000000000000000000000000000000000000000000000000000000000060' +
//'0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' + '00000000000000000000000000000000000000000000000000000000000000a0' +
//'0000000000000000000000000000000000000000000000000000000000000003'}); '0000000000000000000000000000000000000000000000000000000000000009' +
//test({ types: ['string', 'int[]'], values: ['gavofyork', [1, 2, 3]], '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//expected: '0000000000000000000000000000000000000000000000000000000000000040' + '0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000080' + '0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000009' + '0000000000000000000000000000000000000000000000000000000000000002' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000003'});
//'0000000000000000000000000000000000000000000000000000000000000003' + test({ types: ['string', 'int[]'], values: ['gavofyork', [1, 2, 3]],
//'0000000000000000000000000000000000000000000000000000000000000001' + expected: '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000002' + '0000000000000000000000000000000000000000000000000000000000000080' +
//'0000000000000000000000000000000000000000000000000000000000000003'}); '0000000000000000000000000000000000000000000000000000000000000009' +
//test({ types: ['int', 'string'], values: [5, 'gavofyork'], '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//expected: '0000000000000000000000000000000000000000000000000000000000000005' + '0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000040' + '0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000009' + '0000000000000000000000000000000000000000000000000000000000000002' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'}); '0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]], test({ types: ['int', 'string'], values: [5, 'gavofyork'],
//expected: '0000000000000000000000000000000000000000000000000000000000000001' + expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'00000000000000000000000000000000000000000000000000000000000000c0' + '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000002' + '0000000000000000000000000000000000000000000000000000000000000009' +
//'0000000000000000000000000000000000000000000000000000000000000003' + '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//'0000000000000000000000000000000000000000000000000000000000000004' + test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
//'0000000000000000000000000000000000000000000000000000000000000100' + expected: '0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000009' + '00000000000000000000000000000000000000000000000000000000000000c0' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003' + '0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000005' + '0000000000000000000000000000000000000000000000000000000000000004' +
//'0000000000000000000000000000000000000000000000000000000000000006' + '0000000000000000000000000000000000000000000000000000000000000100' +
//'0000000000000000000000000000000000000000000000000000000000000007'}); '0000000000000000000000000000000000000000000000000000000000000009' +
//test({ types: ['int', 'bytes', 'int', 'bytes'], values: [ '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//5, '0000000000000000000000000000000000000000000000000000000000000003' +
//'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' + '0000000000000000000000000000000000000000000000000000000000000005' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', '0000000000000000000000000000000000000000000000000000000000000006' +
//3, '0000000000000000000000000000000000000000000000000000000000000007'});
//'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' + test({ types: ['int', 'bytes', 'int', 'bytes'], values: [
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 5,
//], '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//expected: '0000000000000000000000000000000000000000000000000000000000000005' + '231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//'0000000000000000000000000000000000000000000000000000000000000080' + 3,
//'0000000000000000000000000000000000000000000000000000000000000003' + '0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'00000000000000000000000000000000000000000000000000000000000000e0' + '431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//'0000000000000000000000000000000000000000000000000000000000000040' + ],
//'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' + expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' + '0000000000000000000000000000000000000000000000000000000000000080' +
//'0000000000000000000000000000000000000000000000000000000000000040' + '0000000000000000000000000000000000000000000000000000000000000003' +
//'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' + '00000000000000000000000000000000000000000000000000000000000000e0' +
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'}); '0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
}); });
}); });