-
-
Notifications
You must be signed in to change notification settings - Fork 256
feat: add KeyringController:accountAdded event + use keyring events in AccountsController
#7328
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
3fe4986
b4700b2
537e318
e137a0b
1a2a27c
e6eaaa1
98c92c6
fe85881
cab3f12
fda91cd
f7f52ce
5fee36e
54d6e16
a5c5f5d
81198db
5009b0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -755,43 +755,47 @@ export class AccountsController extends BaseController< | |
| } | ||
|
|
||
| #handleOnKeyringAccountAdded(address: string, keyring: KeyringObject) { | ||
| let account: InternalAccount | undefined; | ||
|
|
||
| this.#update((state) => { | ||
| const { internalAccounts } = state; | ||
|
|
||
| account = this.#getInternalAccountFromAddressAndType(address, keyring); | ||
| if (account) { | ||
| // Re-compute the list of accounts everytime, so we can make sure new names | ||
| // are also considered. | ||
| const accounts = Object.values( | ||
| internalAccounts.accounts, | ||
| ) as InternalAccount[]; | ||
|
|
||
| // Get next account name available for this given keyring. | ||
| const name = this.getNextAvailableAccountName( | ||
| account.metadata.keyring.type, | ||
| accounts, | ||
| ); | ||
|
|
||
| // If it's the first account, we need to select it. | ||
| const lastSelected = | ||
| accounts.length === 0 ? this.#getLastSelectedIndex() : 0; | ||
| let account = this.#getInternalAccountFromAddressAndType(address, keyring); | ||
| if (account) { | ||
| // Re-compute the list of accounts everytime, so we can make sure new names | ||
| // are also considered. | ||
| const accounts = Object.values(this.state.internalAccounts.accounts); | ||
|
|
||
| // Get next account name available for this given keyring. | ||
| const name = this.getNextAvailableAccountName( | ||
| account.metadata.keyring.type, | ||
| accounts, | ||
| ); | ||
|
|
||
| internalAccounts.accounts[account.id] = { | ||
| ...account, | ||
| metadata: { | ||
| ...account.metadata, | ||
| name, | ||
| importTime: Date.now(), | ||
| lastSelected, | ||
| }, | ||
| }; | ||
| } | ||
| }); | ||
| // If it's the first account, we need to select it. | ||
| const lastSelected = | ||
| accounts.length === 0 ? this.#getLastSelectedIndex() : 0; | ||
|
|
||
| // Update account metadata. | ||
| account = { | ||
| ...account, | ||
| metadata: { | ||
| ...account.metadata, | ||
| name, | ||
| importTime: Date.now(), | ||
| lastSelected, | ||
| }, | ||
| }; | ||
|
|
||
| if (account) { | ||
| this.messenger.publish('AccountsController:accountAdded', account); | ||
| // Not sure why, but the compiler still infers `account` as possibly undefined. | ||
| const internalAccount: InternalAccount = account; | ||
| this.#update( | ||
| (state) => { | ||
| state.internalAccounts.accounts[internalAccount.id] = internalAccount; | ||
| }, | ||
| () => { | ||
| // Will be published before `:selectedAccountChange` event is published. | ||
| this.messenger.publish( | ||
| 'AccountsController:accountAdded', | ||
| internalAccount, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -802,10 +806,10 @@ export class AccountsController extends BaseController< | |
|
|
||
| if (account) { | ||
| this.#update((state) => { | ||
| const { internalAccounts } = state; | ||
|
|
||
| delete internalAccounts.accounts[account.id]; | ||
| delete state.internalAccounts.accounts[account.id]; | ||
| }); | ||
|
|
||
| this.messenger.publish('AccountsController:accountRemoved', account.id); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Event ordering inconsistency when removing selected accountThe
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true for the asymmetry, but IMO this makes more sense to call
This way, we don't have any "dangling" account being wrongly selected (from a consumer point of view) |
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.