mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-10-29 13:49:49 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
commit
ec06da0ebc
687 files changed
+48096
No files matched your search
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { compactAddLength } from './index.js';
|
||||
|
||||
describe('compactAddLength', (): void => {
|
||||
it('correctly adds the length prefix', (): void => {
|
||||
expect(
|
||||
compactAddLength(Uint8Array.from([12, 13]))
|
||||
).toEqual(Uint8Array.from([2 << 2, 12, 13]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aConcatStrict } from '../u8a/index.js';
|
||||
import { compactToU8a } from './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])
|
||||
* ```
|
||||
*/
|
||||
export function compactAddLength (input: Uint8Array): Uint8Array {
|
||||
return u8aConcatStrict([
|
||||
compactToU8a(input.length),
|
||||
input
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BitLength } from './types.js';
|
||||
|
||||
/** @internal */
|
||||
export const DEFAULT_BITLENGTH: BitLength = 32;
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/bn.js';
|
||||
import { hexToU8a } from '../hex/toU8a.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { compactFromU8a, compactFromU8aLim } from './index.js';
|
||||
|
||||
describe('compactFromU8a', (): void => {
|
||||
it('decoded u8 value', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([0b11111100]))
|
||||
).toEqual([1, new BN(63)]);
|
||||
});
|
||||
|
||||
it('decodes from same u16 encoded value', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([0b11111101, 0b00000111])).toString()
|
||||
).toEqual(
|
||||
[2, new BN(511)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes from same u32 encoded value (short)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([254, 255, 3, 0])).toString()
|
||||
).toEqual(
|
||||
[4, new BN(0xffff)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes from same u32 encoded value (short, max)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([254, 255, 255, 255])).toString()
|
||||
).toEqual(
|
||||
[4, new BN(1073741823)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes from same u32 encoded value (full)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]))
|
||||
).toEqual([5, new BN(0xfffffff9)]);
|
||||
});
|
||||
|
||||
it('decodes from same u32 as u64 encoded value (full, default)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]))
|
||||
).toEqual([5, new BN(0xfffffff9)]);
|
||||
});
|
||||
|
||||
it('decodes an actual value', (): void => {
|
||||
expect(
|
||||
compactFromU8a(
|
||||
hexToU8a('0x0b00407a10f35a')
|
||||
)
|
||||
).toEqual([7, new BN('5af3107a4000', 16)]);
|
||||
});
|
||||
|
||||
it('decodes an actual value (Buffer)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(
|
||||
Buffer.from('0b00407a10f35a', 'hex')
|
||||
)
|
||||
).toEqual([7, new BN('5af3107a4000', 16)]);
|
||||
});
|
||||
|
||||
it('decodes an actual value (100000000)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(
|
||||
hexToU8a('0x0284d717')
|
||||
)[1].toString()
|
||||
).toEqual('100000000');
|
||||
});
|
||||
|
||||
perf('compactFromU8a (u8)', 1_000_000, [[new Uint8Array([63 << 2])]], compactFromU8a);
|
||||
perf('compactFromU8a (u16)', 1_000_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a);
|
||||
perf('compactFromU8a (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a);
|
||||
perf('compactFromU8aLim (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aLim);
|
||||
perf('compactFromU8a (u48)', 1_000_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
|
||||
perf('compactFromU8a (u96)', 250_000, [[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])]], compactFromU8a);
|
||||
});
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { U8aLike } from '../types.js';
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { u8aToBn, u8aToU8a } from '../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
|
||||
* ```
|
||||
*/
|
||||
export function compactFromU8a (input: U8aLike): [number, BN] {
|
||||
const u8a = 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 BN(u8a[0] >>> 2)];
|
||||
|
||||
case 0b01:
|
||||
return [2, new 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 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 BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00))];
|
||||
|
||||
case 6:
|
||||
return [6, new 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 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, u8aToBn(u8a.subarray(1, offset))];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name compactFromU8aLim
|
||||
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
|
||||
*/
|
||||
export function compactFromU8aLim (u8a: Uint8Array): [number, number] {
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.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.
|
||||
*/
|
||||
|
||||
export { compactAddLength } from './addLength.js';
|
||||
export { compactFromU8a, compactFromU8aLim } from './fromU8a.js';
|
||||
export { compactStripLength } from './stripLength.js';
|
||||
export { compactToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { compactStripLength } from './index.js';
|
||||
|
||||
describe('compactStripLength', (): void => {
|
||||
it('correctly removes the length prefix', (): void => {
|
||||
expect(
|
||||
compactStripLength(Uint8Array.from([2 << 2, 12, 13]))
|
||||
).toEqual([
|
||||
3,
|
||||
Uint8Array.from([12, 13])
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { compactFromU8a } from './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]]
|
||||
* ```
|
||||
*/
|
||||
export function compactStripLength (input: Uint8Array): [number, Uint8Array] {
|
||||
const [offset, length] = compactFromU8a(input);
|
||||
const total = offset + length.toNumber();
|
||||
|
||||
return [
|
||||
total,
|
||||
input.subarray(offset, total)
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { BN } from '../bn/index.js';
|
||||
import { compactToU8a } from './index.js';
|
||||
|
||||
const TESTS: [output: string | Uint8Array, input: BN | number][] = [
|
||||
// Rust tests
|
||||
// Copied from https://github.com/paritytech/parity-codec/blob/master/src/codec.rs
|
||||
['00', new BN('0')],
|
||||
['fc', new BN('63')],
|
||||
['01 01', new BN('64')],
|
||||
['fd ff', new BN('16383')],
|
||||
['02 00 01 00', new BN('16384')],
|
||||
['fe ff ff ff', new BN('1073741823')],
|
||||
['03 00 00 00 40', new BN('1073741824')],
|
||||
['03 ff ff ff ff', new BN(`${1}${'0'.repeat(32)}`, 2).subn(1)],
|
||||
['07 00 00 00 00 01', new BN(`${1}${'0'.repeat(32)}`, 2)],
|
||||
['0b 00 00 00 00 00 01', new BN(`${1}${'0'.repeat(40)}`, 2)],
|
||||
['0f 00 00 00 00 00 00 01', new BN(`${1}${'0'.repeat(48)}`, 2)],
|
||||
['0f ff ff ff ff ff ff ff', new BN(`${1}${'0'.repeat(56)}`, 2).subn(1)],
|
||||
['13 00 00 00 00 00 00 00 01', new BN(`${1}${'0'.repeat(56)}`, 2)],
|
||||
['13 ff ff ff ff ff ff ff ff', new BN(`${1}${'0'.repeat(64)}`, 2).subn(1)],
|
||||
// own tests
|
||||
[new Uint8Array([18 << 2]), 18],
|
||||
[new Uint8Array([0b11111100]), 63],
|
||||
[new Uint8Array([0xbd, 0x01]), 111],
|
||||
[new Uint8Array([0b11111101, 0b00000111]), 511],
|
||||
[new Uint8Array([253, 127]), 0x1fff],
|
||||
[new Uint8Array([254, 255, 3, 0]), 0xffff],
|
||||
[new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]), 0xfffffff9],
|
||||
[new Uint8Array([3 + ((6 - 4) << 2), 0x00, 0x40, 0x7a, 0x10, 0xf3, 0x5a]), new BN('00005af3107a4000', 16)],
|
||||
[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18]), new BN('1200005af3107a4034', 16)]
|
||||
];
|
||||
|
||||
describe('encode', (): void => {
|
||||
it('does not modify the original', (): void => {
|
||||
const original = new BN(123456);
|
||||
|
||||
expect(compactToU8a(original)).toEqual(new Uint8Array([2, 137, 7, 0]));
|
||||
expect(original.toString()).toEqual('123456');
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([output, input], i): void => {
|
||||
it(`#${i}: encodes ${input.toString()}`, (): void => {
|
||||
expect(
|
||||
compactToU8a(input)
|
||||
).toEqual(
|
||||
output instanceof Uint8Array
|
||||
? output
|
||||
: Uint8Array.from(output.split(' ').map((s) => parseInt(s, 16)))
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn/index.js';
|
||||
import { u8aConcatStrict } from '../u8a/index.js';
|
||||
|
||||
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
|
||||
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
|
||||
const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(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])
|
||||
* ```
|
||||
*/
|
||||
export function compactToU8a (value: BN | bigint | number): Uint8Array {
|
||||
const bn = bnToBn(value);
|
||||
|
||||
if (bn.lte(MAX_U8)) {
|
||||
return new Uint8Array([bn.toNumber() << 2]);
|
||||
} else if (bn.lte(MAX_U16)) {
|
||||
return bnToU8a(bn.shln(2).iadd(BN_ONE), BL_16);
|
||||
} else if (bn.lte(MAX_U32)) {
|
||||
return bnToU8a(bn.shln(2).iadd(BN_TWO), BL_32);
|
||||
}
|
||||
|
||||
const u8a = 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 u8aConcatStrict([
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
new Uint8Array([((length - 4) << 2) + 0b11]),
|
||||
u8a.subarray(0, length)
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export type BitLength = 8 | 16 | 32 | 64 | 128 | 256;
|
||||
Reference in new issue
Block a user