Skip to content

Commit 9cae8f4

Browse files
committed
refactor!: remove getLegacySnapKeyring (not usable after running the migration)
1 parent 91d8cbc commit 9cae8f4

4 files changed

Lines changed: 6 additions & 101 deletions

File tree

packages/snap-account-service/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
- Selected-account forwarding now targets v2 Snap keyrings.
4242
- The service messenger now requires the `KeyringController:withKeyringV2Unsafe`.
4343

44+
### Removed
45+
46+
- **BREAKING:** Removed `getLegacySnapKeyring` ([#8732](https://github.com/MetaMask/core/pull/8732))
47+
- The legacy Snap keyring should not be used anymore after the migration has completed.
48+
4449
### Changed
4550

4651
- Bump `@metamask/messenger` from `^1.1.1` to `^1.2.0` ([#8632](https://github.com/MetaMask/core/pull/8632))

packages/snap-account-service/src/SnapAccountService-method-action-types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ export type SnapAccountServiceMigrateAction = {
4848
handler: SnapAccountService['migrate'];
4949
};
5050

51-
/**
52-
* Atomically gets-or-creates the legacy (v1) Snap keyring — the keyring
53-
* associated with {@link KeyringTypes.snap}.
54-
*
55-
* @returns The existing or newly-created Snap keyring instance.
56-
*/
57-
export type SnapAccountServiceGetLegacySnapKeyringAction = {
58-
type: `SnapAccountService:getLegacySnapKeyring`;
59-
handler: SnapAccountService['getLegacySnapKeyring'];
60-
};
61-
6251
/**
6352
* Handle a message from a Snap.
6453
*
@@ -78,5 +67,4 @@ export type SnapAccountServiceMethodActions =
7867
| SnapAccountServiceGetSnapsAction
7968
| SnapAccountServiceEnsureReadyAction
8069
| SnapAccountServiceMigrateAction
81-
| SnapAccountServiceGetLegacySnapKeyringAction
8270
| SnapAccountServiceHandleKeyringSnapMessageAction;

packages/snap-account-service/src/SnapAccountService.test.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AccountGroupId } from '@metamask/account-api';
22
import { SNAP_KEYRING_TYPE } from '@metamask/eth-snap-keyring';
3-
import type { SnapKeyring, SnapMessage } from '@metamask/eth-snap-keyring';
3+
import type { SnapMessage } from '@metamask/eth-snap-keyring';
44
import type { SnapKeyring as SnapKeyringV2 } from '@metamask/eth-snap-keyring/v2';
55
import { KeyringEvent } from '@metamask/keyring-api';
66
import { KeyringType } from '@metamask/keyring-api/v2';
@@ -860,43 +860,6 @@ describe('SnapAccountService', () => {
860860
});
861861
});
862862

863-
describe('getLegacySnapKeyring', () => {
864-
it('returns the existing Snap keyring when one is already present', async () => {
865-
const { service, mocks } = await setup();
866-
const existing = buildKeyringEntry(KeyringTypes.snap);
867-
const { addNewKeyring } = mockWithController(mocks, [
868-
buildKeyringEntry(KeyringTypes.hd),
869-
existing,
870-
]);
871-
872-
const result = await service.getLegacySnapKeyring();
873-
874-
expect(result).toBe(existing.keyring as unknown as SnapKeyring);
875-
expect(addNewKeyring).not.toHaveBeenCalled();
876-
});
877-
878-
it('creates a new Snap keyring when none exists', async () => {
879-
const { service, mocks } = await setup();
880-
const { addNewKeyring } = mockWithController(mocks, [
881-
buildKeyringEntry(KeyringTypes.hd),
882-
]);
883-
884-
const result = await service.getLegacySnapKeyring();
885-
886-
expect(addNewKeyring).toHaveBeenCalledWith(KeyringTypes.snap);
887-
expect(result.type).toBe(KeyringTypes.snap);
888-
});
889-
890-
it('propagates errors thrown by withController', async () => {
891-
const { service, mocks } = await setup();
892-
mocks.KeyringController.withController.mockImplementation(async () => {
893-
throw new Error('boom');
894-
});
895-
896-
await expect(service.getLegacySnapKeyring()).rejects.toThrow('boom');
897-
});
898-
});
899-
900863
describe('handleKeyringSnapMessage', () => {
901864
const MOCK_MESSAGE = {
902865
method: KeyringEvent.AccountUpdated,

packages/snap-account-service/src/SnapAccountService.ts

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type {
1313
KeyringControllerWithControllerAction,
1414
KeyringControllerWithKeyringV2Action,
1515
KeyringControllerWithKeyringV2UnsafeAction,
16-
KeyringEntry,
1716
} from '@metamask/keyring-controller';
1817
import {
1918
isKeyringNotFoundError,
@@ -40,7 +39,6 @@ import type { Json } from '@metamask/utils';
4039
import { projectLogger as log } from './logger';
4140
import type {
4241
SnapAccountServiceEnsureReadyAction,
43-
SnapAccountServiceGetLegacySnapKeyringAction,
4442
SnapAccountServiceGetSnapsAction,
4543
SnapAccountServiceHandleKeyringSnapMessageAction,
4644
SnapAccountServiceMigrateAction,
@@ -71,7 +69,6 @@ export const serviceName = 'SnapAccountService';
7169
const MESSENGER_EXPOSED_METHODS = [
7270
'ensureReady',
7371
'getSnaps',
74-
'getLegacySnapKeyring',
7572
'handleKeyringSnapMessage',
7673
'migrate',
7774
] as const;
@@ -82,7 +79,6 @@ const MESSENGER_EXPOSED_METHODS = [
8279
export type SnapAccountServiceActions =
8380
| SnapAccountServiceEnsureReadyAction
8481
| SnapAccountServiceGetSnapsAction
85-
| SnapAccountServiceGetLegacySnapKeyringAction
8682
| SnapAccountServiceHandleKeyringSnapMessageAction
8783
| SnapAccountServiceMigrateAction;
8884

@@ -535,53 +531,6 @@ export class SnapAccountService {
535531
);
536532
}
537533

538-
/**
539-
* Atomically gets-or-creates the legacy (v1) Snap keyring — the keyring
540-
* associated with {@link KeyringTypes.snap}.
541-
*
542-
* @returns The existing or newly-created Snap keyring instance.
543-
*/
544-
async getLegacySnapKeyring(): Promise<LegacySnapKeyring> {
545-
type Result = {
546-
snapKeyring: LegacySnapKeyring;
547-
};
548-
549-
// `KeyringController:withController` forbids returning a direct keyring
550-
// reference (it checks the result via `Object.is`), so we smuggle the
551-
// instance out wrapped in an object and unwrap it after the call.
552-
// NOTE: This violates the abstraction of `KeyringController:withController`, but this
553-
// is how we currently interact with the legacy Snap keyring. Once we migrate it to
554-
// the Snap keyring v2, we won't be using the same pattern.
555-
const result = await this.#messenger.call(
556-
'KeyringController:withController',
557-
async (controller): Promise<Result> => {
558-
let snapKeyring: KeyringEntry['keyring'] | undefined;
559-
560-
const found = controller.keyrings.find(({ keyring }) =>
561-
isLegacySnapKeyring(keyring),
562-
);
563-
if (found) {
564-
snapKeyring = found.keyring;
565-
}
566-
567-
if (!snapKeyring) {
568-
const {
569-
keyring: newSnapKeyring,
570-
metadata: { id },
571-
} = await controller.addNewKeyring(KeyringTypes.snap);
572-
snapKeyring = newSnapKeyring;
573-
574-
log(`Legacy Snap keyring created. ("${id}")`);
575-
}
576-
577-
// The legacy Snap keyring is not compatible with `EthKeyring`, so we need to cast here.
578-
return { snapKeyring } as unknown as Result;
579-
},
580-
);
581-
582-
return (result as Result).snapKeyring;
583-
}
584-
585534
/**
586535
* Handle a message from a Snap.
587536
*

0 commit comments

Comments
 (0)