Skip to content

Commit 12f84e2

Browse files
committed
Merge branch 'main' into fix/add-missing-settings-from-mobile-patch
2 parents 97ddb39 + 7f6cdfa commit 12f84e2

Some content is hidden

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

60 files changed

+1090
-1410
lines changed

package.json

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

packages/account-tree-controller/CHANGELOG.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.1.0]
11+
12+
### Changed
13+
14+
- Set the `setAccountGroupName`'s option `autoHandleConflict` to `true` for all backup & sync operations ([#6697](https://github.com/MetaMask/core/pull/6697))
15+
- Add new group naming for non-HD keyring accounts ([#6679](https://github.com/MetaMask/core/pull/6679)), ([#6696](https://github.com/MetaMask/core/pull/6696))
16+
- Hardware-wallet account groups are now named: "Ledger|Trezor|QR|Lattice|OneKey Account N".
17+
- Private key account groups are now named: "Imported Account N".
18+
- Snap account groups are now named: "Snap Account N".
19+
- Account group names now use natural indexing as a fallback ([#6677](https://github.com/MetaMask/core/pull/6677)), ([#6679](https://github.com/MetaMask/core/pull/6679)), ([#6696](https://github.com/MetaMask/core/pull/6696))
20+
- If a user names his accounts without any indexes, we would just use the number of accounts to compute the next available index.
21+
1022
### Fixed
1123

12-
- Fix group naming for PK/Hardware accounts ([#6677](https://github.com/MetaMask/core/pull/6677))
13-
- Previously, the first PK/Hardware account would start as `Account 2` as opposed to `Account 1` and thus subsequent group names were off as well.
24+
- Fix group naming for non-HD keyring accounts ([#6677](https://github.com/MetaMask/core/pull/6677)), ([#6679](https://github.com/MetaMask/core/pull/6679))
25+
- Previously, the first non-HD keyring account would start as `Account 2` as opposed to `Account 1` and thus subsequent group names were off as well.
1426

1527
## [1.0.0]
1628

@@ -294,7 +306,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
294306
- Initial release ([#5847](https://github.com/MetaMask/core/pull/5847))
295307
- Grouping accounts into 3 main categories: Entropy source, Snap ID, keyring types.
296308

297-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@1.0.0...HEAD
309+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@1.1.0...HEAD
310+
[1.1.0]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@1.0.0...@metamask/account-tree-controller@1.1.0
298311
[1.0.0]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@0.18.1...@metamask/account-tree-controller@1.0.0
299312
[0.18.1]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@0.18.0...@metamask/account-tree-controller@0.18.1
300313
[0.18.0]: https://github.com/MetaMask/core/compare/@metamask/account-tree-controller@0.17.0...@metamask/account-tree-controller@0.18.0

packages/account-tree-controller/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/account-tree-controller",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Controller to group account together based on some pre-defined rules",
55
"keywords": [
66
"MetaMask",

packages/account-tree-controller/src/AccountTreeController.test.ts

Lines changed: 222 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ describe('AccountTreeController', () => {
587587
type: AccountGroupType.SingleAccount,
588588
accounts: [MOCK_SNAP_ACCOUNT_2.id],
589589
metadata: {
590-
name: 'Account 1', // Updated: per-wallet numbering (different wallet)
590+
name: 'Snap Account 1', // Updated: per-wallet numbering (different wallet)
591591
pinned: false,
592592
hidden: false,
593593
},
@@ -610,7 +610,7 @@ describe('AccountTreeController', () => {
610610
type: AccountGroupType.SingleAccount,
611611
accounts: [MOCK_HARDWARE_ACCOUNT_1.id],
612612
metadata: {
613-
name: 'Account 1', // Updated: per-wallet numbering (different wallet)
613+
name: 'Ledger Account 1', // Updated: per-wallet numbering (different wallet)
614614
pinned: false,
615615
hidden: false,
616616
},
@@ -652,13 +652,13 @@ describe('AccountTreeController', () => {
652652
},
653653
[expectedKeyringWalletIdGroup]: {
654654
name: {
655-
value: 'Account 1', // Updated: per-wallet numbering (different wallet)
655+
value: 'Ledger Account 1', // Updated: per-wallet numbering (different wallet)
656656
lastUpdatedAt: expect.any(Number),
657657
},
658658
},
659659
[expectedSnapWalletIdGroup]: {
660660
name: {
661-
value: 'Account 1', // Updated: per-wallet numbering (different wallet)
661+
value: 'Snap Account 1', // Updated: per-wallet numbering (different wallet)
662662
lastUpdatedAt: expect.any(Number),
663663
},
664664
},
@@ -2902,9 +2902,9 @@ describe('AccountTreeController', () => {
29022902
// Critical assertion: should have 2 unique names (no duplicates)
29032903
expect(uniqueNames.size).toBe(2);
29042904

2905-
// Due to optimization, names start at wallet.length, so we get "Account 1" and "Account 2"
2906-
expect(allNames).toContain('Account 1');
2907-
expect(allNames).toContain('Account 2');
2905+
// Due to optimization, names start at wallet.length, so we get "Account 3" and "Account 4"
2906+
expect(allNames).toContain('Ledger Account 1');
2907+
expect(allNames).toContain('Ledger Account 2');
29082908

29092909
// Verify they're actually different
29102910
expect(group1.metadata.name).not.toBe(group2.metadata.name);
@@ -4033,4 +4033,219 @@ describe('AccountTreeController', () => {
40334033
).toBe('Conflict Name (2)');
40344034
});
40354035
});
4036+
4037+
describe('naming', () => {
4038+
const mockAccount1 = {
4039+
...MOCK_HARDWARE_ACCOUNT_1,
4040+
id: 'mock-id-1',
4041+
address: '0x123',
4042+
};
4043+
const mockAccount2 = {
4044+
...MOCK_HARDWARE_ACCOUNT_1,
4045+
id: 'mock-id-2',
4046+
address: '0x456',
4047+
};
4048+
const mockAccount3 = {
4049+
...MOCK_HARDWARE_ACCOUNT_1,
4050+
id: 'mock-id-3',
4051+
address: '0x789',
4052+
};
4053+
const mockAccount4 = {
4054+
...MOCK_HARDWARE_ACCOUNT_1,
4055+
id: 'mock-id-4',
4056+
address: '0xabc',
4057+
};
4058+
4059+
const mockWalletId = toAccountWalletId(
4060+
AccountWalletType.Keyring,
4061+
KeyringTypes.ledger,
4062+
);
4063+
4064+
const getAccountGroupFromAccount = (
4065+
controller: AccountTreeController,
4066+
mockAccount: InternalAccount,
4067+
) => {
4068+
const groupId = toAccountGroupId(mockWalletId, mockAccount.address);
4069+
return controller.state.accountTree.wallets[mockWalletId].groups[groupId];
4070+
};
4071+
4072+
it('names non-HD keyrings accounts properly', () => {
4073+
const { controller, messenger } = setup();
4074+
4075+
// Add all 3 accounts.
4076+
[mockAccount1, mockAccount2, mockAccount3].forEach(
4077+
(mockAccount, index) => {
4078+
messenger.publish('AccountsController:accountAdded', mockAccount);
4079+
4080+
const mockGroup = getAccountGroupFromAccount(controller, mockAccount);
4081+
expect(mockGroup).toBeDefined();
4082+
expect(mockGroup.metadata.name).toBe(`Ledger Account ${index + 1}`);
4083+
},
4084+
);
4085+
4086+
// Remove account 2, should still create account 4 afterward.
4087+
messenger.publish('AccountsController:accountRemoved', mockAccount2.id);
4088+
4089+
expect(
4090+
getAccountGroupFromAccount(controller, mockAccount4),
4091+
).toBeUndefined();
4092+
messenger.publish('AccountsController:accountAdded', mockAccount4);
4093+
4094+
const mockGroup4 = getAccountGroupFromAccount(controller, mockAccount4);
4095+
expect(mockGroup4).toBeDefined();
4096+
expect(mockGroup4.metadata.name).toBe('Ledger Account 4');
4097+
4098+
// Now, removing account 3 and 4, should defaults to an index of "2" (since only
4099+
// account 1 remains), thus, re-inserting account 2, should be named "* Account 2".
4100+
messenger.publish('AccountsController:accountRemoved', mockAccount4.id);
4101+
messenger.publish('AccountsController:accountRemoved', mockAccount3.id);
4102+
4103+
expect(
4104+
getAccountGroupFromAccount(controller, mockAccount2),
4105+
).toBeUndefined();
4106+
messenger.publish('AccountsController:accountAdded', mockAccount2);
4107+
4108+
const mockGroup2 = getAccountGroupFromAccount(controller, mockAccount2);
4109+
expect(mockGroup2).toBeDefined();
4110+
expect(mockGroup2.metadata.name).toBe('Ledger Account 2');
4111+
});
4112+
4113+
it('ignores bad account group name pattern and fallback to natural indexing', () => {
4114+
const { controller, messenger } = setup({
4115+
accounts: [mockAccount1],
4116+
});
4117+
4118+
controller.init();
4119+
4120+
const mockGroup1 = getAccountGroupFromAccount(controller, mockAccount1);
4121+
expect(mockGroup1).toBeDefined();
4122+
4123+
const mockIndex = 90;
4124+
controller.setAccountGroupName(
4125+
mockGroup1.id,
4126+
`Account${mockIndex}`, // No space, so this should fallback to natural indexing
4127+
);
4128+
4129+
// The first account has a non-matching pattern, thus we should fallback to the next
4130+
// natural index.
4131+
messenger.publish('AccountsController:accountAdded', mockAccount2);
4132+
const mockGroup2 = getAccountGroupFromAccount(controller, mockAccount2);
4133+
expect(mockGroup2).toBeDefined();
4134+
expect(mockGroup2.metadata.name).toBe(`Ledger Account 2`); // Natural indexing.
4135+
});
4136+
4137+
it.each([
4138+
['Account', 'account'],
4139+
['Account', 'aCCount'],
4140+
['Account', 'accOunT'],
4141+
[' ', ' '],
4142+
[' ', '\t'],
4143+
[' ', ' \t'],
4144+
[' ', '\t '],
4145+
])(
4146+
'ignores case (case-insensitive) and spaces when extracting highest index: "$0" -> "$1"',
4147+
(toReplace, replaced) => {
4148+
const { controller, messenger } = setup({
4149+
accounts: [mockAccount1],
4150+
});
4151+
4152+
controller.init();
4153+
4154+
const mockGroup1 = getAccountGroupFromAccount(controller, mockAccount1);
4155+
expect(mockGroup1).toBeDefined();
4156+
4157+
const mockIndex = 90;
4158+
controller.setAccountGroupName(
4159+
mockGroup1.id,
4160+
mockGroup1.metadata.name
4161+
.replace(toReplace, replaced)
4162+
.replace('1', `${mockIndex}`), // Use index different than 1.
4163+
);
4164+
4165+
// Even if the account is not strictly named "Ledger Account 90", we should be able
4166+
// to compute the next index from there.
4167+
messenger.publish('AccountsController:accountAdded', mockAccount2);
4168+
const mockGroup2 = getAccountGroupFromAccount(controller, mockAccount2);
4169+
expect(mockGroup2).toBeDefined();
4170+
expect(mockGroup2.metadata.name).toBe(
4171+
`Ledger Account ${mockIndex + 1}`,
4172+
);
4173+
},
4174+
);
4175+
4176+
it.each([' ', ' ', '\t', ' \t'])(
4177+
'extract name indexes and ignore multiple spaces: "%s"',
4178+
(space) => {
4179+
const { controller, messenger } = setup({
4180+
accounts: [mockAccount1],
4181+
});
4182+
4183+
controller.init();
4184+
4185+
const mockGroup1 = getAccountGroupFromAccount(controller, mockAccount1);
4186+
expect(mockGroup1).toBeDefined();
4187+
4188+
const mockIndex = 90;
4189+
controller.setAccountGroupName(
4190+
mockGroup1.id,
4191+
mockGroup1.metadata.name
4192+
.replace(' ', space)
4193+
.replace('1', `${mockIndex}`), // Use index different than 1.
4194+
);
4195+
4196+
// Even if the account is not strictly named "Ledger Account 90", we should be able
4197+
// to compute the next index from there.
4198+
messenger.publish('AccountsController:accountAdded', mockAccount2);
4199+
const mockGroup2 = getAccountGroupFromAccount(controller, mockAccount2);
4200+
expect(mockGroup2).toBeDefined();
4201+
expect(mockGroup2.metadata.name).toBe(
4202+
`Ledger Account ${mockIndex + 1}`,
4203+
);
4204+
},
4205+
);
4206+
4207+
it('uses natural indexing for pre-existing accounts', () => {
4208+
const { controller } = setup({
4209+
accounts: [mockAccount1, mockAccount2, mockAccount3],
4210+
});
4211+
4212+
controller.init();
4213+
4214+
// After initializing the controller, all accounts should be named appropriately.
4215+
[mockAccount1, mockAccount2, mockAccount3].forEach(
4216+
(mockAccount, index) => {
4217+
const mockGroup = getAccountGroupFromAccount(controller, mockAccount);
4218+
expect(mockGroup).toBeDefined();
4219+
expect(mockGroup.metadata.name).toBe(`Ledger Account ${index + 1}`);
4220+
},
4221+
);
4222+
});
4223+
4224+
it('fallbacks to natural indexing if group names are not using our default name pattern', () => {
4225+
const { controller, messenger } = setup();
4226+
4227+
[mockAccount1, mockAccount2, mockAccount3].forEach((mockAccount) =>
4228+
messenger.publish('AccountsController:accountAdded', mockAccount),
4229+
);
4230+
4231+
const mockGroup1 = getAccountGroupFromAccount(controller, mockAccount1);
4232+
const mockGroup2 = getAccountGroupFromAccount(controller, mockAccount2);
4233+
const mockGroup3 = getAccountGroupFromAccount(controller, mockAccount3);
4234+
expect(mockGroup1).toBeDefined();
4235+
expect(mockGroup2).toBeDefined();
4236+
expect(mockGroup3).toBeDefined();
4237+
4238+
// Rename all accounts to something different than "* Account <index>".
4239+
controller.setAccountGroupName(mockGroup1.id, 'Account A');
4240+
controller.setAccountGroupName(mockGroup2.id, 'The next account');
4241+
controller.setAccountGroupName(mockGroup3.id, 'Best account so far');
4242+
4243+
// Adding a new account should not reset back to "Account 1", but it should
4244+
// use the next natural index, here, "Account 4".
4245+
messenger.publish('AccountsController:accountAdded', mockAccount4);
4246+
const mockGroup4 = getAccountGroupFromAccount(controller, mockAccount4);
4247+
expect(mockGroup4).toBeDefined();
4248+
expect(mockGroup4.metadata.name).toBe('Ledger Account 4');
4249+
});
4250+
});
40364251
});

0 commit comments

Comments
 (0)