diff --git a/CHANGELOG.md b/CHANGELOG.md index ebd780986..8be765390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ Changes: -- Added `arrayShuffle(input)` to shuffle an array into a new output +- `isHex(value)` will now explicitly check for only `0x${string}` (No `String` classes) +- Added `arrayShuffle(array)` to shuffle an array into a new output ## 6.9.1 Jun 19, 2021 diff --git a/packages/util-crypto/src/address/decode.ts b/packages/util-crypto/src/address/decode.ts index fd2865dfe..e19c9a6f2 100644 --- a/packages/util-crypto/src/address/decode.ts +++ b/packages/util-crypto/src/address/decode.ts @@ -29,6 +29,6 @@ export function decodeAddress (encoded?: string | Uint8Array | null, ignoreCheck return decoded.slice(ss58Length, endPos); } catch (error) { - throw new Error(`Decoding ${encoded as string}: ${(error as Error).message}`); + throw new Error(`Decoding ${encoded}: ${(error as Error).message}`); } } diff --git a/packages/util-crypto/src/key/DeriveJunction.ts b/packages/util-crypto/src/key/DeriveJunction.ts index 293166d6b..7a71e2699 100644 --- a/packages/util-crypto/src/key/DeriveJunction.ts +++ b/packages/util-crypto/src/key/DeriveJunction.ts @@ -60,13 +60,11 @@ export class DeriveJunction { public soft (value: number | string | BigInt | BN | Uint8Array): DeriveJunction { if (isNumber(value) || isBn(value) || isBigInt(value)) { return this.soft(bnToHex(value, BN_OPTIONS)); + } else if (isHex(value)) { + return this.soft(hexToU8a(value)); } else if (isString(value)) { - return isHex(value) - ? this.soft(hexToU8a(value)) - : this.soft(compactAddLength(stringToU8a(value))); - } - - if (value.length > JUNCTION_ID_LEN) { + return this.soft(compactAddLength(stringToU8a(value))); + } else if (value.length > JUNCTION_ID_LEN) { return this.soft(blake2AsU8a(value)); } diff --git a/packages/util/src/is/hex.ts b/packages/util/src/is/hex.ts index defec0f70..f0d050e20 100644 --- a/packages/util/src/is/hex.ts +++ b/packages/util/src/is/hex.ts @@ -1,8 +1,6 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { isString } from './string'; - const HEX_REGEX = /^0x[a-fA-F0-9]+$/; /** @@ -20,13 +18,10 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/; * isHex('0x1234', 8); // => false * ``` */ -// eslint-disable-next-line @typescript-eslint/ban-types -export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is string | String { - const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value.toString())); - - if (isValidHex && bitLength !== -1) { - return (value as string).length === (2 + Math.ceil(bitLength / 4)); - } - - return isValidHex && (ignoreLength || ((value as string).length % 2 === 0)); +export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is `0x${string}` { + return (typeof value === 'string' && (value === '0x' || HEX_REGEX.test(value))) + ? bitLength === -1 + ? ((value.length % 2 === 0) || ignoreLength) + : (value.length === (2 + Math.ceil(bitLength / 4))) + : false; } diff --git a/packages/util/src/u8a/toU8a.ts b/packages/util/src/u8a/toU8a.ts index 43d9273a8..8b8fd8894 100644 --- a/packages/util/src/u8a/toU8a.ts +++ b/packages/util/src/u8a/toU8a.ts @@ -27,10 +27,10 @@ import { stringToU8a } from '../string/toU8a'; export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null): Uint8Array { if (!value) { return new Uint8Array(); + } else if (isHex(value)) { + return hexToU8a(value); } else if (isString(value)) { - return isHex(value) - ? hexToU8a(value) - : stringToU8a(value); + return stringToU8a(value); } else if (Array.isArray(value) || isBuffer(value)) { return new Uint8Array(value); }