mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 04:27:59 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+17
@@ -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;
|
||||
@@ -0,0 +1,46 @@
|
||||
import { u8aToU8a } from './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
|
||||
* ```
|
||||
*/
|
||||
export function u8aCmp(a, b) {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = 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++;
|
||||
}
|
||||
}
|
||||
Vendored
+24
@@ -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;
|
||||
@@ -0,0 +1,47 @@
|
||||
import { u8aToU8a } from './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]
|
||||
* ```
|
||||
*/
|
||||
export function u8aConcat(...list) {
|
||||
const count = list.length;
|
||||
const u8as = new Array(count);
|
||||
let length = 0;
|
||||
for (let i = 0; i < count; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
length += u8as[i].length;
|
||||
}
|
||||
return u8aConcatStrict(u8as, length);
|
||||
}
|
||||
/**
|
||||
* @name u8aConcatStrict
|
||||
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export 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;
|
||||
}
|
||||
Vendored
+19
@@ -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;
|
||||
@@ -0,0 +1,26 @@
|
||||
import { u8aToU8a } from './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]
|
||||
* ```
|
||||
*/
|
||||
export function u8aConcat(...list) {
|
||||
const count = list.length;
|
||||
const u8as = new Array(count);
|
||||
for (let i = 0; i < count; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
}
|
||||
return Uint8Array.from(Buffer.concat(u8as));
|
||||
}
|
||||
Vendored
+7
@@ -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;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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 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;
|
||||
}
|
||||
Vendored
+15
@@ -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;
|
||||
@@ -0,0 +1,37 @@
|
||||
import { u8aToU8a } from './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
|
||||
* ```
|
||||
*/
|
||||
export function u8aEq(a, b) {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = 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;
|
||||
}
|
||||
Vendored
+17
@@ -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;
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @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 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;
|
||||
}
|
||||
Vendored
+18
@@ -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';
|
||||
@@ -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';
|
||||
Vendored
+15
@@ -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[];
|
||||
@@ -0,0 +1,18 @@
|
||||
import { u8aCmp } from './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]
|
||||
* ```
|
||||
*/
|
||||
export function u8aSorted(u8as) {
|
||||
return u8as.sort(u8aCmp);
|
||||
}
|
||||
Vendored
+6
@@ -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;
|
||||
@@ -0,0 +1,70 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { _1n } from '../bi/consts.js';
|
||||
const U8_MAX = BigInt(256);
|
||||
const U16_MAX = BigInt(256 * 256);
|
||||
const U64_MAX = BigInt('0x10000000000000000');
|
||||
/**
|
||||
* @name u8aToBigInt
|
||||
* @summary Creates a BigInt from a Uint8Array object.
|
||||
*/
|
||||
export 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 BigInt(0);
|
||||
case 1:
|
||||
return BigInt(((value[0] ^ 0x0000_00ff) * -1) - 1);
|
||||
case 2:
|
||||
return BigInt((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
|
||||
case 4:
|
||||
return 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 = BigInt(0);
|
||||
const mod = count % 2;
|
||||
for (let i = count - 2; i >= mod; i -= 2) {
|
||||
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true) ^ 0xffff);
|
||||
}
|
||||
if (mod) {
|
||||
result = (result * U8_MAX) + BigInt(value[0] ^ 0xff);
|
||||
}
|
||||
return (result * -_1n) - _1n;
|
||||
}
|
||||
switch (count) {
|
||||
case 0:
|
||||
return BigInt(0);
|
||||
case 1:
|
||||
return BigInt(value[0]);
|
||||
case 2:
|
||||
return BigInt(value[0] + (value[1] << 8));
|
||||
case 4:
|
||||
return 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 = BigInt(0);
|
||||
const mod = count % 2;
|
||||
for (let i = count - 2; i >= mod; i -= 2) {
|
||||
result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true));
|
||||
}
|
||||
if (mod) {
|
||||
result = (result * U8_MAX) + BigInt(value[0]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+21
@@ -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;
|
||||
@@ -0,0 +1,78 @@
|
||||
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 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(0);
|
||||
case 1:
|
||||
return new BN(((value[0] ^ 0x0000_00ff) * -1) - 1);
|
||||
case 2:
|
||||
return new BN((((value[0] + (value[1] << 8)) ^ 0x0000_ffff) * -1) - 1);
|
||||
case 3:
|
||||
return new 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((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) * -1) - 1);
|
||||
case 5:
|
||||
return new 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(((((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(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(0);
|
||||
case 1:
|
||||
return new BN(value[0]);
|
||||
case 2:
|
||||
return new BN(value[0] + (value[1] << 8));
|
||||
case 3:
|
||||
return new 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(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
|
||||
case 5:
|
||||
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00));
|
||||
case 6:
|
||||
return new 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(value, 'le');
|
||||
}
|
||||
}
|
||||
Vendored
+16
@@ -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;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { xglobal } from '@pezkuwi/x-global';
|
||||
import { hasBuffer } from '../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])));
|
||||
* ```
|
||||
*/
|
||||
export function u8aToBuffer(value) {
|
||||
return hasBuffer
|
||||
? xglobal.Buffer.from(value || [])
|
||||
: new Uint8Array(value || []);
|
||||
}
|
||||
Vendored
+11
@@ -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 {};
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @name u8aToFloat
|
||||
* @description Converts a Uint8Array value into the float (either 32 or 64-bit)
|
||||
* representation.
|
||||
*/
|
||||
export 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);
|
||||
}
|
||||
Vendored
+16
@@ -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;
|
||||
@@ -0,0 +1,53 @@
|
||||
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
|
||||
* ```
|
||||
*/
|
||||
export 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);
|
||||
}
|
||||
Vendored
+16
@@ -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;
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @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 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')}`;
|
||||
}
|
||||
Vendored
+6
@@ -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;
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @name u8aToNumber
|
||||
* @summary Creates a number from a Uint8Array object.
|
||||
*/
|
||||
export 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');
|
||||
}
|
||||
}
|
||||
Vendored
+15
@@ -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;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { TextDecoder } from '@pezkuwi/x-textdecoder';
|
||||
const decoder = new 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
|
||||
* ```
|
||||
*/
|
||||
export function u8aToString(value) {
|
||||
return value
|
||||
? decoder.decode(value)
|
||||
: '';
|
||||
}
|
||||
Vendored
+19
@@ -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;
|
||||
@@ -0,0 +1,39 @@
|
||||
import { hexToU8a } from '../hex/toU8a.js';
|
||||
import { isBuffer } from '../is/buffer.js';
|
||||
import { isHex } from '../is/hex.js';
|
||||
import { isU8a } from '../is/u8a.js';
|
||||
import { stringToU8a } from '../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])
|
||||
* ```
|
||||
*/
|
||||
export function u8aToU8a(value, strict = false) {
|
||||
if (strict && (value === null || value === undefined)) {
|
||||
throw new Error('u8aToU8a: Expected non-null, non-undefined value');
|
||||
}
|
||||
return 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
|
||||
? isBuffer(value)
|
||||
? new Uint8Array(value)
|
||||
: value
|
||||
: isHex(value)
|
||||
? hexToU8a(value)
|
||||
: Array.isArray(value)
|
||||
? new Uint8Array(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
Vendored
+22
@@ -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;
|
||||
@@ -0,0 +1,43 @@
|
||||
import { u8aConcatStrict } from './concat.js';
|
||||
import { u8aEq } from './eq.js';
|
||||
import { u8aToU8a } from './toU8a.js';
|
||||
/** @internal */
|
||||
export const U8A_WRAP_ETHEREUM = /*#__PURE__*/ u8aToU8a('\x19Ethereum Signed Message:\n');
|
||||
/** @internal */
|
||||
export const U8A_WRAP_PREFIX = /*#__PURE__*/ u8aToU8a('<Bytes>');
|
||||
/** @internal */
|
||||
export const U8A_WRAP_POSTFIX = /*#__PURE__*/ u8aToU8a('</Bytes>');
|
||||
const WRAP_LEN = U8A_WRAP_PREFIX.length + U8A_WRAP_POSTFIX.length;
|
||||
/** @internal */
|
||||
export function u8aIsWrapped(u8a, withEthereum) {
|
||||
return ((u8a.length >= WRAP_LEN &&
|
||||
u8aEq(u8a.subarray(0, U8A_WRAP_PREFIX.length), U8A_WRAP_PREFIX) &&
|
||||
u8aEq(u8a.slice(-U8A_WRAP_POSTFIX.length), U8A_WRAP_POSTFIX)) ||
|
||||
(withEthereum &&
|
||||
u8a.length >= U8A_WRAP_ETHEREUM.length &&
|
||||
u8aEq(u8a.subarray(0, U8A_WRAP_ETHEREUM.length), U8A_WRAP_ETHEREUM)));
|
||||
}
|
||||
/**
|
||||
* @name u8aUnwrapBytes
|
||||
* @description Removes all <Bytes>...</Bytes> wrappers from the supplied value
|
||||
*/
|
||||
export function u8aUnwrapBytes(bytes) {
|
||||
const u8a = u8aToU8a(bytes);
|
||||
// we don't want to unwrap Ethereum-style wraps
|
||||
return u8aIsWrapped(u8a, false)
|
||||
? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - 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
|
||||
*/
|
||||
export function u8aWrapBytes(bytes) {
|
||||
const u8a = u8aToU8a(bytes);
|
||||
return u8aIsWrapped(u8a, true)
|
||||
? u8a
|
||||
: u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);
|
||||
}
|
||||
Reference in New Issue
Block a user