Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(web-extension)!: make KeyAgentFactory methods async #1564

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/e2e/test/web-extension/extension/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ const passphraseByteArray = Uint8Array.from(
);

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

const signingCoordinator = new SigningCoordinator(
{
hwOptions: {
Expand All @@ -212,7 +210,7 @@ const initSigningCoordinator = async () => {
},
{
keyAgentFactory: createKeyAgentFactory({
bip32Ed25519,
getBip32Ed25519: Crypto.SodiumBip32Ed25519.create,
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(),
getBip32Ed25519: 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;
getBip32Ed25519: () => Promise<Bip32Ed25519>;
};

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<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
Loading