From 069b2b5263505652b9b81200707812a3280f88e5 Mon Sep 17 00:00:00 2001 From: Martynas Kazlauskas Date: Tue, 21 Jan 2025 08:11:36 +0200 Subject: [PATCH] refactor(web-extension)!: make KeyAgentFactory methods async Bip32Ed25519 was refactored to synchronous methods by hoisting sodium await to top level in #1558, which makes it difficult to use SigningCoordinator without top level await --- .../e2e/test/web-extension/extension/ui.ts | 4 +--- packages/web-extension/README.md | 2 +- .../SigningCoordinator/KeyAgentFactory.ts | 20 ++++++++++++++----- .../SigningCoordinator/SigningCoordinator.ts | 6 +++--- .../walletManager/SigningCoordinator.test.ts | 2 +- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/e2e/test/web-extension/extension/ui.ts b/packages/e2e/test/web-extension/extension/ui.ts index a5412fa0c70..1b03eaaa948 100644 --- a/packages/e2e/test/web-extension/extension/ui.ts +++ b/packages/e2e/test/web-extension/extension/ui.ts @@ -198,8 +198,6 @@ const passphraseByteArray = Uint8Array.from( ); const initSigningCoordinator = async () => { - const bip32Ed25519 = await Crypto.SodiumBip32Ed25519.create(); - const signingCoordinator = new SigningCoordinator( { hwOptions: { @@ -212,7 +210,7 @@ const initSigningCoordinator = async () => { }, { keyAgentFactory: createKeyAgentFactory({ - bip32Ed25519, + getBip32Ed25519: Crypto.SodiumBip32Ed25519.create, logger }), logger diff --git a/packages/web-extension/README.md b/packages/web-extension/README.md index 124ee9a43ef..0fb2c23c20f 100644 --- a/packages/web-extension/README.md +++ b/packages/web-extension/README.md @@ -127,7 +127,7 @@ const signingCoordinator = new SigningCoordinator( }, { keyAgentFactory: createKeyAgentFactory({ - bip32Ed25519: new Crypto.SodiumBip32Ed25519(), + getBip32Ed25519: Crypto.SodiumBip32Ed25519.create, logger }), logger diff --git a/packages/web-extension/src/walletManager/SigningCoordinator/KeyAgentFactory.ts b/packages/web-extension/src/walletManager/SigningCoordinator/KeyAgentFactory.ts index 3533dfc71cc..37c6fe1653c 100644 --- a/packages/web-extension/src/walletManager/SigningCoordinator/KeyAgentFactory.ts +++ b/packages/web-extension/src/walletManager/SigningCoordinator/KeyAgentFactory.ts @@ -1,11 +1,21 @@ -import { InMemoryKeyAgent, InMemoryKeyAgentProps, KeyAgentDependencies } from '@cardano-sdk/key-management'; +import { Bip32Ed25519 } from '@cardano-sdk/crypto'; +import { InMemoryKeyAgent, InMemoryKeyAgentProps } from '@cardano-sdk/key-management'; import { LedgerKeyAgent, LedgerKeyAgentProps } from '@cardano-sdk/hardware-ledger'; +import { Logger } from 'ts-log'; import { TrezorKeyAgent, TrezorKeyAgentProps } from '@cardano-sdk/hardware-trezor'; -export const createKeyAgentFactory = (dependencies: KeyAgentDependencies) => ({ - InMemory: (props: InMemoryKeyAgentProps) => new InMemoryKeyAgent(props, dependencies), - Ledger: (props: LedgerKeyAgentProps) => new LedgerKeyAgent(props, dependencies), - Trezor: (props: TrezorKeyAgentProps) => new TrezorKeyAgent(props, dependencies) +export type KeyAgentFactoryDependencies = { + logger: Logger; + getBip32Ed25519: () => Promise; +}; + +export const createKeyAgentFactory = ({ logger, getBip32Ed25519 }: KeyAgentFactoryDependencies) => ({ + InMemory: async (props: InMemoryKeyAgentProps) => + new InMemoryKeyAgent(props, { bip32Ed25519: await getBip32Ed25519(), logger }), + Ledger: async (props: LedgerKeyAgentProps) => + new LedgerKeyAgent(props, { bip32Ed25519: await getBip32Ed25519(), logger }), + Trezor: async (props: TrezorKeyAgentProps) => + new TrezorKeyAgent(props, { bip32Ed25519: await getBip32Ed25519(), logger }) }); export type KeyAgentFactory = ReturnType; diff --git a/packages/web-extension/src/walletManager/SigningCoordinator/SigningCoordinator.ts b/packages/web-extension/src/walletManager/SigningCoordinator/SigningCoordinator.ts index 6947e612aff..1a0a0911343 100644 --- a/packages/web-extension/src/walletManager/SigningCoordinator/SigningCoordinator.ts +++ b/packages/web-extension/src/walletManager/SigningCoordinator/SigningCoordinator.ts @@ -154,7 +154,7 @@ export class SigningCoordinator; try { const result = await sign( - this.#keyAgentFactory.InMemory({ + await this.#keyAgentFactory.InMemory({ accountIndex: account.accountIndex, chainId: request.requestContext.chainId, encryptedRootPrivateKeyBytes: [ @@ -184,14 +184,14 @@ export class SigningCoordinator sign( request.walletType === WalletType.Ledger - ? this.#keyAgentFactory.Ledger({ + ? await this.#keyAgentFactory.Ledger({ accountIndex: request.requestContext.accountIndex, chainId: request.requestContext.chainId, communicationType: this.#hwOptions.communicationType, extendedAccountPublicKey: account.extendedAccountPublicKey, purpose: account.purpose || KeyPurpose.STANDARD }) - : this.#keyAgentFactory.Trezor({ + : await this.#keyAgentFactory.Trezor({ accountIndex: request.requestContext.accountIndex, chainId: request.requestContext.chainId, extendedAccountPublicKey: account.extendedAccountPublicKey, diff --git a/packages/web-extension/test/walletManager/SigningCoordinator.test.ts b/packages/web-extension/test/walletManager/SigningCoordinator.test.ts index 0165aef8589..1b86a6ff373 100644 --- a/packages/web-extension/test/walletManager/SigningCoordinator.test.ts +++ b/packages/web-extension/test/walletManager/SigningCoordinator.test.ts @@ -73,7 +73,7 @@ describe('SigningCoordinator', () => { signCip8Data: jest.fn(), signTransaction: jest.fn() } as unknown as jest.Mocked; - keyAgentFactory.InMemory.mockReturnValue(keyAgent); + keyAgentFactory.InMemory.mockResolvedValue(keyAgent); }); describe('signTransaction', () => {