Skip to content

Commit d239176

Browse files
authored
Merge branch 'main' into perf/with-keyring
2 parents b84ab30 + 1e41ed5 commit d239176

File tree

69 files changed

+1909
-847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1909
-847
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
## Accounts Team
1010
/packages/accounts-controller @MetaMask/accounts-engineers
11-
/packages/keyring-controller @MetaMask/accounts-engineers
1211
/packages/multichain-transactions-controller @MetaMask/accounts-engineers
1312

1413
## Assets Team
@@ -75,6 +74,7 @@
7574
/packages/eth-json-rpc-provider @MetaMask/wallet-api-platform-engineers @MetaMask/wallet-framework-engineers
7675
/packages/json-rpc-engine @MetaMask/wallet-api-platform-engineers @MetaMask/wallet-framework-engineers
7776
/packages/json-rpc-middleware-stream @MetaMask/wallet-api-platform-engineers @MetaMask/wallet-framework-engineers
77+
/packages/keyring-controller @MetaMask/accounts-engineers @MetaMask/wallet-framework-engineers
7878
/packages/multichain-network-controller @MetaMask/wallet-framework-engineers @MetaMask/accounts-engineers @MetaMask/metamask-assets
7979
/packages/network-controller @MetaMask/wallet-framework-engineers @MetaMask/metamask-assets
8080
/packages/permission-controller @MetaMask/wallet-api-platform-engineers @MetaMask/wallet-framework-engineers @MetaMask/snaps-devs

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/core-monorepo",
3-
"version": "381.0.0",
3+
"version": "386.0.0",
44
"private": true,
55
"description": "Monorepo for packages shared between MetaMask clients",
66
"repository": {

packages/accounts-controller/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add new `setAccountNameAndSelectAccount` action ([#5714](https://github.com/MetaMask/core/pull/5714))
13+
- Add `entropySource` and `derivationPath` to EVM HD account options ([#5618](https://github.com/MetaMask/core/pull/5618))
1314

1415
### Changed
1516

@@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1819
- **BREAKING:** Bump `@metamask/providers` peer dependency from ^18.1.0 to ^21.0.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
1920
- Bump `@metamask/snaps-sdk` from ^6.17.1 to ^6.22.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
2021
- Bump `@metamask/snaps-utils` from ^8.10.0 to ^9.2.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
22+
- Prevent unnecasary state updates when updating `InternalAccount.metadata.snap` ([#5735](https://github.com/MetaMask/core/pull/5735))
2123

2224
## [27.0.0]
2325

packages/accounts-controller/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
"devDependencies": {
6565
"@metamask/auto-changelog": "^3.4.4",
66-
"@metamask/keyring-controller": "^21.0.4",
66+
"@metamask/keyring-controller": "^21.0.5",
6767
"@metamask/network-controller": "^23.3.0",
6868
"@metamask/providers": "^21.0.0",
6969
"@metamask/snaps-controllers": "^11.2.1",

packages/accounts-controller/src/AccountsController.test.ts

+124-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { AccountsController, EMPTY_ACCOUNT } from './AccountsController';
3131
import {
3232
createExpectedInternalAccount,
3333
createMockInternalAccount,
34+
createMockInternalAccountOptions,
3435
ETH_EOA_METHODS,
3536
} from './tests/mocks';
3637
import {
@@ -279,7 +280,7 @@ describe('AccountsController', () => {
279280
});
280281

281282
describe('onSnapStateChange', () => {
282-
it('be used enable an account if the Snap is enabled and not blocked', async () => {
283+
it('enables an account if the Snap is enabled and not blocked', async () => {
283284
const messenger = buildMessenger();
284285
const mockSnapAccount = createMockInternalAccount({
285286
id: 'mock-id',
@@ -325,7 +326,7 @@ describe('AccountsController', () => {
325326
expect(updatedAccount.metadata.snap?.enabled).toBe(true);
326327
});
327328

328-
it('be used disable an account if the Snap is disabled', async () => {
329+
it('disables an account if the Snap is disabled', async () => {
329330
const messenger = buildMessenger();
330331
const mockSnapAccount = createMockInternalAccount({
331332
id: 'mock-id',
@@ -371,7 +372,7 @@ describe('AccountsController', () => {
371372
expect(updatedAccount.metadata.snap?.enabled).toBe(false);
372373
});
373374

374-
it('be used disable an account if the Snap is blocked', async () => {
375+
it('disables an account if the Snap is blocked', async () => {
375376
const messenger = buildMessenger();
376377
const mockSnapAccount = createMockInternalAccount({
377378
id: 'mock-id',
@@ -416,6 +417,61 @@ describe('AccountsController', () => {
416417

417418
expect(updatedAccount.metadata.snap?.enabled).toBe(false);
418419
});
420+
421+
it('does not trigger any unnecessary updates', async () => {
422+
const messenger = buildMessenger();
423+
const mockSnapAccount = createMockInternalAccount({
424+
id: 'mock-id',
425+
name: 'Snap Account 1',
426+
address: '0x0',
427+
keyringType: KeyringTypes.snap,
428+
snap: {
429+
id: 'mock-snap',
430+
name: 'mock-snap-name',
431+
enabled: false, // Will be enabled later by a `SnapController:stateChange`.
432+
},
433+
});
434+
const mockSnapChangeState = {
435+
snaps: {
436+
'mock-snap': {
437+
enabled: true,
438+
id: 'mock-snap',
439+
blocked: false,
440+
status: SnapStatus.Running,
441+
},
442+
},
443+
// TODO: Replace `any` with type
444+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
445+
} as any as SnapControllerState;
446+
const mockStateChange = jest.fn();
447+
const { accountsController } = setupAccountsController({
448+
initialState: {
449+
internalAccounts: {
450+
accounts: {
451+
[mockSnapAccount.id]: mockSnapAccount,
452+
},
453+
selectedAccount: mockSnapAccount.id,
454+
},
455+
},
456+
messenger,
457+
});
458+
459+
messenger.subscribe('AccountsController:stateChange', mockStateChange);
460+
461+
// First update will update the account's metadata, thus triggering a `AccountsController:stateChange`.
462+
messenger.publish('SnapController:stateChange', mockSnapChangeState, []);
463+
const updatedAccount = accountsController.getAccountExpect(
464+
mockSnapAccount.id,
465+
);
466+
expect(updatedAccount.metadata.snap?.enabled).toBe(true);
467+
expect(mockStateChange).toHaveBeenCalled();
468+
469+
// Second update is the same, thus the account does not need any update, and SHOULD NOT trigger a `AccountsController:stateChange`.
470+
mockStateChange.mockReset();
471+
messenger.publish('SnapController:stateChange', mockSnapChangeState, []);
472+
expect(updatedAccount.metadata.snap?.enabled).toBe(true);
473+
expect(mockStateChange).not.toHaveBeenCalled();
474+
});
419475
});
420476

421477
describe('onKeyringStateChange', () => {
@@ -901,6 +957,7 @@ describe('AccountsController', () => {
901957
name: 'Account 3',
902958
address: mockAccount3.address,
903959
keyringType: KeyringTypes.hd,
960+
options: {},
904961
}),
905962
]);
906963
});
@@ -1744,6 +1801,12 @@ describe('AccountsController', () => {
17441801
keyrings: [
17451802
{ type: KeyringTypes.hd, accounts: [mockAddress1, mockAddress2] },
17461803
],
1804+
keyringsMetadata: [
1805+
{
1806+
id: 'mock-keyring-id-0',
1807+
name: 'mock-keyring-id-name',
1808+
},
1809+
],
17471810
}),
17481811
);
17491812

@@ -1772,12 +1835,14 @@ describe('AccountsController', () => {
17721835
id: 'mock-id',
17731836
address: mockAddress1,
17741837
keyringType: KeyringTypes.hd,
1838+
options: createMockInternalAccountOptions(0, KeyringTypes.hd, 0),
17751839
}),
17761840
createExpectedInternalAccount({
17771841
name: 'Account 2',
17781842
id: 'mock-id2',
17791843
address: mockAddress2,
17801844
keyringType: KeyringTypes.hd,
1845+
options: createMockInternalAccountOptions(0, KeyringTypes.hd, 1),
17811846
}),
17821847
];
17831848
mockUUIDWithNormalAccounts(expectedAccounts);
@@ -1800,6 +1865,12 @@ describe('AccountsController', () => {
18001865
accounts: [mockSnapAccount, mockSnapAccount2],
18011866
},
18021867
],
1868+
keyringsMetadata: [
1869+
{
1870+
id: 'mock-keyring-id-1',
1871+
name: 'mock-keyring-id-name',
1872+
},
1873+
],
18031874
}),
18041875
);
18051876

@@ -1893,6 +1964,12 @@ describe('AccountsController', () => {
18931964
keyrings: [
18941965
{ type: KeyringTypes.hd, accounts: [mockAddress1, mockAddress2] },
18951966
],
1967+
keyringsMetadata: [
1968+
{
1969+
id: 'mock-keyring-id-0',
1970+
name: 'mock-keyring-id-name',
1971+
},
1972+
],
18961973
}),
18971974
);
18981975

@@ -1918,12 +1995,16 @@ describe('AccountsController', () => {
19181995
messenger,
19191996
});
19201997
const expectedAccounts = [
1921-
mockAccount,
1998+
{
1999+
...mockAccount,
2000+
options: createMockInternalAccountOptions(0, KeyringTypes.hd, 0),
2001+
},
19222002
createExpectedInternalAccount({
19232003
name: 'Account 2',
19242004
id: 'mock-id2',
19252005
address: mockAddress2,
19262006
keyringType: KeyringTypes.hd,
2007+
options: createMockInternalAccountOptions(0, KeyringTypes.hd, 1),
19272008
}),
19282009
];
19292010
mockUUIDWithNormalAccounts(expectedAccounts);
@@ -1957,6 +2038,16 @@ describe('AccountsController', () => {
19572038
{ type: KeyringTypes.hd, accounts: [mockAddress1] },
19582039
{ type: KeyringTypes.snap, accounts: ['0x1234'] },
19592040
],
2041+
keyringsMetadata: [
2042+
{
2043+
id: 'mock-keyring-id-0',
2044+
name: 'mock-keyring-id-name',
2045+
},
2046+
{
2047+
id: 'mock-keyring-id-1',
2048+
name: 'mock-keyring-id-name2',
2049+
},
2050+
],
19602051
}),
19612052
);
19622053

@@ -1975,6 +2066,7 @@ describe('AccountsController', () => {
19752066
id: 'mock-id',
19762067
address: mockAddress1,
19772068
keyringType: KeyringTypes.hd,
2069+
options: createMockInternalAccountOptions(0, KeyringTypes.hd, 0),
19782070
}),
19792071
createExpectedInternalAccount({
19802072
name: 'Snap Account 1', // it is Snap Account 1 because it is the only snap account
@@ -2014,6 +2106,16 @@ describe('AccountsController', () => {
20142106
{ type: KeyringTypes.snap, accounts: ['0x1234'] },
20152107
{ type: KeyringTypes.hd, accounts: [mockAddress1] },
20162108
],
2109+
keyringsMetadata: [
2110+
{
2111+
id: 'mock-keyring-id-0',
2112+
name: 'mock-keyring-id-name',
2113+
},
2114+
{
2115+
id: 'mock-keyring-id-1',
2116+
name: 'mock-keyring-id-name2',
2117+
},
2118+
],
20172119
}),
20182120
);
20192121

@@ -2032,6 +2134,7 @@ describe('AccountsController', () => {
20322134
id: 'mock-id',
20332135
address: mockAddress1,
20342136
keyringType: KeyringTypes.hd,
2137+
options: createMockInternalAccountOptions(1, KeyringTypes.hd, 0),
20352138
}),
20362139
createExpectedInternalAccount({
20372140
name: 'Snap Account 1', // it is Snap Account 1 because it is the only snap account
@@ -2057,7 +2160,6 @@ describe('AccountsController', () => {
20572160
KeyringTypes.ledger,
20582161
KeyringTypes.lattice,
20592162
KeyringTypes.qr,
2060-
'Custody - JSON - RPC',
20612163
])('should add accounts for %s type', async (keyringType) => {
20622164
mockUUIDWithNormalAccounts([mockAccount]);
20632165

@@ -2067,6 +2169,12 @@ describe('AccountsController', () => {
20672169
'KeyringController:getState',
20682170
mockGetState.mockReturnValue({
20692171
keyrings: [{ type: keyringType, accounts: [mockAddress1] }],
2172+
keyringsMetadata: [
2173+
{
2174+
id: 'mock-keyring-id-0',
2175+
name: 'mock-keyring-id-name',
2176+
},
2177+
],
20702178
}),
20712179
);
20722180

@@ -2096,6 +2204,7 @@ describe('AccountsController', () => {
20962204
id: 'mock-id',
20972205
address: mockAddress1,
20982206
keyringType: keyringType as KeyringTypes,
2207+
options: createMockInternalAccountOptions(0, keyringType, 0),
20992208
}),
21002209
];
21012210

@@ -2206,6 +2315,16 @@ describe('AccountsController', () => {
22062315
{ type: KeyringTypes.snap, accounts: ['0x1234'] },
22072316
{ type: KeyringTypes.hd, accounts: [mockAddress1] },
22082317
],
2318+
keyringsMetadata: [
2319+
{
2320+
id: 'mock-keyring-id-1',
2321+
name: 'mock-keyring-id-name',
2322+
},
2323+
{
2324+
id: 'mock-keyring-id-2',
2325+
name: 'mock-keyring-id-name2',
2326+
},
2327+
],
22092328
}),
22102329
);
22112330

0 commit comments

Comments
 (0)