mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 10:17:57 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name compactAddLength
|
||||
* @description Adds a length prefix to the input value
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactAddLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
|
||||
* ```
|
||||
*/
|
||||
export declare function compactAddLength(input: Uint8Array): Uint8Array;
|
||||
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compactAddLength = compactAddLength;
|
||||
const index_js_1 = require("../u8a/index.js");
|
||||
const toU8a_js_1 = require("./toU8a.js");
|
||||
/**
|
||||
* @name compactAddLength
|
||||
* @description Adds a length prefix to the input value
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactAddLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
|
||||
* ```
|
||||
*/
|
||||
function compactAddLength(input) {
|
||||
return (0, index_js_1.u8aConcatStrict)([
|
||||
(0, toU8a_js_1.compactToU8a)(input.length),
|
||||
input
|
||||
]);
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import type { BitLength } from './types.js';
|
||||
/** @internal */
|
||||
export declare const DEFAULT_BITLENGTH: BitLength;
|
||||
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DEFAULT_BITLENGTH = void 0;
|
||||
/** @internal */
|
||||
exports.DEFAULT_BITLENGTH = 32;
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import type { U8aLike } from '../types.js';
|
||||
import { BN } from '../bn/index.js';
|
||||
/**
|
||||
* @name compactFromU8a
|
||||
* @description Retrives the offset and encoded length from a compact-prefixed value
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactFromU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]));
|
||||
*
|
||||
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
|
||||
* ```
|
||||
*/
|
||||
export declare function compactFromU8a(input: U8aLike): [number, BN];
|
||||
/**
|
||||
* @name compactFromU8aLim
|
||||
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
|
||||
*/
|
||||
export declare function compactFromU8aLim(u8a: Uint8Array): [number, number];
|
||||
@@ -0,0 +1,92 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compactFromU8a = compactFromU8a;
|
||||
exports.compactFromU8aLim = compactFromU8aLim;
|
||||
const index_js_1 = require("../bn/index.js");
|
||||
const index_js_2 = require("../u8a/index.js");
|
||||
/**
|
||||
* @name compactFromU8a
|
||||
* @description Retrives the offset and encoded length from a compact-prefixed value
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactFromU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]));
|
||||
*
|
||||
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
|
||||
* ```
|
||||
*/
|
||||
function compactFromU8a(input) {
|
||||
const u8a = (0, index_js_2.u8aToU8a)(input);
|
||||
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||
// than doing an additional call to u8aToBn (as with variable length)
|
||||
switch (u8a[0] & 0b11) {
|
||||
case 0b00:
|
||||
return [1, new index_js_1.BN(u8a[0] >>> 2)];
|
||||
case 0b01:
|
||||
return [2, new index_js_1.BN((u8a[0] + (u8a[1] << 8)) >>> 2)];
|
||||
case 0b10:
|
||||
// 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 [4, new index_js_1.BN((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
|
||||
// 0b11
|
||||
default: {
|
||||
// add 5 to shifted (4 for base length, 1 for this byte)
|
||||
const offset = (u8a[0] >>> 2) + 5;
|
||||
// we unroll the loop
|
||||
switch (offset) {
|
||||
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
|
||||
case 5:
|
||||
// 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 [5, new index_js_1.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00))];
|
||||
case 6:
|
||||
return [6, new index_js_1.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00))];
|
||||
// 6 bytes data is the maximum, 48 bits (56 would overflow)
|
||||
case 7:
|
||||
return [7, new index_js_1.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00))];
|
||||
// for anything else, use the non-unrolled version
|
||||
default:
|
||||
return [offset, (0, index_js_2.u8aToBn)(u8a.subarray(1, offset))];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @name compactFromU8aLim
|
||||
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
|
||||
*/
|
||||
function compactFromU8aLim(u8a) {
|
||||
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||
// than doing an additional call to u8aToBn (as with variable length)
|
||||
switch (u8a[0] & 0b11) {
|
||||
case 0b00:
|
||||
return [1, u8a[0] >>> 2];
|
||||
case 0b01:
|
||||
return [2, (u8a[0] + (u8a[1] << 8)) >>> 2];
|
||||
case 0b10:
|
||||
// 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 [4, (u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2];
|
||||
// 0b11
|
||||
default: {
|
||||
// add 5 to shifted (4 for base length, 1 for this byte)
|
||||
// we unroll the loop
|
||||
switch ((u8a[0] >>> 2) + 5) {
|
||||
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
|
||||
case 5:
|
||||
return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00)];
|
||||
case 6:
|
||||
return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00)];
|
||||
// 6 bytes data is the maximum, 48 bits (56 would overflow)
|
||||
case 7:
|
||||
return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00)];
|
||||
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
|
||||
default:
|
||||
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @description
|
||||
* Encoding and decoding of parity-codec compact numbers. The codec is created
|
||||
* to take up the least amount of space for a specific number. It performs the
|
||||
* same function as Length, however differs in that it uses a variable number of
|
||||
* bytes to do the actual encoding. From the Rust implementation for compact
|
||||
* encoding:
|
||||
*
|
||||
* 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
|
||||
* (0 ... 2**6 - 1) (u8)
|
||||
* xx xx xx 00
|
||||
* (2**6 ... 2**14 - 1) (u8, u16) low LH high
|
||||
* yL yL yL 01 / yH yH yH yL
|
||||
* (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
|
||||
* zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
|
||||
* (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
|
||||
* nn nn nn 11 [ / zz zz zz zz ]{4 + n}
|
||||
*
|
||||
* Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
|
||||
*/
|
||||
export { compactAddLength } from './addLength.js';
|
||||
export { compactFromU8a, compactFromU8aLim } from './fromU8a.js';
|
||||
export { compactStripLength } from './stripLength.js';
|
||||
export { compactToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compactToU8a = exports.compactStripLength = exports.compactFromU8aLim = exports.compactFromU8a = exports.compactAddLength = void 0;
|
||||
/**
|
||||
* @description
|
||||
* Encoding and decoding of parity-codec compact numbers. The codec is created
|
||||
* to take up the least amount of space for a specific number. It performs the
|
||||
* same function as Length, however differs in that it uses a variable number of
|
||||
* bytes to do the actual encoding. From the Rust implementation for compact
|
||||
* encoding:
|
||||
*
|
||||
* 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
|
||||
* (0 ... 2**6 - 1) (u8)
|
||||
* xx xx xx 00
|
||||
* (2**6 ... 2**14 - 1) (u8, u16) low LH high
|
||||
* yL yL yL 01 / yH yH yH yL
|
||||
* (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
|
||||
* zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
|
||||
* (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
|
||||
* nn nn nn 11 [ / zz zz zz zz ]{4 + n}
|
||||
*
|
||||
* Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
|
||||
*/
|
||||
var addLength_js_1 = require("./addLength.js");
|
||||
Object.defineProperty(exports, "compactAddLength", { enumerable: true, get: function () { return addLength_js_1.compactAddLength; } });
|
||||
var fromU8a_js_1 = require("./fromU8a.js");
|
||||
Object.defineProperty(exports, "compactFromU8a", { enumerable: true, get: function () { return fromU8a_js_1.compactFromU8a; } });
|
||||
Object.defineProperty(exports, "compactFromU8aLim", { enumerable: true, get: function () { return fromU8a_js_1.compactFromU8aLim; } });
|
||||
var stripLength_js_1 = require("./stripLength.js");
|
||||
Object.defineProperty(exports, "compactStripLength", { enumerable: true, get: function () { return stripLength_js_1.compactStripLength; } });
|
||||
var toU8a_js_1 = require("./toU8a.js");
|
||||
Object.defineProperty(exports, "compactToU8a", { enumerable: true, get: function () { return toU8a_js_1.compactToU8a; } });
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name compactStripLength
|
||||
* @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactStripLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
|
||||
* ```
|
||||
*/
|
||||
export declare function compactStripLength(input: Uint8Array): [number, Uint8Array];
|
||||
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compactStripLength = compactStripLength;
|
||||
const fromU8a_js_1 = require("./fromU8a.js");
|
||||
/**
|
||||
* @name compactStripLength
|
||||
* @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactStripLength } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
|
||||
* ```
|
||||
*/
|
||||
function compactStripLength(input) {
|
||||
const [offset, length] = (0, fromU8a_js_1.compactFromU8a)(input);
|
||||
const total = offset + length.toNumber();
|
||||
return [
|
||||
total,
|
||||
input.subarray(offset, total)
|
||||
];
|
||||
}
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
import { BN } from '../bn/index.js';
|
||||
/**
|
||||
* @name compactToU8a
|
||||
* @description Encodes a number into a compact representation
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
|
||||
* ```
|
||||
*/
|
||||
export declare function compactToU8a(value: BN | bigint | number): Uint8Array;
|
||||
@@ -0,0 +1,48 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compactToU8a = compactToU8a;
|
||||
const index_js_1 = require("../bn/index.js");
|
||||
const index_js_2 = require("../u8a/index.js");
|
||||
const MAX_U8 = index_js_1.BN_TWO.pow(new index_js_1.BN(8 - 2)).isub(index_js_1.BN_ONE);
|
||||
const MAX_U16 = index_js_1.BN_TWO.pow(new index_js_1.BN(16 - 2)).isub(index_js_1.BN_ONE);
|
||||
const MAX_U32 = index_js_1.BN_TWO.pow(new index_js_1.BN(32 - 2)).isub(index_js_1.BN_ONE);
|
||||
const BL_16 = { bitLength: 16 };
|
||||
const BL_32 = { bitLength: 32 };
|
||||
/**
|
||||
* @name compactToU8a
|
||||
* @description Encodes a number into a compact representation
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { compactToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
|
||||
* ```
|
||||
*/
|
||||
function compactToU8a(value) {
|
||||
const bn = (0, index_js_1.bnToBn)(value);
|
||||
if (bn.lte(MAX_U8)) {
|
||||
return new Uint8Array([bn.toNumber() << 2]);
|
||||
}
|
||||
else if (bn.lte(MAX_U16)) {
|
||||
return (0, index_js_1.bnToU8a)(bn.shln(2).iadd(index_js_1.BN_ONE), BL_16);
|
||||
}
|
||||
else if (bn.lte(MAX_U32)) {
|
||||
return (0, index_js_1.bnToU8a)(bn.shln(2).iadd(index_js_1.BN_TWO), BL_32);
|
||||
}
|
||||
const u8a = (0, index_js_1.bnToU8a)(bn);
|
||||
let length = u8a.length;
|
||||
// adjust to the minimum number of bytes
|
||||
while (u8a[length - 1] === 0) {
|
||||
length--;
|
||||
}
|
||||
if (length < 4) {
|
||||
throw new Error('Invalid length, previous checks match anything less than 2^30');
|
||||
}
|
||||
return (0, index_js_2.u8aConcatStrict)([
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
new Uint8Array([((length - 4) << 2) + 0b11]),
|
||||
u8a.subarray(0, length)
|
||||
]);
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export type BitLength = 8 | 16 | 32 | 64 | 128 | 256;
|
||||
@@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
Reference in New Issue
Block a user