Truly immutable BigNumber and hidden constructor.

This commit is contained in:
Richard Moore 2018-07-12 20:07:47 -04:00
parent df2a00a2fc
commit 0ecfe4bafd
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295

@ -12,39 +12,72 @@ import BN from 'bn.js';
import { Arrayish, hexlify, isArrayish, isHexString } from './bytes';
import { defineReadOnly } from './properties';
import * as errors from '../utils/errors';
import * as errors from './errors';
function _isBigNumber(value: any): value is BigNumber {
return isBigNumber(value);
}
const BN_1 = new BN.BN(-1);
export type BigNumberish = BigNumber | string | number | Arrayish;
function fromBN(bn: BN.BN) {
function toHex(bn: BN.BN): string {
let value = bn.toString(16);
if (value[0] === '-') {
return new BigNumber("-0x" + value.substring(1));
if ((value.length % 2) === 0) {
return '-0x0' + value.substring(1);
}
return new BigNumber('0x' + value);
return "-0x" + value.substring(1);
}
if ((value.length % 2) === 1) { return '0x0' + value; }
return '0x' + value;
}
export class BigNumber {
private readonly _bn: BN.BN;
function toBN(value: BigNumberish): BN.BN {
return (<_BigNumber>bigNumberify(value))._bn;
}
function toBigNumber(bn: BN.BN): _BigNumber {
return new _BigNumber(toHex(bn));
}
export interface BigNumber {
fromTwos(value: number): BigNumber;
toTwos(value: number): BigNumber;
add(other: BigNumberish): BigNumber;
sub(other: BigNumberish): BigNumber;
div(other: BigNumberish): BigNumber;
mul(other: BigNumberish): BigNumber;
mod(other: BigNumberish): BigNumber;
pow(other: BigNumberish): BigNumber;
maskn(value: number): BigNumber;
eq(other: BigNumberish): boolean;
lt(other: BigNumberish): boolean;
lte(other: BigNumberish): boolean;
gt(other: BigNumberish): boolean;
gte(other: BigNumberish): boolean;
isZero(): boolean;
toNumber(): number;
toString(): string;
toHexString(): string;
};
class _BigNumber implements BigNumber {
private readonly _hex: string;
constructor(value: BigNumberish) {
errors.checkNew(this, BigNumber);
errors.checkNew(this, _BigNumber);
if (typeof(value) === 'string') {
if (isHexString(value)) {
if (value == '0x') { value = '0x0'; }
defineReadOnly(this, '_bn', new BN.BN(value.substring(2), 16));
defineReadOnly(this, '_hex', value);
} else if (value[0] === '-' && isHexString(value.substring(1))) {
defineReadOnly(this, '_bn', (new BN.BN(value.substring(3), 16)).mul(ConstantNegativeOne._bn));
defineReadOnly(this, '_hex', value);
} else if (value.match(/^-?[0-9]*$/)) {
if (value == '') { value = '0'; }
defineReadOnly(this, '_bn', new BN.BN(value));
defineReadOnly(this, '_hex', toHex(new BN.BN(value)));
} else {
errors.throwError('invalid BigNumber string value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
@ -55,36 +88,46 @@ export class BigNumber {
errors.throwError('underflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'underflow', value: value, outputValue: parseInt(String(value)) });
}
try {
defineReadOnly(this, '_bn', new BN.BN(value));
defineReadOnly(this, '_hex', toHex(new BN.BN(value)));
} catch (error) {
errors.throwError('overflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'overflow', details: error.message });
}
} else if (_isBigNumber(value)) {
defineReadOnly(this, '_bn', value._bn);
} else if (value instanceof _BigNumber) {
defineReadOnly(this, '_hex', value.toHexString());
} else if ((<any>value).toHexString) {
defineReadOnly(this, '_hex', toHex(toBN((<any>value).toHexString())));
} else if (isArrayish(value)) {
defineReadOnly(this, '_bn', new BN.BN(hexlify(value).substring(2), 16));
defineReadOnly(this, '_hex', toHex(new BN.BN(hexlify(value).substring(2), 16)));
} else {
errors.throwError('invalid BigNumber value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
}
get _bn(): BN.BN {
if (this._hex[0] === '-') {
return (new BN.BN(this._hex.substring(3), 16)).mul(BN_1);
}
return new BN.BN(this._hex.substring(2), 16);
}
fromTwos(value: number): BigNumber {
return fromBN(this._bn.fromTwos(value));
return toBigNumber(this._bn.fromTwos(value));
}
toTwos(value: number): BigNumber {
return fromBN(this._bn.toTwos(value));
return toBigNumber(this._bn.toTwos(value));
}
add(other: BigNumberish): BigNumber {
return fromBN(this._bn.add(bigNumberify(other)._bn));
return toBigNumber(this._bn.add(toBN(other)));
}
sub(other: BigNumberish): BigNumber {
return fromBN(this._bn.sub(bigNumberify(other)._bn));
return toBigNumber(this._bn.sub(toBN(other)));
}
div(other: BigNumberish): BigNumber {
@ -92,43 +135,43 @@ export class BigNumber {
if (o.isZero()) {
errors.throwError('division by zero', errors.NUMERIC_FAULT, { operation: 'divide', fault: 'division by zero' });
}
return fromBN(this._bn.div(o._bn));
return toBigNumber(this._bn.div(toBN(other)));
}
mul(other: BigNumberish): BigNumber {
return fromBN(this._bn.mul(bigNumberify(other)._bn));
return toBigNumber(this._bn.mul(toBN(other)));
}
mod(other: BigNumberish): BigNumber {
return fromBN(this._bn.mod(bigNumberify(other)._bn));
return toBigNumber(this._bn.mod(toBN(other)));
}
pow(other: BigNumberish): BigNumber {
return fromBN(this._bn.pow(bigNumberify(other)._bn));
return toBigNumber(this._bn.pow(toBN(other)));
}
maskn(value: number): BigNumber {
return fromBN(this._bn.maskn(value));
return toBigNumber(this._bn.maskn(value));
}
eq(other: BigNumberish): boolean {
return this._bn.eq(bigNumberify(other)._bn);
return this._bn.eq(toBN(other));
}
lt(other: BigNumberish): boolean {
return this._bn.lt(bigNumberify(other)._bn);
return this._bn.lt(toBN(other));
}
lte(other: BigNumberish): boolean {
return this._bn.lte(bigNumberify(other)._bn);
return this._bn.lte(toBN(other));
}
gt(other: BigNumberish): boolean {
return this._bn.gt(bigNumberify(other)._bn);
return this._bn.gt(toBN(other));
}
gte(other: BigNumberish): boolean {
return this._bn.gte(bigNumberify(other)._bn);
return this._bn.gte(toBN(other));
}
isZero(): boolean {
@ -149,19 +192,17 @@ export class BigNumber {
}
toHexString(): string {
var hex = this._bn.toString(16);
if (hex.length % 2) { hex = '0' + hex; }
return '0x' + hex;
return this._hex;
}
}
export function isBigNumber(value: any): boolean {
return (value._bn && value._bn.mod);
return (value instanceof _BigNumber);
}
export function bigNumberify(value: BigNumberish): BigNumber {
if (_isBigNumber(value)) { return value; }
return new BigNumber(value);
if (value instanceof _BigNumber) { return value; }
return new _BigNumber(value);
}
export const ConstantNegativeOne: BigNumber = bigNumberify(-1);