Skip to content

fix: throw error when creating HD keyring with duplicate account #5675

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

PatrykLucka
Copy link
Contributor

@PatrykLucka PatrykLucka commented Apr 18, 2025

Explanation

Currently, we are not checking whether the HD keyring account being added by importing SRP is a duplicate. This creates the possibility of adding the same account first to the simple keyring and then again with import HD keyring.

This PR adds a duplicate account check when adding an HD keyring, preventing this situation.

References

Changelog

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@PatrykLucka PatrykLucka self-assigned this Apr 18, 2025
@PatrykLucka PatrykLucka requested a review from a team as a code owner April 18, 2025 11:30
@PatrykLucka PatrykLucka force-pushed the fix-duplicate-hd-keyring-account branch from f289dfe to 3d7a30d Compare April 18, 2025 11:38
@PatrykLucka
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "27.0.0-preview-3d7a30d",
  "@metamask-previews/address-book-controller": "6.0.3-preview-3d7a30d",
  "@metamask-previews/announcement-controller": "7.0.3-preview-3d7a30d",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-3d7a30d",
  "@metamask-previews/approval-controller": "7.1.3-preview-3d7a30d",
  "@metamask-previews/assets-controllers": "58.0.0-preview-3d7a30d",
  "@metamask-previews/base-controller": "8.0.0-preview-3d7a30d",
  "@metamask-previews/bridge-controller": "16.0.0-preview-3d7a30d",
  "@metamask-previews/bridge-status-controller": "13.1.0-preview-3d7a30d",
  "@metamask-previews/build-utils": "3.0.3-preview-3d7a30d",
  "@metamask-previews/chain-agnostic-permission": "0.4.0-preview-3d7a30d",
  "@metamask-previews/composable-controller": "11.0.0-preview-3d7a30d",
  "@metamask-previews/controller-utils": "11.7.0-preview-3d7a30d",
  "@metamask-previews/delegation-controller": "0.1.0-preview-3d7a30d",
  "@metamask-previews/earn-controller": "0.12.0-preview-3d7a30d",
  "@metamask-previews/eip1193-permission-middleware": "0.1.0-preview-3d7a30d",
  "@metamask-previews/ens-controller": "16.0.0-preview-3d7a30d",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-3d7a30d",
  "@metamask-previews/gas-fee-controller": "23.0.0-preview-3d7a30d",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-3d7a30d",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-3d7a30d",
  "@metamask-previews/keyring-controller": "21.0.3-preview-3d7a30d",
  "@metamask-previews/logging-controller": "6.0.4-preview-3d7a30d",
  "@metamask-previews/message-manager": "12.0.1-preview-3d7a30d",
  "@metamask-previews/multichain": "4.0.0-preview-3d7a30d",
  "@metamask-previews/multichain-api-middleware": "0.2.0-preview-3d7a30d",
  "@metamask-previews/multichain-network-controller": "0.5.0-preview-3d7a30d",
  "@metamask-previews/multichain-transactions-controller": "0.9.0-preview-3d7a30d",
  "@metamask-previews/name-controller": "8.0.3-preview-3d7a30d",
  "@metamask-previews/network-controller": "23.2.0-preview-3d7a30d",
  "@metamask-previews/notification-services-controller": "6.0.0-preview-3d7a30d",
  "@metamask-previews/permission-controller": "11.0.6-preview-3d7a30d",
  "@metamask-previews/permission-log-controller": "3.0.3-preview-3d7a30d",
  "@metamask-previews/phishing-controller": "12.4.1-preview-3d7a30d",
  "@metamask-previews/polling-controller": "13.0.0-preview-3d7a30d",
  "@metamask-previews/preferences-controller": "17.0.0-preview-3d7a30d",
  "@metamask-previews/profile-sync-controller": "12.0.0-preview-3d7a30d",
  "@metamask-previews/queued-request-controller": "10.0.0-preview-3d7a30d",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-3d7a30d",
  "@metamask-previews/remote-feature-flag-controller": "1.6.0-preview-3d7a30d",
  "@metamask-previews/sample-controllers": "0.1.0-preview-3d7a30d",
  "@metamask-previews/selected-network-controller": "22.0.0-preview-3d7a30d",
  "@metamask-previews/signature-controller": "27.1.0-preview-3d7a30d",
  "@metamask-previews/token-search-discovery-controller": "3.1.0-preview-3d7a30d",
  "@metamask-previews/transaction-controller": "54.1.0-preview-3d7a30d",
  "@metamask-previews/user-operation-controller": "33.0.0-preview-3d7a30d"
}

@PatrykLucka PatrykLucka changed the title fix: throw error when adding duplicate hd keyring account fix: throw error when creating hd keyring with duplicate account Apr 18, 2025
@PatrykLucka PatrykLucka requested a review from a team as a code owner April 18, 2025 13:09
@@ -2587,7 +2587,8 @@ export class KeyringController extends BaseController<
const accounts = await this.#getAccountsFromKeyrings();

switch (type) {
case KeyringTypes.simple: {
case KeyringTypes.simple:
case KeyringTypes.hd: {
Copy link
Contributor

@ccharly ccharly Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record:

  • Given that we only check for duplicates when creating new keyrings, this shouldn't impact the "manual" HD keyring account creation.
  • For multi-SRP, this will prevent duplicated addresses when importing a new SRP (and thus, creating a new HD keyring).

@ccharly ccharly changed the title fix: throw error when creating hd keyring with duplicate account fix: throw error when creating HD keyring with duplicate account Apr 18, 2025
@@ -2587,7 +2587,8 @@ export class KeyringController extends BaseController<
const accounts = await this.#getAccountsFromKeyrings();

switch (type) {
case KeyringTypes.simple: {
case KeyringTypes.simple:
case KeyringTypes.hd: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are leaving account additions unchecked, which may be also problematic because if a user:

  • Imports an SRP
  • Imports a private key, part of the above SRP
  • Adds an account until reaching the index of the imported private key

User will endup in the same faulty situation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I'm flagging this is that it is the fastest way to brick the wallet: #5701

Copy link
Member

@mikesposito mikesposito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can mitigate some issues for HD specifically. Is there a reason we are addressing those specifically instead of the account duplication for all types? I feel like we should find a solution that can work for all keyrings or we won't be able to really fix the underlying issues (i.e. hardware wallet accounts, snaps)

@mikesposito
Copy link
Member

I opened an alternative PR that could avoid different cases of duplicates and fix users that are currently locked out: #5710

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyringController does not prevent duplicate accounts properly
3 participants