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

fix: add wildcard for custody keyring #3899

Merged
merged 8 commits into from
Feb 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ describe('AccountsController', () => {
KeyringTypes.ledger,
KeyringTypes.lattice,
KeyringTypes.qr,
KeyringTypes.custody,
'Custody - JSON - RPC',
])('should add accounts for %s type', async (keyringType) => {
mockUUID.mockReturnValue('mock-id');

Expand Down
11 changes: 7 additions & 4 deletions packages/accounts-controller/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyringTypes } from '@metamask/keyring-controller';
import { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';
import { sha256FromString } from 'ethereumjs-util';
import { v4 as uuid } from 'uuid';

Expand All @@ -9,6 +9,12 @@ import { v4 as uuid } from 'uuid';
* @returns The name of the keyring type.
*/
export function keyringTypeToName(keyringType: string): string {
// Custody keyrings are a special case, as they are not a single type
// they just start with the prefix `Custody`
if (isCustodyKeyring(keyringType)) {
return 'Custody';
}

switch (keyringType) {
case KeyringTypes.simple: {
return 'Account';
Expand All @@ -31,9 +37,6 @@ export function keyringTypeToName(keyringType: string): string {
case KeyringTypes.snap: {
return 'Snap Account';
}
case KeyringTypes.custody: {
return 'Custody';
}
default: {
throw new Error(`Unknown keyring ${keyringType}`);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
AccountImportStrategy,
KeyringController,
KeyringTypes,
isCustodyKeyring,
keyringBuilderFactory,
} from './KeyringController';

Expand Down Expand Up @@ -2791,6 +2792,20 @@ describe('KeyringController', () => {
});
});

describe('isCustodyKeyring', () => {
it('should return true if keyring is custody keyring', () => {
expect(isCustodyKeyring('Custody JSON-RPC')).toBe(true);
});

it('should not return true if keyring is not custody keyring', () => {
expect(isCustodyKeyring(KeyringTypes.hd)).toBe(false);
});

it("should not return true if the keyring doesn't start with custody", () => {
expect(isCustodyKeyring('NotCustody')).toBe(false);
});
});

describe('actions', () => {
beforeEach(() => {
jest
Expand Down
11 changes: 10 additions & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ export enum KeyringTypes {
ledger = 'Ledger Hardware',
lattice = 'Lattice Hardware',
snap = 'Snap Keyring',
custody = 'Custody - JSONRPC',
}

/**
* Custody keyring types are a special case, as they are not a single type
* but they all start with the prefix "Custody".
* @param keyringType - The type of the keyring.
* @returns Whether the keyring type is a custody keyring.
*/
export const isCustodyKeyring = (keyringType: string): boolean => {
return keyringType.startsWith('Custody');
};

/**
* @type KeyringControllerState
*
Expand Down
Loading