|
11 | 11 | initialiseOnboardingProfile,
|
12 | 12 | initialiseProfileManagerFromOnboardingProfile,
|
13 | 13 | resetOnboardingProfile,
|
| 14 | + onboardingProfile, |
14 | 15 | } from '@contexts/onboarding'
|
15 |
| - import { STRONGHOLD_VERSION } from '@core/stronghold' |
16 | 16 | import { api, getProfileManager } from '@core/profile-manager'
|
17 | 17 | import { ProfileType } from '@core/profile'
|
18 |
| - import { getDefaultPersistedNetwork, NetworkId } from '@core/network' |
19 |
| -
|
| 18 | + import { getDefaultClientOptions, getDefaultPersistedNetwork, NetworkId } from '@core/network' |
| 19 | + import { CLIENT_ERROR_REGEXES } from '@core/error/constants' |
| 20 | + import { ClientError } from '@core/error/enums' |
| 21 | + import { restoreBackup } from '@core/profile-manager/api' |
| 22 | + import { StrongholdVersion } from '@core/stronghold/enums' |
| 23 | + import { STRONGHOLD_VERSION } from '@core/stronghold' |
| 24 | + import { showAppNotification } from '@auxiliary/notification' |
20 | 25 | interface FileWithPath extends File {
|
21 | 26 | path?: string
|
22 | 27 | }
|
|
42 | 47 | setClipboard(seed)
|
43 | 48 | }
|
44 | 49 |
|
45 |
| - function openUnlockStrongholdPopup(): void { |
| 50 | + function openUnlockStrongholdPopup(shouldMigrateStronghold: boolean): void { |
46 | 51 | openPopup({
|
47 | 52 | id: PopupId.UnlockStronghold,
|
48 | 53 | props: {
|
49 | 54 | returnPassword: true,
|
50 | 55 | restoreBackupFromStronghold: true,
|
51 |
| - onSuccess: () => { |
| 56 | + shouldMigrateStronghold, |
| 57 | + onSuccess: async (password) => { |
| 58 | + if (!shouldMigrateStronghold) { |
| 59 | + updateOnboardingProfile({ strongholdPassword: password }) |
| 60 | + await initialiseProfileManagerFromOnboardingProfile() |
| 61 | + } |
| 62 | +
|
52 | 63 | openPopup({
|
53 | 64 | id: PopupId.GetSeedPopup,
|
54 | 65 | props: {
|
|
110 | 121 |
|
111 | 122 | async function extractSeedFromStrongholdFile(): Promise<void> {
|
112 | 123 | await initialiseOnboardingProfile(false, true)
|
| 124 | + const network = getDefaultPersistedNetwork(NetworkId.Iota) |
| 125 | + const clientOptions = getDefaultClientOptions(NetworkId.Iota) |
113 | 126 | validateBackupFile(importFileName)
|
114 | 127 | updateOnboardingProfile({
|
115 |
| - network: getDefaultPersistedNetwork(NetworkId.Iota), |
| 128 | + network, |
| 129 | + clientOptions, |
116 | 130 | importFile,
|
117 | 131 | importFilePath,
|
118 | 132 | // TODO: we don't have a way to know the stronghold version of the backup file yet
|
119 | 133 | strongholdVersion: STRONGHOLD_VERSION,
|
120 | 134 | type: ProfileType.Software,
|
121 | 135 | })
|
| 136 | +
|
122 | 137 | await initialiseProfileManagerFromOnboardingProfile()
|
123 |
| - openUnlockStrongholdPopup() |
| 138 | +
|
| 139 | + const _shouldMigrate = await shouldMigrate() |
| 140 | + if (_shouldMigrate) { |
| 141 | + updateOnboardingProfile({ strongholdVersion: StrongholdVersion.V2 }) |
| 142 | + } |
| 143 | + openUnlockStrongholdPopup(_shouldMigrate) |
124 | 144 | }
|
125 | 145 |
|
126 | 146 | async function getSeedFromSecretManager(): Promise<void> {
|
127 |
| - const managerId = await getProfileManager().id |
128 |
| - const secretManager = await api.getSecretManager(managerId) |
129 |
| - seed = await secretManager?.getSeed() |
| 147 | + try { |
| 148 | + const managerId = await getProfileManager().id |
| 149 | + const secretManager = await api.getSecretManager(managerId) |
| 150 | + seed = await secretManager?.getSeed() |
| 151 | + } catch (err) { |
| 152 | + showAppNotification({ |
| 153 | + type: 'error', |
| 154 | + message: 'Seed backup was unsuccessful.', |
| 155 | + subMessage: 'Please ensure your Stronghold file is valid and the password is correct.', |
| 156 | + }) |
| 157 | + closePopup() |
| 158 | + if (shouldResetOnboardingProfile) { |
| 159 | + resetOnboardingProfile() |
| 160 | + } |
| 161 | + console.error(err) |
| 162 | + } |
| 163 | + } |
| 164 | +
|
| 165 | + async function shouldMigrate(): Promise<boolean> { |
| 166 | + try { |
| 167 | + await restoreBackup(importFilePath, '', $onboardingProfile.network.protocol.bech32Hrp) |
| 168 | + } catch (err) { |
| 169 | + const isMigrationRequired = CLIENT_ERROR_REGEXES[ClientError.MigrationRequired].test(err?.error) |
| 170 | + return isMigrationRequired |
| 171 | + } |
130 | 172 | }
|
131 | 173 |
|
132 | 174 | if (!readFromFile) {
|
|
0 commit comments