mirror of https://github.com/status-im/web3.js.git
fixed #321, encoding/decodig fixed size arrays of bytes
This commit is contained in:
parent
ef75a5d3a8
commit
72ed89fd1b
|
@ -220,19 +220,18 @@ SolidityCoder.prototype.decodeParams = function (types, bytes) {
|
||||||
|
|
||||||
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
|
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
|
||||||
var lengths = solidityTypes.map(function (solidityType, index) {
|
var lengths = solidityTypes.map(function (solidityType, index) {
|
||||||
return solidityType.staticPartLength(types[index]);
|
return solidityType.staticPartLength(types[index]);
|
||||||
// get length
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (var i = 0; i < lengths.length; i++) {
|
for (var i = 1; i < lengths.length; i++) {
|
||||||
// sum with length of previous element
|
// sum with length of previous element
|
||||||
var previous = (lengths[i - 1] || 0);
|
lengths[i] += lengths[i - 1];
|
||||||
lengths[i] += previous;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return lengths.map(function (length, index) {
|
return lengths.map(function (length, index) {
|
||||||
// remove the current length, so the length is sum of previous elements
|
// remove the current length, so the length is sum of previous elements
|
||||||
return length - solidityTypes[index].staticPartLength(types[index]);
|
var staticPartLength = solidityTypes[index].staticPartLength(types[index]);
|
||||||
|
return length - staticPartLength;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -200,9 +200,10 @@ SolidityType.prototype.decode = function (bytes, offset, name) {
|
||||||
|
|
||||||
var nestedName = self.nestedName(name);
|
var nestedName = self.nestedName(name);
|
||||||
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
|
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
|
||||||
|
var roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32;
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
||||||
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
|
for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) {
|
||||||
result.push(self.decode(bytes, arrayStart + i, nestedName));
|
result.push(self.decode(bytes, arrayStart + i, nestedName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,9 +218,10 @@ SolidityType.prototype.decode = function (bytes, offset, name) {
|
||||||
|
|
||||||
var nestedName = self.nestedName(name);
|
var nestedName = self.nestedName(name);
|
||||||
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
|
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
|
||||||
|
var roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32;
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
||||||
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
|
for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) {
|
||||||
result.push(self.decode(bytes, arrayStart + i, nestedName));
|
result.push(self.decode(bytes, arrayStart + i, nestedName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,9 @@ describe('lib/solidity/coder', function () {
|
||||||
test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
||||||
test({ type: 'int8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
test({ type: 'int8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
|
test({ type: 'int8[2]', expected: [new bn(16), new bn(2)],
|
||||||
|
value: '0000000000000000000000000000000000000000000000000000000000000010' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000002'});
|
||||||
test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
|
@ -188,6 +191,13 @@ describe('lib/solidity/coder', function () {
|
||||||
'0000000000000000000000000000000000000000000000000000000000000020' + // 180 //
|
'0000000000000000000000000000000000000000000000000000000000000020' + // 180 //
|
||||||
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134d'});
|
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134d'});
|
||||||
|
|
||||||
|
test({ type: 'bytes1', expected: '0xcf',
|
||||||
|
value: 'cf00000000000000000000000000000000000000000000000000000000000000'});
|
||||||
|
test({ type: 'bytes1[4]', expected: ['0xcf', '0x68', '0x4d', '0xfb'],
|
||||||
|
value: 'cf00000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'6800000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'4d00000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'fb00000000000000000000000000000000000000000000000000000000000000'});
|
||||||
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
|
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
|
||||||
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
|
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
|
||||||
|
|
||||||
|
|
|
@ -146,6 +146,11 @@ describe('lib/solidity/coder', function () {
|
||||||
'0000000000000000000000000000000000000000000000000000000000000001' +
|
'0000000000000000000000000000000000000000000000000000000000000001' +
|
||||||
'0000000000000000000000000000000000000000000000000000000000000002' +
|
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||||
'0000000000000000000000000000000000000000000000000000000000000003'});
|
'0000000000000000000000000000000000000000000000000000000000000003'});
|
||||||
|
test({ type: 'bytes1[4]', value: ['0xcf', '0x68', '0x4d', '0xfb'],
|
||||||
|
expected: 'cf00000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'6800000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'4d00000000000000000000000000000000000000000000000000000000000000' +
|
||||||
|
'fb00000000000000000000000000000000000000000000000000000000000000'});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue