Skip to content

Commit

Permalink
refactor(web-extension)!: make KeyAgentFactory methods async
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkazlauskas committed Jan 21, 2025
1 parent fdf1e41 commit a9a8ab8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/e2e/test/web-extension/extension/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const passphraseByteArray = Uint8Array.from(
);

const initSigningCoordinator = async () => {
const bip32Ed25519 = await Crypto.SodiumBip32Ed25519.create();
const bip32Ed25519Ready = Crypto.SodiumBip32Ed25519.create();

const signingCoordinator = new SigningCoordinator(
{
Expand All @@ -212,7 +212,7 @@ const initSigningCoordinator = async () => {
},
{
keyAgentFactory: createKeyAgentFactory({
bip32Ed25519,
bip32Ed25519Ready,
logger
}),
logger
Expand Down
2 changes: 1 addition & 1 deletion packages/web-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const signingCoordinator = new SigningCoordinator(
},
{
keyAgentFactory: createKeyAgentFactory({
bip32Ed25519: new Crypto.SodiumBip32Ed25519(),
bip32Ed25519Ready: Crypto.SodiumBip32Ed25519.create(),
logger
}),
logger
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
bip32Ed25519Ready: Promise<Bip32Ed25519>;
};

export const createKeyAgentFactory = ({ logger, bip32Ed25519Ready }: KeyAgentFactoryDependencies) => ({
InMemory: async (props: InMemoryKeyAgentProps) =>
new InMemoryKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger }),
Ledger: async (props: LedgerKeyAgentProps) =>
new LedgerKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger }),
Trezor: async (props: TrezorKeyAgentProps) =>
new TrezorKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger })
});

export type KeyAgentFactory = ReturnType<typeof createKeyAgentFactory>;
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
const wallet = request.requestContext.wallet as InMemoryWallet<WalletMetadata, AccountMetadata>;
try {
const result = await sign(
this.#keyAgentFactory.InMemory({
await this.#keyAgentFactory.InMemory({
accountIndex: account.accountIndex,
chainId: request.requestContext.chainId,
encryptedRootPrivateKeyBytes: [
Expand Down Expand Up @@ -184,14 +184,14 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
async (options?: SignOptions) =>
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('SigningCoordinator', () => {
signCip8Data: jest.fn(),
signTransaction: jest.fn()
} as unknown as jest.Mocked<InMemoryKeyAgent>;
keyAgentFactory.InMemory.mockReturnValue(keyAgent);
keyAgentFactory.InMemory.mockResolvedValue(keyAgent);
});

describe('signTransaction', () => {
Expand Down

0 comments on commit a9a8ab8

Please sign in to comment.