@@ -8,7 +8,10 @@ import {
8
8
type MultichainAccount ,
9
9
} from '@metamask/account-api' ;
10
10
import type { EntropySourceId } from '@metamask/keyring-api' ;
11
- import type { KeyringObject } from '@metamask/keyring-controller' ;
11
+ import type {
12
+ KeyringControllerState ,
13
+ KeyringObject ,
14
+ } from '@metamask/keyring-controller' ;
12
15
import { KeyringTypes } from '@metamask/keyring-controller' ;
13
16
import type { InternalAccount } from '@metamask/keyring-internal-api' ;
14
17
@@ -23,6 +26,16 @@ type MultichainAccountServiceOptions = {
23
26
messenger : MultichainAccountServiceMessenger ;
24
27
} ;
25
28
29
+ /**
30
+ * Select keyrings from keyring controller state.
31
+ *
32
+ * @param state - The keyring controller state.
33
+ * @returns The keyrings.
34
+ */
35
+ function selectKeyringControllerKeyrings ( state : KeyringControllerState ) {
36
+ return state . keyrings ;
37
+ }
38
+
26
39
/**
27
40
* Service to expose multichain accounts capabilities.
28
41
*/
@@ -59,15 +72,16 @@ export class MultichainAccountService {
59
72
*/
60
73
init ( ) : void {
61
74
// Gather all entropy sources first.
62
- const { keyrings } = this . #messenger. call ( 'KeyringController:getState' ) ;
63
- this . #setMultichainAccountWallets( keyrings ) ;
64
-
65
- // TODO: For now, we to every `KeyringController` state change to detect when
66
- // new entropy sources/SRPs are being added. Having a dedicated event when
67
- // new keyrings are added would make this more efficient.
68
- this . #messenger. subscribe ( 'KeyringController:stateChange' , ( state ) => {
69
- this . #setMultichainAccountWallets( state . keyrings ) ;
70
- } ) ;
75
+ const state = this . #messenger. call ( 'KeyringController:getState' ) ;
76
+ this . #setMultichainAccountWallets( state . keyrings ) ;
77
+
78
+ this . #messenger. subscribe (
79
+ 'KeyringController:stateChange' ,
80
+ ( keyrings ) => {
81
+ this . #setMultichainAccountWallets( keyrings ) ;
82
+ } ,
83
+ selectKeyringControllerKeyrings ,
84
+ ) ;
71
85
}
72
86
73
87
#setMultichainAccountWallets( keyrings : KeyringObject [ ] ) {
0 commit comments