diff --git a/packages/util/docs/bn.md b/packages/util/docs/bn.md index 084bb4b3c..ffb7215e7 100644 --- a/packages/util/docs/bn.md +++ b/packages/util/docs/bn.md @@ -3,6 +3,7 @@ Utility methods to convert to and from `BN` objects - [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string. +- [bnToBn](#bntobn) Creates a BN value from a BN.js bignumber or number input. - [bnToHex](#bntohex) Creates a hex value from a BN.js bignumber object. - [bnToU8a](#bntou8a) Creates a Uint8Array object from a BN. @@ -18,6 +19,25 @@ bnFromHex (value?: string): BN +## bnToBn + +Creates a BN value from a BN.js bignumber or number input. + +```js +bnToBn (value?: BN : number): BN +``` + + +`null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation. + +```js +import BN from 'bn.js'; +import { bnToBn } from '@polkadot/util'; + +bnToBn(0x1234); // => BN(0x1234) +bnToBn(new BN(0x1234)); // => BN(0x1234) +``` + ## bnToHex Creates a hex value from a BN.js bignumber object. @@ -41,7 +61,7 @@ bnToHex(new BN(0x123456)); // => '0x123456' Creates a Uint8Array object from a BN. ```js -bnToU8a (value?: BN): Uint8Array +bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array ``` diff --git a/packages/util/src/bn/index.js b/packages/util/src/bn/index.js index 0acf9a9b5..d530ca5da 100644 --- a/packages/util/src/bn/index.js +++ b/packages/util/src/bn/index.js @@ -4,6 +4,7 @@ // @flow const bnFromHex = require('./fromHex'); +const bnToBn = require('./toBn'); const bnToHex = require('./toHex'); const bnToU8a = require('./toU8a'); @@ -12,6 +13,7 @@ const bnToU8a = require('./toU8a'); */ module.exports = { bnFromHex, + bnToBn, bnToHex, bnToU8a }; diff --git a/packages/util/src/bn/toBn.js b/packages/util/src/bn/toBn.js new file mode 100644 index 000000000..b7231256c --- /dev/null +++ b/packages/util/src/bn/toBn.js @@ -0,0 +1,31 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const BN = require('bn.js'); + +/** + @name bnToBn + @signature bnToBn (value?: BN : number): BN + @summary Creates a BN value from a BN.js bignumber or number input. + @description + `null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation. + @example + import BN from 'bn.js'; + import { bnToBn } from '@polkadot/util'; + + bnToBn(0x1234); // => BN(0x1234) + bnToBn(new BN(0x1234)); // => BN(0x1234) +*/ +module.exports = function bnToBn (value?: BN | number): BN { + // flowlint-next-line sketchy-null-number:off + if (!value) { + return new BN(0); + } + + return BN.isBN(value) + // $FlowFixMe type is determined + ? value + : new BN(value); +}; diff --git a/packages/util/src/bn/toBn.spec.js b/packages/util/src/bn/toBn.spec.js new file mode 100644 index 000000000..9bee008fe --- /dev/null +++ b/packages/util/src/bn/toBn.spec.js @@ -0,0 +1,27 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +const BN = require('bn.js'); + +const { bnToBn } = require('./index'); + +describe('bnToBn', () => { + it('converts null values to 0x00', () => { + expect( + bnToBn(null) + ).toEqual(new BN(0)); + }); + + it('converts BN values to BN', () => { + expect( + bnToBn(new BN(128)) + ).toEqual(new BN(128)); + }); + + it('converts number values to BN', () => { + expect( + bnToBn(128) + ).toEqual(new BN(128)); + }); +}); diff --git a/packages/util/src/bn/toHex.js b/packages/util/src/bn/toHex.js index 21c993307..a9df29526 100644 --- a/packages/util/src/bn/toHex.js +++ b/packages/util/src/bn/toHex.js @@ -6,6 +6,7 @@ const BN = require('bn.js'); const hexFixLength = require('../hex/fixLength'); +const bnToBn = require('./toBn'); const ZERO_STR = '0x00'; @@ -21,10 +22,11 @@ const ZERO_STR = '0x00'; bnToHex(new BN(0x123456)); // => '0x123456' */ -module.exports = function bnToHex (value?: BN, bitLength: number = -1): string { +module.exports = function bnToHex (value?: BN | number, bitLength: number = -1): string { + // flowlint-next-line sketchy-null-number:off if (!value) { return ZERO_STR; } - return hexFixLength(value.toString(16), bitLength, true); + return hexFixLength(bnToBn(value).toString(16), bitLength, true); }; diff --git a/packages/util/src/bn/toU8a.js b/packages/util/src/bn/toU8a.js index fcf9d26f2..93a92c8e6 100644 --- a/packages/util/src/bn/toU8a.js +++ b/packages/util/src/bn/toU8a.js @@ -5,12 +5,13 @@ import type BN from 'bn.js'; -const bnToHex = require('./toHex'); const hexToU8a = require('../hex/toU8a'); +const bnToBn = require('./toBn'); +const bnToHex = require('./toHex'); /** @name bnToU8a - @signature bnToU8a (value?: BN): Uint8Array + @signature bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array @summary Creates a Uint8Array object from a BN. @description `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`. @@ -19,12 +20,13 @@ const hexToU8a = require('../hex/toU8a'); bnToU8a(new BN(0x1234)); // => [0x12, 0x34] */ -module.exports = function bnToU8a (value?: BN, bitLength: number = -1): Uint8Array { +module.exports = function bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array { + // flowlint-next-line sketchy-null-number:off if (!value) { return new Uint8Array([]); } return hexToU8a( - bnToHex(value, bitLength) + bnToHex(bnToBn(value), bitLength) ); }; diff --git a/packages/util/src/hex/toBn.js b/packages/util/src/hex/toBn.js index 1f8b72099..c8e5ddb08 100644 --- a/packages/util/src/hex/toBn.js +++ b/packages/util/src/hex/toBn.js @@ -24,7 +24,5 @@ module.exports = function hexToBn (value?: string): BN { return new BN(0); } - console.log('value', value, hexStripPrefix(value), hexStripPrefix(value) || '0'); - return new BN(hexStripPrefix(value) || '0', 16); };