Ensure {bn, n}ToHex respects bitLength with 0 values (#1660)

This commit is contained in:
Jaco
2022-10-07 06:45:49 +03:00
committed by GitHub
parent b7815d43a5
commit 669d8b1a84
9 changed files with 19 additions and 15 deletions
+1
View File
@@ -4,6 +4,7 @@
Changes:
- Ensure `{bn, n}ToHex` respects `bitLength` with 0 values
- Upgrade dependencies to latest stable versions
+6
View File
@@ -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)
+1 -6
View File
@@ -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 }));
}
+1 -1
View File
@@ -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 => {
+1 -1
View File
@@ -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));
}
+6
View File
@@ -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))
+1 -5
View File
@@ -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 }));
}
+1 -1
View File
@@ -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 => {
+1 -1
View File
@@ -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);
}