@@ -7,21 +7,36 @@ import type {
77 Bip44Account ,
88 AccountProvider ,
99} from '@metamask/account-api' ;
10+ import { BaseController } from '@metamask/base-controller' ;
1011import type { EntropySourceId , KeyringAccount } from '@metamask/keyring-api' ;
1112import { KeyringTypes } from '@metamask/keyring-controller' ;
1213
1314import type { MultichainAccountGroup } from './MultichainAccountGroup' ;
15+ import type { MultichainAccountWalletTransientState } from './MultichainAccountWallet' ;
1416import { MultichainAccountWallet } from './MultichainAccountWallet' ;
1517import {
1618 AccountProviderWrapper ,
1719 isAccountProviderWrapper ,
1820} from './providers/AccountProviderWrapper' ;
1921import { EvmAccountProvider } from './providers/EvmAccountProvider' ;
2022import { SolAccountProvider } from './providers/SolAccountProvider' ;
21- import type { MultichainAccountServiceMessenger } from './types' ;
23+ import type { TransientStateMetadata } from './transient-state' ;
24+ import type {
25+ MultichainAccountServiceMessenger ,
26+ MultichainAccountServiceTransientState ,
27+ } from './types' ;
2228
2329export const serviceName = 'MultichainAccountService' ;
2430
31+ const serviceMetadata : TransientStateMetadata < MultichainAccountServiceTransientState > =
32+ {
33+ accountWalletsTransientState : {
34+ // Transient states are never persisted.
35+ persist : false ,
36+ anonymous : false ,
37+ } ,
38+ } ;
39+
2540/**
2641 * The options that {@link MultichainAccountService} takes.
2742 */
@@ -39,7 +54,11 @@ type AccountContext<Account extends Bip44Account<KeyringAccount>> = {
3954/**
4055 * Service to expose multichain accounts capabilities.
4156 */
42- export class MultichainAccountService {
57+ export class MultichainAccountService extends BaseController <
58+ typeof serviceName ,
59+ MultichainAccountServiceTransientState ,
60+ MultichainAccountServiceMessenger
61+ > {
4362 readonly #messenger: MultichainAccountServiceMessenger ;
4463
4564 readonly #providers: AccountProvider < Bip44Account < KeyringAccount > > [ ] ;
@@ -69,6 +88,15 @@ export class MultichainAccountService {
6988 * providers.
7089 */
7190 constructor ( { messenger, providers = [ ] } : MultichainAccountServiceOptions ) {
91+ super ( {
92+ messenger,
93+ name : serviceName ,
94+ metadata : serviceMetadata ,
95+ state : {
96+ accountWalletsTransientState : { } ,
97+ } ,
98+ } ) ;
99+
72100 this . #messenger = messenger ;
73101 this . #wallets = new Map ( ) ;
74102 this . #accountIdToContext = new Map ( ) ;
@@ -150,9 +178,12 @@ export class MultichainAccountService {
150178
151179 // This will automatically "associate" all multichain accounts for that wallet
152180 // (based on the accounts owned by each account providers).
181+ const walletId = toMultichainAccountWalletId ( entropySource ) ;
153182 const wallet = new MultichainAccountWallet ( {
154183 entropySource,
155184 providers : this . #providers,
185+ transientState :
186+ this . #getMultichainAccoultWalletTransientStateProxy( walletId ) ,
156187 } ) ;
157188 this . #wallets. set ( wallet . id , wallet ) ;
158189
@@ -169,6 +200,54 @@ export class MultichainAccountService {
169200 }
170201 }
171202
203+ #getMultichainAccoultWalletTransientStateProxy(
204+ walletId : MultichainAccountWalletId ,
205+ ) : MultichainAccountWalletTransientState {
206+ // eslint-disable-next-line consistent-this, @typescript-eslint/no-this-alias
207+ const service = this ;
208+
209+ const getTransientState = < Return > (
210+ state : MultichainAccountServiceTransientState ,
211+ then : ( transientState : MultichainAccountWalletTransientState ) => Return ,
212+ ) => {
213+ return then ( state . accountWalletsTransientState [ walletId ] ) ;
214+ } ;
215+
216+ const setTransientState = (
217+ state : MultichainAccountServiceTransientState ,
218+ then : ( transientState : MultichainAccountWalletTransientState ) => void ,
219+ ) => {
220+ then ( state . accountWalletsTransientState [ walletId ] ) ;
221+ } ;
222+
223+ // Create initial transient state for this wallet if it does not exists yet.
224+ if ( ! service . state . accountWalletsTransientState [ walletId ] ) {
225+ this . update ( ( state ) => {
226+ state . accountWalletsTransientState [ walletId ] = {
227+ isAlignmentInProgress : false ,
228+ } ;
229+ } ) ;
230+ }
231+
232+ return {
233+ get isAlignmentInProgress ( ) : boolean {
234+ return getTransientState (
235+ service . state ,
236+ ( transientState ) => transientState . isAlignmentInProgress ,
237+ ) ;
238+ } ,
239+ set isAlignmentInProgress ( inProgress : boolean ) {
240+ service . update ( ( state ) => {
241+ setTransientState (
242+ state ,
243+ ( transientState ) =>
244+ ( transientState . isAlignmentInProgress = inProgress ) ,
245+ ) ;
246+ } ) ;
247+ } ,
248+ } ;
249+ }
250+
172251 #handleOnAccountAdded( account : KeyringAccount ) : void {
173252 // We completely omit non-BIP-44 accounts!
174253 if ( ! isBip44Account ( account ) ) {
@@ -177,6 +256,7 @@ export class MultichainAccountService {
177256
178257 let sync = true ;
179258
259+ const walletId = toMultichainAccountWalletId ( account . options . entropy . id ) ;
180260 let wallet = this . #wallets. get (
181261 toMultichainAccountWalletId ( account . options . entropy . id ) ,
182262 ) ;
@@ -185,6 +265,8 @@ export class MultichainAccountService {
185265 wallet = new MultichainAccountWallet ( {
186266 entropySource : account . options . entropy . id ,
187267 providers : this . #providers,
268+ transientState :
269+ this . #getMultichainAccoultWalletTransientStateProxy( walletId ) ,
188270 } ) ;
189271 this . #wallets. set ( wallet . id , wallet ) ;
190272
0 commit comments