mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-28 16:47:58 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexAddPrefix
|
||||
* @summary Adds the `0x` prefix to string values.
|
||||
* @description
|
||||
* Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexAddPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
export declare function hexAddPrefix(value?: string | null): HexString;
|
||||
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexAddPrefix = hexAddPrefix;
|
||||
const hasPrefix_js_1 = require("./hasPrefix.js");
|
||||
/**
|
||||
* @name hexAddPrefix
|
||||
* @summary Adds the `0x` prefix to string values.
|
||||
* @description
|
||||
* Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexAddPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
function hexAddPrefix(value) {
|
||||
return value && (0, hasPrefix_js_1.hexHasPrefix)(value)
|
||||
? value
|
||||
: `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexFixLength
|
||||
* @summary Shifts a hex string to a specific bitLength
|
||||
* @description
|
||||
* Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexFixLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('fixed', hexFixLength('0x12', 16)); // => 0x12
|
||||
* console.log('fixed', hexFixLength('0x12', 16, true)); // => 0x0012
|
||||
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
|
||||
* ```
|
||||
*/
|
||||
export declare function hexFixLength(value: string, bitLength?: number, withPadding?: boolean): HexString;
|
||||
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexFixLength = hexFixLength;
|
||||
const addPrefix_js_1 = require("./addPrefix.js");
|
||||
const stripPrefix_js_1 = require("./stripPrefix.js");
|
||||
/**
|
||||
* @name hexFixLength
|
||||
* @summary Shifts a hex string to a specific bitLength
|
||||
* @description
|
||||
* Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexFixLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('fixed', hexFixLength('0x12', 16)); // => 0x12
|
||||
* console.log('fixed', hexFixLength('0x12', 16, true)); // => 0x0012
|
||||
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
|
||||
* ```
|
||||
*/
|
||||
function hexFixLength(value, bitLength = -1, withPadding = false) {
|
||||
const strLength = Math.ceil(bitLength / 4);
|
||||
const hexLength = strLength + 2;
|
||||
return (0, addPrefix_js_1.hexAddPrefix)((bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength))
|
||||
? (0, stripPrefix_js_1.hexStripPrefix)(value)
|
||||
: (value.length > hexLength)
|
||||
? (0, stripPrefix_js_1.hexStripPrefix)(value).slice(-1 * strLength)
|
||||
: `${'0'.repeat(strLength)}${(0, stripPrefix_js_1.hexStripPrefix)(value)}`.slice(-1 * strLength));
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import type { HexString } from '../types.js';
|
||||
/**
|
||||
* @name hexHasPrefix
|
||||
* @summary Tests for the existence of a `0x` prefix.
|
||||
* @description
|
||||
* Checks for a valid hex input value and if the start matched `0x`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexHasPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
export declare function hexHasPrefix(value?: string | null): value is HexString;
|
||||
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexHasPrefix = hexHasPrefix;
|
||||
const hex_js_1 = require("../is/hex.js");
|
||||
/**
|
||||
* @name hexHasPrefix
|
||||
* @summary Tests for the existence of a `0x` prefix.
|
||||
* @description
|
||||
* Checks for a valid hex input value and if the start matched `0x`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexHasPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
function hexHasPrefix(value) {
|
||||
return !!value && (0, hex_js_1.isHex)(value, -1);
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
export { hexAddPrefix } from './addPrefix.js';
|
||||
export { hexFixLength } from './fixLength.js';
|
||||
export { hexHasPrefix } from './hasPrefix.js';
|
||||
export { hexStripPrefix } from './stripPrefix.js';
|
||||
export { hexToBigInt } from './toBigInt.js';
|
||||
export { hexToBn } from './toBn.js';
|
||||
export { hexToNumber } from './toNumber.js';
|
||||
export { hexToString } from './toString.js';
|
||||
export { hexToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToU8a = exports.hexToString = exports.hexToNumber = exports.hexToBn = exports.hexToBigInt = exports.hexStripPrefix = exports.hexHasPrefix = exports.hexFixLength = exports.hexAddPrefix = void 0;
|
||||
/**
|
||||
* @summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
var addPrefix_js_1 = require("./addPrefix.js");
|
||||
Object.defineProperty(exports, "hexAddPrefix", { enumerable: true, get: function () { return addPrefix_js_1.hexAddPrefix; } });
|
||||
var fixLength_js_1 = require("./fixLength.js");
|
||||
Object.defineProperty(exports, "hexFixLength", { enumerable: true, get: function () { return fixLength_js_1.hexFixLength; } });
|
||||
var hasPrefix_js_1 = require("./hasPrefix.js");
|
||||
Object.defineProperty(exports, "hexHasPrefix", { enumerable: true, get: function () { return hasPrefix_js_1.hexHasPrefix; } });
|
||||
var stripPrefix_js_1 = require("./stripPrefix.js");
|
||||
Object.defineProperty(exports, "hexStripPrefix", { enumerable: true, get: function () { return stripPrefix_js_1.hexStripPrefix; } });
|
||||
var toBigInt_js_1 = require("./toBigInt.js");
|
||||
Object.defineProperty(exports, "hexToBigInt", { enumerable: true, get: function () { return toBigInt_js_1.hexToBigInt; } });
|
||||
var toBn_js_1 = require("./toBn.js");
|
||||
Object.defineProperty(exports, "hexToBn", { enumerable: true, get: function () { return toBn_js_1.hexToBn; } });
|
||||
var toNumber_js_1 = require("./toNumber.js");
|
||||
Object.defineProperty(exports, "hexToNumber", { enumerable: true, get: function () { return toNumber_js_1.hexToNumber; } });
|
||||
var toString_js_1 = require("./toString.js");
|
||||
Object.defineProperty(exports, "hexToString", { enumerable: true, get: function () { return toString_js_1.hexToString; } });
|
||||
var toU8a_js_1 = require("./toU8a.js");
|
||||
Object.defineProperty(exports, "hexToU8a", { enumerable: true, get: function () { return toU8a_js_1.hexToU8a; } });
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexStripPrefix
|
||||
* @summary Strips any leading `0x` prefix.
|
||||
* @description
|
||||
* Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexStripPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
* ```
|
||||
*/
|
||||
export declare function hexStripPrefix(value?: string | null): string;
|
||||
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexStripPrefix = hexStripPrefix;
|
||||
const hex_js_1 = require("../is/hex.js");
|
||||
/**
|
||||
* @name hexStripPrefix
|
||||
* @summary Strips any leading `0x` prefix.
|
||||
* @description
|
||||
* Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexStripPrefix } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
* ```
|
||||
*/
|
||||
function hexStripPrefix(value) {
|
||||
if (!value || value === '0x') {
|
||||
return '';
|
||||
}
|
||||
else if (hex_js_1.REGEX_HEX_PREFIXED.test(value)) {
|
||||
return value.substring(2);
|
||||
}
|
||||
else if (hex_js_1.REGEX_HEX_NOPREFIX.test(value)) {
|
||||
return value;
|
||||
}
|
||||
throw new Error(`Expected hex value to convert, found '${value}'`);
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import type { ToBnOptions } from '../types.js';
|
||||
/**
|
||||
* @name hexToBigInt
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
export declare function hexToBigInt(value?: string | null, { isLe, isNegative }?: ToBnOptions): bigint;
|
||||
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToBigInt = hexToBigInt;
|
||||
const x_bigint_1 = require("@pezkuwi/x-bigint");
|
||||
const toBigInt_js_1 = require("../u8a/toBigInt.js");
|
||||
const toU8a_js_1 = require("./toU8a.js");
|
||||
/**
|
||||
* @name hexToBigInt
|
||||
* @summary Creates a BigInt instance object from a hex string.
|
||||
*/
|
||||
function hexToBigInt(value, { isLe = false, isNegative = false } = {}) {
|
||||
return !value || value === '0x'
|
||||
? (0, x_bigint_1.BigInt)(0)
|
||||
: (0, toBigInt_js_1.u8aToBigInt)((0, toU8a_js_1.hexToU8a)(value), { isLe, isNegative });
|
||||
}
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
import type { ToBnOptions } from '../types.js';
|
||||
import { BN } from '../bn/bn.js';
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @param _value The value to convert
|
||||
* @param _options Options to pass while converting
|
||||
* @param _options.isLe Convert using Little Endian
|
||||
* @param _options.isNegative Convert using two's complement
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToBn(value?: string | null, { isLe, isNegative }?: ToBnOptions): BN;
|
||||
@@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToBn = hexToBn;
|
||||
const bn_js_1 = require("../bn/bn.js");
|
||||
const stripPrefix_js_1 = require("./stripPrefix.js");
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @param _value The value to convert
|
||||
* @param _options Options to pass while converting
|
||||
* @param _options.isLe Convert using Little Endian
|
||||
* @param _options.isNegative Convert using two's complement
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToBn } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
function hexToBn(value, { isLe = false, isNegative = false } = {}) {
|
||||
if (!value || value === '0x') {
|
||||
return new bn_js_1.BN(0);
|
||||
}
|
||||
const stripped = (0, stripPrefix_js_1.hexStripPrefix)(value);
|
||||
const bn = new bn_js_1.BN(stripped, 16, isLe ? 'le' : 'be');
|
||||
// fromTwos takes as parameter the number of bits, which is the hex length
|
||||
// multiplied by 4 (2 bytes being 8 bits)
|
||||
return isNegative
|
||||
? bn.fromTwos(stripped.length * 4)
|
||||
: bn;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexToNumber
|
||||
* @summary Creates a Number value from a Buffer object.
|
||||
* @description
|
||||
* `null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToNumber } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToNumber('0x1234'); // => 0x1234
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToNumber(value?: string | null): number;
|
||||
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToNumber = hexToNumber;
|
||||
const toBn_js_1 = require("./toBn.js");
|
||||
/**
|
||||
* @name hexToNumber
|
||||
* @summary Creates a Number value from a Buffer object.
|
||||
* @description
|
||||
* `null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToNumber } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToNumber('0x1234'); // => 0x1234
|
||||
* ```
|
||||
*/
|
||||
function hexToNumber(value) {
|
||||
return value
|
||||
? (0, toBn_js_1.hexToBn)(value).toNumber()
|
||||
: NaN;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToString } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x68656c6c6f'); // hello
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToString(_value?: string | null): string;
|
||||
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToString = hexToString;
|
||||
const toString_js_1 = require("../u8a/toString.js");
|
||||
const toU8a_js_1 = require("./toU8a.js");
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToString } from '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x68656c6c6f'); // hello
|
||||
* ```
|
||||
*/
|
||||
function hexToString(_value) {
|
||||
return (0, toString_js_1.u8aToString)((0, toU8a_js_1.hexToU8a)(_value));
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToU8a(value?: string | null, bitLength?: number): Uint8Array;
|
||||
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToU8a = hexToU8a;
|
||||
const CHR = '0123456789abcdef';
|
||||
const U8 = new Uint8Array(256);
|
||||
const U16 = new Uint8Array(256 * 256);
|
||||
for (let i = 0, count = CHR.length; i < count; i++) {
|
||||
U8[CHR[i].charCodeAt(0) | 0] = i | 0;
|
||||
if (i > 9) {
|
||||
U8[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const s = i << 8;
|
||||
for (let j = 0; j < 256; j++) {
|
||||
U16[s | j] = (U8[i] << 4) | U8[j];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
function hexToU8a(value, bitLength = -1) {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
let s = value.startsWith('0x')
|
||||
? 2
|
||||
: 0;
|
||||
const decLength = Math.ceil((value.length - s) / 2);
|
||||
const endLength = Math.ceil(bitLength === -1
|
||||
? decLength
|
||||
: bitLength / 8);
|
||||
const result = new Uint8Array(endLength);
|
||||
const offset = endLength > decLength
|
||||
? endLength - decLength
|
||||
: 0;
|
||||
for (let i = offset; i < endLength; i++, s += 2) {
|
||||
// The big factor here is actually the string lookups. If we do
|
||||
// HEX_TO_U16[value.substring()] we get an 10x slowdown. In the
|
||||
// same vein using charCodeAt (as opposed to value[s] or value.charAt(s)) is
|
||||
// also the faster operation by at least 2x with the character map above
|
||||
result[i] = U16[(value.charCodeAt(s) << 8) | value.charCodeAt(s + 1)];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export declare function hexToU8a(_value?: string | null, bitLength?: number): Uint8Array;
|
||||
@@ -0,0 +1,43 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.hexToU8a = hexToU8a;
|
||||
/**
|
||||
* @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 '@pezkuwi/util';
|
||||
*
|
||||
* hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
function hexToU8a(_value, bitLength = -1) {
|
||||
if (!_value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
const value = _value.startsWith('0x')
|
||||
? _value.substring(2)
|
||||
: _value;
|
||||
const buf = Buffer.from(value, 'hex');
|
||||
const valLength = value.length / 2;
|
||||
const resultLength = Math.ceil(bitLength === -1
|
||||
? valLength
|
||||
: bitLength / 8);
|
||||
if (resultLength === valLength) {
|
||||
return Uint8Array.from(buf);
|
||||
}
|
||||
const offset = resultLength > valLength
|
||||
? resultLength - valLength
|
||||
: 0;
|
||||
if (offset) {
|
||||
const u8a = new Uint8Array(resultLength);
|
||||
u8a.set(buf, offset);
|
||||
return u8a;
|
||||
}
|
||||
return Uint8Array.from(buf.subarray(0, resultLength));
|
||||
}
|
||||
Reference in New Issue
Block a user