From 3b9617faf4a346fc11fc3661dbbf6cba3860dd3b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 9 Nov 2020 22:09:53 +0100 Subject: [PATCH] Extract detectPackage /cryptoWait as side effect (#761) --- packages/keyring/src/detectPackage.ts | 7 +++++++ packages/keyring/src/index.ts | 6 ++---- packages/util-crypto/src/crypto.ts | 24 ++++++++++++++++++++++++ packages/util-crypto/src/cryptoInit.ts | 9 +++++++++ packages/util-crypto/src/index.ts | 5 +++-- 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 packages/keyring/src/detectPackage.ts create mode 100644 packages/util-crypto/src/crypto.ts create mode 100644 packages/util-crypto/src/cryptoInit.ts diff --git a/packages/keyring/src/detectPackage.ts b/packages/keyring/src/detectPackage.ts new file mode 100644 index 000000000..b4a9bc379 --- /dev/null +++ b/packages/keyring/src/detectPackage.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2020 @polkadot/keyring authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { detectPackage } from '@polkadot/util'; + +// eslint-disable-next-line @typescript-eslint/no-var-requires +detectPackage(require('./package.json'), typeof __dirname !== 'undefined' && __dirname); diff --git a/packages/keyring/src/index.ts b/packages/keyring/src/index.ts index bf24aba14..bfbf060fd 100644 --- a/packages/keyring/src/index.ts +++ b/packages/keyring/src/index.ts @@ -1,14 +1,12 @@ // Copyright 2017-2020 @polkadot/keyring authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { detectPackage } from '@polkadot/util'; +import './detectPackage'; + import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto'; import { Keyring } from './keyring'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -detectPackage(require('./package.json'), typeof __dirname !== 'undefined' && __dirname); - export default Keyring; export { diff --git a/packages/util-crypto/src/crypto.ts b/packages/util-crypto/src/crypto.ts new file mode 100644 index 000000000..2f1c3f3ba --- /dev/null +++ b/packages/util-crypto/src/crypto.ts @@ -0,0 +1,24 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import wasmCrypto from '@polkadot/wasm-crypto'; + +export function cryptoIsReady (): boolean { + return wasmCrypto.isReady(); +} + +export function cryptoWaitReady (): Promise { + return wasmCrypto + .waitReady() + .then((): boolean => true) + .catch((error): boolean => { + console.error('Unable to initialize @polkadot/util-crypto', error); + + return false; + }); +} + +// start init process immediately +cryptoWaitReady().catch((): void => { + // shouldn't happen, logged above +}); diff --git a/packages/util-crypto/src/cryptoInit.ts b/packages/util-crypto/src/cryptoInit.ts new file mode 100644 index 000000000..dff8b1718 --- /dev/null +++ b/packages/util-crypto/src/cryptoInit.ts @@ -0,0 +1,9 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { cryptoWaitReady } from './crypto'; + +// start init process immediately +cryptoWaitReady().catch((): void => { + // shouldn't happen, logged above +}); diff --git a/packages/util-crypto/src/index.ts b/packages/util-crypto/src/index.ts index 92c8ec06b..03137fabd 100644 --- a/packages/util-crypto/src/index.ts +++ b/packages/util-crypto/src/index.ts @@ -1,8 +1,9 @@ // Copyright 2017-2020 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 -// out of order and early in the chain, this also kicks off initialization on require -export * from './init'; +import './cryptoInit'; + +export * from './crypto'; // all internal exports export * from './address';