Skip to content

Commit a9a8ab8

Browse files
committed
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
1 parent fdf1e41 commit a9a8ab8

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

packages/e2e/test/web-extension/extension/ui.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const passphraseByteArray = Uint8Array.from(
198198
);
199199

200200
const initSigningCoordinator = async () => {
201-
const bip32Ed25519 = await Crypto.SodiumBip32Ed25519.create();
201+
const bip32Ed25519Ready = Crypto.SodiumBip32Ed25519.create();
202202

203203
const signingCoordinator = new SigningCoordinator(
204204
{
@@ -212,7 +212,7 @@ const initSigningCoordinator = async () => {
212212
},
213213
{
214214
keyAgentFactory: createKeyAgentFactory({
215-
bip32Ed25519,
215+
bip32Ed25519Ready,
216216
logger
217217
}),
218218
logger

packages/web-extension/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const signingCoordinator = new SigningCoordinator(
127127
},
128128
{
129129
keyAgentFactory: createKeyAgentFactory({
130-
bip32Ed25519: new Crypto.SodiumBip32Ed25519(),
130+
bip32Ed25519Ready: Crypto.SodiumBip32Ed25519.create(),
131131
logger
132132
}),
133133
logger
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import { InMemoryKeyAgent, InMemoryKeyAgentProps, KeyAgentDependencies } from '@cardano-sdk/key-management';
1+
import { Bip32Ed25519 } from '@cardano-sdk/crypto';
2+
import { InMemoryKeyAgent, InMemoryKeyAgentProps } from '@cardano-sdk/key-management';
23
import { LedgerKeyAgent, LedgerKeyAgentProps } from '@cardano-sdk/hardware-ledger';
4+
import { Logger } from 'ts-log';
35
import { TrezorKeyAgent, TrezorKeyAgentProps } from '@cardano-sdk/hardware-trezor';
46

5-
export const createKeyAgentFactory = (dependencies: KeyAgentDependencies) => ({
6-
InMemory: (props: InMemoryKeyAgentProps) => new InMemoryKeyAgent(props, dependencies),
7-
Ledger: (props: LedgerKeyAgentProps) => new LedgerKeyAgent(props, dependencies),
8-
Trezor: (props: TrezorKeyAgentProps) => new TrezorKeyAgent(props, dependencies)
7+
export type KeyAgentFactoryDependencies = {
8+
logger: Logger;
9+
bip32Ed25519Ready: Promise<Bip32Ed25519>;
10+
};
11+
12+
export const createKeyAgentFactory = ({ logger, bip32Ed25519Ready }: KeyAgentFactoryDependencies) => ({
13+
InMemory: async (props: InMemoryKeyAgentProps) =>
14+
new InMemoryKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger }),
15+
Ledger: async (props: LedgerKeyAgentProps) =>
16+
new LedgerKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger }),
17+
Trezor: async (props: TrezorKeyAgentProps) =>
18+
new TrezorKeyAgent(props, { bip32Ed25519: await bip32Ed25519Ready, logger })
919
});
1020

1121
export type KeyAgentFactory = ReturnType<typeof createKeyAgentFactory>;

packages/web-extension/src/walletManager/SigningCoordinator/SigningCoordinator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
154154
const wallet = request.requestContext.wallet as InMemoryWallet<WalletMetadata, AccountMetadata>;
155155
try {
156156
const result = await sign(
157-
this.#keyAgentFactory.InMemory({
157+
await this.#keyAgentFactory.InMemory({
158158
accountIndex: account.accountIndex,
159159
chainId: request.requestContext.chainId,
160160
encryptedRootPrivateKeyBytes: [
@@ -184,14 +184,14 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
184184
async (options?: SignOptions) =>
185185
sign(
186186
request.walletType === WalletType.Ledger
187-
? this.#keyAgentFactory.Ledger({
187+
? await this.#keyAgentFactory.Ledger({
188188
accountIndex: request.requestContext.accountIndex,
189189
chainId: request.requestContext.chainId,
190190
communicationType: this.#hwOptions.communicationType,
191191
extendedAccountPublicKey: account.extendedAccountPublicKey,
192192
purpose: account.purpose || KeyPurpose.STANDARD
193193
})
194-
: this.#keyAgentFactory.Trezor({
194+
: await this.#keyAgentFactory.Trezor({
195195
accountIndex: request.requestContext.accountIndex,
196196
chainId: request.requestContext.chainId,
197197
extendedAccountPublicKey: account.extendedAccountPublicKey,

packages/web-extension/test/walletManager/SigningCoordinator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('SigningCoordinator', () => {
7373
signCip8Data: jest.fn(),
7474
signTransaction: jest.fn()
7575
} as unknown as jest.Mocked<InMemoryKeyAgent>;
76-
keyAgentFactory.InMemory.mockReturnValue(keyAgent);
76+
keyAgentFactory.InMemory.mockResolvedValue(keyAgent);
7777
});
7878

7979
describe('signTransaction', () => {

0 commit comments

Comments
 (0)