-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcompleteExistingUserQrSyncImport.ts
More file actions
79 lines (70 loc) · 2.54 KB
/
Copy pathcompleteExistingUserQrSyncImport.ts
File metadata and controls
79 lines (70 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { importNewSecretRecoveryPhrase } from '../../actions/multiSrp';
import Routes from '../../constants/navigation/Routes';
import { showAlreadySyncedSheet } from '../../components/Views/AddDeviceToWallet/showAlreadySyncedSheet';
import { showImportFailedSheet } from '../../components/Views/AddDeviceToWallet/showImportFailedSheet';
import Engine from '../Engine';
import type { AppNavigationProp } from '../NavigationService/types';
import { isDuplicateMnemonicError } from './duplicateMnemonicError';
import {
QrSyncOperations,
QrSyncSurfaces,
QrSyncSyncFlows,
QrSyncTelemetrySources,
reportQrSyncFailure,
} from './qrSyncTelemetry';
export {
DUPLICATE_MNEMONIC_ERROR_MESSAGES,
isDuplicateMnemonicError,
} from './duplicateMnemonicError';
/** Prevents Add Device + QR scanner from starting two imports at once. */
let inFlightExistingUserImport: Promise<void> | null = null;
const showAlreadySyncedAndGoHome = (navigation: AppNavigationProp): void => {
navigation.navigate(Routes.WALLET_VIEW);
showAlreadySyncedSheet(navigation);
};
const runExistingUserQrSyncImport = async (
navigation: AppNavigationProp,
mnemonic: string,
): Promise<void> => {
try {
// Do not race import against a timer — Multichain/keyring import cannot be
// cancelled, and a timeout would leave late vault mutations after failure UI.
await importNewSecretRecoveryPhrase(mnemonic);
Engine.context.QrSyncController.resetState();
navigation.navigate(Routes.WALLET_VIEW);
} catch (error) {
Engine.context.QrSyncController.resetState();
if (isDuplicateMnemonicError(error)) {
showAlreadySyncedAndGoHome(navigation);
return;
}
reportQrSyncFailure(error, {
surface: QrSyncSurfaces.IMPORT,
operation: QrSyncOperations.EXISTING_USER_MNEMONIC_IMPORT,
source: QrSyncTelemetrySources.COMPLETE_EXISTING_USER_IMPORT,
syncFlow: QrSyncSyncFlows.EXISTING_USER,
});
navigation.navigate(Routes.WALLET_VIEW);
showImportFailedSheet(navigation);
}
};
/**
* Completes existing-user QR sync by importing the primary mnemonic.
* Duplicate SRP is surfaced with SuccessErrorSheet and treated as
* already-synced.
*/
export const completeExistingUserQrSyncImport = async (
navigation: AppNavigationProp,
mnemonic: string,
): Promise<void> => {
if (inFlightExistingUserImport) {
return inFlightExistingUserImport;
}
inFlightExistingUserImport = runExistingUserQrSyncImport(
navigation,
mnemonic,
).finally(() => {
inFlightExistingUserImport = null;
});
return inFlightExistingUserImport;
};