Ensure {bn, n}ToHex respects bitLength with 0 values (#1660)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Ensure `{bn, n}ToHex` respects `bitLength` with 0 values
|
||||
- Upgrade dependencies to latest stable versions
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,12 @@ describe('nToHex', (): void => {
|
||||
).toBe('0x00');
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (with bitLength)', (): void => {
|
||||
expect(
|
||||
nToHex(null, { bitLength: 32 })
|
||||
).toBe('0x00000000');
|
||||
});
|
||||
|
||||
it('converts values to a prefixed hex representation', (): void => {
|
||||
expect(
|
||||
nToHex(128)
|
||||
|
||||
@@ -7,15 +7,10 @@ import type { HexString, NumberOptions, ToBigInt, ToBn } from '../types';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { nToU8a } from './toU8a';
|
||||
|
||||
/** @internal */
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
* @name nToHex
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(nToU8a(value, { bitLength, isLe, isNegative }));
|
||||
return u8aToHex(nToU8a(value || 0, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ describe('nToU8a', (): void => {
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
nToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (bitLength)', (): void => {
|
||||
|
||||
@@ -48,7 +48,7 @@ export function nToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN |
|
||||
|
||||
if (valueBi === _0n) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array()
|
||||
? new Uint8Array(1)
|
||||
: new Uint8Array(Math.ceil((bitLength || 0) / 8));
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,12 @@ describe('bnToHex', (): void => {
|
||||
).toBe('0x00');
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (with bitLength)', (): void => {
|
||||
expect(
|
||||
bnToHex(null, { bitLength: 32 })
|
||||
).toBe('0x00000000');
|
||||
});
|
||||
|
||||
it('converts BN values to a prefixed hex representation', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(128))
|
||||
|
||||
@@ -7,8 +7,6 @@ import type { BN } from './bn';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { bnToU8a } from './toU8a';
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @summary Creates a hex value from a BN.js bignumber object.
|
||||
@@ -25,7 +23,5 @@ const ZERO_STR = '0x00';
|
||||
* ```
|
||||
*/
|
||||
export function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
: u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
|
||||
return u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ describe('bnToU8a', (): void => {
|
||||
it('converts null values to 0x00', (): void => {
|
||||
expect(
|
||||
bnToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts null values to 0x00000000 (bitLength)', (): void => {
|
||||
|
||||
@@ -30,7 +30,7 @@ export function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint |
|
||||
|
||||
if (!value) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array()
|
||||
? new Uint8Array(1)
|
||||
: new Uint8Array(byteLength);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user