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,19 +98,31 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) {
var result = "";
for (var i = 0; i < types.length; i++) {
if (solidityTypes[i].isDynamicArray(types[i])) {
result += f.formatInputInt(dynamicOffset).encode();
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
}
// TODO: figure out nested arrays
var isDynamic = function (i) {
return solidityTypes[i].isDynamicArray(types[i]) || solidityTypes[i].isDynamicType(types[i]);
}
for (var i = 0; i < types.length; i++) {
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
result += e;
if (isDynamic(i)) {
result += f.formatInputInt(dynamicOffset).encode();
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
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
}
for (var i = 0; i < types.length; i++) {
if (isDynamic(i)) {
var e = this.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
result += e;
}
}
return result;
};
@ -158,7 +170,7 @@ SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded
}
return result;
}
}
return encoded;
};

View File

@ -67,7 +67,8 @@ var formatInputDynamicBytes = function (value) {
var length = result.length / 2;
var l = Math.floor((result.length + 63) / 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 l = Math.floor((result.length + 63) / 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' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'string'], values: ['gavofyork', 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
@ -245,66 +253,71 @@ describe('lib/solidity/coder', function () {
//test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
//expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['string', 'int'], values: ['gavofyork', 5],
//expected: '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['string', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
//expected: '0000000000000000000000000000000000000000000000000000000000000060' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'00000000000000000000000000000000000000000000000000000000000000a0' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['string', 'int[]'], values: ['gavofyork', [1, 2, 3]],
//expected: '0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000080' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['int', 'string'], values: [5, 'gavofyork'],
//expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
//expected: '0000000000000000000000000000000000000000000000000000000000000001' +
//'00000000000000000000000000000000000000000000000000000000000000c0' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000004' +
//'0000000000000000000000000000000000000000000000000000000000000100' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000006' +
//'0000000000000000000000000000000000000000000000000000000000000007'});
//test({ types: ['int', 'bytes', 'int', 'bytes'], values: [
//5,
//'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//3,
//'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//],
//expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000080' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'00000000000000000000000000000000000000000000000000000000000000e0' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ types: ['int', 'string'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'int'], values: ['gavofyork', 5],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000060' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['string', 'int[]'], values: ['gavofyork', [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int', 'string'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000100' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
test({ types: ['int', 'bytes', 'int', 'bytes'], values: [
5,
'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
3,
'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
});
});