nested encoding in progress

This commit is contained in:
debris 2015-07-28 00:14:41 +02:00
parent b2f2202450
commit d319ee8d92
4 changed files with 299 additions and 301 deletions

View File

@ -76,6 +76,53 @@ SolidityType.prototype.formatInput = function (param, arrayType) {
return this._inputFormatter(param);
};
SolidityType.prototype.totalOffset = function (value, name) {
if (this.isDynamicArray(name)) {
var nestedName = this.nestedName(name);
var self = this;
return value.reduce(function (acc, current) {
return acc + self.totalOffset(current, nestedName);
}, 64); // add pointer to data && length of data
} else if (this.isStaticArray(name)) {
var nestedName = this.nestedName(name);
var self = this;
return value.reduce(function (acc, current) {
return acc + self.totalOffset(current, nestedName);
}, 0);
}
return this.staticPartLength(name);
};
SolidityType.prototype.encode = function (value, name) {
if (this.isDynamicArray(name)) {
var length = value.length; // in int
var nestedName = this.nestedName(name);
var result = [];
result.push(f.formatInputInt(length));
var self = this;
value.forEach(function (v) {
result.push(self.encode(v, nestedName));
});
return result;
} else if (this.isStaticArray(name)) {
var length = this.staticArrayLength(name); // in int
var nestedName = this.nestedName(name);
var result = [];
for (var i = 0; i < length; i++) {
result.push(this.encode(value[i], nestedName));
}
return result;
}
return this._inputFormatter(value, name).encode();
};
/**
* Should be used to decode params from bytes
*
@ -167,7 +214,7 @@ SolidityCoder.prototype._formatInput = function (type, param) {
* @return {String} encoded plain param
*/
SolidityCoder.prototype.encodeParam = function (type, param) {
return this._formatInput(type, param).encode();
return this.encodeParams([type], [param]);
};
/**
@ -179,12 +226,16 @@ SolidityCoder.prototype.encodeParam = function (type, param) {
* @return {String} encoded list of params
*/
SolidityCoder.prototype.encodeParams = function (types, params) {
var self = this;
var solidityParams = types.map(function (type, index) {
return self._formatInput(type, params[index]);
var solidityTypes = this.getSolidityTypes(types);
var offsets = this.getOffsets(types, solidityTypes);
var encoded = solidityTypes.map(function (solidityType, index) {
return solidityType.encode(params[index], types[index]);
});
return SolidityParam.encodeList(solidityParams);
console.log(encoded);
return encoded[0];
};
/**
@ -208,13 +259,16 @@ SolidityCoder.prototype.decodeParam = function (type, bytes) {
* @return {Array} array of plain params
*/
SolidityCoder.prototype.decodeParams = function (types, bytes) {
var self = this;
var solidityTypes = this.getSolidityTypes(types);
var offsets = this.getOffsets(types, solidityTypes);
var solidityTypes = types.map(function (type) {
return self._requireType(type);
return solidityTypes.map(function (solidityType, index) {
return solidityType.decode(bytes, offsets[index], types[index], index);
});
};
var offsets = solidityTypes.map(function (solidityType, index) {
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
return solidityTypes.map(function (solidityType, index) {
return solidityType.staticPartLength(types[index]);
// get length
}).map(function (length, index, lengths) {
@ -224,9 +278,12 @@ SolidityCoder.prototype.decodeParams = function (types, bytes) {
// remove the current length, so the length is sum of previous elements
return length - solidityTypes[index].staticPartLength(types[index]);
});
};
return solidityTypes.map(function (solidityType, index) {
return solidityType.decode(bytes, offsets[index], types[index], index);
SolidityCoder.prototype.getSolidityTypes = function (types) {
var self = this;
return types.map(function (type) {
return self._requireType(type);
});
};

View File

@ -146,66 +146,5 @@ SolidityParam.encodeList = function (params) {
}, ''));
};
/**
* This method should be used to decode plain (static) solidity param at given index
*
* @method decodeParam
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeParam = function (bytes, index) {
index = index || 0;
return new SolidityParam(bytes.substr(index * 64, 64));
};
/**
* This method should be called to get offset value from bytes at given index
*
* @method getOffset
* @param {String} bytes
* @param {Number} index
* @returns {Number} offset as number
*/
var getOffset = function (bytes, index) {
// we can do this cause offset is rather small
return parseInt('0x' + bytes.substr(index * 64, 64));
};
/**
* This method should be called to decode solidity bytes param at given index
*
* @method decodeBytes
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeBytes = function (bytes, index) {
index = index || 0;
var offset = getOffset(bytes, index);
var l = parseInt('0x' + bytes.substr(offset * 2, 64));
l = Math.floor((l + 31) / 32);
// (1 + l) * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0);
};
/**
* This method should be used to decode solidity array at given index
*
* @method decodeArray
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeArray = function (bytes, index) {
index = index || 0;
var offset = getOffset(bytes, index);
var length = parseInt('0x' + bytes.substr(offset * 2, 64));
return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64), 0);
};
module.exports = SolidityParam;

View File

@ -153,7 +153,7 @@ describe('lib/solidity/coder', function () {
describe('decodeParams', function () {
var test = function (t) {
it('should turn ' + t.values + ' to ' + t.expected, function () {
//assert.deepEqual(coder.decodeParams(t.types, t.values), t.expected);
assert.deepEqual(coder.decodeParams(t.types, t.values), t.expected);
});
};
@ -163,45 +163,45 @@ describe('lib/solidity/coder', function () {
test({ types: ['address', 'address'], expected: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
values: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3'});
test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)],
values: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], expected: [new bn(5), '0x6761766f66796f726b0000000000000000000000000000000000000000000000'],
values: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4),
[new bn(5), new bn(6), new bn(7)]],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000100' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
test({ types: ['int', 'bytes', 'int', 'bytes'], expected: [
new bn(5),
'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
new bn(3),
'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
],
values: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)],
//values: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000005'});
//test({ types: ['int', 'bytes32'], expected: [new bn(5), '0x6761766f66796f726b0000000000000000000000000000000000000000000000'],
//values: '0000000000000000000000000000000000000000000000000000000000000005' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4),
//[new bn(5), new bn(6), new bn(7)]],
//values: '0000000000000000000000000000000000000000000000000000000000000001' +
//'00000000000000000000000000000000000000000000000000000000000000c0' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000004' +
//'0000000000000000000000000000000000000000000000000000000000000100' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000006' +
//'0000000000000000000000000000000000000000000000000000000000000007'});
//test({ types: ['int', 'bytes', 'int', 'bytes'], expected: [
//new bn(5),
//'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//new bn(3),
//'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//],
//values: '0000000000000000000000000000000000000000000000000000000000000005' +
//'0000000000000000000000000000000000000000000000000000000000000080' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'00000000000000000000000000000000000000000000000000000000000000e0' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
});
});

View File

@ -7,101 +7,103 @@ describe('lib/solidity/coder', function () {
describe('encodeParam', function () {
var test = function (t) {
it('should turn ' + t.value + ' to ' + t.expected, function () {
//assert.equal(coder.encodeParam(t.type, t.value), t.expected);
assert.equal(coder.encodeParam(t.type, t.value), t.expected);
});
};
test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'bytes32', value: '0x6761766f66796f726b',
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes32', value: '0x02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29',
expected: '02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29'});
test({ type: 'bytes', value: '0x6761766f66796f726b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'string', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: '0xc3a40000c3a4',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0xc3a40000c3a4',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes64', value: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'ää',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000008' +
'c383c2a4c383c2a4000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'ü',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'c3bc000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'Ã',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'c383000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int256[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int[]', value: [1,2,3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'real', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'real', value: -1, expected: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
test({ type: 'ureal', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'ureal', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'ureal', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000060' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'string', value: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'000000000000000000000000000000000000000000000000000000000000003e' +
'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
//test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
//test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
//test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
//test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
//test({ type: 'bytes32', value: '0x6761766f66796f726b',
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//expected: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ type: 'bytes32', value: '0x02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29',
//expected: '02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29'});
//test({ type: 'bytes', value: '0x6761766f66796f726b',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ type: 'bytes', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000020' +
//'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ type: 'string', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ type: 'bytes', value: '0xc3a40000c3a4',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000006' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', value: '0xc3a40000c3a4',
//expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes64', value: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000',
//expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'string', value: 'ää',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000008' +
//'c383c2a4c383c2a4000000000000000000000000000000000000000000000000'});
//test({ type: 'string', value: 'ü',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'c3bc000000000000000000000000000000000000000000000000000000000000'});
//test({ type: 'string', value: 'Ã',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'c383000000000000000000000000000000000000000000000000000000000000'});
//test({ type: 'int[]', value: [], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000000'});
//test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ type: 'int256[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ type: 'int[]', value: [1,2,3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
//test({ type: 'real', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
//test({ type: 'real', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
//test({ type: 'real', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
//test({ type: 'real', value: -1, expected: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
//test({ type: 'ureal', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
//test({ type: 'ureal', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
//test({ type: 'ureal', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
//test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000040' +
//'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000060' +
//'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
//'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ type: 'string', value: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'000000000000000000000000000000000000000000000000000000000000003e' +
//'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
//'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
});
});
@ -110,108 +112,108 @@ describe('lib/solidity/coder', function () {
describe('encodeParams', function () {
var test = function (t) {
it('should turn ' + t.values + ' to ' + t.expected, function () {
//assert.equal(coder.encodeParams(t.types, t.values), t.expected);
assert.equal(coder.encodeParams(t.types, t.values), t.expected);
});
};
test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int256[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int256[]'], values: [[1,2,3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int[]', 'int[]'], values: [[1,2], [3,4]],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004'});
test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
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'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
//test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
//test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000009' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['int256[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['int256[]'], values: [[1,2,3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003'});
//test({ types: ['int[]', 'int[]'], values: [[1,2], [3,4]],
//expected: '0000000000000000000000000000000000000000000000000000000000000040' +
//'00000000000000000000000000000000000000000000000000000000000000a0' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000003' +
//'0000000000000000000000000000000000000000000000000000000000000004'});
//test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000005'});
//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'});
});
});