From ab3268407c0d424fe81a27eb1e0a5f4e1f7c0e1a Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 23 Jun 2021 13:55:29 +0300 Subject: [PATCH] Use describe.each for onlyJs flag (#1029) * Use describe.each for onlyJs flag * base validate checks --- packages/util-crypto/src/base58/validate.ts | 4 +- packages/util-crypto/src/base64/validate.ts | 2 +- .../util-crypto/src/ethereum/isAddress.ts | 4 +- .../util-crypto/src/ethereum/isChecksum.ts | 6 +- packages/util-crypto/src/keccak/asU8a.spec.ts | 20 ++--- .../util-crypto/src/mnemonic/generate.spec.ts | 18 ++-- .../src/mnemonic/toEntropy.spec.ts | 6 +- .../src/mnemonic/toLegacySeed.spec.ts | 28 +++---- .../src/mnemonic/toMiniSecret.spec.ts | 40 ++++----- packages/util-crypto/src/nacl/sign.spec.ts | 4 +- packages/util-crypto/src/nacl/verify.spec.ts | 82 ++++++++++--------- .../util-crypto/src/pbkdf2/encode.spec.ts | 22 +++-- packages/util-crypto/src/sha512/asU8a.spec.ts | 4 +- packages/util-crypto/src/xxhash/asU8a.spec.ts | 12 +-- 14 files changed, 113 insertions(+), 139 deletions(-) diff --git a/packages/util-crypto/src/base58/validate.ts b/packages/util-crypto/src/base58/validate.ts index 32ff9e84b..42fa05dbc 100644 --- a/packages/util-crypto/src/base58/validate.ts +++ b/packages/util-crypto/src/base58/validate.ts @@ -13,8 +13,8 @@ interface CheckConfig { const BASE_CONFIG = { alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' }; -export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: string | null, ipfsCompat?: boolean): true { - assert(value, () => `Expected non-null, non-empty ${type} input`); +export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: unknown, ipfsCompat?: boolean): true { + assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`); assert(!ipfsCompat || value[0] === ipfsChar, () => `Expected ${type} to start with '${ipfsChar}'`); for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) { diff --git a/packages/util-crypto/src/base64/validate.ts b/packages/util-crypto/src/base64/validate.ts index 9aac6b3c4..d1e21f66e 100644 --- a/packages/util-crypto/src/base64/validate.ts +++ b/packages/util-crypto/src/base64/validate.ts @@ -10,7 +10,7 @@ import { assert } from '@polkadot/util'; * Validates that the supplied value is valid base64 */ export function base64Validate (value?: string | null): value is string { - assert(value, 'Expected non-null, non-empty base64 input'); + assert(value && typeof value === 'string', 'Expected non-null, non-empty base64 string input'); assert(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value), 'Invalid base64 encoding'); return true; diff --git a/packages/util-crypto/src/ethereum/isAddress.ts b/packages/util-crypto/src/ethereum/isAddress.ts index 9a31d0843..ef82e767a 100644 --- a/packages/util-crypto/src/ethereum/isAddress.ts +++ b/packages/util-crypto/src/ethereum/isAddress.ts @@ -8,9 +8,7 @@ import { isEthereumChecksum } from './isChecksum'; export function isEthereumAddress (address?: string): boolean { if (!address || address.length !== 42 || !isHex(address)) { return false; - } - - if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { + } else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { return true; } diff --git a/packages/util-crypto/src/ethereum/isChecksum.ts b/packages/util-crypto/src/ethereum/isChecksum.ts index 7af7f314e..459cd1a85 100644 --- a/packages/util-crypto/src/ethereum/isChecksum.ts +++ b/packages/util-crypto/src/ethereum/isChecksum.ts @@ -6,7 +6,11 @@ import { u8aToHex } from '@polkadot/util'; import { keccakAsU8a } from '../keccak'; function isInvalidChar (char: string, byte: number): boolean { - return (byte > 7 && char !== char.toUpperCase()) || (byte <= 7 && char !== char.toLowerCase()); + return char !== ( + byte > 7 + ? char.toUpperCase() + : char.toLowerCase() + ); } export function isEthereumChecksum (_address: string): boolean { diff --git a/packages/util-crypto/src/keccak/asU8a.spec.ts b/packages/util-crypto/src/keccak/asU8a.spec.ts index b10903bb0..4ca9a9736 100644 --- a/packages/util-crypto/src/keccak/asU8a.spec.ts +++ b/packages/util-crypto/src/keccak/asU8a.spec.ts @@ -6,8 +6,6 @@ import { waitReady } from '@polkadot/wasm-crypto'; import { keccakAsU8a } from '.'; -const BITS: (256 | 512)[] = [256, 512]; - describe('keccakAsU8a', (): void => { beforeEach(async (): Promise => { await waitReady(); @@ -23,29 +21,21 @@ describe('keccakAsU8a', (): void => { ) }; - BITS.forEach((bitLength): void => { - [false, true].forEach((onlyJs): void => { - it(`returns an hex representation (string, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => { + it('returns an hex representation (string)', (): void => { expect( keccakAsU8a(input, bitLength, onlyJs) ).toEqual(output[bitLength]); }); - }); - }); - BITS.forEach((bitLength): void => { - [false, true].forEach((onlyJs): void => { - it(`returns an hex representation (Buffer, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => { + it('returns an hex representation (Buffer)', (): void => { expect( keccakAsU8a(Buffer.from(input), bitLength, onlyJs) ).toEqual(output[bitLength]); }); - }); - }); - BITS.forEach((bitLength): void => { - [false, true].forEach((onlyJs): void => { - it(`returns an hex representation (Uint8Array, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => { + it('returns an hex representation (Uint8Array)', (): void => { expect( keccakAsU8a(stringToU8a(input), bitLength, onlyJs) ).toEqual(output[bitLength]); diff --git a/packages/util-crypto/src/mnemonic/generate.spec.ts b/packages/util-crypto/src/mnemonic/generate.spec.ts index c73c3a91c..d0244fc01 100644 --- a/packages/util-crypto/src/mnemonic/generate.spec.ts +++ b/packages/util-crypto/src/mnemonic/generate.spec.ts @@ -16,20 +16,16 @@ describe('mnemonicGenerate', (): void => { ).toEqual(true); }); - [false, true].forEach((onlyJs): void => { - ([12, 15, 18, 21, 24] as WordCount[]).forEach((num: WordCount): void => { - it(`generates a valid mnemonic (${num} words, onlyJs=${onlyJs.toString()})`, (): void => { - const mnemonic = mnemonicGenerate(num, onlyJs); - const isValid = mnemonicValidate(mnemonic); + 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 => { + const mnemonic = mnemonicGenerate(num, onlyJs); + const isValid = mnemonicValidate(mnemonic); - expect(mnemonic.split(' ')).toHaveLength(num); - expect(isValid).toEqual(true); - }); + expect(mnemonic.split(' ')).toHaveLength(num); + expect(isValid).toEqual(true); }); - }); - [true, false].forEach((onlyJs): void => { - it(`generates not deterministic (onlyJs=${onlyJs.toString()})`, (): void => { + it('generates not deterministic', (): void => { const m1 = mnemonicGenerate(24, onlyJs); const m2 = mnemonicGenerate(24, onlyJs); diff --git a/packages/util-crypto/src/mnemonic/toEntropy.spec.ts b/packages/util-crypto/src/mnemonic/toEntropy.spec.ts index f3b8dc5f2..081649653 100644 --- a/packages/util-crypto/src/mnemonic/toEntropy.spec.ts +++ b/packages/util-crypto/src/mnemonic/toEntropy.spec.ts @@ -12,9 +12,9 @@ describe('mnemonicToEntropy', (): void => { await cryptoWaitReady(); }); - tests.forEach(([mnemonic, entropy], index): void => { - [false, true].forEach((onlyJs): void => { - it(`Created correct entropy for ${index} (onlyJs = ${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + tests.forEach(([mnemonic, entropy], index): void => { + it(`Created correct entropy for ${index}`, (): void => { expect(u8aToHex(mnemonicToEntropy(mnemonic, onlyJs))).toEqual(entropy); }); }); diff --git a/packages/util-crypto/src/mnemonic/toLegacySeed.spec.ts b/packages/util-crypto/src/mnemonic/toLegacySeed.spec.ts index 209bfd2e3..d7198ab24 100644 --- a/packages/util-crypto/src/mnemonic/toLegacySeed.spec.ts +++ b/packages/util-crypto/src/mnemonic/toLegacySeed.spec.ts @@ -15,35 +15,29 @@ describe('mnemonicToLegacySeed', (): void => { await waitReady(); }); - [undefined, 'foo', 'bar'].forEach((password): void => { - it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => { - expect( - u8aEq( - mnemonicToLegacySeed(MNEMONIC, password, true), - mnemonicToLegacySeed(MNEMONIC, password, false) - ) - ).toEqual(true); - }); + it.each([undefined, 'foo', 'bar'])('generates Wasm & Js equivalents (password = %p', (password): void => { + expect( + u8aEq( + mnemonicToLegacySeed(MNEMONIC, password, true), + mnemonicToLegacySeed(MNEMONIC, password, false) + ) + ).toEqual(true); }); - [false, true].forEach((onlyJs): void => { - it(`generates a valid 64bytes seed (onlyJs = ${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('generates a valid 64bytes seed', (): void => { expect( u8aToHex(mnemonicToLegacySeed(MNEMONIC, undefined, onlyJs, 64)) ).toEqual(SEED_64); }); - }); - [false, true].forEach((onlyJs): void => { - it(`generates a valid 32bytes seed (onlyJs = ${onlyJs.toString()})`, (): void => { + it('generates a valid 32bytes seed', (): void => { expect( u8aToHex(mnemonicToLegacySeed(MNEMONIC, undefined, onlyJs)) ).toEqual(SEED_32); }); - }); - [false, true].forEach((onlyJs): void => { - it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => { + it('fails with non-mnemonics', (): void => { expect( () => mnemonicToLegacySeed('foo bar baz', undefined, onlyJs) ).toThrow(/mnemonic specified/); diff --git a/packages/util-crypto/src/mnemonic/toMiniSecret.spec.ts b/packages/util-crypto/src/mnemonic/toMiniSecret.spec.ts index 1b2788864..d22070a48 100644 --- a/packages/util-crypto/src/mnemonic/toMiniSecret.spec.ts +++ b/packages/util-crypto/src/mnemonic/toMiniSecret.spec.ts @@ -15,28 +15,30 @@ describe('mnemonicToMiniSecret', (): void => { await cryptoWaitReady(); }); - [undefined, 'foo', 'bar'].forEach((password): void => { - it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => { - expect( - u8aEq( - mnemonicToMiniSecret(MNEMONIC, password, true), - mnemonicToMiniSecret(MNEMONIC, password, false) - ) - ).toEqual(true); - }); + it.each([undefined, 'foo', 'bar'])('generates Wasm & Js equivalents for password = %p', (password): void => { + expect( + u8aEq( + mnemonicToMiniSecret(MNEMONIC, password, true), + mnemonicToMiniSecret(MNEMONIC, password, false) + ) + ).toEqual(true); }); - [false, true].forEach((onlyJs): void => { - it(`generates a valid seed (onlyJs = ${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('generates a valid seed', (): void => { expect( u8aToHex(mnemonicToMiniSecret(MNEMONIC, undefined, onlyJs)) ).toEqual(SEED); }); - }); - tests.forEach(([mnemonic, , seed], index): void => { - [false, true].forEach((onlyJs): void => { - it(`Created correct seed for ${index} (onlyJs = ${onlyJs.toString()})`, (): void => { + it('fails with non-mnemonics', (): void => { + expect( + () => mnemonicToMiniSecret('foo bar baz', undefined, onlyJs) + ).toThrow(/mnemonic specified/); + }); + + tests.forEach(([mnemonic, , seed], index): void => { + it(`Created correct seed for ${index}`, (): void => { expect( u8aToHex(mnemonicToMiniSecret(mnemonic, 'Substrate', onlyJs)) ).toEqual( @@ -46,12 +48,4 @@ describe('mnemonicToMiniSecret', (): void => { }); }); }); - - [false, true].forEach((onlyJs): void => { - it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => { - expect( - () => mnemonicToMiniSecret('foo bar baz', undefined, onlyJs) - ).toThrow(/mnemonic specified/); - }); - }); }); diff --git a/packages/util-crypto/src/nacl/sign.spec.ts b/packages/util-crypto/src/nacl/sign.spec.ts index 788e151c7..febe4b92d 100644 --- a/packages/util-crypto/src/nacl/sign.spec.ts +++ b/packages/util-crypto/src/nacl/sign.spec.ts @@ -11,8 +11,8 @@ describe('naclSign', (): void => { await waitReady(); }); - [false, true].forEach((onlyJs): void => { - it(`returns a valid signature for the message (onlyJs=${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('returns a valid signature for the message', (): void => { expect( naclSign( new Uint8Array([0x61, 0x62, 0x63, 0x64]), diff --git a/packages/util-crypto/src/nacl/verify.spec.ts b/packages/util-crypto/src/nacl/verify.spec.ts index 57cf211ab..389e10f19 100644 --- a/packages/util-crypto/src/nacl/verify.spec.ts +++ b/packages/util-crypto/src/nacl/verify.spec.ts @@ -19,8 +19,8 @@ describe('naclVerify', (): void => { signature = new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7]); }); - [false, true].forEach((onlyJs): void => { - it(`validates a correctly signed message (onlyJs=${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('validates a correctly signed message', (): void => { expect( naclVerify( new Uint8Array([0x61, 0x62, 0x63, 0x64]), @@ -30,47 +30,51 @@ describe('naclVerify', (): void => { ) ).toEqual(true); }); - }); - it('fails a correctly signed message (message changed)', (): void => { - expect( - naclVerify( - new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]), - signature, - publicKey - ) - ).toEqual(false); - }); + it('fails a correctly signed message (message changed)', (): void => { + expect( + naclVerify( + new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]), + signature, + publicKey, + onlyJs + ) + ).toEqual(false); + }); - it('fails a correctly signed message (signature changed)', (): void => { - signature[0] = 0xff; + it('fails a correctly signed message (signature changed)', (): void => { + signature[0] = 0xff; - expect( - naclVerify( - new Uint8Array([0x61, 0x62, 0x63, 0x64]), - signature, - publicKey - ) - ).toEqual(false); - }); + expect( + naclVerify( + new Uint8Array([0x61, 0x62, 0x63, 0x64]), + signature, + publicKey, + onlyJs + ) + ).toEqual(false); + }); - it('throws error when publicKey lengths do not match', (): void => { - expect( - () => naclVerify( - new Uint8Array([0x61, 0x62, 0x63, 0x64]), - signature, - new Uint8Array([1, 2]) - ) - ).toThrow(/Invalid publicKey/); - }); + it('throws error when publicKey lengths do not match', (): void => { + expect( + () => naclVerify( + new Uint8Array([0x61, 0x62, 0x63, 0x64]), + signature, + new Uint8Array([1, 2]), + onlyJs + ) + ).toThrow(/Invalid publicKey/); + }); - it('throws error when signature lengths do not match', (): void => { - expect( - () => naclVerify( - new Uint8Array([0x61, 0x62, 0x63, 0x64]), - new Uint8Array([1, 2]), - publicKey - ) - ).toThrow(/Invalid signature/); + it('throws error when signature lengths do not match', (): void => { + expect( + () => naclVerify( + new Uint8Array([0x61, 0x62, 0x63, 0x64]), + new Uint8Array([1, 2]), + publicKey, + onlyJs + ) + ).toThrow(/Invalid signature/); + }); }); }); diff --git a/packages/util-crypto/src/pbkdf2/encode.spec.ts b/packages/util-crypto/src/pbkdf2/encode.spec.ts index 7900d92e6..6777c527b 100644 --- a/packages/util-crypto/src/pbkdf2/encode.spec.ts +++ b/packages/util-crypto/src/pbkdf2/encode.spec.ts @@ -15,21 +15,19 @@ describe('pbkdf2Encode', (): void => { await waitReady(); }); - [256, 1024, 2048].forEach((rounds): void => { - it(`has equivalent Wasm & JS results (${rounds} rounds)`, (): void => { - const salt = randomAsU8a(); + it.each([256, 1024, 2048])('has equivalent Wasm & JS results (%p rounds)', (rounds): void => { + const salt = randomAsU8a(); - expect( - u8aEq( - pbkdf2Encode(TEST_PASSWORD, salt, rounds, false).password, - pbkdf2Encode(TEST_PASSWORD, salt, rounds, true).password - ) - ).toBe(true); - }); + expect( + u8aEq( + pbkdf2Encode(TEST_PASSWORD, salt, rounds, false).password, + pbkdf2Encode(TEST_PASSWORD, salt, rounds, true).password + ) + ).toBe(true); }); - [false, true].forEach((onlyJs): void => { - it(`creates known iterations (onlyJs = ${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('creates known iterations', (): void => { expect( u8aToHex(pbkdf2Encode(TEST_PASSWORD, KNOWN_SALT, 2048, onlyJs).password) ).toEqual( diff --git a/packages/util-crypto/src/sha512/asU8a.spec.ts b/packages/util-crypto/src/sha512/asU8a.spec.ts index a1a5781a6..84c673667 100644 --- a/packages/util-crypto/src/sha512/asU8a.spec.ts +++ b/packages/util-crypto/src/sha512/asU8a.spec.ts @@ -10,8 +10,8 @@ describe('sha512AsU8a', (): void => { await waitReady(); }); - [false, true].forEach((onlyJs): void => { - it(`creates a sha-512 hash (onlyJs=${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('creates a sha-512 hash', (): void => { expect( sha512AsU8a(Uint8Array.from([0x61, 0x62, 0x63, 0x64]), onlyJs) ).toEqual( diff --git a/packages/util-crypto/src/xxhash/asU8a.spec.ts b/packages/util-crypto/src/xxhash/asU8a.spec.ts index 66d07f09c..c644d194e 100644 --- a/packages/util-crypto/src/xxhash/asU8a.spec.ts +++ b/packages/util-crypto/src/xxhash/asU8a.spec.ts @@ -11,28 +11,24 @@ describe('xxhashAsU8a', (): void => { await waitReady(); }); - [false, true].forEach((onlyJs): void => { - it(`returns a 64-bit value by default (onlyJs=${onlyJs.toString()})`, (): void => { + describe.each([false, true])('onlyJs=%p', (onlyJs): void => { + it('returns a 64-bit value by default', (): void => { expect( xxhashAsU8a('abc', undefined, onlyJs) ).toEqual( hexToU8a('0x990977adf52cbc44') ); }); - }); - [false, true].forEach((onlyJs): void => { - it(`returns a 128-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => { + it('returns a 128-bit value (as specified', (): void => { expect( xxhashAsU8a('abc', 128, onlyJs) ).toEqual( hexToU8a('0x990977adf52cbc440889329981caa9be') ); }); - }); - [false, true].forEach((onlyJs): void => { - it(`returns a 256-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => { + it('returns a 256-bit value (as specified)', (): void => { expect( xxhashAsU8a('abc', 256, onlyJs) ).toEqual(