Skip to content

fix: discard duplicate accounts on unlock #32621

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 5 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
1 change: 0 additions & 1 deletion app/scripts/controllers/mmi-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ describe('MMIController', function () {
state: {
keyrings: [],
isUnlocked: true,
keyringsMetadata: [],
},
encryptor: {
encrypt(_, object) {
Expand Down
50 changes: 20 additions & 30 deletions app/scripts/controllers/preferences-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,10 @@ describe('preferences controller', () => {
{
type: 'HD Key Tree',
accounts: [firstAddress, secondAddress],
},
],
keyringsMetadata: [
{
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
metadata: {
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
},
},
],
},
Expand Down Expand Up @@ -204,12 +202,10 @@ describe('preferences controller', () => {
{
type: 'HD Key Tree',
accounts: [firstAddress, secondAddress],
},
],
keyringsMetadata: [
{
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
metadata: {
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
},
},
],
},
Expand Down Expand Up @@ -261,12 +257,10 @@ describe('preferences controller', () => {
{
type: 'HD Key Tree',
accounts: [firstAddress, secondAddress],
},
],
keyringsMetadata: [
{
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
metadata: {
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
},
},
],
},
Expand Down Expand Up @@ -302,12 +296,10 @@ describe('preferences controller', () => {
{
type: 'HD Key Tree',
accounts: [firstAddress, secondAddress],
},
],
keyringsMetadata: [
{
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
metadata: {
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
},
},
],
},
Expand Down Expand Up @@ -512,12 +504,10 @@ describe('preferences controller', () => {
{
type: 'HD Key Tree',
accounts: [firstAddress, secondAddress],
},
],
keyringsMetadata: [
{
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
metadata: {
id: '01JKDGGBRE3DGZA7N1PZJSQK4W',
name: '',
},
},
],
},
Expand Down
21 changes: 11 additions & 10 deletions app/scripts/metamask-controller.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,19 @@ describe('MetaMaskController', function () {
const result2 = metamaskController.keyringController.state;

// On restore, a new keyring metadata is generated.
expect(result1.keyringsMetadata[0].id).toBe(mockULIDs[0]);
expect(result2).toStrictEqual(
expect.objectContaining({
...result1,
keyringsMetadata: [
{
expect(result1.keyrings[0].metadata.id).toBe(mockULIDs[0]);
expect(result2).toStrictEqual({
...result1,
keyrings: [
{
...result1.keyrings[0],
metadata: {
...result1.keyrings[0].metadata,
id: mockULIDs[1],
name: '',
},
],
}),
);
},
],
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7055,8 +7055,8 @@ export default class MetamaskController extends EventEmitter {
.map((keyring, index) => {
if (keyring.type === KeyringTypes.hd) {
return {
id: state.keyringsMetadata[index].id,
name: state.keyringsMetadata[index].name,
id: keyring.metadata.id,
name: keyring.metadata.name,
type: 'mnemonic',
primary: index === 0,
};
Expand Down
7 changes: 3 additions & 4 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3992,10 +3992,9 @@ describe('MetaMaskController', () => {
metamaskController.keyringController.state.keyrings;

const newlyAddedKeyringId =
metamaskController.keyringController.state.keyringsMetadata[
metamaskController.keyringController.state.keyringsMetadata.length -
2 // -1 for the snap keyring, -1 for the newly added keyring
].id;
metamaskController.keyringController.state.keyrings[
metamaskController.keyringController.state.keyrings.length - 2 // -1 for the snap keyring, -1 for the newly added keyring
].metadata.id;

const newSRP = Buffer.from(
await metamaskController.getSeedPhrase(password, newlyAddedKeyringId),
Expand Down
4 changes: 2 additions & 2 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1678,8 +1678,7 @@
},
"@metamask/keyring-controller": {
"globals": {
"console.error": true,
"console.log": true
"console.error": true
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
Expand All @@ -1691,6 +1690,7 @@
"@metamask/utils": true,
"@metamask/name-controller>async-mutex": true,
"@metamask/keyring-controller>ethereumjs-wallet": true,
"lodash": true,
"@metamask/keyring-controller>ulid": true
}
},
Expand Down
4 changes: 2 additions & 2 deletions lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1678,8 +1678,7 @@
},
"@metamask/keyring-controller": {
"globals": {
"console.error": true,
"console.log": true
"console.error": true
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
Expand All @@ -1691,6 +1690,7 @@
"@metamask/utils": true,
"@metamask/name-controller>async-mutex": true,
"@metamask/keyring-controller>ethereumjs-wallet": true,
"lodash": true,
"@metamask/keyring-controller>ulid": true
}
},
Expand Down
4 changes: 2 additions & 2 deletions lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1678,8 +1678,7 @@
},
"@metamask/keyring-controller": {
"globals": {
"console.error": true,
"console.log": true
"console.error": true
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
Expand All @@ -1691,6 +1690,7 @@
"@metamask/utils": true,
"@metamask/name-controller>async-mutex": true,
"@metamask/keyring-controller>ethereumjs-wallet": true,
"lodash": true,
"@metamask/keyring-controller>ulid": true
}
},
Expand Down
4 changes: 2 additions & 2 deletions lavamoat/browserify/mmi/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1809,8 +1809,7 @@
},
"@metamask/keyring-controller": {
"globals": {
"console.error": true,
"console.log": true
"console.error": true
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
Expand All @@ -1822,6 +1821,7 @@
"@metamask/utils": true,
"@metamask/name-controller>async-mutex": true,
"@metamask/keyring-controller>ethereumjs-wallet": true,
"lodash": true,
"@metamask/keyring-controller>ulid": true
}
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@
"gridplus-sdk/elliptic": "^6.6.1",
"@ethereumjs/tx": "patch:@ethereumjs/tx@npm%3A5.4.0#~/.yarn/patches/@ethereumjs-tx-npm-5.4.0-0c4a0f973e.patch",
"@keystonehq/metamask-airgapped-keyring": "patch:@keystonehq/metamask-airgapped-keyring@npm%3A0.15.2#~/.yarn/patches/@keystonehq-metamask-airgapped-keyring-npm-0.15.2-94dd4b40d7.patch",
"@trezor/connect-web": "~9.4.7"
"@trezor/connect-web": "~9.4.7",
"@metamask/keyring-controller": "npm:@metamask-previews/[email protected]",
"@metamask/accounts-controller": "npm:@metamask-previews/[email protected]"
},
"dependencies": {
"@babel/runtime": "patch:@babel/runtime@npm%3A7.25.9#~/.yarn/patches/@babel-runtime-npm-7.25.9-fe8c62510a.patch",
Expand Down
16 changes: 5 additions & 11 deletions shared/lib/keyring.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import {
KeyringObject,
KeyringMetadata,
KeyringTypes,
} from '@metamask/keyring-controller';
import { KeyringObject, KeyringTypes } from '@metamask/keyring-controller';
import { isEqualCaseInsensitive } from '../modules/string-utils';

// TODO: This kind of logic should be inside the `KeyringController` (using `KeyringSelector` query, or make `addNewKeyring` returns it keyring ID alongside

export function findKeyringId(
keyrings: KeyringObject[],
keyringsMetadata: KeyringMetadata[],
selector: { address?: string; type?: KeyringTypes },
): string {
const keyringIndex = keyrings.findIndex((keyring) => {
const keyring = keyrings.find((keyring) => {

Check failure on line 10 in shared/lib/keyring.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

'keyring' is already declared in the upper scope on line 10 column 9
if (selector.address && selector.type) {
return (
keyring.accounts.some((account) =>
Expand All @@ -31,17 +26,16 @@

throw new Error('Must provide either address or type selector');
});
if (keyringIndex === -1) {
if (!keyring) {
throw new Error('Could not find keyring with specified criteria');
}

return keyringsMetadata[keyringIndex].id;
return keyring.metadata.id;
}

export function findKeyringIdByAddress(
keyrings: KeyringObject[],
keyringsMetadata: KeyringMetadata[],
address: string,
): string {
return findKeyringId(keyrings, keyringsMetadata, { address });
return findKeyringId(keyrings, { address });
}
32 changes: 15 additions & 17 deletions test/data/mock-send-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,29 +262,27 @@
"accounts": [
"0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
"0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b"
]
],
"metadata": {
"id": "01JKAF3DSGM3AB87EM9N0K41AJ",
"name": ""
}
},
{
"type": "Ledger Hardware",
"accounts": ["0xc42edfcc21ed14dda456aa0756c153f7985d8813"]
"accounts": ["0xc42edfcc21ed14dda456aa0756c153f7985d8813"],
"metadata": {
"id": "01JKAF3KP7VPAG0YXEDTDRB6ZV",
"name": ""
}
},
{
"type": "Simple Key Pair",
"accounts": ["0xeb9e64b93097bc15f01f13eae97015c57ab64823"]
}
],
"keyringsMetadata": [
{
"id": "01JKAF3DSGM3AB87EM9N0K41AJ",
"name": ""
},
{
"id": "01JKAF3KP7VPAG0YXEDTDRB6ZV",
"name": ""
},
{
"id": "01JKAF3PJ247KAM6C03G5Q0NP8",
"name": ""
"accounts": ["0xeb9e64b93097bc15f01f13eae97015c57ab64823"],
"metadata": {
"id": "01JKAF3PJ247KAM6C03G5Q0NP8",
"name": ""
}
}
],
"identities": {
Expand Down
52 changes: 25 additions & 27 deletions test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -622,45 +622,43 @@
"accounts": [
"0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
"0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b"
]
],
"metadata": {
"id": "01JKAF3DSGM3AB87EM9N0K41AJ",
"name": ""
}
},
{
"type": "Ledger Hardware",
"accounts": ["0xc42edfcc21ed14dda456aa0756c153f7985d8813"]
"accounts": ["0xc42edfcc21ed14dda456aa0756c153f7985d8813"],
"metadata": {
"id": "01JKAF3KP7VPAG0YXEDTDRB6ZV",
"name": ""
}
},
{
"type": "Simple Key Pair",
"accounts": ["0xeb9e64b93097bc15f01f13eae97015c57ab64823"]
"accounts": ["0xeb9e64b93097bc15f01f13eae97015c57ab64823"],
"metadata": {
"id": "01JKAF3PJ247KAM6C03G5Q0NP8",
"name": ""
}
},
{
"type": "Snap Keyring",
"accounts": ["0xb552685e3d2790efd64a175b00d51f02cdafee5d"]
"accounts": ["0xb552685e3d2790efd64a175b00d51f02cdafee5d"],
"metadata": {
"id": "01JKAF3SSWRQK818R830GE8PHX",
"name": ""
}
},
{
"type": "Custody test",
"accounts": ["0xca8f1F0245530118D0cf14a06b01Daf8f76Cf281"]
}
],
"keyringsMetadata": [
{
"id": "01JKAF3DSGM3AB87EM9N0K41AJ",
"name": ""
},
{
"id": "01JKAF3KP7VPAG0YXEDTDRB6ZV",
"name": ""
},
{
"id": "01JKAF3PJ247KAM6C03G5Q0NP8",
"name": ""
},
{
"id": "01JKAF3SSWRQK818R830GE8PHX",
"name": ""
},
{
"id": "01JKAF3WYGWF7JX6PC4YYVSK2Q",
"name": ""
"accounts": ["0xca8f1F0245530118D0cf14a06b01Daf8f76Cf281"],
"metadata": {
"id": "01JKAF3WYGWF7JX6PC4YYVSK2Q",
"name": ""
}
}
],
"identities": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
"KeyringController": {
"isUnlocked": true,
"keyrings": "object",
"keyringsMetadata": "object",
"vault": "string",
"encryptionKey": "string",
"encryptionSalt": "string"
Expand Down
Loading
Loading