From 0e30c483790e3dc01a42e8a116ab977738fb1347 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 19 Dec 2018 18:43:44 +0100 Subject: [PATCH] Add conversion to negative BN's (#277) * Support negative number hexToBn * Fix u8aToBn * Update signature * Update params description --- packages/util/src/hex/toBn.spec.js | 49 ---------------- packages/util/src/hex/toBn.spec.ts | 80 +++++++++++++++++++++++++ packages/util/src/hex/toBn.ts | 25 ++++++-- packages/util/src/types.ts | 5 ++ packages/util/src/u8a/toBn.spec.js | 43 -------------- packages/util/src/u8a/toBn.spec.ts | 93 ++++++++++++++++++++++++++++++ packages/util/src/u8a/toBn.ts | 11 +++- 7 files changed, 207 insertions(+), 99 deletions(-) delete mode 100644 packages/util/src/hex/toBn.spec.js create mode 100644 packages/util/src/hex/toBn.spec.ts delete mode 100644 packages/util/src/u8a/toBn.spec.js create mode 100644 packages/util/src/u8a/toBn.spec.ts diff --git a/packages/util/src/hex/toBn.spec.js b/packages/util/src/hex/toBn.spec.js deleted file mode 100644 index 331d1458c..000000000 --- a/packages/util/src/hex/toBn.spec.js +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017-2018 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { hexToBn } from './index'; - -describe('hexToBn', () => { - it('converts prefixed hex values to BN', () => { - expect( - hexToBn(0x81).toString(16) - ).toEqual('81'); - }); - - it('converts null values to BN(0)', () => { - expect( - hexToBn(null).toString() - ).toEqual('0'); - }); - - it('converts 0x values to BN(0)', () => { - expect( - hexToBn('0x').toString() - ).toEqual('0'); - }); - - it('converts 0x values to BN(0) (LE)', () => { - expect( - hexToBn('0x', true).toString() - ).toEqual('0'); - }); - - it('converts little-endian', () => { - expect( - hexToBn('0x4500000000000000', true).toString() - ).toEqual('69'); - }); - - it('handles starting zeros correctly (BE)', () => { - expect( - hexToBn('0x0000000000000100', false).toString() - ).toEqual('256'); - }); - - it('handles starting zeros correctly (LE)', () => { - expect( - hexToBn('0x0001000000000000', true).toString() - ).toEqual('256'); - }); -}); diff --git a/packages/util/src/hex/toBn.spec.ts b/packages/util/src/hex/toBn.spec.ts new file mode 100644 index 000000000..ac96436d3 --- /dev/null +++ b/packages/util/src/hex/toBn.spec.ts @@ -0,0 +1,80 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { hexToBn } from './index'; + +describe('hexToBn', () => { + it('converts prefixed hex values to BN', () => { + expect( + hexToBn(0x81).toString(16) + ).toBe('81'); + }); + + it('converts null values to BN(0)', () => { + expect( + hexToBn(null).toNumber() + ).toBe(0); + }); + + it('converts 0x values to BN(0)', () => { + expect( + hexToBn('0x').toNumber() + ).toBe(0); + }); + + it('should convert with Big Endian by default', () => { + expect( + hexToBn('0x0100').toNumber() + ).toBe(256); + }); + + it('converts 0x values to BN(0) (LE)', () => { + expect( + hexToBn('0x', { isLe: true }).toNumber() + ).toBe(0); + }); + + it('converts little-endian', () => { + expect( + hexToBn('0x4500000000000000', { isLe: true }).toNumber() + ).toBe(69); + }); + + it('handles negative numbers (LE)', () => { + expect( + hexToBn('0xd600', { isLe: true, isNegative: true }).toNumber() + ).toBe(-42); + }); + + it('handles negative numbers (BE)', () => { + expect( + hexToBn('0x00d6', { isLe: false, isNegative: true }).toNumber() + ).toBe(-42); + }); + + it('handles starting zeros correctly (BE)', () => { + expect( + hexToBn('0x0000000000000100', { isLe: false }).toNumber() + ).toBe(256); + }); + + it('handles starting zeros correctly (LE)', () => { + expect( + hexToBn('0x0001000000000000', { isLe: true }).toNumber() + ).toBe(256); + }); + + it('handles backwards compatibility', () => { + expect( + hexToBn('0x0001000000000000', false).eq( + hexToBn('0x0001000000000000', { isLe: false }) + ) + ).toBe(true); + expect( + hexToBn('0x0001000000000000', true).eq( + hexToBn('0x0001000000000000', { isLe: true }) + ) + ).toBe(true); + }); +}); diff --git a/packages/util/src/hex/toBn.ts b/packages/util/src/hex/toBn.ts index 629dc956f..6d89c386e 100644 --- a/packages/util/src/hex/toBn.ts +++ b/packages/util/src/hex/toBn.ts @@ -4,7 +4,9 @@ import BN from 'bn.js'; +import isBoolean from '../is/boolean'; import hexStripPrefix from './stripPrefix'; +import { ToBnOptions } from '../types'; function reverse (value: string): string { return (value.match(new RegExp('.{1,2}', 'g')) || []) @@ -14,10 +16,14 @@ function reverse (value: string): string { /** * @name hexToBn - * @signature hexToBn (value?: string, isLe: boolean = false): BN + * @signature hexToBn (value?: string, _options: ToBnOptions | boolean = { isLe: false, isNegative: false }): BN * @summary Creates a BN.js bignumber object from a hex string. * @description * `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error. + * @param _value The value to convert + * @param _options Options to pass while converting + * @param _options.isLe Convert using Little Endian + * @param _options.isNegative Convert using two's complement * @example *
* @@ -27,12 +33,23 @@ function reverse (value: string): string { * hexToBn('0x123480001f'); // => BN(0x123480001f) * ``` */ -export default function hexToBn (_value?: string, isLe: boolean = false): BN { +export default function hexToBn ( + _value?: string | number | null, + _options: ToBnOptions | boolean = { isLe: false, isNegative: false } +): BN { if (!_value) { return new BN(0); } - const value = hexStripPrefix(_value); + const options: ToBnOptions = isBoolean(_options) + ? { isLe: _options, isNegative: false } + : _options; - return new BN((isLe ? reverse(value) : value) || '00', 16); + const value = hexStripPrefix(_value as string); + + // FIXME: Use BN's 3rd argument `isLe` once this issue is fixed + // https://github.com/indutny/bn.js/issues/208 + const bn = new BN((options.isLe ? reverse(value) : value) || '00', 16); + + return options.isNegative ? bn.fromTwos(8) : bn; } diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 6ec2a0b97..e45746f3a 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -19,3 +19,8 @@ export type Logger = { noop: (...values: Logger$Data) => void, warn: (...values: Logger$Data) => void }; + +export interface ToBnOptions { + isLe?: boolean; + isNegative?: boolean; +} diff --git a/packages/util/src/u8a/toBn.spec.js b/packages/util/src/u8a/toBn.spec.js deleted file mode 100644 index cf70bba06..000000000 --- a/packages/util/src/u8a/toBn.spec.js +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2017-2018 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { u8aToBn } from './index'; - -describe('u8aToBn', () => { - it('converts values (big-endian)', () => { - expect( - u8aToBn( - new Uint8Array([0x12, 0x34]), - false - ).toString(16) - ).toEqual('1234'); - }); - - it('converts values (little-endian)', () => { - expect( - u8aToBn( - new Uint8Array([0x12, 0x34]), - true - ).toString(16) - ).toEqual('3412'); - }); - - it('converts empty', () => { - expect( - u8aToBn( - new Uint8Array([]), - true - ).toString(16) - ).toEqual('0'); - }); - - it('handled overflows correctly (little-endian)', () => { - expect( - u8aToBn( - new Uint8Array([ 0, 1, 0, 0, 0, 0, 0, 0 ]), - true - ).toString() - ).toEqual('256'); - }); -}); diff --git a/packages/util/src/u8a/toBn.spec.ts b/packages/util/src/u8a/toBn.spec.ts new file mode 100644 index 000000000..5ac717945 --- /dev/null +++ b/packages/util/src/u8a/toBn.spec.ts @@ -0,0 +1,93 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { u8aToBn } from './index'; + +describe('u8aToBn', () => { + it('converts little-endian by default', () => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34]) + ).toString(16) + ).toBe('3412'); + }); + it('converts values (big-endian)', () => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34]), + { isLe: false } + ).toString(16) + ).toBe('1234'); + }); + + it('converts values (little-endian)', () => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34]), + { isLe: true } + ).toString(16) + ).toBe('3412'); + }); + + it('converts empty', () => { + expect( + u8aToBn( + new Uint8Array([]), + { isLe: true } + ).toString(16) + ).toBe('0'); + }); + + it('handles negative numbers (little-endian)', () => { + expect( + u8aToBn( + new Uint8Array([214, 0]), + { isLe: true, isNegative: true } + ).toNumber() + ).toBe(-42); + }); + + it('handles negative numbers (big-endian)', () => { + expect( + u8aToBn( + new Uint8Array([0, 214]), + { isLe: false, isNegative: true } + ).toNumber() + ).toBe(-42); + }); + + it('handles overflows correctly (little-endian)', () => { + expect( + u8aToBn( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]), + { isLe: true } + ).toNumber() + ).toBe(256); + }); + + it('handles backward compatibility)', () => { + expect( + u8aToBn( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]), + false + ).eq( + u8aToBn( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]), + { isLe: false } + ) + ) + ).toBe(true); + expect( + u8aToBn( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]), + true + ).eq( + u8aToBn( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]), + { isLe: true } + ) + ) + ).toBe(true); + }); +}); diff --git a/packages/util/src/u8a/toBn.ts b/packages/util/src/u8a/toBn.ts index 25e7c0a7e..8db3cd4aa 100644 --- a/packages/util/src/u8a/toBn.ts +++ b/packages/util/src/u8a/toBn.ts @@ -6,13 +6,18 @@ import BN from 'bn.js'; import hexToBn from '../hex/toBn'; import u8aToHex from './toHex'; +import { ToBnOptions } from '../types'; /** * @name u8aToBn - * @signature u8aToHex (value?: Uint8Array, isLe: boolean = false): BN + * @signature u8aToHex (value?: Uint8Array, options: ToBnOptions | boolean = true): BN * @summary Creates a BN from a Uint8Array object. * @description * `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value. + * @param value The value to convert + * @param options Options to pass while converting + * @param options.isLe Convert using Little Endian + * @param options.isNegative Convert using two's complement * @example *
* @@ -22,9 +27,9 @@ import u8aToHex from './toHex'; * u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f * ``` */ -export default function u8aToBn (value: Uint8Array, isLe: boolean): BN { +export default function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = true): BN { return hexToBn( u8aToHex(value), - isLe + options ); }