real && ureal types

This commit is contained in:
debris 2015-07-28 15:56:18 +02:00
parent 5ec50f3ee5
commit 3f8ec7ed13
5 changed files with 169 additions and 87 deletions

View File

@ -32,6 +32,8 @@ var SolidityTypeInt = require('./int');
var SolidityTypeUInt = require('./uint');
var SolidityTypeDynamicBytes = require('./dynamicbytes');
var SolidityTypeString = require('./string');
var SolidityTypeReal = require('./real');
var SolidityTypeUReal = require('./ureal');
/**
* SolidityCoder prototype should be used to encode/decode solidity params of any type
@ -247,38 +249,6 @@ SolidityTypeBytes.prototype.staticPartLength = function (name) {
return parseInt(name.match(/^bytes([0-9]{1,3})/)[1]);
};
var SolidityTypeReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputReal;
};
SolidityTypeReal.prototype = new SolidityType({});
SolidityTypeReal.prototype.constructor = SolidityTypeReal;
SolidityTypeReal.prototype.isType = function (name) {
return !!name.match(/^real([0-9]{1,3})?/);
};
SolidityTypeReal.prototype.staticPartLength = function (name) {
return 32;
};
var SolidityTypeUReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputUReal;
};
SolidityTypeUReal.prototype = new SolidityType({});
SolidityTypeUReal.prototype.constructor = SolidityTypeUReal;
SolidityTypeUReal.prototype.isType = function (name) {
return !!name.match(/^ureal([0-9]{1,3})?/);
};
SolidityTypeUReal.prototype.staticPartLength = function (name) {
return 32;
};
var coder = new SolidityCoder([
new SolidityTypeAddress(),
new SolidityTypeBool(),

57
lib/solidity/real.js Normal file
View File

@ -0,0 +1,57 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeReal is a prootype that represents real type
* It matches:
* real
* real[]
* real[4]
* real[][]
* real[3][]
* real[][6][], ...
* real32
* real64[]
* real8[4]
* real256[][]
* real[3][]
* real64[][6][], ...
*/
var SolidityTypeReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputReal;
};
SolidityTypeReal.prototype = new SolidityType({});
SolidityTypeReal.prototype.constructor = SolidityTypeReal;
SolidityTypeReal.prototype.isType = function (name) {
return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/);
};
SolidityTypeReal.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
SolidityTypeReal.prototype.isDynamicArray = function (name) {
var matches = name.match(/real([0-9]*)?(\[([0-9]*)\])?/);
// is array && doesn't have length specified
return !!matches[2] && !matches[3];
};
SolidityTypeReal.prototype.isStaticArray = function (name) {
var matches = name.match(/real([0-9]*)?(\[([0-9]*)\])?/);
// is array && have length specified
return !!matches[2] && !!matches[3];
};
SolidityTypeReal.prototype.staticArrayLength = function (name) {
return name.match(/real([0-9]*)?(\[([0-9]*)\])?/)[3] || 1;
};
SolidityTypeReal.prototype.nestedName = function (name) {
// removes first [] in name
return name.replace(/\[([0-9])*\]/, '');
};
module.exports = SolidityTypeReal;

57
lib/solidity/ureal.js Normal file
View File

@ -0,0 +1,57 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeUReal is a prootype that represents ureal type
* It matches:
* ureal
* ureal[]
* ureal[4]
* ureal[][]
* ureal[3][]
* ureal[][6][], ...
* ureal32
* ureal64[]
* ureal8[4]
* ureal256[][]
* ureal[3][]
* ureal64[][6][], ...
*/
var SolidityTypeUReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputUReal;
};
SolidityTypeUReal.prototype = new SolidityType({});
SolidityTypeUReal.prototype.constructor = SolidityTypeUReal;
SolidityTypeUReal.prototype.isType = function (name) {
return !!name.match(/ureal([0-9]*)?(\[([0-9]*)\])?/);
};
SolidityTypeUReal.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
SolidityTypeUReal.prototype.isDynamicArray = function (name) {
var matches = name.match(/ureal([0-9]*)?(\[([0-9]*)\])?/);
// is array && doesn't have length specified
return !!matches[2] && !matches[3];
};
SolidityTypeUReal.prototype.isStaticArray = function (name) {
var matches = name.match(/ureal([0-9]*)?(\[([0-9]*)\])?/);
// is array && have length specified
return !!matches[2] && !!matches[3];
};
SolidityTypeUReal.prototype.staticArrayLength = function (name) {
return name.match(/ureal([0-9]*)?(\[([0-9]*)\])?/)[3] || 1;
};
SolidityTypeUReal.prototype.nestedName = function (name) {
// removes first [] in name
return name.replace(/\[([0-9])*\]/, '');
};
module.exports = SolidityTypeUReal;

View File

@ -221,22 +221,20 @@ describe('lib/solidity/coder', function () {
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
//value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bool', expected: true, value: '0000000000000000000000000000000000000000000000000000000000000001'});
//test({ type: 'bool', expected: false, value: '0000000000000000000000000000000000000000000000000000000000000000'});
//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'});
//test({ type: 'real', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
//test({ type: 'ureal', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
//test({ type: 'ureal', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
//test({ type: 'ureal', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
//test({ type: 'address', expected: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
//value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
//test({ type: 'string', expected: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
//value: '0000000000000000000000000000000000000000000000000000000000000020' +
//'000000000000000000000000000000000000000000000000000000000000003e' +
//'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
//'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
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'});
test({ type: 'real', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
test({ type: 'ureal', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'ureal', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'ureal', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'address', expected: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'string', expected: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'000000000000000000000000000000000000000000000000000000000000003e' +
'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
});
});
@ -309,24 +307,24 @@ describe('lib/solidity/coder', function () {
'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', '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

@ -144,27 +144,27 @@ describe('lib/solidity/coder', function () {
//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: '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' +