diff --git a/packages/account-tree-controller/src/rules/keyring.ts b/packages/account-tree-controller/src/rules/keyring.ts index 881e8d86d74..4d83d44934e 100644 --- a/packages/account-tree-controller/src/rules/keyring.ts +++ b/packages/account-tree-controller/src/rules/keyring.ts @@ -43,6 +43,9 @@ export function getAccountWalletNameFromKeyringType(type: KeyringTypes) { case KeyringTypes.snap: { return 'Snap Wallet'; } + case KeyringTypes.mpc: { + return 'MPC Wallet'; + } // ------------------------------------------------------------------------ default: { return 'Unknown'; @@ -84,6 +87,9 @@ export function getAccountGroupPrefixFromKeyringType(type: KeyringTypes) { case KeyringTypes.snap: { return 'Snap Account'; } + case KeyringTypes.mpc: { + return 'MPC Account'; + } // ------------------------------------------------------------------------ default: { return 'Unknown Account'; @@ -99,6 +105,22 @@ export class KeyringRule readonly groupType = AccountGroupType.SingleAccount; + #findKeyringIdForAccount( + keyringType: string, + address: string, + ): string | undefined { + const { keyrings } = this.messenger.call('KeyringController:getState'); + const matchingKeyrings = keyrings.filter((k) => k.type === keyringType); + for (const keyring of matchingKeyrings) { + if ( + keyring.accounts.some((a) => a.toLowerCase() === address.toLowerCase()) + ) { + return keyring.metadata?.id; + } + } + return undefined; + } + match( account: InternalAccount, // No `| undefined` return type for this rule, as it cannot fail. @@ -106,7 +128,14 @@ export class KeyringRule // We assume that `type` is really a `KeyringTypes`. const keyringType = account.metadata.keyring.type as KeyringTypes; - const walletId = toAccountWalletId(this.walletType, keyringType); + // For MPC keyrings, we store the keyring ID in metadata and create a separate wallet per keyring instance + const keyringId = + keyringType === KeyringTypes.mpc + ? this.#findKeyringIdForAccount(keyringType, account.address) + : undefined; + + const walletSubId = keyringId ? `${keyringType}/${keyringId}` : keyringType; + const walletId = toAccountWalletId(this.walletType, walletSubId); const groupId = toAccountGroupId(walletId, account.address); return { @@ -116,6 +145,7 @@ export class KeyringRule metadata: { keyring: { type: keyringType, + ...(keyringId && { id: keyringId }), }, }, }, @@ -134,6 +164,17 @@ export class KeyringRule getDefaultAccountWalletName( wallet: AccountWalletObjectOf, ): string { + if ( + wallet.metadata.keyring.type === KeyringTypes.mpc && + wallet.metadata.keyring.id + ) { + const { keyrings } = this.messenger.call('KeyringController:getState'); + const mpcKeyrings = keyrings.filter((k) => k.type === KeyringTypes.mpc); + const index = mpcKeyrings.findIndex( + (k) => k.metadata?.id === wallet.metadata.keyring.id, + ); + return `MPC Wallet ${index === -1 ? '' : index + 1}`.trim(); + } return getAccountWalletNameFromKeyringType(wallet.metadata.keyring.type); } diff --git a/packages/account-tree-controller/src/wallet.ts b/packages/account-tree-controller/src/wallet.ts index fdfc46270f5..b1b8658c3d7 100644 --- a/packages/account-tree-controller/src/wallet.ts +++ b/packages/account-tree-controller/src/wallet.ts @@ -100,6 +100,8 @@ export type AccountWalletKeyringObject = { metadata: AccountTreeWalletMetadata & { keyring: { type: KeyringTypes; + /** Keyring instance ID, used for keyrings that can have multiple instances (e.g., MPC) */ + id?: string; }; }; }; diff --git a/packages/accounts-controller/src/utils.ts b/packages/accounts-controller/src/utils.ts index 5620031d37b..9e40f15adca 100644 --- a/packages/accounts-controller/src/utils.ts +++ b/packages/accounts-controller/src/utils.ts @@ -42,6 +42,9 @@ export function keyringTypeToName(keyringType: string): string { case KeyringTypes.snap: { return 'Snap Account'; } + case KeyringTypes.mpc: { + return 'MPC'; + } default: { throw new Error(`Unknown keyring ${keyringType}`); } diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index add11e8183b..ef97ec0c4be 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -61,6 +61,7 @@ export enum KeyringTypes { ledger = 'Ledger Hardware', lattice = 'Lattice Hardware', snap = 'Snap Keyring', + mpc = 'MPC Keyring', /* eslint-enable @typescript-eslint/naming-convention */ }