Skip to content

Commit 72e10fc

Browse files
authored
Merge pull request #1396 from AmbireTech/fix/activity-chain-id-migration
Fix / Activity ChainId Storage Migration
2 parents ba2b8b6 + 9bc969e commit 72e10fc

File tree

3 files changed

+125
-64
lines changed

3 files changed

+125
-64
lines changed

src/controllers/activity/activity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export interface Filters {
4242
chainId?: bigint
4343
}
4444

45-
interface InternalAccountsOps {
45+
export interface InternalAccountsOps {
4646
// account => network => SubmittedAccountOp[]
4747
[key: string]: { [key: string]: SubmittedAccountOp[] }
4848
}

src/controllers/storage/storage.ts

+120-63
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,20 @@ export class StorageController {
6060

6161
if (passedMigrations.includes('migrateNetworkPreferencesToNetworks')) return
6262

63+
const storageUpdates = [
64+
this.#storage.set('passedMigrations', [
65+
...new Set([...passedMigrations, 'migrateNetworkPreferencesToNetworks'])
66+
])
67+
]
68+
6369
if (!Object.keys(networks).length && networkPreferences) {
6470
const migratedNetworks = await migrateNetworkPreferencesToNetworks(networkPreferences)
6571

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'))
7374
}
75+
76+
await Promise.all(storageUpdates)
7477
}
7578

7679
// As of version 4.25.0, a new Account interface has been introduced,
@@ -83,30 +86,32 @@ export class StorageController {
8386
this.#storage.get('accounts', []),
8487
this.#storage.get('accountPreferences')
8588
])
86-
if (!accountPreferences) return
8789

8890
if (passedMigrations.includes('migrateAccountPreferencesToAccounts')) return
8991

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 = [
10493
this.#storage.set('passedMigrations', [
10594
...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)
110115
}
111116

112117
// As of version v4.33.0, user can change the HD path when importing a seed.
@@ -120,19 +125,22 @@ export class StorageController {
120125

121126
if (passedMigrations.includes('migrateKeystoreSeedsWithoutHdPathTemplate')) return
122127

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 = [
131129
this.#storage.set('passedMigrations', [
132130
...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)
136144
}
137145

138146
// 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 {
147155

148156
if (passedMigrations.includes('migrateKeyPreferencesToKeystoreKeys')) return
149157

158+
const storageUpdates = [
159+
this.#storage.set('passedMigrations', [
160+
...new Set([...passedMigrations, 'migrateKeyPreferencesToKeystoreKeys'])
161+
])
162+
]
150163
const shouldMigrateKeyPreferencesToKeystoreKeys = keyPreferences.length > 0
151164

152-
if (!shouldMigrateKeyPreferencesToKeystoreKeys) return
165+
if (shouldMigrateKeyPreferencesToKeystoreKeys) {
166+
const migratedKeystoreKeys = keystoreKeys.map((key) => {
167+
if (key.label) return key
153168

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)
156170

157-
const keyPref = keyPreferences.find((k) => k.addr === key.addr && k.type === key.type)
171+
if (keyPref) return { ...key, label: keyPref.label }
158172

159-
if (keyPref) return { ...key, label: keyPref.label }
173+
return key
174+
})
160175

161-
return key
162-
})
176+
storageUpdates.push(this.#storage.set('keystoreKeys', migratedKeystoreKeys))
177+
storageUpdates.push(this.#storage.remove('keyPreferences'))
178+
}
163179

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)
171181
}
172182

173183
// 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 {
263273

264274
if (passedMigrations.includes('migrateTokenPreferences')) return
265275

