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

feat: Allow overwriting built-in keyring builders #4362

Merged
merged 1 commit into from
Jun 4, 2024
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
45 changes: 45 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SignTypedDataVersion,
encrypt,
} from '@metamask/eth-sig-util';
import SimpleKeyring from '@metamask/eth-simple-keyring/dist/simple-keyring';
import type { EthKeyring } from '@metamask/keyring-api';
import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';
import type { KeyringClass } from '@metamask/utils';
Expand Down Expand Up @@ -100,6 +101,32 @@ describe('KeyringController', () => {
}),
).toThrow(KeyringControllerError.UnsupportedEncryptionKeyExport);
});

it('allows overwriting the built-in Simple keyring builder', async () => {
const mockSimpleKeyringBuilder =
// @ts-expect-error The simple keyring doesn't yet conform to the KeyringClass type
buildKeyringBuilderWithSpy(SimpleKeyring);
await withController(
{ keyringBuilders: [mockSimpleKeyringBuilder] },
async ({ controller }) => {
await controller.addNewKeyring(KeyringTypes.simple);

expect(mockSimpleKeyringBuilder).toHaveBeenCalledTimes(1);
},
);
});

it('allows overwriting the built-in HD keyring builder', async () => {
const mockHdKeyringBuilder = buildKeyringBuilderWithSpy(HDKeyring);
await withController(
{ keyringBuilders: [mockHdKeyringBuilder] },
async () => {
// This is called as part of initializing the controller
// because the first keyring is assumed to always be an HD keyring
expect(mockHdKeyringBuilder).toHaveBeenCalledTimes(1);
},
);
});
});

describe('addNewAccount', () => {
Expand Down Expand Up @@ -3538,3 +3565,21 @@ async function withController<ReturnValue>(
messenger,
});
}

/**
* Construct a keyring builder with a spy.
*
* @param KeyringConstructor - The constructor to use for building the keyring.
* @returns A keyring builder that uses `jest.fn()` to spy on invocations.
*/
function buildKeyringBuilderWithSpy(KeyringConstructor: KeyringClass<Json>): {
(): EthKeyring<Json>;
type: string;
} {
const keyringBuilderWithSpy: { (): EthKeyring<Json>; type?: string } = jest
.fn()
.mockImplementation((...args) => new KeyringConstructor(...args));
keyringBuilderWithSpy.type = KeyringConstructor.type;
// Not sure why TypeScript isn't smart enough to infer that `type` is set here.
return keyringBuilderWithSpy as { (): EthKeyring<Json>; type: string };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:
We can also do:

function buildKeyringBuilderWithSpy(KeyringConstructor: KeyringClass<Json>): {
  (): EthKeyring<Json>;
  type: string;
} {
  const keyringBuilderWithSpy = jest
    .fn()
    .mockImplementation(
      (...args) => new KeyringConstructor(...args),
    ) as jest.Mock & { type: string };
  keyringBuilderWithSpy.type = KeyringConstructor.type;
  return keyringBuilderWithSpy;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yes that would also work but it seems equivalent to me (in terms of type safety and readability and whatnot).

I was trying to do it with no assertion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do this without an assertion by doing something like

keyringBuilderWithSpy.type = KeyringConstructor.type
if (!hasProperty(keyringBuilderWithSpy, 'type')) {
  throw new Error('Missing type');
}

TypeScript would be able to infer it then. But that would be very silly to have to do, because we already know that type is set by that point (KeyringConstructor.type is of type string, it's not optional).

2 changes: 1 addition & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ export class KeyringController extends BaseController<
});

this.#keyringBuilders = keyringBuilders
? defaultKeyringBuilders.concat(keyringBuilders)
? keyringBuilders.concat(defaultKeyringBuilders)
: defaultKeyringBuilders;

this.#encryptor = encryptor;
Expand Down
Loading