@@ -18,7 +18,11 @@ import type {
18
18
KeyringControllerUnlockEvent ,
19
19
KeyringControllerAddNewAccountAction ,
20
20
} from '@metamask/keyring-controller' ;
21
- import type { NetworkConfiguration } from '@metamask/network-controller' ;
21
+ import type {
22
+ NetworkConfiguration ,
23
+ NetworkController ,
24
+ NetworkControllerGetStateAction ,
25
+ } from '@metamask/network-controller' ;
22
26
import type { HandleSnapRequest } from '@metamask/snaps-controllers' ;
23
27
24
28
import { createSnapSignMessageRequest } from '../authentication/auth-snap-requests' ;
@@ -35,7 +39,10 @@ import {
35
39
mapInternalAccountToUserStorageAccount ,
36
40
} from './accounts/user-storage' ;
37
41
import { createSHA256Hash } from './encryption' ;
38
- import { startNetworkSyncing } from './network-syncing/controller-integration' ;
42
+ import {
43
+ performMainNetworkSync ,
44
+ startNetworkSyncing ,
45
+ } from './network-syncing/controller-integration' ;
39
46
import type {
40
47
UserStoragePathWithFeatureAndKey ,
41
48
UserStoragePathWithFeatureOnly ,
@@ -46,26 +53,32 @@ import {
46
53
upsertUserStorage ,
47
54
} from './services' ;
48
55
49
- // TODO: add external NetworkController event
50
- // Need to listen for when a network gets added
56
+ // TODO - replace shimmed interface with actual interfaces once merged
57
+ // Waiting on #4698
51
58
type NetworkControllerNetworkAddedEvent = {
52
59
type : 'NetworkController:networkAdded' ;
53
60
payload : [ networkConfiguration : NetworkConfiguration ] ;
54
61
} ;
55
-
56
- // TODO: add external NetworkController event
57
- // Need to listen for when a network is updated, or the default rpc/block explorer changes
58
- type NetworkControllerNetworkChangedEvent = {
59
- type : 'NetworkController:networkChanged' ;
62
+ type NetworkControllerNetworkUpdatedEvent = {
63
+ type : 'NetworkController:networkUpdated' ;
60
64
payload : [ networkConfiguration : NetworkConfiguration ] ;
61
65
} ;
62
-
63
- // TODO: add external NetworkController event
64
- // Need to listen for when a network gets deleted
65
- type NetworkControllerNetworkDeletedEvent = {
66
- type : 'NetworkController:networkDeleted' ;
66
+ type NetworkControllerNetworkRemovedEvent = {
67
+ type : 'NetworkController:networkRemoved' ;
67
68
payload : [ networkConfiguration : NetworkConfiguration ] ;
68
69
} ;
70
+ type NetworkControllerAddNetworkAction = {
71
+ type : 'NetworkController:addNetwork' ;
72
+ handler : NetworkController [ 'addNetwork' ] ;
73
+ } ;
74
+ type NetworkControllerUpdateNetworkAction = {
75
+ type : 'NetworkController:updateNetwork' ;
76
+ handler : NetworkController [ 'updateNetwork' ] ;
77
+ } ;
78
+ type NetworkControllerRemoveNetworkAction = {
79
+ type : 'NetworkController:removeNetwork' ;
80
+ handler : NetworkController [ 'removeNetwork' ] ;
81
+ } ;
69
82
70
83
// TODO: fix external dependencies
71
84
export declare type NotificationServicesControllerDisableNotificationServices =
@@ -173,10 +186,15 @@ export type AllowedActions =
173
186
// Metamask Notifications
174
187
| NotificationServicesControllerDisableNotificationServices
175
188
| NotificationServicesControllerSelectIsNotificationServicesEnabled
176
- // Account syncing
189
+ // Account Syncing
177
190
| AccountsControllerListAccountsAction
178
191
| AccountsControllerUpdateAccountMetadataAction
179
- | KeyringControllerAddNewAccountAction ;
192
+ | KeyringControllerAddNewAccountAction
193
+ // Network Syncing
194
+ | NetworkControllerGetStateAction
195
+ | NetworkControllerAddNetworkAction
196
+ | NetworkControllerUpdateNetworkAction
197
+ | NetworkControllerRemoveNetworkAction ;
180
198
181
199
// Messenger events
182
200
export type UserStorageControllerStateChangeEvent = ControllerStateChangeEvent <
@@ -207,8 +225,8 @@ export type AllowedEvents =
207
225
| AccountsControllerAccountRenamedEvent
208
226
// Network Syncing Events
209
227
| NetworkControllerNetworkAddedEvent
210
- | NetworkControllerNetworkChangedEvent
211
- | NetworkControllerNetworkDeletedEvent ;
228
+ | NetworkControllerNetworkUpdatedEvent
229
+ | NetworkControllerNetworkRemovedEvent ;
212
230
213
231
// Messenger
214
232
export type UserStorageControllerMessenger = RestrictedControllerMessenger <
@@ -232,6 +250,13 @@ export default class UserStorageController extends BaseController<
232
250
UserStorageControllerState ,
233
251
UserStorageControllerMessenger
234
252
> {
253
+ // This is replaced with the actual value in the constructor
254
+ // We will remove this once the feature will be released
255
+ #env = {
256
+ isAccountSyncingEnabled : false ,
257
+ isNetworkSyncingEnabled : false ,
258
+ } ;
259
+
235
260
#auth = {
236
261
getBearerToken : async ( ) => {
237
262
return await this . messagingSystem . call (
@@ -260,17 +285,12 @@ export default class UserStorageController extends BaseController<
260
285
} ;
261
286
262
287
#accounts = {
263
- // This is replaced with the actual value in the constructor
264
- // We will remove this once the feature will be released
265
- isAccountSyncingEnabled : false ,
266
288
isAccountSyncingInProgress : false ,
267
289
canSync : ( ) => {
268
290
try {
269
291
this . #assertProfileSyncingEnabled( ) ;
270
292
271
- return (
272
- this . #accounts. isAccountSyncingEnabled && this . #auth. isAuthEnabled ( )
273
- ) ;
293
+ return this . #env. isAccountSyncingEnabled && this . #auth. isAuthEnabled ( ) ;
274
294
} catch {
275
295
return false ;
276
296
}
@@ -406,9 +426,8 @@ export default class UserStorageController extends BaseController<
406
426
state : { ...defaultState , ...state } ,
407
427
} ) ;
408
428
409
- this . #accounts. isAccountSyncingEnabled = Boolean (
410
- env ?. isAccountSyncingEnabled ,
411
- ) ;
429
+ this . #env. isAccountSyncingEnabled = Boolean ( env ?. isAccountSyncingEnabled ) ;
430
+ this . #env. isNetworkSyncingEnabled = Boolean ( env ?. isNetworkSyncingEnabled ) ;
412
431
413
432
this . getMetaMetricsState = getMetaMetricsState ;
414
433
this . #keyringController. setupLockedStateSubscriptions ( ) ;
@@ -417,18 +436,10 @@ export default class UserStorageController extends BaseController<
417
436
this . #accounts. setupAccountSyncingSubscriptions ( ) ;
418
437
419
438
// Network Syncing
420
- if ( env ? .isNetworkSyncingEnabled ) {
439
+ if ( this . # env. isNetworkSyncingEnabled ) {
421
440
startNetworkSyncing ( {
422
441
messenger,
423
- getStorageConfig : async ( ) => {
424
- const { storageKey, bearerToken } =
425
- await this . #getStorageKeyAndBearerToken( ) ;
426
- return {
427
- storageKey,
428
- bearerToken,
429
- nativeScryptCrypto : this . #nativeScryptCrypto,
430
- } ;
431
- } ,
442
+ getStorageConfig : this . #getStorageOptions,
432
443
} ) ;
433
444
}
434
445
}
@@ -479,6 +490,20 @@ export default class UserStorageController extends BaseController<
479
490
) ;
480
491
}
481
492
493
+ async #getStorageOptions( ) {
494
+ if ( ! this . state . isProfileSyncingEnabled ) {
495
+ return null ;
496
+ }
497
+
498
+ const { storageKey, bearerToken } =
499
+ await this . #getStorageKeyAndBearerToken( ) ;
500
+ return {
501
+ storageKey,
502
+ bearerToken,
503
+ nativeScryptCrypto : this . #nativeScryptCrypto,
504
+ } ;
505
+ }
506
+
482
507
public async enableProfileSyncing ( ) : Promise < void > {
483
508
try {
484
509
this . #setIsProfileSyncingUpdateLoading( true ) ;
@@ -868,4 +893,15 @@ export default class UserStorageController extends BaseController<
868
893
) ;
869
894
}
870
895
}
896
+
897
+ async syncNetworks ( ) {
898
+ if ( ! this . #env. isNetworkSyncingEnabled ) {
899
+ return ;
900
+ }
901
+
902
+ await performMainNetworkSync ( {
903
+ messenger : this . messagingSystem ,
904
+ getStorageConfig : this . #getStorageOptions,
905
+ } ) ;
906
+ }
871
907
}
0 commit comments