Skip to content

Conversation

ccharly
Copy link
Contributor

@ccharly ccharly commented Sep 24, 2025

Explanation

I initially thought calling init implicitly would solve some issues, but it actually adds one when it comes to automatically migrating accounts from v1 state.

In the clients we usually use the tree like so:

this.accountsController.updateAccounts();
this.accountTreeController.init();

The updateAccounts part re-creates the list of internal accounts and potentially migrate them if needed (in the case of BIP-44, this is needed to inject the options.entropy for those accounts).

If a KeyingController:stateChange happens before updateAccounts, this might trigger some :accountAdded event, but some accounts might not have been automatically migrated yet (since updateAccounts has not been called at this point). Thus, this can lead the AccountTreeController to misuse those accounts and put them in wrong wallet (like older HD accounts had not options.entropy objects, thus they are not considered BIP-44 accounts yet).

To avoid such issues (which seems to mainly happen for E2E), we wait for init to have been called explicitly.

In anyway, calling updateAccounts right before init would make sure that the list of accounts is up-to-date AND migrated.

References

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

@ccharly ccharly force-pushed the fix/account-tree-controller-prevent-account-added branch from 391c05b to 5077f36 Compare September 24, 2025 20:51
@ccharly
Copy link
Contributor Author

ccharly commented Sep 24, 2025

@metamaskbot publish-preview

@ccharly
Copy link
Contributor Author

ccharly commented Sep 24, 2025

@metamaskbot publish-preview

@ccharly ccharly marked this pull request as ready for review September 24, 2025 21:58
@ccharly ccharly requested review from a team as code owners September 24, 2025 21:58
cursor[bot]

This comment was marked as outdated.

@ccharly ccharly force-pushed the fix/account-tree-controller-prevent-account-added branch from 054c4dd to 33a5889 Compare September 25, 2025 07:03
@ccharly ccharly enabled auto-merge (squash) September 25, 2025 07:23

### Fixed

- Prevent `:account{Added,Removed}` to used if `init` has not been called yet ([#6717](https://github.com/MetaMask/core/pull/6717))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm writing this comment last, and writing the other comments before made me realize that this might be a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could be seen as a breaking change in terms of behavior, but I actually think the current behavior is not valid either 😅

Also, "we" are the consumer the AccountsController and clients don't really have control over those :account{Added,Removed} event strictly speaking. I think it's implied that init has to be called before being able to use the tree anyway.

Lastly, making this a "true breaking change" would quickly bubble up to other controllers, while the public interface remains the same.

But I get your point and I'm on a fence here 😅 for simplicity, I'd would just treat it as a fix (given it does not require any change as of now from any consumers).

Would that be ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with all your points! Thanks for elaborating ❤️ Yep that'd be alright with me!

ccharly and others added 2 commits September 25, 2025 09:50
Co-authored-by: Mathieu Artu <mathieu.artu@consensys.net>
@ccharly ccharly merged commit 0f63355 into main Sep 25, 2025
239 checks passed
@ccharly ccharly deleted the fix/account-tree-controller-prevent-account-added branch September 25, 2025 08:07
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.

2 participants