2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BigNumber
|
|
|
|
*
|
|
|
|
* A wrapper around the BN.js object. We use the BN.js library
|
|
|
|
* because it is used by elliptic, so it is required regardles.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
import BN from 'bn.js';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
import { Hexable, hexlify, isArrayish, isHexString } from './bytes';
|
|
|
|
import { defineReadOnly, isType, setType } from './properties';
|
2018-07-15 00:19:08 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
import { Arrayish } from './bytes';
|
2018-07-15 00:19:08 +03:00
|
|
|
|
2018-09-24 22:55:17 +03:00
|
|
|
import * as errors from '../errors';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-13 03:07:47 +03:00
|
|
|
const BN_1 = new BN.BN(-1);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-13 03:07:47 +03:00
|
|
|
function toHex(bn: BN.BN): string {
|
2018-06-23 03:30:50 +03:00
|
|
|
let value = bn.toString(16);
|
|
|
|
if (value[0] === '-') {
|
2018-07-13 03:07:47 +03:00
|
|
|
if ((value.length % 2) === 0) {
|
|
|
|
return '-0x0' + value.substring(1);
|
|
|
|
}
|
|
|
|
return "-0x" + value.substring(1);
|
2018-06-23 03:30:50 +03:00
|
|
|
}
|
2018-07-13 03:07:47 +03:00
|
|
|
if ((value.length % 2) === 1) { return '0x0' + value; }
|
|
|
|
return '0x' + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toBN(value: BigNumberish): BN.BN {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(bigNumberify(value));
|
2018-07-13 03:07:47 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 10:27:49 +03:00
|
|
|
function toBigNumber(bn: BN.BN): BigNumber {
|
|
|
|
return new BigNumber(toHex(bn));
|
2018-06-23 03:30:50 +03:00
|
|
|
}
|
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
function _bnify(value: BigNumber): BN.BN {
|
|
|
|
let hex: string = (<any>value)._hex;
|
|
|
|
if (hex[0] === '-') {
|
|
|
|
return (new BN.BN(hex.substring(3), 16)).mul(BN_1);
|
|
|
|
}
|
|
|
|
return new BN.BN(hex.substring(2), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type BigNumberish = BigNumber | string | number | Arrayish;
|
|
|
|
|
|
|
|
export class BigNumber implements Hexable {
|
2018-07-13 03:07:47 +03:00
|
|
|
private readonly _hex: string;
|
2018-06-23 03:30:50 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
constructor(value: BigNumberish) {
|
2018-07-16 10:27:49 +03:00
|
|
|
errors.checkNew(this, BigNumber);
|
2018-07-31 01:59:52 +03:00
|
|
|
setType(this, 'BigNumber');
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
if (typeof(value) === 'string') {
|
|
|
|
if (isHexString(value)) {
|
|
|
|
if (value == '0x') { value = '0x0'; }
|
2018-07-13 03:07:47 +03:00
|
|
|
defineReadOnly(this, '_hex', value);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
} else if (value[0] === '-' && isHexString(value.substring(1))) {
|
2018-07-13 03:07:47 +03:00
|
|
|
defineReadOnly(this, '_hex', value);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
} else if (value.match(/^-?[0-9]*$/)) {
|
|
|
|
if (value == '') { value = '0'; }
|
2018-07-13 03:07:47 +03:00
|
|
|
defineReadOnly(this, '_hex', toHex(new BN.BN(value)));
|
2018-07-03 21:58:24 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
errors.throwError('invalid BigNumber string value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (typeof(value) === 'number') {
|
2018-06-23 03:30:50 +03:00
|
|
|
if (parseInt(String(value)) !== value) {
|
|
|
|
errors.throwError('underflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'underflow', value: value, outputValue: parseInt(String(value)) });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
try {
|
2018-07-13 03:07:47 +03:00
|
|
|
defineReadOnly(this, '_hex', toHex(new BN.BN(value)));
|
2018-06-13 22:39:39 +03:00
|
|
|
} catch (error) {
|
|
|
|
errors.throwError('overflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'overflow', details: error.message });
|
|
|
|
}
|
|
|
|
|
2018-07-16 10:59:25 +03:00
|
|
|
} else if (value instanceof BigNumber) {
|
|
|
|
defineReadOnly(this, '_hex', value._hex);
|
2018-07-13 03:07:47 +03:00
|
|
|
|
|
|
|
} else if ((<any>value).toHexString) {
|
|
|
|
defineReadOnly(this, '_hex', toHex(toBN((<any>value).toHexString())));
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-10-04 02:54:38 +03:00
|
|
|
} else if ((<any>value)._hex && isHexString((<any>value)._hex)) {
|
|
|
|
defineReadOnly(this, '_hex', (<any>value)._hex);
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
} else if (isArrayish(value)) {
|
2018-07-13 03:07:47 +03:00
|
|
|
defineReadOnly(this, '_hex', toHex(new BN.BN(hexlify(value).substring(2), 16)));
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
errors.throwError('invalid BigNumber value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
fromTwos(value: number): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).fromTwos(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
toTwos(value: number): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).toTwos(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-12-13 00:10:28 +03:00
|
|
|
abs(): BigNumber {
|
|
|
|
if (this._hex[0] === '-') {
|
|
|
|
return toBigNumber(_bnify(this).mul(BN_1));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
add(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).add(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sub(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).sub(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
div(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
let o: BigNumber = bigNumberify(other);
|
2018-06-13 22:39:39 +03:00
|
|
|
if (o.isZero()) {
|
|
|
|
errors.throwError('division by zero', errors.NUMERIC_FAULT, { operation: 'divide', fault: 'division by zero' });
|
|
|
|
}
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).div(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mul(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).mul(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mod(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).mod(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pow(other: BigNumberish): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).pow(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
maskn(value: number): BigNumber {
|
2018-07-31 01:59:52 +03:00
|
|
|
return toBigNumber(_bnify(this).maskn(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
eq(other: BigNumberish): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).eq(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lt(other: BigNumberish): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).lt(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lte(other: BigNumberish): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).lte(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gt(other: BigNumberish): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).gt(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gte(other: BigNumberish): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).gte(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isZero(): boolean {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).isZero();
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toNumber(): number {
|
|
|
|
try {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).toNumber();
|
2018-06-13 22:39:39 +03:00
|
|
|
} catch (error) {
|
|
|
|
errors.throwError('overflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'overflow', details: error.message });
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
2018-07-31 01:59:52 +03:00
|
|
|
return _bnify(this).toString(10);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toHexString(): string {
|
2018-07-13 03:07:47 +03:00
|
|
|
return this._hex;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-07-31 01:59:52 +03:00
|
|
|
|
|
|
|
static isBigNumber(value: any): value is BigNumber {
|
|
|
|
return isType(value, 'BigNumber');
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
export function bigNumberify(value: BigNumberish): BigNumber {
|
2018-07-26 04:15:43 +03:00
|
|
|
if (BigNumber.isBigNumber(value)) { return value; }
|
2018-07-16 10:27:49 +03:00
|
|
|
return new BigNumber(value);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|