Skip to content

Commit 4a4813e

Browse files
refactor(suite): inject suite sync migration wrapper factory
Refs #28176
1 parent 3376ea8 commit 4a4813e

9 files changed

Lines changed: 98 additions & 73 deletions

File tree

packages/suite/src/support/extraDependencies.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import { lockDevice } from '@suite/locks';
77
import {
88
metadataActions,
99
metadataLabelingActions,
10+
selectHasLegacyLabelsMigrated,
11+
selectIsMetadataEnabled,
1012
selectLabelingDataForAccount,
13+
selectSelectedProviderForLabels,
1114
} from '@suite/metadata';
12-
import { createMetadataMigrationCompositionRoot } from '@suite/metadata-migration';
15+
import {
16+
createEnsureWalletSuiteSyncOnWithMigration,
17+
createMetadataMigrationCompositionRoot,
18+
} from '@suite/metadata-migration';
1319
import type { MetadataMigrationDep } from '@suite/metadata-migration';
1420
import { closeModal, openModal } from '@suite/modal';
1521
import {
@@ -42,6 +48,7 @@ import {
4248
selectIsSuiteSyncEnabled,
4349
selectSuiteSyncWalletLabel,
4450
} from '@suite-common/suite-sync';
51+
import { type SuiteSync } from '@suite-common/suite-sync-types';
4552
import {
4653
type TokenDefinitionsState,
4754
buildTokenDefinitionsFromStorage,
@@ -108,7 +115,22 @@ export const createSuiteServicesCompositionRoot = (deps: SuiteAppDeps): SuiteSer
108115

109116
const analytics = createAnalytics();
110117

111-
const suiteSync = createSuiteSyncDesktopCompositionRoot({
118+
const getCurrentAccountLabels = toGetter(deps.getState, selectAllLabelsForAccount);
119+
const getAccountsByDeviceState = toGetter(deps.getState, selectAccountsByDeviceState);
120+
const migrateLegacyLabelsToSuiteSyncRef: {
121+
current: MetadataMigrationDep['migrateLegacyLabelsToSuiteSync'] | undefined;
122+
} = { current: undefined };
123+
124+
const createDecoratedEnsureWalletSuiteSyncOn = createEnsureWalletSuiteSyncOnWithMigration({
125+
dispatch: deps.dispatch,
126+
getMigrateLegacyLabelsToSuiteSync: () => migrateLegacyLabelsToSuiteSyncRef.current,
127+
getIsMetadataEnabled: toGetter(deps.getState, selectIsMetadataEnabled),
128+
getSelectedProviderForLabels: toGetter(deps.getState, selectSelectedProviderForLabels),
129+
getHasLegacyLabelsMigrated: toGetter(deps.getState, selectHasLegacyLabelsMigrated),
130+
getDeviceByStaticSessionId: toGetter(deps.getState, selectDeviceByStaticSessionId),
131+
});
132+
133+
const suiteSync: SuiteSync = createSuiteSyncDesktopCompositionRoot({
112134
dispatch: deps.dispatch,
113135
getState: deps.getState,
114136
platformEncryption: deps.platformEncryption,
@@ -122,9 +144,18 @@ export const createSuiteServicesCompositionRoot = (deps: SuiteAppDeps): SuiteSer
122144
dispatch: deps.dispatch,
123145
deviceStaticSessionId: device?.state?.staticSessionId ?? null,
124146
}),
147+
createDecoratedEnsureWalletSuiteSyncOn,
125148
});
126149

127-
const getCurrentAccountLabels = toGetter(deps.getState, selectAllLabelsForAccount);
150+
const { migrateLegacyLabelsToSuiteSync } = createMetadataMigrationCompositionRoot({
151+
getState: deps.getState,
152+
getAccountsByDeviceState,
153+
getCurrentWalletLabel: toGetter(deps.getState, selectSuiteSyncWalletLabel),
154+
getCurrentAccountLabels,
155+
labeling: suiteSync.labeling,
156+
});
157+
158+
migrateLegacyLabelsToSuiteSyncRef.current = migrateLegacyLabelsToSuiteSync;
128159

129160
const { bip329 } = createBip329CompositionRoot({
130161
getIsSuiteSyncEnabled: toGetter(deps.getState, selectIsSuiteSyncEnabled),
@@ -134,27 +165,8 @@ export const createSuiteServicesCompositionRoot = (deps: SuiteAppDeps): SuiteSer
134165
updateOutputLabel: suiteSync.labeling.updateOutputLabel,
135166
});
136167

137-
const getAccountsByDeviceState = toGetter(deps.getState, selectAccountsByDeviceState);
138-
139-
const {
140-
ensureWalletSuiteSyncOn: ensureWalletSuiteSyncOnWithMigration,
141-
migrateLegacyLabelsToSuiteSync,
142-
} = createMetadataMigrationCompositionRoot({
143-
dispatch: deps.dispatch,
144-
getState: deps.getState,
145-
ensureWalletSuiteSyncOn: suiteSync.ensureWalletSuiteSyncOn,
146-
getAccountsByDeviceState,
147-
getCurrentWalletLabel: toGetter(deps.getState, selectSuiteSyncWalletLabel),
148-
getCurrentAccountLabels,
149-
getDeviceByStaticSessionId: toGetter(deps.getState, selectDeviceByStaticSessionId),
150-
labeling: suiteSync.labeling,
151-
});
152-
153168
return {
154-
suiteSync: {
155-
...suiteSync,
156-
ensureWalletSuiteSyncOn: ensureWalletSuiteSyncOnWithMigration,
157-
},
169+
suiteSync,
158170
bip329,
159171
migrateLegacyLabelsToSuiteSync,
160172
ensureDelegatedIdentityKey,
@@ -216,6 +228,7 @@ export const extraDependencies: ExtraDependenciesStatic = {
216228
storageLoadBlockchain: (state: BlockchainState, { payload }: StorageLoadAction) => {
217229
payload.backendSettings.forEach(backend => {
218230
const blockchain = state[backend.key];
231+
219232
if (blockchain) {
220233
blockchain.backends = backend.value;
221234
}
@@ -238,9 +251,11 @@ export const extraDependencies: ExtraDependenciesStatic = {
238251
networkSymbol: item.tx.symbol,
239252
deviceStaticSessionId: item.tx.deviceState,
240253
});
254+
241255
if (!state.transactions[k]) {
242256
state.transactions[k] = [];
243257
}
258+
244259
state.transactions[k][item.order] = item.tx;
245260
});
246261

suite-common/suite-sync-types/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export {
5757
selectTurnOffSuiteSyncForWalletDep,
5858
} from './storage/turnOffSuiteSyncForWallet';
5959
export {
60+
type CreateDecoratedEnsureWalletSuiteSyncOn,
61+
type CreateDecoratedEnsureWalletSuiteSyncOnDep,
6062
type EnsureWalletSuiteSyncOnAsync,
6163
type EnsureWalletSuiteSyncOnAsyncDep,
6264
type EnsureWalletSuiteSyncOn,

suite-common/suite-sync-types/src/storage/ensureWalletSuiteSyncOn.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ export type EnsureWalletSuiteSyncOn = (
3535
params: EnsureWalletSuiteSyncOnParams,
3636
) => Promise<Result<SuiteSyncStorage, EnsureWalletSuiteSyncOnErrors>>;
3737

38+
export type CreateDecoratedEnsureWalletSuiteSyncOn = (
39+
ensureWalletSuiteSyncOn: EnsureWalletSuiteSyncOn,
40+
) => EnsureWalletSuiteSyncOn;
41+
42+
export type CreateDecoratedEnsureWalletSuiteSyncOnDep = {
43+
createDecoratedEnsureWalletSuiteSyncOn: CreateDecoratedEnsureWalletSuiteSyncOn;
44+
};
45+
3846
export type EnsureWalletSuiteSyncOnDep = { ensureWalletSuiteSyncOn: EnsureWalletSuiteSyncOn };
3947

4048
export const selectEnsureWalletSuiteSyncOnDep = (services: any): EnsureWalletSuiteSyncOnDep => ({

suite-common/suite-sync/src/createSuiteSyncCompositionRoot.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
type CreateSuiteStorageDep,
2020
type CreateSuiteSyncOwnerDep,
2121
} from '@suite-common/suite-sync-storage';
22-
import { type SuiteSync, type SuiteSyncInternalErrorHandler } from '@suite-common/suite-sync-types';
22+
import {
23+
type CreateDecoratedEnsureWalletSuiteSyncOnDep,
24+
type SuiteSync,
25+
type SuiteSyncInternalErrorHandler,
26+
} from '@suite-common/suite-sync-types';
2327
import { type AccountsRootState, selectAccounts } from '@suite-common/wallet-core';
2428
import { type Analytics } from '@trezor/analytics-uploader';
2529
import type TrezorConnect from '@trezor/connect';
@@ -92,7 +96,8 @@ type CreateSuiteSyncCompositionRootDeps = {
9296
CreateSuiteSyncOwnerDep &
9397
PlatformEncryptionDep &
9498
FetchDep &
95-
SuiteSyncAsyncErrorHandlerDep;
99+
SuiteSyncAsyncErrorHandlerDep &
100+
CreateDecoratedEnsureWalletSuiteSyncOnDep;
96101

97102
export const createSuiteSyncCompositionRoot = (
98103
deps: CreateSuiteSyncCompositionRootDeps,
@@ -171,12 +176,14 @@ export const createSuiteSyncCompositionRoot = (
171176

172177
const ensureWalletSuiteSyncOn = createEnsureWalletSuiteSyncOnWithErrorHandler({
173178
dispatch: deps.dispatch,
174-
ensureWalletSuiteSyncOn: createEnsureWalletSuiteSyncOn({
175-
getState: deps.getState,
176-
ensureSuiteSyncKeys,
177-
ensureSubscribedStorage,
178-
subscriptionStorage,
179-
}),
179+
ensureWalletSuiteSyncOn: deps.createDecoratedEnsureWalletSuiteSyncOn(
180+
createEnsureWalletSuiteSyncOn({
181+
getState: deps.getState,
182+
ensureSuiteSyncKeys,
183+
ensureSubscribedStorage,
184+
subscriptionStorage,
185+
}),
186+
),
180187
});
181188

182189
const ensureWalletSuiteSyncOnAsync = createEnsureWalletSuiteSyncOnAsync({

suite-native/suite-sync/src/createSuiteSyncNativeCompositionRoot.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const createSuiteSyncNativeCompositionRoot = (
4343

4444
return createSuiteSyncCompositionRoot({
4545
...deps,
46+
createDecoratedEnsureWalletSuiteSyncOn: ensureWalletSuiteSyncOn => ensureWalletSuiteSyncOn,
4647
createSuiteStorage: createEvoluStorageFactory({
4748
createEvoluInstance: createEvoluInstanceFactory({ run }),
4849
}),

suite/metadata-migration/src/createEnsureWalletSuiteSyncOnWithMigration.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { suiteSyncErrorHandler } from '@suite/suite-sync';
55
import { isTrezorDeviceWithState } from '@suite-common/device';
66
import { type MetadataProvider } from '@suite-common/metadata-types';
77
import {
8+
type CreateDecoratedEnsureWalletSuiteSyncOn,
89
type EnsureWalletSuiteSyncOn,
9-
type EnsureWalletSuiteSyncOnDep,
1010
} from '@suite-common/suite-sync-types';
1111
import { type TrezorDevice } from '@suite-common/suite-types';
1212
import { notificationsActions } from '@suite-common/toast-notifications';
@@ -18,23 +18,27 @@ import type { MigrateLegacyLabelsToSuiteSync } from './migrateLegacyLabelsToSuit
1818

1919
type CreateEnsureWalletSuiteSyncOnWithMigrationDeps = {
2020
dispatch: Dispatch;
21-
migrateLegacyLabelsToSuiteSync: MigrateLegacyLabelsToSuiteSync;
21+
getMigrateLegacyLabelsToSuiteSync: () => MigrateLegacyLabelsToSuiteSync | undefined;
2222
getIsMetadataEnabled: () => boolean;
2323
getSelectedProviderForLabels: () => MetadataProvider | undefined;
2424
getHasLegacyLabelsMigrated: (walletDescriptor: WalletDescriptor) => boolean;
2525
getDeviceByStaticSessionId: (
2626
deviceStaticSessionId: Parameters<GetLegacyWalletLabels>[0],
2727
) => TrezorDevice | undefined;
28-
} & EnsureWalletSuiteSyncOnDep;
28+
};
2929

30-
export const createEnsureWalletSuiteSyncOnWithMigration =
31-
(deps: CreateEnsureWalletSuiteSyncOnWithMigrationDeps): EnsureWalletSuiteSyncOn =>
32-
async params => {
33-
const result = await deps.ensureWalletSuiteSyncOn(params);
30+
export const createEnsureWalletSuiteSyncOnWithMigration = (
31+
deps: CreateEnsureWalletSuiteSyncOnWithMigrationDeps,
32+
): CreateDecoratedEnsureWalletSuiteSyncOn => {
33+
let isMigrating = false;
3434

35-
if (!result.success) {
35+
return (ensureWalletSuiteSyncOn: EnsureWalletSuiteSyncOn) => async params => {
36+
const result = await ensureWalletSuiteSyncOn(params);
37+
38+
if (!result.success || isMigrating) {
3639
return result;
3740
}
41+
3842
const device = deps.getDeviceByStaticSessionId(params.deviceStaticSessionId);
3943

4044
if (!isTrezorDeviceWithState(device)) {
@@ -51,7 +55,17 @@ export const createEnsureWalletSuiteSyncOnWithMigration =
5155
return result;
5256
}
5357

54-
const migrationResult = await deps.migrateLegacyLabelsToSuiteSync(device);
58+
const migrateLegacyLabelsToSuiteSync = deps.getMigrateLegacyLabelsToSuiteSync();
59+
60+
if (migrateLegacyLabelsToSuiteSync === undefined) {
61+
return result;
62+
}
63+
64+
isMigrating = true;
65+
66+
const migrationResult = await migrateLegacyLabelsToSuiteSync(device);
67+
68+
isMigrating = false;
5569

5670
if (!migrationResult.success) {
5771
suiteSyncErrorHandler({
@@ -77,3 +91,4 @@ export const createEnsureWalletSuiteSyncOnWithMigration =
7791

7892
return result;
7993
};
94+
};
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import { type Dispatch } from '@reduxjs/toolkit';
2-
31
import {
42
type MetadataRootState,
5-
selectHasLegacyLabelsMigrated,
6-
selectIsMetadataEnabled,
73
selectLabelingDataForAccount,
84
selectLabelingDataForWallet,
9-
selectSelectedProviderForLabels,
105
} from '@suite/metadata';
116
import { toGetter } from '@suite-common/dependency-injection';
12-
import { type EnsureWalletSuiteSyncOnDep, type LabelingDep } from '@suite-common/suite-sync-types';
13-
import { type TrezorDevice } from '@suite-common/suite-types';
7+
import { type LabelingDep } from '@suite-common/suite-sync-types';
148

15-
import { createEnsureWalletSuiteSyncOnWithMigration } from './createEnsureWalletSuiteSyncOnWithMigration';
169
import { createMigrateAccountLabels } from './entities/createMigrateAccountLabels';
1710
import { createMigrateAddressLabels } from './entities/createMigrateAddressLabels';
1811
import { createMigrateOutputLabels } from './entities/createMigrateOutputLabels';
@@ -21,7 +14,6 @@ import type {
2114
GetAccountsByDeviceState,
2215
GetCurrentAccountLabels,
2316
GetCurrentWalletLabel,
24-
GetLegacyWalletLabels,
2517
} from './legacyLabelsMigration';
2618
import {
2719
type MigrateLegacyLabelsToSuiteSync,
@@ -37,26 +29,17 @@ export const selectMetadataMigrationDep = (services: any): MetadataMigrationDep
3729
});
3830

3931
type CreateMetadataMigrationCompositionRootDeps = {
40-
dispatch: Dispatch;
4132
getState: () => MetadataRootState;
4233
getAccountsByDeviceState: GetAccountsByDeviceState;
4334
getCurrentWalletLabel: GetCurrentWalletLabel;
4435
getCurrentAccountLabels: GetCurrentAccountLabels;
45-
getDeviceByStaticSessionId: (
46-
deviceStaticSessionId: Parameters<GetLegacyWalletLabels>[0],
47-
) => TrezorDevice | undefined;
48-
} & EnsureWalletSuiteSyncOnDep &
49-
LabelingDep;
36+
} & LabelingDep;
5037

51-
type MetadataMigrationCompositionRootResult = MetadataMigrationDep & EnsureWalletSuiteSyncOnDep;
38+
type MetadataMigrationCompositionRootResult = MetadataMigrationDep;
5239

5340
export const createMetadataMigrationCompositionRoot = (
5441
deps: CreateMetadataMigrationCompositionRootDeps,
5542
): MetadataMigrationCompositionRootResult => {
56-
const getIsMetadataEnabled = toGetter(deps.getState, selectIsMetadataEnabled);
57-
const getSelectedProviderForLabels = toGetter(deps.getState, selectSelectedProviderForLabels);
58-
const getHasLegacyLabelsMigrated = toGetter(deps.getState, selectHasLegacyLabelsMigrated);
59-
6043
const migrateLegacyLabelsToSuiteSync = createMigrateLegacyLabelsToSuiteSync({
6144
getAccountsByDeviceState: deps.getAccountsByDeviceState,
6245
getLegacyAccountLabels: toGetter(deps.getState, selectLabelingDataForAccount),
@@ -77,18 +60,7 @@ export const createMetadataMigrationCompositionRoot = (
7760
}),
7861
});
7962

80-
const ensureWalletSuiteSyncOn = createEnsureWalletSuiteSyncOnWithMigration({
81-
dispatch: deps.dispatch,
82-
ensureWalletSuiteSyncOn: deps.ensureWalletSuiteSyncOn,
83-
migrateLegacyLabelsToSuiteSync,
84-
getIsMetadataEnabled,
85-
getSelectedProviderForLabels,
86-
getHasLegacyLabelsMigrated,
87-
getDeviceByStaticSessionId: deps.getDeviceByStaticSessionId,
88-
});
89-
9063
return {
91-
ensureWalletSuiteSyncOn,
9264
migrateLegacyLabelsToSuiteSync,
9365
};
9466
};

suite/metadata-migration/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {
33
type MetadataMigrationDep,
44
selectMetadataMigrationDep,
55
} from './createMetadataMigrationCompositionRoot';
6+
export { createEnsureWalletSuiteSyncOnWithMigration } from './createEnsureWalletSuiteSyncOnWithMigration';
67
export {
78
LegacyLabelingMigration,
89
type LegacyLabelingMigrationProps,

suite/suite-sync/src/createSuiteSyncDesktopCompositionRoot.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import {
1616
evoluCreateSuiteSyncOwner,
1717
} from '@suite-common/suite-sync-evolu';
1818
import { type FetchDep } from '@suite-common/suite-sync-quota-manager';
19-
import { type SuiteSync } from '@suite-common/suite-sync-types';
19+
import {
20+
type CreateDecoratedEnsureWalletSuiteSyncOnDep,
21+
type SuiteSync,
22+
} from '@suite-common/suite-sync-types';
2023
import { type TrezorConnect } from '@trezor/connect';
2124

2225
import { createOnSharedWorkerUnsupported } from './createOnSharedWorkerUnsupported';
@@ -30,7 +33,8 @@ type SuiteSyncDesktopCompositionRootDeps = {
3033
EnsureDelegatedIdentityKeyDep &
3134
DesktopAnalyticsDep &
3235
FetchDep &
33-
SuiteSyncAsyncErrorHandlerDep;
36+
SuiteSyncAsyncErrorHandlerDep &
37+
CreateDecoratedEnsureWalletSuiteSyncOnDep;
3438

3539
export const createSuiteSyncDesktopCompositionRoot = (
3640
deps: SuiteSyncDesktopCompositionRootDeps,

0 commit comments

Comments
 (0)