Skip to content
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
43 changes: 42 additions & 1 deletion packages/account-tree-controller/src/rules/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function getAccountWalletNameFromKeyringType(type: KeyringTypes) {
case KeyringTypes.snap: {
return 'Snap Wallet';
}
case KeyringTypes.mpc: {
return 'MPC Wallet';
}
// ------------------------------------------------------------------------
default: {
return 'Unknown';
Expand Down Expand Up @@ -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';
Expand All @@ -99,14 +105,37 @@ 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.
): RuleResult<AccountWalletType.Keyring, AccountGroupType.SingleAccount> {
// 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 {
Expand All @@ -116,6 +145,7 @@ export class KeyringRule
metadata: {
keyring: {
type: keyringType,
...(keyringId && { id: keyringId }),
},
},
},
Expand All @@ -134,6 +164,17 @@ export class KeyringRule
getDefaultAccountWalletName(
wallet: AccountWalletObjectOf<AccountWalletType.Keyring>,
): 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);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/account-tree-controller/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
};
Expand Down
3 changes: 3 additions & 0 deletions packages/accounts-controller/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
1 change: 1 addition & 0 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

Expand Down
Loading