Skip to content

Commit 751df16

Browse files
refactor(wip): refactor profilemanager core module
1 parent ee2931c commit 751df16

File tree

84 files changed

+272
-365
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+272
-365
lines changed

packages/desktop/components/modals/AccountActionsMenu.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { selectedAccount } from '@core/account/stores'
55
import { localize } from '@core/i18n'
66
import { activeAccounts, activeProfile, isActiveLedgerProfile, visibleActiveAccounts } from '@core/profile/stores'
7-
import { deleteAccount } from '@core/profile-manager/actions'
7+
import { deleteWallet } from '@core/profile-manager/actions'
88
99
import { Icon } from '@auxiliary/icon/enums'
1010
import { openPopup, PopupId } from '@auxiliary/popup'
@@ -62,7 +62,7 @@
6262
id: PopupId.DeleteAccount,
6363
props: {
6464
account: selectedAccount,
65-
deleteAccount,
65+
deleteWallet,
6666
},
6767
})
6868
modal?.close()

packages/desktop/components/popups/DeleteAccountPopup.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { handleError } from '@core/error/handlers/handleError'
99
import { TextHintVariant } from 'shared/components/enums'
1010
11-
export let deleteAccount: (index: number) => Promise<void> = async () => {}
11+
export let deleteWallet: (index: number) => Promise<void> = async () => {}
1212
1313
let password: string
1414
let error: string
@@ -26,7 +26,7 @@
2626
if ($isSoftwareProfile) {
2727
await setStrongholdPassword(password)
2828
}
29-
await deleteAccount($selectedAccount?.index)
29+
await deleteWallet($selectedAccount?.index)
3030
closePopup()
3131
} catch (err) {
3232
error = err.error

packages/desktop/electron/preload.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ try {
124124
// TODO(2.0): also remove from file system
125125
deleteWallet(id) {
126126
if (id && id in wallets) {
127+
const wallet = wallets[id]
128+
wallet.destroy()
127129
delete wallets[id]
128130
}
129131
},
@@ -145,7 +147,11 @@ try {
145147
return accounts
146148
},
147149
clearWalletsFromMemory() {
148-
Object.keys(wallets).forEach((id) => delete wallets[id])
150+
Object.keys(wallets).forEach((id) => {
151+
const wallet = wallets[id]
152+
wallet.destroy()
153+
delete wallets[id]
154+
})
149155
},
150156
async migrateStrongholdSnapshotV2ToV3(currentPath, newPath, currentPassword, newPassword) {
151157
const snapshotSaltV2 = 'wallet.rs'

packages/desktop/views/login/views/SelectProfileView.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
} from '@core/app'
1010
import { localize } from '@core/i18n'
1111
import { ProfileType, loadPersistedProfileIntoActiveProfile, profiles, removeProfileFolder } from '@core/profile'
12-
import { destroyProfileManager } from '@core/profile-manager/actions'
12+
import { clearProfileFromMemory } from '@core/profile-manager/actions'
1313
import { loginRouter, routerManager } from '@core/router'
1414
import features from '@features/features'
1515
import { Icon, Logo, Profile } from '@ui'
@@ -41,7 +41,7 @@
4141
// Clean up if user has navigated back to this view from onboarding
4242
if ($onboardingProfile) {
4343
if ($onboardingProfile.hasInitialisedProfileManager) {
44-
await destroyProfileManager()
44+
await clearProfileFromMemory()
4545
await removeProfileFolder($onboardingProfile.id)
4646
}
4747
$onboardingProfile = undefined

packages/desktop/views/onboarding/views/create-profile/views/ChooseCreateProfileFlowView.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import { Animation, OnboardingButton, Text } from '@ui'
1313
import { onMount } from 'svelte'
1414
import { createProfileRouter } from '../create-profile-router'
15-
import { destroyProfileManager } from '@core/profile-manager/actions'
15+
import { clearProfileFromMemory } from '@core/profile-manager/actions'
1616
import { Icon as IconEnum } from '@auxiliary/icon'
1717
import { AnimationEnum } from '@auxiliary/animation'
1818
@@ -40,7 +40,7 @@
4040
onMount(async () => {
4141
// Clean up if user has navigated back to this view
4242
if ($onboardingProfile.hasInitialisedProfileManager) {
43-
await destroyProfileManager()
43+
await clearProfileFromMemory()
4444
await removeProfileFolder($onboardingProfile.id)
4545
}
4646
updateOnboardingProfile({ type: undefined, createProfileType: undefined, hasInitialisedProfileManager: false })

packages/desktop/views/onboarding/views/restore-profile/views/ChooseRestoreProfileFlowView.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import { Animation, OnboardingButton, Text } from '@ui'
1414
import { onMount } from 'svelte'
1515
import { restoreProfileRouter } from '../restore-profile-router'
16-
import { destroyProfileManager } from '@core/profile-manager/actions'
16+
import { clearProfileFromMemory } from '@core/profile-manager/actions'
1717
import { Icon as IconEnum } from '@auxiliary/icon'
1818
import { AnimationEnum } from '@auxiliary/animation'
1919
@@ -42,7 +42,7 @@
4242
onMount(async () => {
4343
// Clean up if user has navigated back to this view
4444
if ($onboardingProfile.hasInitialisedProfileManager) {
45-
await destroyProfileManager()
45+
await clearProfileFromMemory()
4646
await removeProfileFolder($onboardingProfile.id)
4747
}
4848
updateOnboardingProfile({ type: undefined, restoreProfileType: undefined, hasInitialisedProfileManager: false })

packages/desktop/views/update-stronghold/views/ChangePasswordView.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
const profileManagerOptions = await buildProfileManagerOptionsFromProfileData($activeProfile)
105105
const { storagePath, coinType, clientOptions, secretManager } = profileManagerOptions
106106
updateActiveProfile({ clientOptions })
107+
// TODO(2.0): Update initialiseProfileManager to new logic
107108
const manager = await initialiseProfileManager(
108109
$activeProfile?.id,
109110
storagePath,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { get } from 'svelte/store'
22

3-
import { destroyProfileManager, profileManager } from '@core/profile-manager'
3+
import { clearProfileFromMemory, profileManager } from '@core/profile-manager'
44
import { removeProfileFolder } from '@core/profile'
55

66
export async function cleanupOnboardingProfileManager(): Promise<void> {
@@ -10,6 +10,6 @@ export async function cleanupOnboardingProfileManager(): Promise<void> {
1010

1111
const { id } = get(profileManager)
1212

13-
await destroyProfileManager()
13+
await clearProfileFromMemory()
1414
await removeProfileFolder(id)
1515
}

packages/shared/lib/contexts/onboarding/actions/createShimmerClaimingProfileManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export async function createShimmerClaimingProfileManager(): Promise<void> {
1818
const clientOptions = $onboardingProfile?.clientOptions
1919
const secretManager = getSecretManagerFromProfileType($onboardingProfile?.type, storagePath)
2020

21+
// TODO(2.0): Fix all shimmer claiming
2122
const manager = await initialiseProfileManager(
2223
generateRandomId(),
2324
storagePath,

packages/shared/lib/contexts/onboarding/actions/deleteOnboardingProfile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { destroyProfileManager } from '@core/profile-manager'
1+
import { clearProfileFromMemory } from '@core/profile-manager'
22
import { get } from 'svelte/store'
33
import { onboardingProfile } from '../stores'
44
import { removeProfileFolder } from '@core/profile/utils'
@@ -12,7 +12,7 @@ export async function deleteOnboardingProfile(): Promise<void> {
1212
const profile = get(onboardingProfile)
1313
if (profile) {
1414
try {
15-
await destroyProfileManager()
15+
await clearProfileFromMemory()
1616
await removeProfileFolder(profile.id)
1717
} catch (err) {
1818
console.error(err)

packages/shared/lib/contexts/onboarding/actions/destroyShimmerClaimingProfileManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { get } from 'svelte/store'
22

3-
import { destroyProfileManager } from '@core/profile-manager'
3+
import { clearProfileFromMemory } from '@core/profile-manager'
44
import { Platform } from '@core/app'
55

66
import { getTemporaryProfileManagerStorageDirectory } from '../helpers'
@@ -11,7 +11,7 @@ export async function destroyShimmerClaimingProfileManager(): Promise<void> {
1111
if (!_shimmerClaimingProfileManager) {
1212
return
1313
}
14-
await destroyProfileManager(shimmerClaimingProfileManager)
14+
await clearProfileFromMemory(shimmerClaimingProfileManager)
1515
const profilePath = await getTemporaryProfileManagerStorageDirectory()
1616
await Platform.removeProfileFolder(profilePath)
1717
}

packages/shared/lib/contexts/onboarding/actions/initialiseOnboardingFlow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { get } from 'svelte/store'
33
import { stopPollingLedgerNanoStatus } from '@core/ledger/actions'
44
import { isPollingLedgerDeviceStatus } from '@core/ledger/stores'
55
import { getDefaultPersistedNetwork } from '@core/network/utils'
6-
import { destroyProfileManager, unsubscribeFromWalletApiEvents } from '@core/profile-manager/actions'
6+
import { clearProfileFromMemory, unsubscribeFromWalletApiEvents } from '@core/profile-manager/actions'
77
import { resetActiveProfile } from '@core/profile/actions'
88

99
import { IOnboardingInitialisationOptions } from '../interfaces'
@@ -20,7 +20,7 @@ export async function initialiseOnboardingFlow(options: IOnboardingInitialisatio
2020
stopPollingLedgerNanoStatus()
2121
}
2222
await unsubscribeFromWalletApiEvents()
23-
await destroyProfileManager()
23+
await clearProfileFromMemory()
2424

2525
const { isDeveloperProfile, networkId } = options
2626

packages/shared/lib/contexts/onboarding/actions/initialiseOnboardingProfile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stopPollingLedgerNanoStatus } from '@core/ledger'
2-
import { destroyProfileManager, profileManager } from '@core/profile-manager'
2+
import { clearProfileFromMemory, profileManager } from '@core/profile-manager'
33
import { get } from 'svelte/store'
44
import { OnboardingProfileManagerAlreadyInitializedError } from '../errors'
55
import { buildInitialOnboardingProfile } from '../helpers'
@@ -17,7 +17,7 @@ export async function initialiseOnboardingProfile(
1717
if (get(isOnboardingLedgerProfile)) {
1818
stopPollingLedgerNanoStatus()
1919
}
20-
await destroyProfileManager()
20+
await clearProfileFromMemory()
2121
} else {
2222
throw new OnboardingProfileManagerAlreadyInitializedError()
2323
}
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import { get } from 'svelte/store'
22

33
import {
4-
buildProfileManagerOptionsFromProfileData,
5-
initialiseProfileManager,
6-
profileManager,
4+
getSecretManagerFromProfileType
75
} from '@core/profile-manager'
6+
import { onboardingProfileSecretManager } from '../stores'
87

8+
import { getStorageDirectoryOfProfile, removeProfileFolder } from '@core/profile'
99
import { onboardingProfile, updateOnboardingProfile } from '../stores'
10-
import { removeProfileFolder } from '@core/profile'
11-
12-
export async function initialiseProfileManagerFromOnboardingProfile(checkForExistingManager?: boolean): Promise<void> {
13-
const existingManager = get(profileManager)
14-
if (existingManager) {
15-
if (!checkForExistingManager) {
16-
await existingManager.destroy()
17-
removeProfileFolder(existingManager.id)
10+
11+
export async function initialiseProfileManagerFromOnboardingProfile(checkForExistingSecretManager?: boolean): Promise<void> {
12+
const secretManager = get(onboardingProfileSecretManager)
13+
const activeOnboardingProfile = get(onboardingProfile)
14+
15+
if (!activeOnboardingProfile) {
16+
return
17+
}
18+
19+
if (secretManager) {
20+
if (!checkForExistingSecretManager) {
21+
removeProfileFolder(activeOnboardingProfile.id)
1822
} else {
1923
return
2024
}
2125
}
2226

23-
const profileManagerOptions = await buildProfileManagerOptionsFromProfileData(get(onboardingProfile))
24-
const { storagePath, coinType, clientOptions, secretManager } = profileManagerOptions
25-
const { id } = get(onboardingProfile)
26-
const manager = await initialiseProfileManager(id, storagePath, coinType, clientOptions, secretManager)
27-
profileManager.set(manager)
28-
updateOnboardingProfile({ hasInitialisedProfileManager: true })
27+
const storagePath = await getStorageDirectoryOfProfile(activeOnboardingProfile.id)
28+
29+
const secretManagerOptions = getSecretManagerFromProfileType(activeOnboardingProfile.type, storagePath)
30+
31+
updateOnboardingProfile({ secretManagerOptions, hasInitialisedProfileManager: true })
2932
}

packages/shared/lib/contexts/onboarding/actions/migrateStrongholdFromOnboardingProfile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { get } from 'svelte/store'
22

33
import { getStorageDirectoryOfProfile } from '@core/profile/utils'
4-
import { destroyProfileManager } from '@core/profile-manager/actions'
4+
import { clearProfileFromMemory } from '@core/profile-manager/actions'
55
import { api } from '@core/profile-manager/api'
66
import { getSecretManagerPath } from '@core/profile-manager/utils'
77
import { StrongholdVersion } from '@core/stronghold/enums'
@@ -23,6 +23,6 @@ export async function migrateStrongholdFromOnboardingProfile(password: string):
2323
updateOnboardingProfile({ strongholdVersion: StrongholdVersion.V3 })
2424
}
2525

26-
await destroyProfileManager()
26+
await clearProfileFromMemory()
2727
await initialiseProfileManagerFromOnboardingProfile()
2828
}

packages/shared/lib/contexts/onboarding/interfaces/onboarding-profile.interface.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { IPersistedProfile } from '@core/profile'
1+
import { IPersistedProfile, ProfileType } from '@core/profile'
22
import { CreateProfileType, OnboardingType, RestoreProfileType } from '../enums'
33
import { ImportFile, Mnemonic } from '../types'
44
import { IShimmerClaimingAccount } from './shimmer-claiming-account.interface'
55

6-
export interface IOnboardingProfile extends IPersistedProfile {
6+
export interface IOnboardingProfile extends Omit<Partial<IPersistedProfile>, 'id' | 'type'> {
7+
id: string,
8+
type: ProfileType
9+
710
// Onboarding flow indicators
811
onboardingType?: OnboardingType
912
createProfileType?: CreateProfileType
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './onboarding-profile.store'
2+
export * from './onboarding-secret-manager.store'
23
export * from './shimmer-claiming-profile-manager.store'
34
export * from './shimmer-claiming-transactions.store'

packages/shared/lib/contexts/onboarding/stores/onboarding-profile.store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { derived, get, Readable, writable } from 'svelte/store'
22

3-
import { isLedgerProfile } from '@core/profile'
3+
import { isLedgerProfile, ProfileType } from '@core/profile'
44

55
import { IOnboardingProfile, IShimmerClaimingAccount } from '../interfaces'
66
import { IBaseToken } from '@core/wallet/interfaces'
77
import { IPersistedNetwork } from '@core/network'
88

9-
export const onboardingProfile = writable<Partial<IOnboardingProfile>>(null)
9+
10+
export const onboardingProfile = writable<IOnboardingProfile | null>(null)
1011

1112
export const isOnboardingLedgerProfile: Readable<boolean> = derived(onboardingProfile, ($onboardingProfile) =>
1213
isLedgerProfile($onboardingProfile?.type)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { onboardingProfile } from '@contexts/onboarding/stores';
2+
import { api } from '@core/api';
3+
import { SecretManager } from '@iota/sdk';
4+
import { Readable, derived } from 'svelte/store';
5+
6+
const onboardingProfileSecretManagerOptions = derived(onboardingProfile, (onboardingProfile) => onboardingProfile.secretManagerOptions);
7+
8+
export const onboardingProfileSecretManager: Readable<SecretManager | null> = derived(onboardingProfileSecretManagerOptions, (onboardingProfileSecretManagerOptions, set) => {
9+
if (onboardingProfileSecretManagerOptions) {
10+
api.createSecretManager(onboardingProfileSecretManagerOptions)
11+
.then((secretManager) => {
12+
set(secretManager)
13+
})
14+
} else {
15+
set(null)
16+
}
17+
})

packages/shared/lib/core/account/interfaces/account.interface.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@ import type {
3535
ParticipationEventType,
3636
ParticipationEventId,
3737
Burn,
38+
WalletEventType,
39+
IAuth,
3840
} from '@iota/sdk/out/types'
41+
import type {
42+
IClientOptions,
43+
GenerateAddressOptions,
44+
LedgerNanoStatus,
45+
INodeInfoWrapper,
46+
SyncOptions,
47+
WalletEvent,
48+
WalletEventType,
49+
} from '@iota/sdk/out/types'
50+
import { WalletApiEventHandler } from '@core/wallet'
3951

52+
// TODO(2.0): rename to IWallet & check all functions in this interface to make sure they still exist
4053
export interface IAccount {
4154
isStrongholdPasswordAvailable(): Promise<boolean>
4255
getClient(): Promise<Client>
@@ -118,4 +131,19 @@ export interface IAccount {
118131
transactions(): Promise<Transaction[]>
119132
unspentOutputs(filterOptions?: FilterOptions): Promise<OutputData[]>
120133
prepareVote(eventId?: string, answers?: number[]): Promise<PreparedTransaction>
134+
listen(eventTypes: WalletEventType[], callback: WalletApiEventHandler): Promise<void>
135+
clearListeners(eventTypes: WalletEventType[]): Promise<void>
136+
backup(destination: string, password: string): Promise<void>
137+
destroy(): Promise<void>
138+
emitTestEvent(event: WalletEvent): Promise<void>
139+
restoreBackup(
140+
source: string,
141+
password: string,
142+
ignoreIfCoinTypeMismatch: boolean,
143+
ignoreIfBech32Mismatch: string
144+
): Promise<void>
145+
setClientOptions(options: IClientOptions): Promise<void>
146+
startBackgroundSync(options?: SyncOptions, intervalInMilliseconds?: number): Promise<void>
147+
stopBackgroundSync(): Promise<void>
148+
updateNodeAuth(url: string, auth?: IAuth): Promise<void>
121149
}

packages/shared/lib/core/api/interfaces/api.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IAccount } from '@core/account'
22
import { SecretManager } from '@iota/sdk'
33
import { AccountId, FoundryId, HexEncodedString, NftId, Output, OutputId, SecretManagerType, TransactionId, WalletOptions, Bech32Address } from '@iota/sdk/out/types'
44

5+
// TODO(2.0): Every method should return a promise (maybe except Utils, needs research)
56
export interface IApi {
67
createSecretManager(options: SecretManagerType): Promise<SecretManager>
78
createAccount(id: string, payload: WalletOptions): Promise<IAccount>

packages/shared/lib/core/network/interfaces/auth.interface.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/shared/lib/core/network/interfaces/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './auth.interface'
21
export * from './chain-metadata.interface'
32
export * from './connected-chain.interface'
43
export * from './client-options.interface'

0 commit comments

Comments
 (0)