Small readability cleanups (#1493)
This commit is contained in:
@@ -10,8 +10,6 @@ import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
|||||||
|
|
||||||
import { createAsHex } from '../helpers';
|
import { createAsHex } from '../helpers';
|
||||||
|
|
||||||
type BitLength = 64 | 128 | 256 | 384 | 512;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name blake2AsU8a
|
* @name blake2AsU8a
|
||||||
* @summary Creates a blake2b u8a from the input.
|
* @summary Creates a blake2b u8a from the input.
|
||||||
@@ -26,7 +24,7 @@ type BitLength = 64 | 128 | 256 | 384 | 512;
|
|||||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength: BitLength = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||||
const byteLength = Math.ceil(bitLength / 8);
|
const byteLength = Math.ceil(bitLength / 8);
|
||||||
const u8a = u8aToU8a(data);
|
const u8a = u8aToU8a(data);
|
||||||
|
|
||||||
|
|||||||
@@ -14,13 +14,12 @@ interface DualHash {
|
|||||||
512: (u8a: Uint8Array) => Uint8Array;
|
512: (u8a: Uint8Array) => Uint8Array;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
export function createAsHex <T extends (...args: never[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||||
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
|
||||||
return (...args: Parameters<T>): HexString =>
|
return (...args: Parameters<T>): HexString =>
|
||||||
u8aToHex(fn(...args));
|
u8aToHex(fn(...args));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer | Uint8Array | string, bitLength: T, onlyJs?: boolean) => Uint8Array): (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
export function createBitHasher (bitLength: 256 | 512, fn: (data: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512, onlyJs?: boolean) => Uint8Array): (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
||||||
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||||
fn(data, bitLength, onlyJs);
|
fn(data, bitLength, onlyJs);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import { sha512 } from '@noble/hashes/sha512';
|
|||||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||||
import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
||||||
|
|
||||||
type BitLength = 256 | 512;
|
|
||||||
|
|
||||||
const JS_HASH = {
|
const JS_HASH = {
|
||||||
256: sha256,
|
256: sha256,
|
||||||
512: sha512
|
512: sha512
|
||||||
@@ -20,7 +18,7 @@ const WA_MHAC = {
|
|||||||
512: hmacSha512
|
512: hmacSha512
|
||||||
};
|
};
|
||||||
|
|
||||||
function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
function createSha (bitLength: 256 | 512): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||||
}
|
}
|
||||||
@@ -29,7 +27,7 @@ function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint
|
|||||||
* @name hmacShaAsU8a
|
* @name hmacShaAsU8a
|
||||||
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
||||||
*/
|
*/
|
||||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||||
const u8aKey = u8aToU8a(key);
|
const u8aKey = u8aToU8a(key);
|
||||||
|
|
||||||
return !hasBigInt || (!onlyJs && isReady())
|
return !hasBigInt || (!onlyJs && isReady())
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import { cryptoWaitReady } from '..';
|
import { cryptoWaitReady } from '..';
|
||||||
import { mnemonicGenerate, WordCount } from './generate';
|
import { mnemonicGenerate } from './generate';
|
||||||
import { mnemonicValidate } from './validate';
|
import { mnemonicValidate } from './validate';
|
||||||
|
|
||||||
describe('mnemonicGenerate', (): void => {
|
describe('mnemonicGenerate', (): void => {
|
||||||
@@ -17,7 +17,7 @@ describe('mnemonicGenerate', (): void => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||||
it.each([12, 15, 18, 21, 24] as WordCount[])('generates a valid mnemonic (%p words)', (num): void => {
|
it.each([12, 15, 18, 21, 24] as 12[])('generates a valid mnemonic (%p words)', (num): void => {
|
||||||
const mnemonic = mnemonicGenerate(num, onlyJs);
|
const mnemonic = mnemonicGenerate(num, onlyJs);
|
||||||
const isValid = mnemonicValidate(mnemonic);
|
const isValid = mnemonicValidate(mnemonic);
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
|
|||||||
|
|
||||||
import { generateMnemonic } from './bip39';
|
import { generateMnemonic } from './bip39';
|
||||||
|
|
||||||
export type WordCount = 12 | 15 | 18 | 21 | 24;
|
|
||||||
|
|
||||||
// mapping of words to the actual strength (as expected)
|
// mapping of words to the actual strength (as expected)
|
||||||
const STRENGTH_MAP = {
|
const STRENGTH_MAP = {
|
||||||
12: 16 * 8,
|
12: 16 * 8,
|
||||||
@@ -29,7 +27,7 @@ const STRENGTH_MAP = {
|
|||||||
* const mnemonic = mnemonicGenerate(); // => string
|
* const mnemonic = mnemonicGenerate(); // => string
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs?: boolean): string {
|
export function mnemonicGenerate (numWords: 12 | 15 | 18 | 21 | 24 = 12, onlyJs?: boolean): string {
|
||||||
return !hasBigInt || (!onlyJs && isReady())
|
return !hasBigInt || (!onlyJs && isReady())
|
||||||
? bip39Generate(numWords)
|
? bip39Generate(numWords)
|
||||||
: generateMnemonic(STRENGTH_MAP[numWords]);
|
: generateMnemonic(STRENGTH_MAP[numWords]);
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import { isReady, twox } from '@polkadot/wasm-crypto';
|
|||||||
import { createAsHex } from '../helpers';
|
import { createAsHex } from '../helpers';
|
||||||
import { xxhash64 } from './xxhash64';
|
import { xxhash64 } from './xxhash64';
|
||||||
|
|
||||||
type BitLength = 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name xxhashAsU8a
|
* @name xxhashAsU8a
|
||||||
* @summary Creates a xxhash64 u8a from the input.
|
* @summary Creates a xxhash64 u8a from the input.
|
||||||
@@ -25,7 +23,7 @@ type BitLength = 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512;
|
|||||||
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 64, onlyJs?: boolean): Uint8Array {
|
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength: 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512 = 64, onlyJs?: boolean): Uint8Array {
|
||||||
const rounds = Math.ceil(bitLength / 64);
|
const rounds = Math.ceil(bitLength / 64);
|
||||||
const u8a = u8aToU8a(data);
|
const u8a = u8aToU8a(data);
|
||||||
|
|
||||||
|
|||||||
@@ -22,20 +22,24 @@ import { u8aToBn, u8aToU8a } from '../u8a';
|
|||||||
*/
|
*/
|
||||||
export function compactFromU8a (input: U8aLike): [number, BN] {
|
export function compactFromU8a (input: U8aLike): [number, BN] {
|
||||||
const u8a = u8aToU8a(input);
|
const u8a = u8aToU8a(input);
|
||||||
const flag = u8a[0] & 0b11;
|
|
||||||
|
|
||||||
// The u8a is manually converted here, it is 2x faster that doing an
|
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||||
// additional call to u8aToBn
|
// than doing an additional call to u8aToBn (as with variable length)
|
||||||
if (flag === 0b00) {
|
switch (u8a[0] & 0b11) {
|
||||||
return [1, new BN(u8a[0] >>> 2)];
|
case 0b00:
|
||||||
} else if (flag === 0b01) {
|
return [1, new BN(u8a[0] >>> 2)];
|
||||||
return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)];
|
|
||||||
} else if (flag === 0b10) {
|
case 0b01:
|
||||||
return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
|
return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)];
|
||||||
|
|
||||||
|
case 0b10:
|
||||||
|
return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
|
||||||
|
|
||||||
|
default: { // 0b11
|
||||||
|
// add 5 to shifted (4 for base length, 1 for this byte)
|
||||||
|
const offset = (u8a[0] >>> 2) + 5;
|
||||||
|
|
||||||
|
return [offset, u8aToBn(u8a.subarray(1, offset))];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add 5 to shifted (4 for base length, 1 for this byte)
|
|
||||||
const offset = (u8a[0] >>> 2) + 5;
|
|
||||||
|
|
||||||
return [offset, u8aToBn(u8a.subarray(1, offset))];
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user