Skip to content

perf!: remove InternalAccount snap metadata #5734

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
"resolutions": {
"[email protected]": "^6.5.7",
"fast-xml-parser@^4.3.4": "^4.4.1",
"[email protected]": "^7.5.10"
"[email protected]": "^7.5.10",
"@metamask/keyring-api@^17.4.0": "npm:@metamask-previews/[email protected]",
"@metamask/keyring-internal-api@^6.0.1": "npm:@metamask-previews/[email protected]",
"@metamask/eth-snap-keyring@^12.1.1": "npm:@metamask-previews/[email protected]"
},
"devDependencies": {
"@babel/core": "^7.23.5",
Expand Down
151 changes: 0 additions & 151 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import { KeyringTypes } from '@metamask/keyring-controller';
import type { InternalAccount } from '@metamask/keyring-internal-api';
import type { NetworkClientId } from '@metamask/network-controller';
import type { SnapControllerState } from '@metamask/snaps-controllers';
import { SnapStatus } from '@metamask/snaps-utils';
import type { CaipChainId } from '@metamask/utils';
import * as uuid from 'uuid';

Check warning on line 18 in packages/accounts-controller/src/AccountsController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (22.x)

No exported names found in module 'uuid'
import type { V4Options } from 'uuid';

import type {
Expand Down Expand Up @@ -94,9 +92,7 @@
name: '',
keyring: { type: KeyringTypes.snap },
snap: {
enabled: true,
id: 'mock-snap-id',
name: 'snap-name',
},
importTime: 1691565967600,
lastSelected: 1955565967656,
Expand All @@ -114,9 +110,7 @@
name: 'Custom Name',
keyring: { type: KeyringTypes.snap },
snap: {
enabled: true,
id: 'mock-snap-id',
name: 'snap-name',
},
importTime: 1955565967656,
lastSelected: 1955565967656,
Expand Down Expand Up @@ -201,7 +195,6 @@
return messenger.getRestricted({
name: 'AccountsController',
allowedEvents: [
'SnapController:stateChange',
'KeyringController:stateChange',
'SnapKeyring:accountAssetListUpdated',
'SnapKeyring:accountBalancesUpdated',
Expand Down Expand Up @@ -278,146 +271,6 @@
lastSelected: 22222,
});

describe('onSnapStateChange', () => {
it('be used enable an account if the Snap is enabled and not blocked', async () => {
const messenger = buildMessenger();
const mockSnapAccount = createMockInternalAccount({
id: 'mock-id',
name: 'Snap Account 1',
address: '0x0',
keyringType: KeyringTypes.snap,
snap: {
id: 'mock-snap',
name: 'mock-snap-name',
enabled: false,
},
});
const mockSnapChangeState = {
snaps: {
'mock-snap': {
enabled: true,
id: 'mock-snap',
blocked: false,
status: SnapStatus.Running,
},
},
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any as SnapControllerState;
const { accountsController } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockSnapAccount.id]: mockSnapAccount,
},
selectedAccount: mockSnapAccount.id,
},
},
messenger,
});

messenger.publish('SnapController:stateChange', mockSnapChangeState, []);

const updatedAccount = accountsController.getAccountExpect(
mockSnapAccount.id,
);

expect(updatedAccount.metadata.snap?.enabled).toBe(true);
});

it('be used disable an account if the Snap is disabled', async () => {
const messenger = buildMessenger();
const mockSnapAccount = createMockInternalAccount({
id: 'mock-id',
name: 'Snap Account 1',
address: '0x0',
keyringType: KeyringTypes.snap,
snap: {
id: 'mock-snap',
name: 'mock-snap-name',
enabled: true,
},
});
const mockSnapChangeState = {
snaps: {
'mock-snap': {
enabled: false,
id: 'mock-snap',
blocked: false,
status: SnapStatus.Running,
},
},
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any as SnapControllerState;
const { accountsController } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockSnapAccount.id]: mockSnapAccount,
},
selectedAccount: mockSnapAccount.id,
},
},
messenger,
});

messenger.publish('SnapController:stateChange', mockSnapChangeState, []);

const updatedAccount = accountsController.getAccountExpect(
mockSnapAccount.id,
);

expect(updatedAccount.metadata.snap?.enabled).toBe(false);
});

