chore: update to version 14.0.11 and align website URLs

This commit is contained in:
2026-01-11 11:34:13 +03:00
parent ef74383349
commit 19c8d69bd8
1499 changed files with 53633 additions and 89 deletions
+17
View File
@@ -0,0 +1,17 @@
/**
* @name u8aCmp
* @summary Compares two Uint8Arrays for sorting.
* @description
* For `UInt8Array` (or hex string) input values returning -1, 0 or +1
* @example
* <BR>
*
* ```javascript
* import { u8aCmp } from '@pezkuwi/util';
*
* u8aCmp(new Uint8Array([0x67, 0x65]), new Uint8Array([0x68, 0x65])); // -1
* u8aCmp(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // 0
* u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1
* ```
*/
export declare function u8aCmp(a: string | Uint8Array, b: string | Uint8Array): number;
+49
View File
@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aCmp = u8aCmp;
const toU8a_js_1 = require("./toU8a.js");
/**
* @name u8aCmp
* @summary Compares two Uint8Arrays for sorting.
* @description
* For `UInt8Array` (or hex string) input values returning -1, 0 or +1
* @example
* <BR>
*
* ```javascript
* import { u8aCmp } from '@pezkuwi/util';
*
* u8aCmp(new Uint8Array([0x67, 0x65]), new Uint8Array([0x68, 0x65])); // -1
* u8aCmp(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // 0
* u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1
* ```
*/
function u8aCmp(a, b) {
const u8aa = (0, toU8a_js_1.u8aToU8a)(a);
const u8ab = (0, toU8a_js_1.u8aToU8a)(b);
let i = 0;
while (true) {
const overA = i >= u8aa.length;
const overB = i >= u8ab.length;
if (overA && overB) {
// both ends reached
return 0;
}
else if (overA) {
// a has no more data, b has data
return -1;
}
else if (overB) {
// b has no more data, a has data
return 1;
}
else if (u8aa[i] !== u8ab[i]) {
// the number in this index doesn't match
// (we don't use u8aa[i] - u8ab[i] since that doesn't match with localeCompare)
return u8aa[i] > u8ab[i]
? 1
: -1;
}
i++;
}
}
+24
View File
@@ -0,0 +1,24 @@
import type { U8aLike } from '../types.js';
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@pezkuwi/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export declare function u8aConcat(...list: readonly U8aLike[]): Uint8Array;
/**
* @name u8aConcatStrict
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
*/
export declare function u8aConcatStrict(u8as: readonly Uint8Array[], length?: number): Uint8Array;
+51
View File
@@ -0,0 +1,51 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aConcat = u8aConcat;
exports.u8aConcatStrict = u8aConcatStrict;
const toU8a_js_1 = require("./toU8a.js");
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@pezkuwi/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
function u8aConcat(...list) {
const count = list.length;
const u8as = new Array(count);
let length = 0;
for (let i = 0; i < count; i++) {
u8as[i] = (0, toU8a_js_1.u8aToU8a)(list[i]);
length += u8as[i].length;
}
return u8aConcatStrict(u8as, length);
}
/**
* @name u8aConcatStrict
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
*/
function u8aConcatStrict(u8as, length = 0) {
const count = u8as.length;
let offset = 0;
if (!length) {
for (let i = 0; i < count; i++) {
length += u8as[i].length;
}
}
const result = new Uint8Array(length);
for (let i = 0; i < count; i++) {
result.set(u8as[i], offset);
offset += u8as[i].length;
}
return result;
}
+19
View File
@@ -0,0 +1,19 @@
import type { U8aLike } from '../types.js';
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@pezkuwi/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export declare function u8aConcat(...list: readonly U8aLike[]): Uint8Array;
+29
View File
@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aConcat = u8aConcat;
const toU8a_js_1 = require("./toU8a.js");
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@pezkuwi/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
function u8aConcat(...list) {
const count = list.length;
const u8as = new Array(count);
for (let i = 0; i < count; i++) {
u8as[i] = (0, toU8a_js_1.u8aToU8a)(list[i]);
}
return Uint8Array.from(Buffer.concat(u8as));
}
+7
View File
@@ -0,0 +1,7 @@
/**
* @name u8aEmpty
* @summary Tests for a `Uint8Array` for emptyness
* @description
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
*/
export declare function u8aEmpty(value: Uint8Array): boolean;
+20
View File
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aEmpty = u8aEmpty;
/**
* @name u8aEmpty
* @summary Tests for a `Uint8Array` for emptyness
* @description
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
*/
function u8aEmpty(value) {
const len = value.length | 0;
// on smaller sizes, the byte-by-byte compare is faster than allocating
// another object for DataView (on very large arrays the DataView is faster)
for (let i = 0; i < len; i++) {
if (value[i] | 0) {
return false;
}
}
return true;
}
+15
View File
@@ -0,0 +1,15 @@
/**
* @name u8aEq
* @summary Compares two Uint8Arrays for equality.
* @description
* For `UInt8Array` (or hex string) input values true if there is a match.
* @example
* <BR>
*
* ```javascript
* import { u8aEq } from '@pezkuwi/util';
*
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
* ```
*/
export declare function u8aEq(a: string | Uint8Array, b: string | Uint8Array): boolean;
+40
View File
@@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aEq = u8aEq;
const toU8a_js_1 = require("./toU8a.js");
/**
* @name u8aEq
* @summary Compares two Uint8Arrays for equality.
* @description
* For `UInt8Array` (or hex string) input values true if there is a match.
* @example
* <BR>
*
* ```javascript
* import { u8aEq } from '@pezkuwi/util';
*
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
* ```
*/
function u8aEq(a, b) {
const u8aa = (0, toU8a_js_1.u8aToU8a)(a);
const u8ab = (0, toU8a_js_1.u8aToU8a)(b);
if (u8aa.length === u8ab.length) {
const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
const mod = (u8aa.length % 4) | 0;
const length = (u8aa.length - mod) | 0;
for (let i = 0; i < length; i += 4) {
if (dvA.getUint32(i) !== dvB.getUint32(i)) {
return false;
}
}
for (let i = length, count = u8aa.length; i < count; i++) {
if (u8aa[i] !== u8ab[i]) {
return false;
}
}
return true;
}
return false;
}
+17
View File
@@ -0,0 +1,17 @@
/**
* @name u8aFixLength
* @summary Shifts a Uint8Array to a specific bitLength
* @description
* Returns a uint8Array 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.
* @example
* <BR>
*
* ```javascript
* import { u8aFixLength } from '@pezkuwi/util';
*
* u8aFixLength('0x12') // => 0x12
* u8aFixLength('0x12', 16) // => 0x0012
* u8aFixLength('0x1234', 8) // => 0x12
* ```
*/
export declare function u8aFixLength(value: Uint8Array, bitLength?: number, atStart?: boolean): Uint8Array;
+31
View File
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aFixLength = u8aFixLength;
/**
* @name u8aFixLength
* @summary Shifts a Uint8Array to a specific bitLength
* @description
* Returns a uint8Array 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.
* @example
* <BR>
*
* ```javascript
* import { u8aFixLength } from '@pezkuwi/util';
*
* u8aFixLength('0x12') // => 0x12
* u8aFixLength('0x12', 16) // => 0x0012
* u8aFixLength('0x1234', 8) // => 0x12
* ```
*/
function u8aFixLength(value, bitLength = -1, atStart = false) {
const byteLength = Math.ceil(bitLength / 8);
if (bitLength === -1 || value.length === byteLength) {
return value;
}
else if (value.length > byteLength) {
return value.subarray(0, byteLength);
}
const result = new Uint8Array(byteLength);
result.set(value, atStart ? 0 : (byteLength - value.length));
return result;
}
+18
View File
@@ -0,0 +1,18 @@
/**
* @summary Utility methods to convert to and from `Uint8Array` objects
*/
export { u8aCmp } from './cmp.js';
export { u8aConcat, u8aConcatStrict } from './concat.js';
export { u8aEmpty } from './empty.js';
export { u8aEq } from './eq.js';
export { u8aFixLength } from './fixLength.js';
export { u8aSorted } from './sorted.js';
export { u8aToBigInt } from './toBigInt.js';
export { u8aToBn } from './toBn.js';
export { u8aToBuffer } from './toBuffer.js';
export { u8aToFloat } from './toFloat.js';
export { u8aToHex } from './toHex.js';
export { u8aToNumber } from './toNumber.js';
export { u8aToString } from './toString.js';
export { u8aToU8a } from './toU8a.js';
export { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aIsWrapped, u8aUnwrapBytes, u8aWrapBytes } from './wrap.js';
+42
View File
@@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aWrapBytes = exports.u8aUnwrapBytes = exports.u8aIsWrapped = exports.U8A_WRAP_PREFIX = exports.U8A_WRAP_POSTFIX = exports.U8A_WRAP_ETHEREUM = exports.u8aToU8a = exports.u8aToString = exports.u8aToNumber = exports.u8aToHex = exports.u8aToFloat = exports.u8aToBuffer = exports.u8aToBn = exports.u8aToBigInt = exports.u8aSorted = exports.u8aFixLength = exports.u8aEq = exports.u8aEmpty = exports.u8aConcatStrict = exports.u8aConcat = exports.u8aCmp = void 0;
/**
* @summary Utility methods to convert to and from `Uint8Array` objects
*/
var cmp_js_1 = require("./cmp.js");
Object.defineProperty(exports, "u8aCmp", { enumerable: true, get: function () { return cmp_js_1.u8aCmp; } });
var concat_js_1 = require("./concat.js");
Object.defineProperty(exports, "u8aConcat", { enumerable: true, get: function () { return concat_js_1.u8aConcat; } });
Object.defineProperty(exports, "u8aConcatStrict", { enumerable: true, get: function () { return concat_js_1.u8aConcatStrict; } });
var empty_js_1 = require("./empty.js");
Object.defineProperty(exports, "u8aEmpty", { enumerable: true, get: function () { return empty_js_1.u8aEmpty; } });
var eq_js_1 = require("./eq.js");
Object.defineProperty(exports, "u8aEq", { enumerable: true, get: function () { return eq_js_1.u8aEq; } });
var fixLength_js_1 = require("./fixLength.js");
Object.defineProperty(exports, "u8aFixLength", { enumerable: true, get: function () { return fixLength_js_1.u8aFixLength; } });
var sorted_js_1 = require("./sorted.js");
Object.defineProperty(exports, "u8aSorted", { enumerable: true, get: function () { return sorted_js_1.u8aSorted; } });
var toBigInt_js_1 = require("./toBigInt.js");
Object.defineProperty(exports, "u8aToBigInt", { enumerable: true, get: function () { return toBigInt_js_1.u8aToBigInt; } });
var toBn_js_1 = require("./toBn.js");
Object.defineProperty(exports, "u8aToBn", { enumerable: true, get: function () { return toBn_js_1.u8aToBn; } });
var toBuffer_js_1 = require("./toBuffer.js");
Object.defineProperty(exports, "u8aToBuffer", { enumerable: true, get: function () { return toBuffer_js_1.u8aToBuffer; } });
var toFloat_js_1 = require("./toFloat.js");
Object.defineProperty(exports, "u8aToFloat", { enumerable: true, get: function () { return toFloat_js_1.u8aToFloat; } });
var toHex_js_1 = require("./toHex.js");
Object.defineProperty(exports, "u8aToHex", { enumerable: true, get: function () { return toHex_js_1.u8aToHex; } });
var toNumber_js_1 = require("./toNumber.js");
Object.defineProperty(exports, "u8aToNumber", { enumerable: true, get: function () { return toNumber_js_1.u8aToNumber; } });
var toString_js_1 = require("./toString.js");
Object.defineProperty(exports, "u8aToString", { enumerable: true, get: function () { return toString_js_1.u8aToString; } });
var toU8a_js_1 = require("./toU8a.js");
Object.defineProperty(exports, "u8aToU8a", { enumerable: true, get: function () { return toU8a_js_1.u8aToU8a; } });
var wrap_js_1 = require("./wrap.js");
Object.defineProperty(exports, "U8A_WRAP_ETHEREUM", { enumerable: true, get: function () { return wrap_js_1.U8A_WRAP_ETHEREUM; } });
Object.defineProperty(exports, "U8A_WRAP_POSTFIX", { enumerable: true, get: function () { return wrap_js_1.U8A_WRAP_POSTFIX; } });
Object.defineProperty(exports, "U8A_WRAP_PREFIX", { enumerable: true, get: function () { return wrap_js_1.U8A_WRAP_PREFIX; } });
Object.defineProperty(exports, "u8aIsWrapped", { enumerable: true, get: function () { return wrap_js_1.u8aIsWrapped; } });
Object.defineProperty(exports, "u8aUnwrapBytes", { enumerable: true, get: function () { return wrap_js_1.u8aUnwrapBytes; } });
Object.defineProperty(exports, "u8aWrapBytes", { enumerable: true, get: function () { return wrap_js_1.u8aWrapBytes; } });
+15
View File
@@ -0,0 +1,15 @@
/**
* @name u8aSorted
* @summary Sorts an array of Uint8Arrays
* @description
* For input `UInt8Array[]` return the sorted result
* @example
* <BR>
*
* ```javascript
* import { u8aSorted} from '@pezkuwi/util';
*
* u8aSorted([new Uint8Array([0x69]), new Uint8Array([0x68])]); // [0x68, 0x69]
* ```
*/
export declare function u8aSorted(u8as: Uint8Array[]): Uint8Array[];
+21
View File
@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aSorted = u8aSorted;
const cmp_js_1 = require("./cmp.js");
/**
* @name u8aSorted
* @summary Sorts an array of Uint8Arrays
* @description
* For input `UInt8Array[]` return the sorted result
* @example
* <BR>
*
* ```javascript
* import { u8aSorted} from '@pezkuwi/util';
*
* u8aSorted([new Uint8Array([0x69]), new Uint8Array([0x68])]); // [0x68, 0x69]
* ```
*/
function u8aSorted(u8as) {
return u8as.sort(cmp_js_1.u8aCmp);
}
+6
View File
@@ -0,0 +1,6 @@
import type { ToBnOptions } from '../types.js';
/**
* @name u8aToBigInt
* @summary Creates a BigInt from a Uint8Array object.
*/
export declare function u8aToBigInt(value: Uint8Array, { isLe, isNegative }?: ToBnOptions): bigint;
+73
View File
@@ -0,0 +1,73 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToBigInt = u8aToBigInt;
const x_bigint_1 = require("@pezkuwi/x-bigint");
const consts_js_1 = require("../bi/consts.js");
const U8_MAX = (0, x_bigint_1.BigInt)(256);
const U16_MAX = (0, x_bigint_1.BigInt)(256 * 256);
const U64_MAX = (0, x_bigint_1.BigInt)('0x10000000000000000');
/**
* @name u8aToBigInt
* @summary Creates a BigInt from a Uint8Array object.
*/
function u8aToBigInt(value, { isLe = true, isNegative = false } = {}) {
// slice + reverse is expensive, however SCALE is LE by default so this is the path
// we are most interested in (the BE is added for the sake of being comprehensive)
if (!isLe) {
value = value.slice().reverse();
}
const count = value.length;
if (isNegative && count && (value[count - 1] & 0x80)) {
switch (count) {
case 0:
return (0, x_bigint_1.BigInt)(0);
case 1:
return (0, x_bigint_1.BigInt)(((value[0] ^ 0x0000_00ff) * -1) - 1);
case 2:
return (0, x_bigint_1.BigInt)((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
case 4:
return (0, x_bigint_1.BigInt)((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
}
const dvI = new DataView(value.buffer, value.byteOffset);
if (count === 8) {
return dvI.getBigInt64(0, true);
}
let result = (0, x_bigint_1.BigInt)(0);
const mod = count % 2;
for (let i = count - 2; i >= mod; i -= 2) {
result = (result * U16_MAX) + (0, x_bigint_1.BigInt)(dvI.getUint16(i, true) ^ 0xffff);
}
if (mod) {
result = (result * U8_MAX) + (0, x_bigint_1.BigInt)(value[0] ^ 0xff);
}
return (result * -consts_js_1._1n) - consts_js_1._1n;
}
switch (count) {
case 0:
return (0, x_bigint_1.BigInt)(0);
case 1:
return (0, x_bigint_1.BigInt)(value[0]);
case 2:
return (0, x_bigint_1.BigInt)(value[0] + (value[1] << 8));
case 4:
return (0, x_bigint_1.BigInt)(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
}
const dvI = new DataView(value.buffer, value.byteOffset);
switch (count) {
case 8:
return dvI.getBigUint64(0, true);
case 16:
return (dvI.getBigUint64(8, true) * U64_MAX) + dvI.getBigUint64(0, true);
default: {
let result = (0, x_bigint_1.BigInt)(0);
const mod = count % 2;
for (let i = count - 2; i >= mod; i -= 2) {
result = (result * U16_MAX) + (0, x_bigint_1.BigInt)(dvI.getUint16(i, true));
}
if (mod) {
result = (result * U8_MAX) + (0, x_bigint_1.BigInt)(value[0]);
}
return result;
}
}
}
+21
View File
@@ -0,0 +1,21 @@
import type { ToBnOptions } from '../types.js';
import { BN } from '../bn/bn.js';
/**
* @name u8aToBn
* @summary Creates a BN from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
* @param value The value to convert
* @param options Options to pass while converting
* @param options.isLe Convert using Little Endian (default)
* @param options.isNegative Convert using two's complement
* @example
* <BR>
*
* ```javascript
* import { u8aToBn } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export declare function u8aToBn(value: Uint8Array, { isLe, isNegative }?: ToBnOptions): BN;
+81
View File
@@ -0,0 +1,81 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToBn = u8aToBn;
const bn_js_1 = require("../bn/bn.js");
/**
* @name u8aToBn
* @summary Creates a BN from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
* @param value The value to convert
* @param options Options to pass while converting
* @param options.isLe Convert using Little Endian (default)
* @param options.isNegative Convert using two's complement
* @example
* <BR>
*
* ```javascript
* import { u8aToBn } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
function u8aToBn(value, { isLe = true, isNegative = false } = {}) {
// slice + reverse is expensive, however SCALE is LE by default so this is the path
// we are most interested in (the BE is added for the sake of being comprehensive)
if (!isLe) {
value = value.slice().reverse();
}
const count = value.length;
// shortcut for <= u48 values - in this case the manual conversion
// here seems to be more efficient than passing the full array
if (isNegative && count && (value[count - 1] & 0x80)) {
// Most common case i{8, 16, 32} default LE SCALE-encoded
// For <= 32, we also optimize the xor to a single op
switch (count) {
case 0:
return new bn_js_1.BN(0);
case 1:
return new bn_js_1.BN(((value[0] ^ 0x0000_00ff) * -1) - 1);
case 2:
return new bn_js_1.BN((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
case 3:
return new bn_js_1.BN((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 0x00ff_ffff) * -1) - 1);
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return new bn_js_1.BN((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
case 5:
return new bn_js_1.BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00)) * -1) - 1);
case 6:
return new bn_js_1.BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00)) * -1) - 1);
default:
return new bn_js_1.BN(value, 'le').fromTwos(count * 8);
}
}
// Most common case - u{8, 16, 32} default LE SCALE-encoded
//
// There are some slight benefits in unrolling this specific loop,
// however it comes with diminishing returns since here the actual
// `new BN` does seem to take up the bulk of the time
switch (count) {
case 0:
return new bn_js_1.BN(0);
case 1:
return new bn_js_1.BN(value[0]);
case 2:
return new bn_js_1.BN(value[0] + (value[1] << 8));
case 3:
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16));
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
case 5:
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00));
case 6:
return new bn_js_1.BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00));
default:
return new bn_js_1.BN(value, 'le');
}
}
+16
View File
@@ -0,0 +1,16 @@
import type { BufferObject } from '../types.js';
/**
* @name u8aToBuffer
* @summary Creates a Buffer object from a hex string.
* @description
* `null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
* @example
* <BR>
*
* ```javascript
* import { u8aToBuffer } from '@pezkuwi/util';
*
* console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3])));
* ```
*/
export declare function u8aToBuffer<T = BufferObject>(value?: Uint8Array | null): T;
+24
View File
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToBuffer = u8aToBuffer;
const x_global_1 = require("@pezkuwi/x-global");
const has_js_1 = require("../has.js");
/**
* @name u8aToBuffer
* @summary Creates a Buffer object from a hex string.
* @description
* `null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
* @example
* <BR>
*
* ```javascript
* import { u8aToBuffer } from '@pezkuwi/util';
*
* console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3])));
* ```
*/
function u8aToBuffer(value) {
return has_js_1.hasBuffer
? x_global_1.xglobal.Buffer.from(value || [])
: new Uint8Array(value || []);
}
+11
View File
@@ -0,0 +1,11 @@
interface Options {
bitLength?: 32 | 64;
isLe?: boolean;
}
/**
* @name u8aToFloat
* @description Converts a Uint8Array value into the float (either 32 or 64-bit)
* representation.
*/
export declare function u8aToFloat(value: Uint8Array, { bitLength, isLe }?: Options): number;
export {};
+20
View File
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToFloat = u8aToFloat;
/**
* @name u8aToFloat
* @description Converts a Uint8Array value into the float (either 32 or 64-bit)
* representation.
*/
function u8aToFloat(value, { bitLength = 32, isLe = true } = {}) {
if (bitLength !== 32 && bitLength !== 64) {
throw new Error('Invalid bitLength provided, expected 32 or 64');
}
else if (value.length < (bitLength / 8)) {
throw new Error(`Invalid input buffer provided, expected at least ${bitLength / 8} bytes, found ${value.length}`);
}
const dv = new DataView(value.buffer, value.byteOffset);
return bitLength === 32
? dv.getFloat32(0, isLe)
: dv.getFloat64(0, isLe);
}
+16
View File
@@ -0,0 +1,16 @@
import type { HexString } from '../types.js';
/**
* @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
* <BR>
*
* ```javascript
* import { u8aToHex } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export declare function u8aToHex(value?: Uint8Array | null, bitLength?: number, isPrefixed?: boolean): HexString;
+56
View File
@@ -0,0 +1,56 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToHex = u8aToHex;
const U8 = new Array(256);
const U16 = new Array(256 * 256);
for (let n = 0; n < 256; n++) {
U8[n] = n.toString(16).padStart(2, '0');
}
for (let i = 0; i < 256; i++) {
const s = i << 8;
for (let j = 0; j < 256; j++) {
U16[s | j] = U8[i] + U8[j];
}
}
/** @internal */
function hex(value, result) {
const mod = (value.length % 2) | 0;
const length = (value.length - mod) | 0;
for (let i = 0; i < length; i += 2) {
result += U16[(value[i] << 8) | value[i + 1]];
}
if (mod) {
result += U8[value[length] | 0];
}
return result;
}
/**
* @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
* <BR>
*
* ```javascript
* import { u8aToHex } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
function u8aToHex(value, bitLength = -1, isPrefixed = true) {
// this is not 100% correct sinmce we support isPrefixed = false....
const empty = isPrefixed
? '0x'
: '';
if (!value?.length) {
return empty;
}
else if (bitLength > 0) {
const length = Math.ceil(bitLength / 8);
if (value.length > length) {
return `${hex(value.subarray(0, length / 2), empty)}${hex(value.subarray(value.length - length / 2), '')}`;
}
}
return hex(value, empty);
}
+16
View File
@@ -0,0 +1,16 @@
import type { HexString } from '../types.js';
/**
* @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
* <BR>
*
* ```javascript
* import { u8aToHex } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export declare function u8aToHex(value?: Uint8Array | null, bitLength?: number, isPrefixed?: boolean): HexString;
+25
View File
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToHex = u8aToHex;
/**
* @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
* <BR>
*
* ```javascript
* import { u8aToHex } from '@pezkuwi/util';
*
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
function u8aToHex(value, bitLength = -1, isPrefixed = true) {
const length = Math.ceil(bitLength / 8);
return `${isPrefixed ? '0x' : ''}${!value?.length
? ''
: (bitLength > 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')}`;
}
+6
View File
@@ -0,0 +1,6 @@
import type { ToBnOptions } from '../types.js';
/**
* @name u8aToNumber
* @summary Creates a number from a Uint8Array object.
*/
export declare function u8aToNumber(value: Uint8Array, { isLe, isNegative }?: ToBnOptions): number;
+61
View File
@@ -0,0 +1,61 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToNumber = u8aToNumber;
/**
* @name u8aToNumber
* @summary Creates a number from a Uint8Array object.
*/
function u8aToNumber(value, { isLe = true, isNegative = false } = {}) {
// slice + reverse is expensive, however SCALE is LE by default so this is the path
// we are most interested in (the BE is added for the sake of being comprehensive)
if (!isLe) {
value = value.slice().reverse();
}
const count = value.length;
// When the value is a i{8, 16, 24, 32, 40, 40} values and the top-most bit
// indicates a signed value, we use a two's complement conversion. If one of these
// flags are not set, we just do a normal unsigned conversion (the same shortcut
// applies in both the u8aTo{BigInt, Bn} conversions as well)
if (isNegative && count && (value[count - 1] & 0x80)) {
switch (count) {
case 0:
return 0;
case 1:
return (((value[0] ^ 0x0000_00ff) * -1) - 1);
case 2:
return ((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
case 3:
return ((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 0x00ff_ffff) * -1) - 1);
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return ((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
case 5:
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00)) * -1) - 1);
case 6:
return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00)) * -1) - 1);
default:
throw new Error('Value more than 48-bits cannot be reliably converted');
}
}
switch (count) {
case 0:
return 0;
case 1:
return value[0];
case 2:
return value[0] + (value[1] << 8);
case 3:
return value[0] + (value[1] << 8) + (value[2] << 16);
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00);
case 5:
return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00);
case 6:
return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00);
default:
throw new Error('Value more than 48-bits cannot be reliably converted');
}
}
+15
View File
@@ -0,0 +1,15 @@
/**
* @name u8aToString
* @summary Creates a utf-8 string from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
* @example
* <BR>
*
* ```javascript
* import { u8aToString } from '@pezkuwi/util';
*
* u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
* ```
*/
export declare function u8aToString(value?: Uint8Array | null): string;
+24
View File
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToString = u8aToString;
const x_textdecoder_1 = require("@pezkuwi/x-textdecoder");
const decoder = new x_textdecoder_1.TextDecoder('utf-8');
/**
* @name u8aToString
* @summary Creates a utf-8 string from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
* @example
* <BR>
*
* ```javascript
* import { u8aToString } from '@pezkuwi/util';
*
* u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
* ```
*/
function u8aToString(value) {
return value
? decoder.decode(value)
: '';
}
+19
View File
@@ -0,0 +1,19 @@
import type { U8aLike } from '../types.js';
/**
* @name u8aToU8a
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
* @description
* `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
* If `strict` is true, `null` or `undefined` will throw an error instead of returning an empty array.
* Supports input types: Uint8Array, Buffer, hex string, string, or number array.
* @example
* <BR>
*
* ```javascript
* import { u8aToU8a } from '@pezkuwi/util';
*
* u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
* ```
*/
export declare function u8aToU8a(value?: U8aLike | null, strict?: boolean): Uint8Array;
+42
View File
@@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.u8aToU8a = u8aToU8a;
const toU8a_js_1 = require("../hex/toU8a.js");
const buffer_js_1 = require("../is/buffer.js");
const hex_js_1 = require("../is/hex.js");
const u8a_js_1 = require("../is/u8a.js");
const toU8a_js_2 = require("../string/toU8a.js");
/**
* @name u8aToU8a
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
* @description
* `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
* If `strict` is true, `null` or `undefined` will throw an error instead of returning an empty array.
* Supports input types: Uint8Array, Buffer, hex string, string, or number array.
* @example
* <BR>
*
* ```javascript
* import { u8aToU8a } from '@pezkuwi/util';
*
* u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
* ```
*/
function u8aToU8a(value, strict = false) {
if (strict && (value === null || value === undefined)) {
throw new Error('u8aToU8a: Expected non-null, non-undefined value');
}
return (0, u8a_js_1.isU8a)(value)
// NOTE isBuffer needs to go here since it actually extends
// Uint8Array on Node.js environments, so all Buffer are Uint8Array,
// but Uint8Array is not Buffer
? (0, buffer_js_1.isBuffer)(value)
? new Uint8Array(value)
: value
: (0, hex_js_1.isHex)(value)
? (0, toU8a_js_1.hexToU8a)(value)
: Array.isArray(value)
? new Uint8Array(value)
: (0, toU8a_js_2.stringToU8a)(value);
}
+22
View File
@@ -0,0 +1,22 @@
import type { U8aLike } from '../types.js';
/** @internal */
export declare const U8A_WRAP_ETHEREUM: Uint8Array;
/** @internal */
export declare const U8A_WRAP_PREFIX: Uint8Array;
/** @internal */
export declare const U8A_WRAP_POSTFIX: Uint8Array;
/** @internal */
export declare function u8aIsWrapped(u8a: Uint8Array, withEthereum: boolean): boolean;
/**
* @name u8aUnwrapBytes
* @description Removes all <Bytes>...</Bytes> wrappers from the supplied value
*/
export declare function u8aUnwrapBytes(bytes: U8aLike): Uint8Array;
/**
* @name u8aWrapBytes
* @description
* Adds a <Bytes>...</Bytes> wrapper to the supplied value, if
* - We don't already have a Bytes wrapper
* - The message is not an Ethereum-style message
*/
export declare function u8aWrapBytes(bytes: U8aLike): Uint8Array;
+49
View File
@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.U8A_WRAP_POSTFIX = exports.U8A_WRAP_PREFIX = exports.U8A_WRAP_ETHEREUM = void 0;
exports.u8aIsWrapped = u8aIsWrapped;
exports.u8aUnwrapBytes = u8aUnwrapBytes;
exports.u8aWrapBytes = u8aWrapBytes;
const concat_js_1 = require("./concat.js");
const eq_js_1 = require("./eq.js");
const toU8a_js_1 = require("./toU8a.js");
/** @internal */
exports.U8A_WRAP_ETHEREUM = (0, toU8a_js_1.u8aToU8a)('\x19Ethereum Signed Message:\n');
/** @internal */
exports.U8A_WRAP_PREFIX = (0, toU8a_js_1.u8aToU8a)('<Bytes>');
/** @internal */
exports.U8A_WRAP_POSTFIX = (0, toU8a_js_1.u8aToU8a)('</Bytes>');
const WRAP_LEN = exports.U8A_WRAP_PREFIX.length + exports.U8A_WRAP_POSTFIX.length;
/** @internal */
function u8aIsWrapped(u8a, withEthereum) {
return ((u8a.length >= WRAP_LEN &&
(0, eq_js_1.u8aEq)(u8a.subarray(0, exports.U8A_WRAP_PREFIX.length), exports.U8A_WRAP_PREFIX) &&
(0, eq_js_1.u8aEq)(u8a.slice(-exports.U8A_WRAP_POSTFIX.length), exports.U8A_WRAP_POSTFIX)) ||
(withEthereum &&
u8a.length >= exports.U8A_WRAP_ETHEREUM.length &&
(0, eq_js_1.u8aEq)(u8a.subarray(0, exports.U8A_WRAP_ETHEREUM.length), exports.U8A_WRAP_ETHEREUM)));
}
/**
* @name u8aUnwrapBytes
* @description Removes all <Bytes>...</Bytes> wrappers from the supplied value
*/
function u8aUnwrapBytes(bytes) {
const u8a = (0, toU8a_js_1.u8aToU8a)(bytes);
// we don't want to unwrap Ethereum-style wraps
return u8aIsWrapped(u8a, false)
? u8a.subarray(exports.U8A_WRAP_PREFIX.length, u8a.length - exports.U8A_WRAP_POSTFIX.length)
: u8a;
}
/**
* @name u8aWrapBytes
* @description
* Adds a <Bytes>...</Bytes> wrapper to the supplied value, if
* - We don't already have a Bytes wrapper
* - The message is not an Ethereum-style message
*/
function u8aWrapBytes(bytes) {
const u8a = (0, toU8a_js_1.u8aToU8a)(bytes);
return u8aIsWrapped(u8a, true)
? u8a
: (0, concat_js_1.u8aConcatStrict)([exports.U8A_WRAP_PREFIX, u8a, exports.U8A_WRAP_POSTFIX]);
}