Fix negative BNs (#280)

* Fix bug fromTwos

* Better default values

* Fix bugs

* Fix tests

* Eth uses BE!

* Add back compat

* Fix test

* Remove useless arg
This commit is contained in:
Amaury Martiny
2018-12-20 17:00:57 +01:00
committed by GitHub
parent 18a4162946
commit 74744e694b
11 changed files with 155 additions and 64 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ export default function getTests (file: string) {
name,
input: tests[name].in[0] !== '#'
? tests[name].in
: bnToU8a(new BN(tests[name].in.slice(1)), -1, true),
: bnToU8a(new BN(tests[name].in.slice(1)), { isLe: false }),
output: tests[name].out
? `0x${tests[name].out.toLowerCase()}`
: undefined,
+1 -1
View File
@@ -21,7 +21,7 @@ import BN from 'bn.js';
* bnToBn(new BN(0x1234)); // => BN(0x1234)
* ```
*/
export default function bnToBn (value?: BN | number): BN {
export default function bnToBn (value?: BN | number | null): BN {
if (!value) {
return new BN(0);
}
-27
View File
@@ -1,27 +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 BN from 'bn.js';
import { bnToHex } from './index';
describe('bnToHex', () => {
it('converts null values to 0x00', () => {
expect(
bnToHex(null)
).toEqual('0x00');
});
it('converts BN values to a prefixed hex representation', () => {
expect(
bnToHex(new BN(128))
).toEqual('0x80');
});
it('converts BN values to a prefixed hex representation (bitLength)', () => {
expect(
bnToHex(new BN(128), 16)
).toEqual('0x0080');
});
});
+53
View File
@@ -0,0 +1,53 @@
// 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 BN from 'bn.js';
import { bnToHex } from './index';
describe('bnToHex', () => {
it('converts null values to 0x00', () => {
expect(
bnToHex(null)
).toBe('0x00');
});
it('converts BN values to a prefixed hex representation', () => {
expect(
bnToHex(new BN(128))
).toBe('0x80');
});
it('converts BN values to a prefixed hex representation (bitLength)', () => {
expect(
bnToHex(new BN(128), 16)
).toBe('0x0080');
});
it('converts BN values to a prefixed hex representation (LE)', () => {
expect(
bnToHex(new BN(128), { bitLength: 16, isLe: true })
).toBe('0x8000');
});
it('handles negative numbers', () => {
expect(
bnToHex(new BN(-1234), { isNegative: true })
).toBe('0xfb2e');
});
it('handles negative numbers (with bitLength)', () => {
expect(
bnToHex(new BN(-1234), { bitLength: 32, isNegative: true })
).toBe('0xfffffb2e');
});
it('handles backwards compatibility', () => {
expect(
bnToHex(new BN(1234), 32)
).toBe(
bnToHex(new BN(1234), { bitLength: 32 })
);
});
});
+20 -4
View File
@@ -4,11 +4,17 @@
import BN from 'bn.js';
import hexFixLength from '../hex/fixLength';
import bnToBn from './toBn';
import isNumber from '../is/number';
import bnToU8a from './toU8a';
import { ToBnOptions } from '../types';
import { u8aToHex } from '../u8a';
const ZERO_STR = '0x00';
interface Options extends ToBnOptions {
bitLength?: number;
}
/**
* @name bnToHex
* @signature bnToHex (value?: BN, bitLength: number = -1): string
@@ -25,10 +31,20 @@ const ZERO_STR = '0x00';
* bnToHex(new BN(0x123456)); // => '0x123456'
* ```
*/
export default function bnToHex (value?: BN | number, bitLength: number = -1): string {
export default function bnToHex (
value?: BN | number | null,
options: number | Options = { bitLength: -1, isLe: false, isNegative: false }
): string {
if (!value) {
return ZERO_STR;
}
return hexFixLength(bnToBn(value).toString(16), bitLength, true);
const _options = {
isLe: false,
isNegative: false,
// Backwards-compatibility
...(isNumber(options) ? { bitLength: options } : options)
};
return u8aToHex(bnToU8a(value, _options));
}
@@ -36,4 +36,30 @@ describe('bnToU8a', () => {
bnToU8a(new BN(0x123456), 32, true)
).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
});
it('converts negative numbers', () => {
expect(
bnToU8a(new BN(-1234), { isNegative: true })
).toEqual(new Uint8Array([46, 251]));
});
it('converts negative numbers (BE)', () => {
expect(
bnToU8a(new BN(-1234), { isNegative: true, isLe: false })
).toEqual(new Uint8Array([251, 46]));
});
it('converts negative numbers (bitLength)', () => {
expect(
bnToU8a(new BN(-1234), { isNegative: true, bitLength: 32 })
).toEqual(new Uint8Array([46, 251, 255, 255]));
});
it('handles backwards compatibility', () => {
expect(
bnToU8a(new BN(1234), 32, false)
).toEqual(
bnToU8a(new BN(1234), { bitLength: 32, isLe: false })
);
});
});
+27 -12
View File
@@ -4,9 +4,13 @@
import BN from 'bn.js';
import hexToU8a from '../hex/toU8a';
import isNumber from '../is/number';
import bnToBn from './toBn';
import bnToHex from './toHex';
import { ToBnOptions } from '../types';
interface Options extends ToBnOptions {
bitLength?: number;
}
/**
* @name bnToU8a
@@ -23,25 +27,36 @@ import bnToHex from './toHex';
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
* ```
*/
export default function bnToU8a (value: BN | number, bitLength: number, isLe: boolean): Uint8Array {
const byteLength = Math.ceil(bitLength / 8);
export default function bnToU8a (value: BN | number | null, options?: Options): Uint8Array;
export default function bnToU8a (value: BN | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
export default function bnToU8a (
value: BN | number | null,
arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false },
arg2?: boolean
): Uint8Array {
const _options: Options = {
isLe: true,
isNegative: false,
bitLength: -1,
...isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1
};
const valueBn = bnToBn(value);
let byteLength = _options.bitLength === -1
? Math.ceil(valueBn.bitLength() / 8)
: Math.ceil(_options.bitLength! / 8);
if (!value) {
return bitLength === -1
return _options.bitLength === -1
? new Uint8Array([])
: new Uint8Array(byteLength);
}
if (bitLength === -1) {
return hexToU8a(
bnToHex(bnToBn(value), bitLength)
);
}
const output = new Uint8Array(byteLength);
const bn = _options.isNegative ? valueBn.toTwos(byteLength * 8) : valueBn;
output.set(
bnToBn(value).toArray(isLe ? 'le' : 'be', byteLength),
bn.toArray(_options.isLe ? 'le' : 'be', byteLength),
0
);
+4 -4
View File
@@ -43,14 +43,14 @@ describe('hexToBn', () => {
it('handles negative numbers (LE)', () => {
expect(
hexToBn('0xd600', { isLe: true, isNegative: true }).toNumber()
).toBe(-42);
hexToBn('0x2efb', { isLe: true, isNegative: true }).toNumber()
).toBe(-1234);
});
it('handles negative numbers (BE)', () => {
expect(
hexToBn('0x00d6', { isLe: false, isNegative: true }).toNumber()
).toBe(-42);
hexToBn('0xfb2e', { isLe: false, isNegative: true }).toNumber()
).toBe(-1234);
});
it('handles starting zeros correctly (BE)', () => {
+14 -9
View File
@@ -34,22 +34,27 @@ function reverse (value: string): string {
* ```
*/
export default function hexToBn (
_value?: string | number | null,
_options: ToBnOptions | boolean = { isLe: false, isNegative: false }
value?: string | number | null,
options: ToBnOptions | boolean = { isLe: false, isNegative: false }
): BN {
if (!_value) {
if (!value) {
return new BN(0);
}
const options: ToBnOptions = isBoolean(_options)
? { isLe: _options, isNegative: false }
: _options;
const _options: ToBnOptions = {
isLe: false,
isNegative: false,
// Backwards-compatibility
...(isBoolean(options) ? { isLe: options } : options)
};
const value = hexStripPrefix(_value as string);
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);
const bn = new BN((_options.isLe ? reverse(_value) : _value) || '00', 16);
return options.isNegative ? bn.fromTwos(8) : bn;
// fromTwos takes as parameter the number of bits, which is the hex length
// multiplied by 4.
return _options.isNegative ? bn.fromTwos(_value.length * 4) : bn;
}
+4 -4
View File
@@ -42,19 +42,19 @@ describe('u8aToBn', () => {
it('handles negative numbers (little-endian)', () => {
expect(
u8aToBn(
new Uint8Array([214, 0]),
new Uint8Array([46, 251]),
{ isLe: true, isNegative: true }
).toNumber()
).toBe(-42);
).toBe(-1234);
});
it('handles negative numbers (big-endian)', () => {
expect(
u8aToBn(
new Uint8Array([0, 214]),
new Uint8Array([251, 46]),
{ isLe: false, isNegative: true }
).toNumber()
).toBe(-42);
).toBe(-1234);
});
it('handles overflows correctly (little-endian)', () => {
+5 -2
View File
@@ -10,7 +10,7 @@ import { ToBnOptions } from '../types';
/**
* @name u8aToBn
* @signature u8aToHex (value?: Uint8Array, options: ToBnOptions | boolean = true): BN
* @signature u8aToHex (value?: Uint8Array, options: ToBnOptions | boolean = { isLe: true, isNegative: false }): 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.
@@ -27,7 +27,10 @@ import { ToBnOptions } from '../types';
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export default function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = true): BN {
export default function u8aToBn (
value: Uint8Array,
options: ToBnOptions | boolean = { isLe: true, isNegative: false }
): BN {
return hexToBn(
u8aToHex(value),
options