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
+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