it('be used disable an account if the Snap is blocked', async () => {
const messenger = buildMessenger();
const mockSnapAccount = createMockInternalAccount({
id: 'mock-id',
name: 'Snap Account 1',
address: '0x0',
keyringType: KeyringTypes.snap,
snap: {
id: 'mock-snap',
name: 'mock-snap-name',
enabled: true,
},
});
const mockSnapChangeState = {
snaps: {
'mock-snap': {
enabled: true,
id: 'mock-snap',
blocked: true,
status: SnapStatus.Running,
},
},
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any as SnapControllerState;
const { accountsController } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockSnapAccount.id]: mockSnapAccount,
},
selectedAccount: mockSnapAccount.id,
},
},
messenger,
});

messenger.publish('SnapController:stateChange', mockSnapChangeState, []);

const updatedAccount = accountsController.getAccountExpect(
mockSnapAccount.id,
);

expect(updatedAccount.metadata.snap?.enabled).toBe(false);
});
});

describe('onKeyringStateChange', () => {
it('uses listMultichainAccounts', async () => {
const messenger = buildMessenger();
Expand Down Expand Up @@ -1528,8 +1381,6 @@
keyringType: KeyringTypes.snap,
snap: {
id: 'mock-snap',
name: 'mock-snap-name',
enabled: true,
},
type: BtcAccountType.P2wpkh,
});
Expand Down Expand Up @@ -2679,8 +2530,6 @@
keyringType: KeyringTypes.snap,
snap: {
id: 'mock-non-evm-snap',
name: 'mock-non-evm-snap-name',
enabled: true,
},
type: BtcAccountType.P2wpkh,
});
Expand Down
40 changes: 0 additions & 40 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ import {
import type { InternalAccount } from '@metamask/keyring-internal-api';
import { isScopeEqualToAny } from '@metamask/keyring-utils';
import type { NetworkClientId } from '@metamask/network-controller';
import type {
SnapControllerState,
SnapStateChange,
} from '@metamask/snaps-controllers';
import type { SnapId } from '@metamask/snaps-sdk';
import type { Snap } from '@metamask/snaps-utils';
import { type CaipChainId, isCaipChainId } from '@metamask/utils';
import type { WritableDraft } from 'immer/dist/internal.js';

Expand Down Expand Up @@ -184,7 +178,6 @@ export type AccountsControllerAccountAssetListUpdatedEvent = {
};

export type AllowedEvents =
| SnapStateChange
| KeyringControllerStateChangeEvent
| SnapKeyringAccountAssetListUpdatedEvent
| SnapKeyringAccountBalancesUpdatedEvent
Expand Down Expand Up @@ -965,34 +958,6 @@ export class AccountsController extends BaseController<
}
}

/**
* Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.
*
* @param snapState - The new SnapControllerState.
*/
#handleOnSnapStateChange(snapState: SnapControllerState) {
// only check if snaps changed in status
const { snaps } = snapState;
const accounts = this.listMultichainAccounts().filter(
(account) => account.metadata.snap,
);

this.update((currentState) => {
accounts.forEach((account) => {
const currentAccount =
currentState.internalAccounts.accounts[account.id];
if (currentAccount.metadata.snap) {
const snapId = currentAccount.metadata.snap.id;
const storedSnap: Snap = snaps[snapId as SnapId];
if (storedSnap) {
currentAccount.metadata.snap.enabled =
storedSnap.enabled && !storedSnap.blocked;
}
}
});
});
}

/**
* Returns the list of accounts for a given keyring type.
*
Expand Down Expand Up @@ -1179,11 +1144,6 @@ export class AccountsController extends BaseController<
* Subscribes to message events.
*/
#subscribeToMessageEvents() {
this.messagingSystem.subscribe(
'SnapController:stateChange',
(snapStateState) => this.#handleOnSnapStateChange(snapStateState),
);

this.messagingSystem.subscribe(
'KeyringController:stateChange',
(keyringState) => this.#handleOnKeyringStateChange(keyringState),
Expand Down
2 changes: 0 additions & 2 deletions packages/accounts-controller/src/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export const createMockInternalAccount = ({
methods?: (EthMethod | BtcMethod)[];
snap?: {
id: string;
enabled: boolean;
name: string;
};
importTime?: number;
lastSelected?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const mockSolanaAccount: InternalAccount = {
},
snap: {
id: 'local:http://localhost:8080',
name: 'Solana',
enabled: true,
},
lastSelected: 0,
},
Expand All @@ -67,8 +65,6 @@ const mockEthAccount: InternalAccount = {
},
snap: {
id: 'mock-eth-snap',
name: 'mock-eth-snap',
enabled: true,
},
lastSelected: 0,
},
Expand Down
Loading
Loading