276+
const storageUpdates = [
277+
this.#storage.set('passedMigrations', [
278+
...new Set([...passedMigrations, 'migrateTokenPreferences'])
279+
])
280+
]
281+
266282
if (
267283
(tokenPreferences as LegacyTokenPreference[]).some(
268284
({ symbol, decimals }) => !!symbol || !!decimals
269285
)
270286
) {
271-
await Promise.all([
272-
this.#storage.set('passedMigrations', [
273-
...new Set([...passedMigrations, 'migrateTokenPreferences'])
274-
]),
287+
storageUpdates.push(
275288
this.#storage.set(
276289
'tokenPreferences',
277290
migrateHiddenTokens(tokenPreferences as LegacyTokenPreference[])
278-
),
291+
)
292+
)
293+
storageUpdates.push(
279294
this.#storage.set(
280295
'customTokens',
281296
migrateCustomTokens(tokenPreferences as LegacyTokenPreference[])
282297
)
283-
])
298+
)
284299
}
300+
await Promise.all(storageUpdates)
285301
}
286302

287303
async #migrateNetworkIdToChainId() {
@@ -292,23 +308,33 @@ export class StorageController {
292308
customTokens,
293309
tokenPreferences,
294310
networksWithAssetsByAccount,
295-
networksWithPositionsByAccounts
311+
networksWithPositionsByAccounts,
312+
accountsOps,
313+
signedMessages
296314
] = await Promise.all([
297315
this.#storage.get('passedMigrations', []),
298316
this.#storage.get('networks', {}),
299317
this.#storage.get('previousHints', []),
300318
this.#storage.get('customTokens', []),
301319
this.#storage.get('tokenPreferences', []),
302320
this.#storage.get('networksWithAssetsByAccount', {}),
303-
this.#storage.get('networksWithPositionsByAccounts', {})
321+
this.#storage.get('networksWithPositionsByAccounts', {}),
322+
this.#storage.get('accountsOps', {}),
323+
this.#storage.get('signedMessages', {})
304324
])
305325

306-
if (!Object.keys(networks).length) return
307-
308326
if (passedMigrations.includes('migrateNetworkIdToChainId')) return
309327

328+
if (!Object.keys(networks).length) {
329+
await this.#storage.set('passedMigrations', [
330+
...new Set([...passedMigrations, 'migrateNetworkIdToChainId'])
331+
])
332+
333+
return
334+
}
335+
310336
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])
312338
)
313339

314340
const migrateKeys = <T>(obj: Record<string, T>) =>
@@ -353,6 +379,35 @@ export class StorageController {
353379
])
354380
)
355381

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+
356411
const migratedNetworks = Object.fromEntries(
357412
// eslint-disable-next-line @typescript-eslint/no-unused-vars
358413
Object.entries(networks).map(([_, { id, ...rest }]: any) => [rest.chainId.toString(), rest])
@@ -367,7 +422,9 @@ export class StorageController {
367422
this.#storage.set('customTokens', migratedCustomTokens),
368423
this.#storage.set('tokenPreferences', migratedTokenPreferences),
369424
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)
371428
])
372429
}
373430

src/interfaces/storage.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { SignedMessage } from '../controllers/activity/types'
12
// eslint-disable-next-line import/no-cycle
23
import { StoredPhishingDetection } from '../controllers/phishing/phishing'
4+
import { SubmittedAccountOp } from '../libs/accountOp/submittedAccountOp'
35
import { NetworksWithPositionsByAccounts } from '../libs/defiPositions/types'
46
import { CustomToken, TokenPreference } from '../libs/portfolio/customToken'
57
import {
@@ -20,6 +22,8 @@ export type StorageProps = {
2022
accounts: Account[]
2123
networkPreferences?: { [key: string]: Partial<Network> }
2224
accountPreferences?: { [key: AccountId]: AccountPreferences }
25+
accountsOps: { [key: string]: { [key: string]: SubmittedAccountOp[] } }
26+
signedMessages: { [key: AccountId]: SignedMessage[] }
2327
networksWithAssetsByAccount: { [accountId: string]: PortfolioAccountAssetsState }
2428
networksWithPositionsByAccounts: NetworksWithPositionsByAccounts
2529
tokenPreferences: TokenPreference[]

0 commit comments

Comments
 (0)