From d9231d26aa61caebace0734e04b74891f538b007 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 13 Nov 2021 10:38:07 +0100 Subject: [PATCH] Additional Node-specific u8a utils (#1177) * Additional Node-specific u8a utils * toHexBuffer --- packages/util/package.json | 12 +++++ packages/util/src/u8a/concatBuffer.spec.ts | 30 +++++++++++ packages/util/src/u8a/concatBuffer.ts | 33 ++++++++++++ packages/util/src/u8a/toHexBuffer.spec.ts | 60 ++++++++++++++++++++++ packages/util/src/u8a/toHexBuffer.ts | 30 +++++++++++ 5 files changed, 165 insertions(+) create mode 100644 packages/util/src/u8a/concatBuffer.spec.ts create mode 100644 packages/util/src/u8a/concatBuffer.ts create mode 100644 packages/util/src/u8a/toHexBuffer.spec.ts create mode 100644 packages/util/src/u8a/toHexBuffer.ts diff --git a/packages/util/package.json b/packages/util/package.json index 50b862cd7..c5372ae8d 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -12,6 +12,18 @@ "require": "./hex/toU8aBuffer.cjs", "default": "./hex/toU8aBuffer.js" } + }, + "./u8a/concat": { + "node": { + "require": "./u8a/concatBuffer.cjs", + "default": "./u8a/concatBuffer.js" + } + }, + "./u8a/toHex": { + "node": { + "require": "./u8a/toHexBuffer.cjs", + "default": "./u8a/toHexBuffer.js" + } } }, "homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme", diff --git a/packages/util/src/u8a/concatBuffer.spec.ts b/packages/util/src/u8a/concatBuffer.spec.ts new file mode 100644 index 000000000..bc1c53d03 --- /dev/null +++ b/packages/util/src/u8a/concatBuffer.spec.ts @@ -0,0 +1,30 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { u8aConcat } from './concatBuffer'; + +describe('u8aConcat', (): void => { + it('concatenates arrays', (): void => { + expect( + u8aConcat( + new Uint8Array([1, 2, 3, 4]), + new Uint8Array([5, 6]), + new Uint8Array([7, 8, 9]) + ) + ).toEqual( + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]) + ); + }); + + it('concatenates arrays & hex values', (): void => { + expect( + u8aConcat( + new Uint8Array([1, 2, 3, 4]), + '0x0506', + '0x070809' + ) + ).toEqual( + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]) + ); + }); +}); diff --git a/packages/util/src/u8a/concatBuffer.ts b/packages/util/src/u8a/concatBuffer.ts new file mode 100644 index 000000000..29978f6da --- /dev/null +++ b/packages/util/src/u8a/concatBuffer.ts @@ -0,0 +1,33 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { U8aLike } from '../types'; + +import { u8aToU8a } from './toU8a'; + +/** + * @name u8aConcat + * @summary Creates a concatenated Uint8Array from the inputs. + * @description + * Concatenates the input arrays into a single `UInt8Array`. + * @example + *
+ * + * ```javascript + * import { { u8aConcat } from '@polkadot/util'; + * + * u8aConcat( + * new Uint8Array([1, 2, 3]), + * new Uint8Array([4, 5, 6]) + * ); // [1, 2, 3, 4, 5, 6] + * ``` + */ +export function u8aConcat (...list: U8aLike[]): Uint8Array { + const u8as = new Array(list.length); + + for (let i = 0; i < list.length; i++) { + u8as[i] = u8aToU8a(list[i]); + } + + return Uint8Array.from(Buffer.concat(u8as)); +} diff --git a/packages/util/src/u8a/toHexBuffer.spec.ts b/packages/util/src/u8a/toHexBuffer.spec.ts new file mode 100644 index 000000000..ea16f174a --- /dev/null +++ b/packages/util/src/u8a/toHexBuffer.spec.ts @@ -0,0 +1,60 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { u8aToHex } from './toHexBuffer'; + +describe('u8aToHex', (): void => { + it('returns empty as 0x', (): void => { + expect( + u8aToHex() + ).toEqual('0x'); + }); + + it('returns empty as "" (unprefixed)', (): void => { + expect( + u8aToHex(null, -1, false) + ).toEqual(''); + }); + + it('returns the hex value for the array', (): void => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10]) + ) + ).toEqual('0x80000a'); + }); + + it('returns the hex value for the array (unprefixed)', (): void => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10]), + -1, false + ) + ).toEqual('80000a'); + }); + + it('handles starting zeros correctly', (): void => { + expect( + u8aToHex( + new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0]) + ) + ).toEqual('0x0001000000000000'); + }); + + it('returns the hex value where allowed < max', (): void => { + expect( + u8aToHex( + new Uint8Array(Uint8Array.from([128, 0, 10, 11]), 64) + ) + ).toEqual('0x80000a0b'); + }); + + it('returns the trimmed hex value where allowed >= max', (): void => { + expect( + u8aToHex( + new Uint8Array([128, 0, 10, 11, 12, 13]), + 32 + ) + ).toEqual('0x8000…0c0d'); + }); +}); diff --git a/packages/util/src/u8a/toHexBuffer.ts b/packages/util/src/u8a/toHexBuffer.ts new file mode 100644 index 000000000..dd76641c2 --- /dev/null +++ b/packages/util/src/u8a/toHexBuffer.ts @@ -0,0 +1,30 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { HexString } from '../types'; + +/** + * @name u8aToHex + * @summary Creates a hex string from a Uint8Array object. + * @description + * `UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string. + * @example + *
+ * + * ```javascript + * import { u8aToHex } from '@polkadot/util'; + * + * u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f + * ``` + */ +export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString { + const length = Math.ceil(bitLength / 8); + + return `${isPrefixed ? '0x' : ''}${ + !value || !value.length + ? '' + : (length > 0 && value.length > length) + ? `${Buffer.from(value.subarray(0, length / 2)).toString('hex')}…${Buffer.from(value.subarray(value.length - length / 2)).toString('hex')}` + : Buffer.from(value).toString('hex') + }` as HexString; +}