mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 19:48:13 +00:00
new encoding
This commit is contained in:
parent
00072f47e2
commit
bdd68cde1a
@ -233,8 +233,6 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
|
|||||||
return solidityType.encode(params[index], types[index]);
|
return solidityType.encode(params[index], types[index]);
|
||||||
});
|
});
|
||||||
|
|
||||||
//console.log(encodeds);
|
|
||||||
|
|
||||||
var totalOffset = solidityTypes.reduce(function (acc, solidityType, index) {
|
var totalOffset = solidityTypes.reduce(function (acc, solidityType, index) {
|
||||||
return acc + solidityType.staticPartLength(types[index]);
|
return acc + solidityType.staticPartLength(types[index]);
|
||||||
}, 0);
|
}, 0);
|
||||||
@ -250,7 +248,6 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
|
|||||||
return acc + encodeds[index];
|
return acc + encodeds[index];
|
||||||
}, "");
|
}, "");
|
||||||
|
|
||||||
//console.log(result);
|
|
||||||
result = solidityTypes.reduce(function (acc, solidityType, index) {
|
result = solidityTypes.reduce(function (acc, solidityType, index) {
|
||||||
if (solidityType.isDynamicArray(types[index])) {
|
if (solidityType.isDynamicArray(types[index])) {
|
||||||
var offset = acc.length / 2;
|
var offset = acc.length / 2;
|
||||||
@ -259,8 +256,6 @@ SolidityCoder.prototype.encodeParams = function (types, params) {
|
|||||||
return acc;
|
return acc;
|
||||||
}, result);
|
}, result);
|
||||||
|
|
||||||
//console.log(result);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -271,15 +266,18 @@ SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded
|
|||||||
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
|
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
|
||||||
var result = encoded[0];
|
var result = encoded[0];
|
||||||
|
|
||||||
|
var previousLength = 2; // in int
|
||||||
if (solidityType.isDynamicArray(nestedName)) {
|
if (solidityType.isDynamicArray(nestedName)) {
|
||||||
for (var i = 0; i < encoded.length; i++) {
|
for (var i = 1; i < encoded.length; i++) {
|
||||||
result += f.formatInputInt(offset + i * nestedStaticPartLength + encoded.length * 32).encode();
|
previousLength += +(encoded[i - 1] || {})[0] || 0;
|
||||||
|
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// first element is length, skip it
|
// first element is length, skip it
|
||||||
for (var i = 0; i < encoded.length - 1; i++) {
|
for (var i = 0; i < encoded.length - 1; i++) {
|
||||||
result += this.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + i * nestedStaticPartLength);
|
var additionalOffset = result / 2;
|
||||||
|
result += this.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -293,7 +291,7 @@ SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded
|
|||||||
if (solidityType.isDynamicArray(nestedName)) {
|
if (solidityType.isDynamicArray(nestedName)) {
|
||||||
for (var i = 0; i < encoded.length; i++) {
|
for (var i = 0; i < encoded.length; i++) {
|
||||||
// calculate length of previous item
|
// calculate length of previous item
|
||||||
previousLength += (encoded[i - 1] || {})[0] || 0;
|
previousLength += +(encoded[i - 1] || {})[0] || 0;
|
||||||
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
|
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,20 +26,42 @@ describe('lib/solidity/coder', function () {
|
|||||||
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
|
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
|
||||||
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
|
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
|
||||||
'00000000000000000000000000000000000000000000000000000000000000a0' +
|
'00000000000000000000000000000000000000000000000000000000000000a0' +
|
||||||
'0000000000000000000000000000000000000000000000000000000000000002' + /* 40 */
|
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + /* 60 */
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
|
||||||
'0000000000000000000000000000000000000000000000000000000000000002' + /* a0 */
|
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
|
||||||
test({ type: 'address[][2]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
|
test({ type: 'address[][2]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
|
||||||
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
|
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
|
||||||
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
|
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
|
||||||
'0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */
|
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
|
||||||
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
|
||||||
|
test({ type: 'address[][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1'],
|
||||||
|
['0x407d73d8a49eeb85d32cf465507dd71d507100c3']],
|
||||||
|
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000080' +
|
||||||
|
'00000000000000000000000000000000000000000000000000000000000000c0' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000001' + /* 80 */
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + /* a0 */
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000001' + /* c0 */
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
|
||||||
|
test({ type: 'address[][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
|
||||||
|
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
|
||||||
|
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000080' +
|
||||||
|
'00000000000000000000000000000000000000000000000000000000000000e0' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000002' + /* 80 */
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + /* a0 */
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
|
||||||
|
'0000000000000000000000000000000000000000000000000000000000000002' + /* e0 */
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
|
||||||
|
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
|
||||||
//test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
|
//test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
|
||||||
//test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
|
//test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||||
//test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
//test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user