Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Simplify onboarding wallet initialization #8404

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
activeProfile,
activeWallets,
addWalletPersistedDataToActiveProfile,
addWalletToActiveWallets,
createWallet,
DirectoryManager,
IPersistedProfile,
Expand All @@ -11,12 +10,7 @@ import {
import { get } from 'svelte/store'
import { onboardingProfile } from '../stores'
import { createNewProfileFromOnboardingProfile } from './createNewProfileFromOnboardingProfile'
import {
addEmptyWalletActivitiesToAllWalletActivities,
buildWalletStateAndPersistedData,
IWalletState,
} from '@core/wallet'
import { DEFAULT_SYNC_OPTIONS } from '@core/wallet/constants'
import { buildWalletPersistedData } from '@core/wallet'
import { localize } from '@core/i18n'
import { IOnboardingProfile } from '../interfaces'

Expand All @@ -34,33 +28,30 @@ export async function completeOnboardingProcess(): Promise<void> {
}
const { strongholdPassword } = profile

await initWallet(profile, strongholdPassword)
await initWalletAndPersistedData(profile, strongholdPassword)
void login({ isFromOnboardingFlow: true })

onboardingProfile.set(undefined)
}

export async function initWallet(profile: IOnboardingProfile, strongholdPassword?: string): Promise<IWalletState> {
export async function initWalletAndPersistedData(
profile: IOnboardingProfile,
strongholdPassword?: string
): Promise<void> {
// 1. Get the wallet name
const walletName = `${localize('general.wallet')} ${(get(activeWallets)?.length ?? 0) + 1}`

// 2. Create the wallet instance
const wallet = await createWallet(profile as IPersistedProfile, strongholdPassword)

// 3. Restore from stronghold if needed
if (profile.importFilePath && strongholdPassword) {
const strongholdBackupPath = await DirectoryManager.forStrongholdBackup(profile.id)
await wallet.restoreFromStrongholdSnapshot(strongholdBackupPath, strongholdPassword, true)
}

// 3. Sync the wallet with the Node
await wallet.sync(DEFAULT_SYNC_OPTIONS)

// 4. Create a wrapper over the wallet instance and the persisted data
const [walletState, walletPersistedData] = await buildWalletStateAndPersistedData(profile.id, wallet, walletName)

addWalletToActiveWallets(walletState)
addWalletPersistedDataToActiveProfile(walletState.id, walletPersistedData)
addEmptyWalletActivitiesToAllWalletActivities(walletState.id)
// 4. Create the persisted data
const walletPersistedData = await buildWalletPersistedData(profile.id, wallet, walletName)

return walletState
addWalletPersistedDataToActiveProfile(wallet.id, walletPersistedData)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ export async function buildWalletStateAndPersistedData(
name?: string,
color?: string
): Promise<[IWalletState, IPersistedWalletData]> {
const persistedWalletData = await buildWalletPersistedData(profileId, wallet, name, color)
const walletState = await buildWalletState(wallet, persistedWalletData)
walletState.balances = await getTotalWalletBalance(walletState, true)
return [walletState, persistedWalletData]
}

export async function buildWalletPersistedData(
profileId: string,
wallet: IWallet,
name?: string,
color?: string
): Promise<IPersistedWalletData> {
const walletPath = await DirectoryManager.forWallet(profileId, wallet.id)
const secretManagerPath = await DirectoryManager.forSecretManager(profileId)

const walletOptions = getWalletOptions(get(activeProfile), walletPath, secretManagerPath)

const persistedWalletData: IPersistedWalletData = {
return {
name: name || `${localize('general.wallet')}`,
color: color || getRandomWalletColor(),
hidden: false,
shouldRevote: false,
walletOptions,
}
const walletState = await buildWalletState(wallet, persistedWalletData)
walletState.balances = await getTotalWalletBalance(walletState, true)
return [walletState, persistedWalletData]
}
Loading