diff --git a/packages/util-crypto/src/nacl/convertKey.spec.ts b/packages/util-crypto/src/nacl/convertKey.spec.ts new file mode 100644 index 000000000..2ed8acdf0 --- /dev/null +++ b/packages/util-crypto/src/nacl/convertKey.spec.ts @@ -0,0 +1,36 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { u8aToHex } from '@polkadot/util'; + +import { convertPublicKeyToCurve25519, convertSecretKeyToCurve25519 } from '.'; + +describe('convertPublicKeyToCurve25519', (): void => { + it('converts a known key correctly', (): void => { + expect( + u8aToHex(convertPublicKeyToCurve25519( + new Uint8Array([ + 180, 114, 93, 155, 165, 255, 217, 82, + 16, 250, 209, 11, 193, 10, 88, 218, + 190, 190, 41, 193, 236, 252, 1, 152, + 216, 214, 0, 41, 45, 138, 13, 53 + ]) + )) + ).toEqual('0x5e05848ceeef4f306d6e285a26faf1a8070cf0c89f8d28ab966002fe4ad17746'); + }); +}); + +describe('convertSecretKeyToCurve25519', (): void => { + it('converts a known key correctly', (): void => { + expect( + u8aToHex(convertSecretKeyToCurve25519( + new Uint8Array([ + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8 + ]) + )) + ).toEqual('0xd875a82940b9174dabb1c0c895485810cc2870f5808565b59d4ef708bdcf526c'); + }); +}); diff --git a/packages/util-crypto/src/nacl/convertKey.ts b/packages/util-crypto/src/nacl/convertKey.ts index 2cdf48022..19e0cb667 100644 --- a/packages/util-crypto/src/nacl/convertKey.ts +++ b/packages/util-crypto/src/nacl/convertKey.ts @@ -3,10 +3,12 @@ import ed2curve from 'ed2curve'; +import { assertReturn } from '@polkadot/util'; + export function convertSecretKeyToCurve25519 (secretKey: Uint8Array): Uint8Array { return ed2curve.convertSecretKey(secretKey); } export function convertPublicKeyToCurve25519 (publicKey: Uint8Array): Uint8Array { - return ed2curve.convertPublicKey(publicKey) as Uint8Array; + return assertReturn(ed2curve.convertPublicKey(publicKey), 'Unable to convert publicKey to ed25519'); } diff --git a/packages/util-crypto/src/pbkdf2/encode.spec.ts b/packages/util-crypto/src/pbkdf2/encode.spec.ts index 6777c527b..f093d9211 100644 --- a/packages/util-crypto/src/pbkdf2/encode.spec.ts +++ b/packages/util-crypto/src/pbkdf2/encode.spec.ts @@ -7,7 +7,12 @@ import { waitReady } from '@polkadot/wasm-crypto'; import { randomAsU8a } from '../random/asU8a'; import { pbkdf2Encode } from '.'; -const KNOWN_SALT = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]); +const KNOWN_SALT = new Uint8Array([ + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32 +]); const TEST_PASSWORD = 'test password'; describe('pbkdf2Encode', (): void => { diff --git a/packages/util/src/assert.spec.ts b/packages/util/src/assert.spec.ts index 2a8cb30e6..e7a5ec93e 100644 --- a/packages/util/src/assert.spec.ts +++ b/packages/util/src/assert.spec.ts @@ -10,13 +10,13 @@ describe('assert', (): void => { it('should throw an error when test is not true', (): void => { expect( - (): void => assert(false, 'error thrown') + () => assert(false, 'error thrown') ).toThrow(/error thrown/); }); it('should throw an error when message: () => string', (): void => { expect( - (): void => assert(false, (): string => 'message from function') + () => assert(false, (): string => 'message from function') ).toThrow(/message from function/); }); }); diff --git a/packages/util/src/assert.ts b/packages/util/src/assert.ts index 59db28dee..5991c89b5 100644 --- a/packages/util/src/assert.ts +++ b/packages/util/src/assert.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { isFunction } from './is/function'; +import { isNull } from './is/null'; import { isUndefined } from './is/undefined'; type MessageFn = () => string; @@ -36,8 +37,8 @@ export function assert (condition: unknown, message: string | MessageFn): assert * @name assertReturn * @summart Returns when the value is not undefined, otherwise throws assertion error */ -export function assertReturn (value: T | undefined, message: string | MessageFn): T { - assert(!isUndefined(value), message); +export function assertReturn (value: T | undefined | null, message: string | MessageFn): T { + assert(!isUndefined(value) && !isNull(value), message); return value; }