encoding/decoding all tests passing

This commit is contained in:
debris 2015-07-28 16:47:56 +02:00
parent 96543d57c0
commit ae34877774
6 changed files with 96 additions and 57 deletions

59
lib/solidity/bytes.js Normal file
View File

@ -0,0 +1,59 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeBytes is a prootype that represents bytes type
* It matches:
* bytes
* bytes[]
* bytes[4]
* bytes[][]
* bytes[3][]
* bytes[][6][], ...
* bytes32
* bytes64[]
* bytes8[4]
* bytes256[][]
* bytes[3][]
* bytes64[][6][], ...
*/
var SolidityTypeBytes = function () {
this._inputFormatter = f.formatInputBytes;
this._outputFormatter = f.formatOutputBytes;
};
SolidityTypeBytes.prototype = new SolidityType({});
SolidityTypeBytes.prototype.constructor = SolidityTypeBytes;
SolidityTypeBytes.prototype.isType = function (name) {
return !!name.match(/bytes([0-9]*)(\[([0-9]*)\])?/);
};
SolidityTypeBytes.prototype.staticPartLength = function (name) {
var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/);
var size = parseInt(matches[1]);
return size * this.staticArrayLength(name);
};
SolidityTypeBytes.prototype.isDynamicArray = function (name) {
var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/);
// is array && doesn't have length specified
return !!matches[2] && !matches[3];
};
SolidityTypeBytes.prototype.isStaticArray = function (name) {
var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/);
// is array && have length specified
return !!matches[2] && !!matches[3];
};
SolidityTypeBytes.prototype.staticArrayLength = function (name) {
return name.match(/bytes([0-9]*)(\[([0-9]*)\])?/)[3] || 1;
};
SolidityTypeBytes.prototype.nestedName = function (name) {
// removes first [] in name
return name.replace(/\[([0-9])*\]/, '');
};
module.exports = SolidityTypeBytes;

View File

@ -34,6 +34,7 @@ var SolidityTypeDynamicBytes = require('./dynamicbytes');
var SolidityTypeString = require('./string');
var SolidityTypeReal = require('./real');
var SolidityTypeUReal = require('./ureal');
var SolidityTypeBytes = require('./bytes');
/**
* SolidityCoder prototype should be used to encode/decode solidity params of any type
@ -231,24 +232,6 @@ SolidityCoder.prototype.getSolidityTypes = function (types) {
});
};
var SolidityTypeBytes = function () {
this._inputFormatter = f.formatInputBytes;
this._outputFormatter = f.formatOutputBytes;
};
SolidityTypeBytes.prototype = new SolidityType({});
SolidityTypeBytes.prototype.constructor = SolidityTypeBytes;
SolidityTypeBytes.prototype.isType = function (name) {
return !!name.match(/^bytes([0-9]{1,3})/);
};
SolidityTypeBytes.prototype.staticPartLength = function (name) {
return parseInt(name.match(/^bytes([0-9]{1,3})/)[1]);
};
var coder = new SolidityCoder([
new SolidityTypeAddress(),
new SolidityTypeBool(),

View File

@ -14,22 +14,25 @@ SolidityTypeDynamicBytes.prototype.staticPartLength = function (name) {
};
SolidityTypeDynamicBytes.prototype.isType = function (name) {
return !!name.match(/bytes(\[([0-9]*)\])?/);
return !!name.match(/^bytes(\[([0-9]*)\])*$/);
};
SolidityTypeDynamicBytes.prototype.isDynamicArray = function (name) {
var matches = name.match(/bytes(\[([0-9]*)\])?/);
// use * not ?. we do not want to match all []
var matches = name.match(/^bytes(\[([0-9]*)\])?/);
// is array && doesn't have length specified
return !!matches[1] && !matches[2];
};
SolidityTypeDynamicBytes.prototype.isStaticArray = function (name) {
// use * not ?. we do not want to match all []
var matches = name.match(/bytes(\[([0-9]*)\])?/);
// is array && have length specified
return !!matches[1] && !!matches[2];
};
SolidityTypeDynamicBytes.prototype.staticArrayLength = function (name) {
// use * not ?. we do not want to match all []
return name.match(/bytes(\[([0-9]*)\])?/)[2] || 1;
};

View File

@ -67,7 +67,6 @@ 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);
};
@ -83,7 +82,6 @@ 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);
};
@ -204,6 +202,7 @@ var formatOutputBytes = function (param) {
* @returns {String} hex string
*/
var formatOutputDynamicBytes = function (param) {
console.log(param);
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return '0x' + param.dynamicPart().substr(64, length);
};

View File

@ -188,16 +188,13 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000020' + // 180 //
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134d'});
//test({ type: 'bytes32', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
//test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
//value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//value: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
////test({ type: 'bytes64', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
////'c3a40000c3a40000000000000000000000000000000000000000000000000000',
////value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
////'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes64', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' +
@ -219,8 +216,8 @@ describe('lib/solidity/coder', function () {
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
//value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'real', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
@ -286,13 +283,12 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)],
//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: ['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' +

View File

@ -89,12 +89,12 @@ describe('lib/solidity/coder', function () {
test({ type: 'uint', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'uint256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
//test({ type: 'bytes32', value: '0x6761766f66796f726b',
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
//expected: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
//test({ type: 'bytes32', value: '0x02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29',
//expected: '02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29'});
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' +
@ -110,8 +110,8 @@ describe('lib/solidity/coder', function () {
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', value: '0xc3a40000c3a4',
//expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0xc3a40000c3a4',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes64', value: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000',
//expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
@ -232,9 +232,8 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
//test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
//test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
@ -247,12 +246,12 @@ describe('lib/solidity/coder', function () {
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
//test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
//expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
//'0000000000000000000000000000000000000000000000000000000000000005'});
//test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
//expected: '0000000000000000000000000000000000000000000000000000000000000005' +
//'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'string'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +