moved to BN.js

This commit is contained in:
Fabian Vogelsteller 2017-02-23 18:21:50 +01:00
parent 7593c52cd4
commit acf45217d3
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
23 changed files with 58 additions and 210 deletions

View File

@ -32,7 +32,7 @@
"lerna": "2.0.0-beta.32",
"crypto-js": "^3.1.4",
"underscore": "^1.8.3",
"bignumber.js": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
"bn.js": "^4.11.6"
},
"scripts": {
"postinstall": "lerna bootstrap",

View File

@ -23,7 +23,6 @@
"use strict";
// var BigNumber = require('bignumber.js');
// var ETH_UNITS = [
// 'wei',

View File

@ -37,7 +37,7 @@ var config = require('./config');
* @returns {BigNumber} object
*/
var outputBigNumberFormatter = function (number) {
return utils.toBigNumber(number).toFixed();
return utils.toBigNumber(number).toString(10);
};
var isPredefinedBlockNumber = function (blockNumber) {

View File

@ -12,7 +12,7 @@
"web3-utils": "^1.0.0",
"underscore": "^1.8.3",
"web3-core-promiEvent": "^1.0.0",
"bignumber.js": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
"number-to-bn": "^1.7.0",
"bn.js": "^4.11.6"
}
}

View File

@ -28,8 +28,6 @@ var SolidityTypeInt = require('./int');
var SolidityTypeUInt = require('./uint');
var SolidityTypeDynamicBytes = require('./dynamicbytes');
var SolidityTypeString = require('./string');
var SolidityTypeReal = require('./real');
var SolidityTypeUReal = require('./ureal');
var SolidityTypeBytes = require('./bytes');
var isDynamic = function (solidityType, type) {
@ -255,9 +253,7 @@ var coder = new SolidityCoder([
new SolidityTypeUInt(),
new SolidityTypeDynamicBytes(),
new SolidityTypeBytes(),
new SolidityTypeString(),
new SolidityTypeReal(),
new SolidityTypeUReal()
new SolidityTypeString()
]);
module.exports = coder;

View File

@ -21,7 +21,8 @@
*/
var _ = require('underscore');
var BigNumber = require('bignumber.js');
var numberToBN = require('number-to-bn');
var BigNumber = require('bn.js');
var utils = require('web3-utils');
var SolidityParam = require('./param');
@ -35,7 +36,7 @@ var SolidityParam = require('./param');
*/
var isBigNumber = function (object) {
return object instanceof BigNumber ||
(object && object.constructor && object.constructor.name === 'BigNumber');
(object && object.constructor && object.constructor.name === 'BN');
};
/**
@ -52,7 +53,7 @@ var toBigNumber = function(number) {
return number;
if (_.isString(number) && (number.indexOf('0x') === 0 || number.indexOf('-0x') === 0)) {
return new BigNumber(number.replace('0x',''), 16);
return numberToBN(number);
}
return new BigNumber(number.toString(10), 10);
@ -66,11 +67,7 @@ var toBigNumber = function(number) {
* @return {BigNumber}
*/
var toTwosComplement = function (number) {
var bigNumber = toBigNumber(number).round();
if (bigNumber.lessThan(0)) {
return new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(bigNumber).plus(1);
}
return bigNumber;
return toBigNumber(number).toTwos(256).toString(16, 64);
};
@ -84,11 +81,10 @@ var toTwosComplement = function (number) {
* @returns {SolidityParam}
*/
var formatInputInt = function (value) {
BigNumber.config({
ROUNDING_MODE: BigNumber.ROUND_DOWN
});
var result = utils.padLeft(toTwosComplement(value).toString(16), 64);
return new SolidityParam(result);
if(_.isNumber(value)) {
value = Math.trunc(value);
}
return new SolidityParam(toTwosComplement(value));
};
/**
@ -147,17 +143,6 @@ var formatInputBool = function (value) {
return new SolidityParam(result);
};
/**
* Formats input value to byte representation of real
* Values are multiplied by 2^m and encoded as integers
*
* @method formatInputReal
* @param {String|Number|BigNumber}
* @returns {SolidityParam}
*/
var formatInputReal = function (value) {
return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128)));
};
/**
* Check if input value is negative
@ -183,9 +168,9 @@ var formatOutputInt = function (param) {
// check if it's negative number
// it it is, return two's complement
if (signedIsNegative(value)) {
return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1).toFixed();
return new BigNumber(value, 16).fromTwos(256).toString(10);
}
return new BigNumber(value, 16).toFixed();
return new BigNumber(value, 16).toString(10);
};
/**
@ -197,30 +182,10 @@ var formatOutputInt = function (param) {
*/
var formatOutputUInt = function (param) {
var value = param.staticPart() || "0";
return new BigNumber(value, 16).toFixed();
return new BigNumber(value, 16).toString(10);
};
/**
* Formats right-aligned output bytes to real
*
* @method formatOutputReal
* @param {SolidityParam}
* @returns {BigNumber} input bytes formatted to real
*/
var formatOutputReal = function (param) {
return new BigNumber(formatOutputInt(param)).dividedBy(new BigNumber(2).pow(128)).toFixed();
};
/**
* Formats right-aligned output bytes to ureal
*
* @method formatOutputUReal
* @param {SolidityParam}
* @returns {BigNumber} input bytes formatted to ureal
*/
var formatOutputUReal = function (param) {
return new BigNumber(formatOutputUInt(param)).dividedBy(new BigNumber(2).pow(128)).toFixed();
};
/**
* Should be used to format output bool
@ -294,11 +259,8 @@ module.exports = {
formatInputDynamicBytes: formatInputDynamicBytes,
formatInputString: formatInputString,
formatInputBool: formatInputBool,
formatInputReal: formatInputReal,
formatOutputInt: formatOutputInt,
formatOutputUInt: formatOutputUInt,
formatOutputReal: formatOutputReal,
formatOutputUReal: formatOutputUReal,
formatOutputBool: formatOutputBool,
formatOutputBytes: formatOutputBytes,
formatOutputDynamicBytes: formatOutputDynamicBytes,

View File

@ -20,7 +20,6 @@
* @date 2015
*/
var utils = require('web3-utils');
var formatters = require('./formatters.js');
/**
@ -83,7 +82,7 @@ SolidityParam.prototype.isDynamic = function () {
* @returns {String} bytes representation of offset
*/
SolidityParam.prototype.offsetAsBytes = function () {
return !this.isDynamic() ? '' : utils.padLeft(formatters.toTwosComplement(this.offset).toString(16), 64);
return !this.isDynamic() ? '' : formatters.toTwosComplement(this.offset);
};
/**

View File

@ -1,32 +0,0 @@
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]*)\])?/);
};
module.exports = SolidityTypeReal;

View File

@ -1,32 +0,0 @@
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]*)\])*$/);
};
module.exports = SolidityTypeUReal;

View File

@ -7,6 +7,7 @@
"main": "src/index.js",
"dependencies": {
"web3-utils": "^1.0.0",
"bignumber.js": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
"bases": "^0.2.1",
"bn.js": "^4.11.6"
}
}

View File

@ -27,7 +27,7 @@
var utils = require('web3-utils');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var padLeft = function (string, bytes) {
@ -132,6 +132,8 @@ Iban.fromAddress = function (address) {
throw new Error('Provided address is not a valid address: '+ address);
}
address = address.replace('0x','').replace('0X','');
var asBn = new BigNumber(address, 16);
var base36 = asBn.toString(36);
var padded = padLeft(base36, 15);
@ -253,7 +255,7 @@ Iban.prototype.toAddress = function () {
if (this.isDirect()) {
var base36 = this._iban.substr(4);
var asBn = new BigNumber(base36, 36);
return utils.toChecksumAddress(padLeft(asBn.toString(16), 20));
return utils.toChecksumAddress(asBn.toString(16, 20));
}
return '';

View File

@ -7,8 +7,10 @@
"main": "src/index.js",
"dependencies": {
"utf8": "^2.1.1",
"ethjs-sha3": "0.1.2",
"bignumber.js": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
"js-sha3": "^0.5.7",
"ethjs-unit": "^0.1.6",
"number-to-bn": "^1.7.0",
"bn.js": "^4.11.6"
}
}

View File

@ -22,39 +22,12 @@
*/
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var ethjsUnit = require('ethjs-unit');
var numberToBN = require('number-to-bn');
var utf8 = require('utf8');
var ethjsSha3 = require("ethjs-sha3");
var keccak256 = require("js-sha3").keccak_256; // jshint ignore:line
var unitMap = {
'noether': '0',
'wei': '1',
'kwei': '1000',
'Kwei': '1000',
'babbage': '1000',
'femtoether': '1000',
'mwei': '1000000',
'Mwei': '1000000',
'lovelace': '1000000',
'picoether': '1000000',
'gwei': '1000000000',
'Gwei': '1000000000',
'shannon': '1000000000',
'nanoether': '1000000000',
'nano': '1000000000',
'szabo': '1000000000000',
'microether': '1000000000000',
'micro': '1000000000000',
'finney': '1000000000000000',
'milliether': '1000000000000000',
'milli': '1000000000000000',
'ether': '1000000000000000000',
'kether': '1000000000000000000000',
'grand': '1000000000000000000000',
'mether': '1000000000000000000000000',
'gether': '1000000000000000000000000000',
'tether': '1000000000000000000000000000000'
};
/**
* Sha3 encodes
@ -62,9 +35,9 @@ var unitMap = {
* @method sha3
* @return {Object} the sha3
*/
var sha3 = function (value, returnBuffer) {
var sha3 = function (value) {
return ethjsSha3(value, returnBuffer);
return '0x'+ keccak256(value);
};
/**
@ -277,7 +250,7 @@ var fromDecimal = function (value) {
var number = toBigNumber(value);
var result = number.toString(16);
return number.lessThan(0) ? '-0x' + result.substr(1) : '0x' + result;
return number.lt(new BigNumber(0)) ? '-0x' + result.substr(1) : '0x' + result;
};
/**
@ -317,18 +290,17 @@ var toHex = function (val) {
/**
* Returns value of unit in Wei
*
* @method getValueOfUnit
* @method getUnitValue
* @param {String} unit the unit to convert to, default ether
* @returns {BigNumber} value of the unit (in Wei)
* @throws error if the unit is not correct:w
*/
var getValueOfUnit = function (unit) {
var getUnitValue = function (unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = unitMap[unit];
if (unitValue === undefined) {
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
if (!ethjsUnit.unitMap[unit]) {
throw new Error('This unit "'+ unit +'" doesn\'t exist, please use the one of the following units' + JSON.stringify(ethjsUnit.unitMap, null, 2));
}
return new BigNumber(unitValue, 10);
return unit;
};
/**
@ -353,9 +325,9 @@ var getValueOfUnit = function (unit) {
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var fromWei = function(number, unit) {
var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit));
unit = getUnitValue(unit);
return isBigNumber(number) ? returnValue : returnValue.toString(10);
return isBigNumber(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10);
};
/**
@ -381,9 +353,9 @@ var fromWei = function(number, unit) {
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var toWei = function(number, unit) {
var returnValue = toBigNumber(number).times(getValueOfUnit(unit));
unit = getUnitValue(unit);
return isBigNumber(number) ? returnValue : returnValue.toString(10);
return isBigNumber(number) ? ethjsUnit.toWei(number, unit) : ethjsUnit.toWei(number, unit).toString(10);
};
/**
@ -400,7 +372,7 @@ var toBigNumber = function(number) {
return number;
if (isString(number) && (number.indexOf('0x') === 0 || number.indexOf('-0x') === 0)) {
return new BigNumber(number.replace('0x',''), 16);
return numberToBN(number);
}
return new BigNumber(number.toString(10), 10);
@ -504,7 +476,7 @@ var toAddress = function (address) {
*/
var isBigNumber = function (object) {
return object instanceof BigNumber ||
(object && object.constructor && object.constructor.name === 'BigNumber');
(object && object.constructor && object.constructor.name === 'BN');
};
/**
@ -606,7 +578,7 @@ module.exports = {
isBoolean: isBoolean,
isArray: isArray,
isJson: isJson,
sha3: require("ethjs-sha3"),
sha3: sha3,
// iban: iban
};

View File

@ -221,13 +221,6 @@ describe('lib/solidity/coder', function () {
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'real', expected: '1', value: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', expected: '2.125', value: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', expected: '8.5', value: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'real', expected: '-1', value: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
test({ type: 'ureal', expected: '1', value: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'ureal', expected: '2.125', value: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'ureal', expected: '8.5', value: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'address', expected: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1',
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'string', expected: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
@ -360,17 +353,9 @@ describe('lib/solidity/coder', function () {
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['real[2][1]', 'bool'], expected: [[['1', '2.125']], true],
values: '0000000000000000000000000000000100000000000000000000000000000000' +
'0000000000000000000000000000000220000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['uint[2][1]', 'bool'], expected: [[['1', '2']], true],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['ureal[2][1]', 'bool'], expected: [[['1', '2.125']], true],
values: '0000000000000000000000000000000100000000000000000000000000000000' +
'0000000000000000000000000000000220000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000001'});
});
});

View File

@ -166,13 +166,6 @@ 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' +

View File

@ -8,7 +8,7 @@ var net = new FakeIpcRequest();
SandboxedModule.registerBuiltInSourceTransformer('istanbul');
var IpcProvider = SandboxedModule.require('../packages/web3-core-requestmanager/src/providers/ipcprovider', {
requires: {
'bignumber.js': require('bignumber.js'),
'bn.js': require('bn.js'),
},
singleOnly: true
});

View File

@ -11,7 +11,7 @@ var tests = [
];
describe('formatters', function () {
describe('inputAddressFormatter', function () {
describe('formatInputInt', function () {
tests.forEach(function(test){
it('should return the correct value', function () {
assert.deepEqual(formatters.formatInputInt(test.input), test.result);

View File

@ -5,6 +5,7 @@ var assert = chai.assert;
var tests = [
{ value: 1, expected: '0x1' },
{value: '21345678976543214567869765432145647586', expected: '0x100f073a3d694d13d1615dc9bc3097e2'},
{ value: '1', expected: '0x1' },
{ value: '0x1', expected: '0x1'},
{ value: '0x01', expected: '0x1'},

View File

@ -1,7 +1,7 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var assert = chai.assert;

View File

@ -4,7 +4,6 @@ var sha3 = require('../packages/web3-utils').sha3;
var CryptoJS = require('crypto-js');
var cjsSha3 = require('crypto-js/sha3');
var method = 'sha3';
describe('web3.sha3', function () {
it('should return sha3 with hex prefix', function() {

View File

@ -1,7 +1,7 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var assert = chai.assert;
@ -40,7 +40,7 @@ describe('lib/utils/utils', function () {
describe('toBigNumber', function () {
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.equal(utils.toBigNumber(test.value).toFixed(), test.expected);
assert.equal(utils.toBigNumber(test.value).toString(10), test.expected);
});
});
});

View File

@ -5,10 +5,11 @@ describe('lib/utils/utils', function () {
describe('toDecimal', function () {
it('should return the correct value', function () {
assert.equal(utils.toDecimal("0x3e8"), '1000');
assert.equal(utils.toDecimal("0x3e8"), 1000);
assert.equal(utils.toDecimal('0x1f0fe294a36'), 2134567897654);
// allow compatiblity
assert.equal(utils.toDecimal(100000), '100000');
assert.equal(utils.toDecimal('100000'), '100000');
assert.equal(utils.toDecimal(100000), 100000);
assert.equal(utils.toDecimal('100000'), 100000);
});
});
});

View File

@ -1,7 +1,7 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var assert = chai.assert;