Revery isWasmOnly helper (#1229)

* Revery isWasmOnly helper

* Adjust
This commit is contained in:
Jaco
2021-11-22 08:51:03 +02:00
committed by GitHub
parent 28f0040080
commit 277c80eb96
15 changed files with 32 additions and 50 deletions
+3 -3
View File
@@ -6,9 +6,9 @@ import type { HexString } from '@polkadot/util/types';
import js from 'blakejs';
import { u8aToU8a } from '@polkadot/util';
import { blake2b } from '@polkadot/wasm-crypto';
import { blake2b, isReady } from '@polkadot/wasm-crypto';
import { createAsHex, isWasmOnly } from '../helpers';
import { createAsHex } from '../helpers';
/**
* @name blake2AsU8a
@@ -28,7 +28,7 @@ export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength =
const byteLength = Math.ceil(bitLength / 8);
const u8a = u8aToU8a(data);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? blake2b(u8a, u8aToU8a(key), byteLength)
: js.blake2b(u8a, key || undefined, byteLength);
}
-5
View File
@@ -4,7 +4,6 @@
import type { HexString } from '@polkadot/util/types';
import { u8aToHex } from '@polkadot/util';
import { isReady } from '@polkadot/wasm-crypto';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
@@ -16,7 +15,3 @@ export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
fn(data, bitLength, onlyJs);
}
export function isWasmOnly (onlyJs?: boolean): boolean {
return isReady() && !onlyJs;
}
+2 -4
View File
@@ -4,9 +4,7 @@
import hash from 'hash.js';
import { u8aToU8a } from '@polkadot/util';
import { hmacSha256, hmacSha512 } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
type HashFn = (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array;
@@ -19,7 +17,7 @@ export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLen
const is256 = bitLength === 256;
const u8aKey = u8aToU8a(key);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? is256
? hmacSha256(u8aKey, data)
: hmacSha512(u8aKey, data)
+3 -3
View File
@@ -6,9 +6,9 @@ import type { HexString } from '@polkadot/util/types';
import js from 'js-sha3';
import { u8aToU8a } from '@polkadot/util';
import { keccak256, keccak512 } from '@polkadot/wasm-crypto';
import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
import { createAsHex, createBitHasher, isWasmOnly } from '../helpers';
import { createAsHex, createBitHasher } from '../helpers';
/**
* @name keccakAsU8a
@@ -28,7 +28,7 @@ export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bi
const is256 = bitLength === 256;
const u8a = u8aToU8a(value);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? is256
? keccak256(u8a)
: keccak512(u8a)
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bip39Generate } from '@polkadot/wasm-crypto';
import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { generateMnemonic } from './bip39';
export type WordCount = 12 | 15 | 18 | 21 | 24;
@@ -30,7 +29,7 @@ const STRENGTH_MAP = {
* ```
*/
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs?: boolean): string {
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? bip39Generate(numWords)
: generateMnemonic(STRENGTH_MAP[numWords]);
}
@@ -1,13 +1,12 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bip39ToEntropy } from '@polkadot/wasm-crypto';
import { bip39ToEntropy, isReady } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { mnemonicToEntropy as jsToEntropy } from './bip39';
export function mnemonicToEntropy (mnemonic: string, onlyJs?: boolean): Uint8Array {
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? bip39ToEntropy(mnemonic)
: jsToEntropy(mnemonic);
}
@@ -2,9 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import { assert } from '@polkadot/util';
import { bip39ToSeed } from '@polkadot/wasm-crypto';
import { bip39ToSeed, isReady } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { mnemonicToSeedSync } from './bip39';
import { mnemonicValidate } from './validate';
@@ -30,7 +29,7 @@ export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs?:
assert([32, 64].includes(byteLength), () => `Invalid seed length ${byteLength}, expected 32 or 64`);
if (byteLength === 32) {
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? bip39ToSeed(mnemonic, password)
: mnemonicToSeedSync(mnemonic, password).subarray(0, 32);
} else if (byteLength === 64) {
@@ -2,9 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import { assert, stringToU8a } from '@polkadot/util';
import { bip39ToMiniSecret } from '@polkadot/wasm-crypto';
import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { pbkdf2Encode } from '../pbkdf2';
import { mnemonicToEntropy } from './toEntropy';
import { mnemonicValidate } from './validate';
@@ -12,7 +11,7 @@ import { mnemonicValidate } from './validate';
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs?: boolean): Uint8Array {
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
if (isWasmOnly(onlyJs)) {
if (!onlyJs && isReady()) {
return bip39ToMiniSecret(mnemonic, password);
}
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bip39Validate } from '@polkadot/wasm-crypto';
import { bip39Validate, isReady } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { validateMnemonic } from './bip39';
/**
@@ -20,7 +19,7 @@ import { validateMnemonic } from './bip39';
* ```
*/
export function mnemonicValidate (mnemonic: string, onlyJs?: boolean): boolean {
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? bip39Validate(mnemonic)
: validateMnemonic(mnemonic);
}
+2 -4
View File
@@ -7,9 +7,7 @@ import type { Keypair } from '../types';
import nacl from 'tweetnacl';
import { assert, u8aToU8a } from '@polkadot/util';
import { ed25519Sign } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { ed25519Sign, isReady } from '@polkadot/wasm-crypto';
/**
* @name naclSign
@@ -30,7 +28,7 @@ export function naclSign (message: HexString | Uint8Array | string, { publicKey,
const messageU8a = u8aToU8a(message);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? ed25519Sign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
: nacl.sign.detached(messageU8a, secretKey);
}
+2 -4
View File
@@ -6,9 +6,7 @@ import type { HexString } from '@polkadot/util/types';
import nacl from 'tweetnacl';
import { assert, u8aToU8a } from '@polkadot/util';
import { ed25519Verify } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { ed25519Verify, isReady } from '@polkadot/wasm-crypto';
/**
* @name naclSign
@@ -32,7 +30,7 @@ export function naclVerify (message: HexString | Uint8Array | string, signature:
assert(publicKeyU8a.length === 32, () => `Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? ed25519Verify(signatureU8a, messageU8a, publicKeyU8a)
: nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
}
+2 -3
View File
@@ -4,9 +4,8 @@
import type { HexString } from '@polkadot/util/types';
import { u8aToU8a } from '@polkadot/util';
import { pbkdf2 } from '@polkadot/wasm-crypto';
import { isReady, pbkdf2 } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { randomAsU8a } from '../random/asU8a';
import { pbkdf2Sync } from './pbkdf2';
@@ -21,7 +20,7 @@ export function pbkdf2Encode (passphrase?: HexString | Buffer | Uint8Array | str
const u8aSalt = u8aToU8a(salt);
return {
password: isWasmOnly(onlyJs)
password: !onlyJs && isReady()
? pbkdf2(u8aPass, u8aSalt, rounds)
: pbkdf2Sync(u8aPass, u8aSalt, rounds),
rounds,
+2 -3
View File
@@ -7,9 +7,8 @@ import type { Params } from './types';
import scryptsy from 'scryptsy';
import { bufferToU8a, u8aToBuffer, u8aToU8a } from '@polkadot/util';
import { scrypt } from '@polkadot/wasm-crypto';
import { isReady, scrypt } from '@polkadot/wasm-crypto';
import { isWasmOnly } from '../helpers';
import { randomAsU8a } from '../random/asU8a';
import { DEFAULT_PARAMS } from './defaults';
@@ -22,7 +21,7 @@ interface Result {
export function scryptEncode (passphrase?: HexString | Uint8Array | string, salt = randomAsU8a(), params = DEFAULT_PARAMS, onlyJs?: boolean): Result {
const u8a = u8aToU8a(passphrase);
const password = isWasmOnly(onlyJs)
const password = !onlyJs && isReady()
? scrypt(u8a, salt, Math.log2(params.N), params.r, params.p)
: bufferToU8a(
scryptsy(u8aToBuffer(u8a), u8aToBuffer(salt), params.N, params.r, params.p, 64)
+3 -3
View File
@@ -6,9 +6,9 @@ import type { HexString } from '@polkadot/util/types';
import hash from 'hash.js';
import { u8aToU8a } from '@polkadot/util';
import { sha256, sha512 } from '@polkadot/wasm-crypto';
import { isReady, sha256, sha512 } from '@polkadot/wasm-crypto';
import { createBitHasher, isWasmOnly } from '../helpers';
import { createBitHasher } from '../helpers';
/**
* @name shaAsU8a
@@ -18,7 +18,7 @@ export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLe
const is256 = bitLength === 256;
const u8a = u8aToU8a(value);
return isWasmOnly(onlyJs)
return !onlyJs && isReady()
? is256
? sha256(u8a)
: sha512(u8a)
+3 -3
View File
@@ -4,9 +4,9 @@
import type { HexString } from '@polkadot/util/types';
import { u8aToU8a } from '@polkadot/util';
import { twox } from '@polkadot/wasm-crypto';
import { isReady, twox } from '@polkadot/wasm-crypto';
import { createAsHex, isWasmOnly } from '../helpers';
import { createAsHex } from '../helpers';
import xxhash64AsBn from './xxhash64/asBn';
/**
@@ -27,7 +27,7 @@ export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bit
const iterations = Math.ceil(bitLength / 64);
const u8a = u8aToU8a(data);
if (isWasmOnly(onlyJs)) {
if (!onlyJs && isReady()) {
return twox(u8a, iterations);
}