hexToU8aBuffer
This commit is contained in:
@@ -6,6 +6,14 @@
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"exports": {
|
||||
"./hex/toU8a": {
|
||||
"node": {
|
||||
"require": "./hex/toU8aBuffer.cjs",
|
||||
"default": "./hex/toU8aBuffer.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
||||
"license": "Apache-2.0",
|
||||
"maintainers": [],
|
||||
|
||||
@@ -18,7 +18,7 @@ describe('hexToU8a', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLengfth)', (): void => {
|
||||
it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => {
|
||||
expect(
|
||||
hexToU8a('0x80000a', 32)
|
||||
).toEqual(
|
||||
@@ -26,6 +26,14 @@ describe('hexToU8a', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLength < provided)', (): void => {
|
||||
expect(
|
||||
hexToU8a('0x80000a', 16)
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0])
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when non-hex value provided', (): void => {
|
||||
expect(
|
||||
(): Uint8Array => hexToU8a('notahex')
|
||||
|
||||
@@ -31,15 +31,15 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
|
||||
|
||||
const value = hexStripPrefix(_value);
|
||||
const valLength = value.length / 2;
|
||||
const bufLength = Math.ceil(
|
||||
const resultLength = Math.ceil(
|
||||
bitLength === -1
|
||||
? valLength
|
||||
: bitLength / 8
|
||||
);
|
||||
const result = new Uint8Array(bufLength);
|
||||
const offset = Math.max(0, bufLength - valLength);
|
||||
const result = new Uint8Array(resultLength);
|
||||
const offset = Math.max(0, resultLength - valLength);
|
||||
|
||||
for (let index = 0; index < bufLength; index++) {
|
||||
for (let index = 0; index < resultLength; index++) {
|
||||
result[index + offset] = parseInt(value.substr(index * 2, 2), 16);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '.';
|
||||
|
||||
describe('hexToU8a', (): void => {
|
||||
it('returns an empty Uint8Array when null provided', (): void => {
|
||||
expect(
|
||||
hexToU8a(null)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values', (): void => {
|
||||
expect(
|
||||
hexToU8a('0x80000a')
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => {
|
||||
expect(
|
||||
hexToU8a('0x80000a', 32)
|
||||
).toEqual(
|
||||
new Uint8Array([0, 128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLength < provided)', (): void => {
|
||||
expect(
|
||||
hexToU8a('0x80000a', 16)
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0])
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when non-hex value provided', (): void => {
|
||||
expect(
|
||||
(): Uint8Array => hexToU8a('notahex')
|
||||
).toThrow(/hex value to convert/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { isHex } from '../is/hex';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToU8a } from '@polkadot/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a (value?: HexString | string | null, bitLength = -1): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
assert(isHex(value), () => `Expected hex value to convert, found '${value}'`);
|
||||
|
||||
const buf = Buffer.from(hexStripPrefix(value), 'hex');
|
||||
const valLength = buf.length / 2;
|
||||
const resultLength = Math.ceil(
|
||||
bitLength === -1
|
||||
? valLength
|
||||
: bitLength / 8
|
||||
);
|
||||
|
||||
if (resultLength === valLength) {
|
||||
return Uint8Array.from(buf);
|
||||
}
|
||||
|
||||
const offset = Math.max(0, resultLength - valLength);
|
||||
|
||||
if (offset) {
|
||||
const u8a = new Uint8Array(resultLength);
|
||||
|
||||
u8a.set(buf, offset);
|
||||
|
||||
return u8a;
|
||||
}
|
||||
|
||||
return Uint8Array.from(buf.slice(0, resultLength));
|
||||
}
|
||||
Reference in New Issue
Block a user