@@ -60,17 +60,20 @@ export class StorageController {
60
60
61
61
if ( passedMigrations . includes ( 'migrateNetworkPreferencesToNetworks' ) ) return
62
62
63
+ const storageUpdates = [
64
+ this . #storage. set ( 'passedMigrations' , [
65
+ ...new Set ( [ ...passedMigrations , 'migrateNetworkPreferencesToNetworks' ] )
66
+ ] )
67
+ ]
68
+
63
69
if ( ! Object . keys ( networks ) . length && networkPreferences ) {
64
70
const migratedNetworks = await migrateNetworkPreferencesToNetworks ( networkPreferences )
65
71
66
- await Promise . all ( [
67
- this . #storage. set ( 'passedMigrations' , [
68
- ...new Set ( [ ...passedMigrations , 'migrateNetworkPreferencesToNetworks' ] )
69
- ] ) ,
70
- this . #storage. set ( 'networks' , migratedNetworks ) ,
71
- this . #storage. remove ( 'networkPreferences' )
72
- ] )
72
+ storageUpdates . push ( this . #storage. set ( 'networks' , migratedNetworks ) )
73
+ storageUpdates . push ( this . #storage. remove ( 'networkPreferences' ) )
73
74
}
75
+
76
+ await Promise . all ( storageUpdates )
74
77
}
75
78
76
79
// As of version 4.25.0, a new Account interface has been introduced,
@@ -83,30 +86,32 @@ export class StorageController {
83
86
this . #storage. get ( 'accounts' , [ ] ) ,
84
87
this . #storage. get ( 'accountPreferences' )
85
88
] )
86
- if ( ! accountPreferences ) return
87
89
88
90
if ( passedMigrations . includes ( 'migrateAccountPreferencesToAccounts' ) ) return
89
91
90
- const migratedAccounts = getUniqueAccountsArray (
91
- accounts . map ( ( a : any ) => {
92
- return {
93
- ...a ,
94
- // @ts -ignore
95
- preferences : this . #storage. accountPreferences [ a . addr ] || {
96
- label : DEFAULT_ACCOUNT_LABEL ,
97
- pfp : a . addr
98
- }
99
- }
100
- } )
101
- )
102
-
103
- await Promise . all ( [
92
+ const storageUpdates = [
104
93
this . #storage. set ( 'passedMigrations' , [
105
94
...new Set ( [ ...passedMigrations , 'migrateAccountPreferencesToAccounts' ] )
106
- ] ) ,
107
- this . #storage. set ( 'accounts' , migratedAccounts ) ,
108
- this . #storage. remove ( 'accountPreferences' )
109
- ] )
95
+ ] )
96
+ ]
97
+ if ( accountPreferences ) {
98
+ const migratedAccounts = getUniqueAccountsArray (
99
+ accounts . map ( ( a : any ) => {
100
+ return {
101
+ ...a ,
102
+ // @ts -ignore
103
+ preferences : this . #storage. accountPreferences [ a . addr ] || {
104
+ label : DEFAULT_ACCOUNT_LABEL ,
105
+ pfp : a . addr
106
+ }
107
+ }
108
+ } )
109
+ )
110
+ storageUpdates . push ( this . #storage. set ( 'accounts' , migratedAccounts ) )
111
+ storageUpdates . push ( this . #storage. remove ( 'accountPreferences' ) )
112
+ }
113
+
114
+ await Promise . all ( storageUpdates )
110
115
}
111
116
112
117
// As of version v4.33.0, user can change the HD path when importing a seed.
@@ -120,19 +125,22 @@ export class StorageController {
120
125
121
126
if ( passedMigrations . includes ( 'migrateKeystoreSeedsWithoutHdPathTemplate' ) ) return
122
127
123
- if ( ! getShouldMigrateKeystoreSeedsWithoutHdPath ( keystoreSeeds ) ) return
124
-
125
- const migratedKeystoreSeeds = keystoreSeeds . map ( ( seed ) => ( {
126
- seed,
127
- hdPathTemplate : BIP44_STANDARD_DERIVATION_TEMPLATE
128
- } ) )
129
-
130
- await Promise . all ( [
128
+ const storageUpdates = [
131
129
this . #storage. set ( 'passedMigrations' , [
132
130
...new Set ( [ ...passedMigrations , 'migrateKeystoreSeedsWithoutHdPathTemplate' ] )
133
- ] ) ,
134
- this . #storage. set ( 'keystoreSeeds' , migratedKeystoreSeeds )
135
- ] )
131
+ ] )
132
+ ]
133
+
134
+ if ( getShouldMigrateKeystoreSeedsWithoutHdPath ( keystoreSeeds ) ) {
135
+ const migratedKeystoreSeeds = keystoreSeeds . map ( ( seed ) => ( {
136
+ seed,
137
+ hdPathTemplate : BIP44_STANDARD_DERIVATION_TEMPLATE
138
+ } ) )
139
+
140
+ storageUpdates . push ( this . #storage. set ( 'keystoreSeeds' , migratedKeystoreSeeds ) )
141
+ }
142
+
143
+ await Promise . all ( storageUpdates )
136
144
}
137
145
138
146
// As of version 4.33.0, we no longer store the key preferences in a separate object called keyPreferences in the storage.
@@ -147,27 +155,29 @@ export class StorageController {
147
155
148
156
if ( passedMigrations . includes ( 'migrateKeyPreferencesToKeystoreKeys' ) ) return
149
157
158
+ const storageUpdates = [
159
+ this . #storage. set ( 'passedMigrations' , [
160
+ ...new Set ( [ ...passedMigrations , 'migrateKeyPreferencesToKeystoreKeys' ] )
161
+ ] )
162
+ ]
150
163
const shouldMigrateKeyPreferencesToKeystoreKeys = keyPreferences . length > 0
151
164
152
- if ( ! shouldMigrateKeyPreferencesToKeystoreKeys ) return
165
+ if ( shouldMigrateKeyPreferencesToKeystoreKeys ) {
166
+ const migratedKeystoreKeys = keystoreKeys . map ( ( key ) => {
167
+ if ( key . label ) return key
153
168
154
- const migratedKeystoreKeys = keystoreKeys . map ( ( key ) => {
155
- if ( key . label ) return key
169
+ const keyPref = keyPreferences . find ( ( k ) => k . addr === key . addr && k . type === key . type )
156
170
157
- const keyPref = keyPreferences . find ( ( k ) => k . addr === key . addr && k . type === key . type )
171
+ if ( keyPref ) return { ... key , label : keyPref . label }
158
172
159
- if ( keyPref ) return { ...key , label : keyPref . label }
173
+ return key
174
+ } )
160
175
161
- return key
162
- } )
176
+ storageUpdates . push ( this . #storage. set ( 'keystoreKeys' , migratedKeystoreKeys ) )
177
+ storageUpdates . push ( this . #storage. remove ( 'keyPreferences' ) )
178
+ }
163
179
164
- await Promise . all ( [
165
- this . #storage. set ( 'passedMigrations' , [
166
- ...new Set ( [ ...passedMigrations , 'migrateKeyPreferencesToKeystoreKeys' ] )
167
- ] ) ,
168
- this . #storage. set ( 'keystoreKeys' , migratedKeystoreKeys ) ,
169
- this . #storage. remove ( 'keyPreferences' )
170
- ] )
180
+ await Promise . all ( storageUpdates )
171
181
}
172
182
173
183
// As of version 4.33.0, we introduced createdAt prop to the Key interface to help with sorting and add more details for the Keys.
@@ -263,25 +273,31 @@ export class StorageController {
263
273
264
274
if ( passedMigrations . includes ( 'migrateTokenPreferences' ) ) return
265
275
276
+ const storageUpdates = [
277
+ this . #storage. set ( 'passedMigrations' , [
278
+ ...new Set ( [ ...passedMigrations , 'migrateTokenPreferences' ] )
279
+ ] )
280
+ ]
281
+
266
282
if (
267
283
( tokenPreferences as LegacyTokenPreference [ ] ) . some (
268
284
( { symbol, decimals } ) => ! ! symbol || ! ! decimals
269
285
)
270
286
) {
271
- await Promise . all ( [
272
- this . #storage. set ( 'passedMigrations' , [
273
- ...new Set ( [ ...passedMigrations , 'migrateTokenPreferences' ] )
274
- ] ) ,
287
+ storageUpdates . push (
275
288
this . #storage. set (
276
289
'tokenPreferences' ,
277
290
migrateHiddenTokens ( tokenPreferences as LegacyTokenPreference [ ] )
278
- ) ,
291
+ )
292
+ )
293
+ storageUpdates . push (
279
294
this . #storage. set (
280
295
'customTokens' ,
281
296
migrateCustomTokens ( tokenPreferences as LegacyTokenPreference [ ] )
282
297
)
283
- ] )
298
+ )
284
299
}
300
+ await Promise . all ( storageUpdates )
285
301
}
286
302
287
303
async #migrateNetworkIdToChainId( ) {
@@ -292,23 +308,33 @@ export class StorageController {
292
308
customTokens ,
293
309
tokenPreferences ,
294
310
networksWithAssetsByAccount ,
295
- networksWithPositionsByAccounts
311
+ networksWithPositionsByAccounts ,
312
+ accountsOps ,
313
+ signedMessages
296
314
] = await Promise . all ( [
297
315
this . #storage. get ( 'passedMigrations' , [ ] ) ,
298
316
this . #storage. get ( 'networks' , { } ) ,
299
317
this . #storage. get ( 'previousHints' , [ ] ) ,
300
318
this . #storage. get ( 'customTokens' , [ ] ) ,
301
319
this . #storage. get ( 'tokenPreferences' , [ ] ) ,
302
320
this . #storage. get ( 'networksWithAssetsByAccount' , { } ) ,
303
- this . #storage. get ( 'networksWithPositionsByAccounts' , { } )
321
+ this . #storage. get ( 'networksWithPositionsByAccounts' , { } ) ,
322
+ this . #storage. get ( 'accountsOps' , { } ) ,
323
+ this . #storage. get ( 'signedMessages' , { } )
304
324
] )
305
325
306
- if ( ! Object . keys ( networks ) . length ) return
307
-
308
326
if ( passedMigrations . includes ( 'migrateNetworkIdToChainId' ) ) return
309
327
328
+ if ( ! Object . keys ( networks ) . length ) {
329
+ await this . #storage. set ( 'passedMigrations' , [
330
+ ...new Set ( [ ...passedMigrations , 'migrateNetworkIdToChainId' ] )
331
+ ] )
332
+
333
+ return
334
+ }
335
+
310
336
const networkIdToChainId = Object . fromEntries (
311
- Object . values ( networks ) . map ( ( { id, chainId } : any ) => [ id as string , chainId as bigint ] )
337
+ Object . values ( networks ) . map ( ( { id, chainId } : any ) => [ id , chainId as bigint ] )
312
338
)
313
339
314
340
const migrateKeys = < T > ( obj : Record < string , T > ) =>
@@ -353,6 +379,35 @@ export class StorageController {
353
379
] )
354
380
)
355
381
382
+ const migratedAccountsOps = Object . fromEntries (
383
+ Object . entries ( accountsOps ) . map ( ( [ accountId , opsByNetwork ] ) => [
384
+ accountId ,
385
+ Object . fromEntries (
386
+ Object . entries ( opsByNetwork ) . map ( ( [ networkId , ops ] ) => {
387
+ const chainId = networkIdToChainId [ networkId ]
388
+ return [
389
+ chainId ,
390
+ // eslint-disable-next-line @typescript-eslint/no-shadow
391
+ ops . map ( ( { networkId, ...rest } : any ) => ( {
392
+ ...rest ,
393
+ chainId // Migrate networkId inside SubmittedAccountOp
394
+ } ) )
395
+ ]
396
+ } )
397
+ )
398
+ ] )
399
+ )
400
+
401
+ const migratedSignedMessages = Object . fromEntries (
402
+ Object . entries ( signedMessages ) . map ( ( [ accountId , messages ] ) => [
403
+ accountId ,
404
+ messages . map ( ( { networkId, ...rest } : any ) => ( {
405
+ ...rest ,
406
+ chainId : networkIdToChainId [ networkId ] // Migrate networkId inside SignedMessage
407
+ } ) )
408
+ ] )
409
+ )
410
+
356
411
const migratedNetworks = Object . fromEntries (
357
412
// eslint-disable-next-line @typescript-eslint/no-unused-vars
358
413
Object . entries ( networks ) . map ( ( [ _ , { id, ...rest } ] : any ) => [ rest . chainId . toString ( ) , rest ] )
@@ -367,7 +422,9 @@ export class StorageController {
367
422
this . #storage. set ( 'customTokens' , migratedCustomTokens ) ,
368
423
this . #storage. set ( 'tokenPreferences' , migratedTokenPreferences ) ,
369
424
this . #storage. set ( 'networksWithAssetsByAccount' , migratedNetworksWithAssetsByAccount ) ,
370
- this . #storage. set ( 'networksWithPositionsByAccounts' , migratedNetworksWithPositionsByAccounts )
425
+ this . #storage. set ( 'networksWithPositionsByAccounts' , migratedNetworksWithPositionsByAccounts ) ,
426
+ this . #storage. set ( 'accountsOps' , migratedAccountsOps ) ,
427
+ this . #storage. set ( 'signedMessages' , migratedSignedMessages )
371
428
] )
372
429
}
373
430
0 commit comments