Add conversion to negative BN's (#277)

* Support negative number hexToBn

* Fix u8aToBn

* Update signature

* Update params description
This commit is contained in:
Amaury Martiny
2018-12-19 18:43:44 +01:00
committed by GitHub
parent e134d9545a
commit 0e30c48379
7 changed files with 207 additions and 99 deletions
-49
View File
@@ -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');
});
});
+80
View File
@@ -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);
});
});
+21 -4
View File
@@ -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
* <BR>
*
@@ -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;
}
+5
View File
@@ -19,3 +19,8 @@ export type Logger = {
noop: (...values: Logger$Data) => void,
warn: (...values: Logger$Data) => void
};
export interface ToBnOptions {
isLe?: boolean;
isNegative?: boolean;
}
-43
View File
@@ -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');
});
});
+93
View File
@@ -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);
});
});
+8 -3
View File
@@ -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
* <BR>
*
@@ -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
);
}