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

refactor: Onboarding #7763

Merged
merged 4 commits into from
Dec 12, 2023
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 @@ -31,6 +31,7 @@
async function onAddProfileClick(): Promise<void> {
onboardingRouter.set(new OnboardingRouter())
await initialiseOnboardingProfile(shouldBeDeveloperProfile())
console.log("shuld be created")

Check failure on line 34 in packages/desktop/views/login/views/SelectProfileView.svelte

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 34 in packages/desktop/views/login/views/SelectProfileView.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
$routerManager.goToAppContext(AppContext.Onboarding)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
}

async function _continue(): Promise<void> {
console.log($onboardingProfile)

Check failure on line 31 in packages/desktop/views/onboarding/views/complete-onboarding/views/FinishOnboardingView.svelte

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
// Note: needed to cover the cases where the user has waited so long that the stronghold is locked
if ($onboardingProfile?.restoreProfileType === RestoreProfileType.Stronghold) {
await setStrongholdPassword($onboardingProfile.strongholdPassword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { OnboardingLayout } from '@components'
import {
CreateProfileType,
initialiseProfileManagerFromOnboardingProfile,
onboardingProfile,
updateOnboardingProfile,
} from '@contexts/onboarding'
import { localize } from '@core/i18n'
import { ProfileType, clearProfileFromMemory, removeProfileFolder } from '@core/profile'
import { ProfileType, clearProfileFromMemory, getSecretManagerFromProfileType, getStorageDirectoryOfProfile, removeProfileFolder } from '@core/profile'
import features from '@features/features'
import { Animation, OnboardingButton, Text } from '@ui'
import { onMount } from 'svelte'
Expand All @@ -27,8 +26,10 @@
async function onProfileTypeClick(createProfileType: CreateProfileType): Promise<void> {
isBusy = { ...isBusy, [createProfileType]: true }
const type = createProfileType === CreateProfileType.Ledger ? ProfileType.Ledger : ProfileType.Software
updateOnboardingProfile({ createProfileType, type })
await initialiseProfileManagerFromOnboardingProfile()
const storagePath = await getStorageDirectoryOfProfile($onboardingProfile.id)
const secretManagerOptions = getSecretManagerFromProfileType(type, storagePath)
console.log("secretManagerOptions", secretManagerOptions)

Check failure on line 31 in packages/desktop/views/onboarding/views/create-profile/views/ChooseCreateProfileFlowView.svelte

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 31 in packages/desktop/views/onboarding/views/create-profile/views/ChooseCreateProfileFlowView.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
updateOnboardingProfile({ createProfileType, type, secretManagerOptions })
$createProfileRouter.next()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { activeProfile, activeWallets, addWalletPersistedDataToActiveProfile, createWallet, login } from '@core/profile'
import { IWallet, activeProfile, activeWallets, addWalletPersistedDataToActiveProfile, createWallet, login } from '@core/profile'

Check failure on line 1 in packages/shared/lib/contexts/onboarding/actions/completeOnboardingProcess.ts

View workflow job for this annotation

GitHub Actions / lint

'addWalletPersistedDataToActiveProfile' is defined but never used
import { get } from 'svelte/store'
import { OnboardingType } from '../enums'
import { addWalletPersistedDataToOnboardingProfile, onboardingProfile } from '../stores'

Check failure on line 4 in packages/shared/lib/contexts/onboarding/actions/completeOnboardingProcess.ts

View workflow job for this annotation

GitHub Actions / lint

'addWalletPersistedDataToOnboardingProfile' is defined but never used
import { createNewProfileFromOnboardingProfile } from './createNewProfileFromOnboardingProfile'
import { showBalanceOverviewPopup } from '@contexts/dashboard/stores'
import { buildWalletStateAndPersistedData, createNewWallet, IWalletState } from '@core/wallet'

Check failure on line 7 in packages/shared/lib/contexts/onboarding/actions/completeOnboardingProcess.ts

View workflow job for this annotation

GitHub Actions / lint

'buildWalletStateAndPersistedData' is defined but never used

Check failure on line 7 in packages/shared/lib/contexts/onboarding/actions/completeOnboardingProcess.ts

View workflow job for this annotation

GitHub Actions / lint

'createNewWallet' is defined but never used
import { localize } from '@core/i18n'

export async function completeOnboardingProcess(): Promise<void> {
// if we already have an active profile
// it means we are trying to load again after an error
// and we don't need to add it again
console.log("get(activeProfile), get(onboardingProfile)", get(activeProfile), get(onboardingProfile))
if (!get(activeProfile)?.id) {
createNewProfileFromOnboardingProfile()
}
Expand All @@ -19,15 +20,13 @@
const shouldRecoverAccounts = onboardingType === OnboardingType.Restore || onboardingType === OnboardingType.Claim
showBalanceOverviewPopup.set(shouldRecoverAccounts)


await createOnboardingWallet()

void login({ isFromOnboardingFlow: true, shouldRecoverAccounts })

onboardingProfile.set(undefined)
}

export async function createOnboardingWallet(name?: string, color?: string): Promise<IWalletState> {
export async function createOnboardingWallet(name?: string, color?: string): Promise<IWallet> {
// 1. Get the wallet name
const walletName = name || `${localize('general.account')} ${(get(activeWallets)?.length ?? 0) + 1}`;

Expand All @@ -36,17 +35,16 @@

// 3. Sync the wallet with the Node
// TODO(2.0): test & fix sync when we have iota2.0 nodes
//await account.sync(DEFAULT_SYNC_OPTIONS)

//await wallet.sync(DEFAULT_SYNC_OPTIONS)
// 4. Create a wrapper over the wallet instance and the persisted data
const [walletState, accountPersistedData] = await buildWalletStateAndPersistedData(wallet, walletName, color)
// const [walletState, accountPersistedData] = await buildWalletStateAndPersistedData(wallet, walletName, color)

// TODO(2.0) Fix
// addAccountToActiveAccounts(walletState)
addWalletPersistedDataToOnboardingProfile(walletState.id, accountPersistedData)
// addWalletPersistedDataToOnboardingProfile(walletState.id, accountPersistedData)
// TODO(2.0) Fix
// addEmptyAccountActivitiesToAllAccountActivities(walletState.id)


return walletState
return wallet
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export async function initialiseOnboardingProfile(
isDeveloperProfile: boolean,
destroyPreviousManager = false
): Promise<void> {
// TODO(2.0) Refactor this logic
if (get(profileManager)) {
if (destroyPreviousManager) {
if (get(isOnboardingLedgerProfile)) {
stopPollingLedgerNanoStatus()
}
await clearProfileFromMemory()
} else {
throw new OnboardingProfileManagerAlreadyInitializedError()
}
}
// TODO(2.0) Profile manager is gone, we should maybe check onboarding SecretManager insteadd
// if (get(profileManager)) {
// if (destroyPreviousManager) {
// if (get(isOnboardingLedgerProfile)) {
// stopPollingLedgerNanoStatus()
// }
// await clearProfileFromMemory()
// } else {
// throw new OnboardingProfileManagerAlreadyInitializedError()
// }
// }

const _newProfile = buildInitialOnboardingProfile(isDeveloperProfile)
onboardingProfile.set(_newProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export async function initialiseOnboardingProfileWithSecretManager(checkForExist

const secretManagerOptions = getSecretManagerFromProfileType(activeOnboardingProfile.type, storagePath)

console.log("secretManagerOptions", secretManagerOptions)

updateOnboardingProfile({ secretManagerOptions, hasInitialisedProfileManager: true })
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import { IOnboardingProfile, IShimmerClaimingWallet } from '../interfaces'
import { IBaseToken, IPersistedWalletData } from '@core/wallet/interfaces'
import { IPersistedNetwork } from '@core/network'

export const onboardingProfile = writable<IOnboardingProfile | null>(null)
export const onboardingProfile = writable<IOnboardingProfile | null | undefined>(null)

export const isOnboardingLedgerProfile: Readable<boolean> = derived(onboardingProfile, ($onboardingProfile) =>
isLedgerProfile($onboardingProfile?.type)
)
export const isOnboardingLedgerProfile: Readable<boolean> = derived(onboardingProfile, ($onboardingProfile) =>{
console.log("Changed?")
return isLedgerProfile($onboardingProfile?.type)
})

export const onboardingProfileNetwork: Readable<IPersistedNetwork | undefined> = derived(
onboardingProfile,
($onboardingProfile) => $onboardingProfile?.network
)

export function updateOnboardingProfile(payload: Partial<IOnboardingProfile>): void {
return onboardingProfile.update((state) => ({ ...state, ...payload }))
onboardingProfile.update((state) => ({ ...state, ...payload }))
console.log("UPDATED", get(onboardingProfile))
}

export function updateShimmerClaimingAccount(shimmerClaimingAccount: IShimmerClaimingWallet): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { api } from '@core/api';
import { SecretManager } from '@iota/sdk';
import { Readable, derived, get } from 'svelte/store';

const onboardingProfileSecretManagerOptions = derived(onboardingProfile, (onboardingProfile) => onboardingProfile?.secretManagerOptions);
onboardingProfile.subscribe(console.log)

export const onboardingProfileSecretManager: Readable<SecretManager | null> = derived(onboardingProfileSecretManagerOptions, (onboardingProfileSecretManagerOptions, set) => {
if (onboardingProfileSecretManagerOptions) {
api.createSecretManager(onboardingProfileSecretManagerOptions)

const onboardingProfileSecretManagerOptions = derived(onboardingProfile, ($onboardingProfile) => {
console.log("derived", $onboardingProfile?.secretManagerOptions)
return $onboardingProfile?.secretManagerOptions
});

export const onboardingProfileSecretManager: Readable<SecretManager | null> = derived(onboardingProfileSecretManagerOptions, ($onboardingProfileSecretManagerOptions, set) => {
console.log("creating SC", $onboardingProfileSecretManagerOptions)
if ($onboardingProfileSecretManagerOptions) {
api.createSecretManager($onboardingProfileSecretManagerOptions)
.then((secretManager) => {
console.log("secret manager created", secretManager)
set(secretManager)
})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadWallet } from '@core/wallet'
import { get } from 'svelte/store'
import { activeAccounts, activeProfile } from '../../stores'
import { activeWallets, activeProfile } from '../../stores'
import { getWallets } from '../getWallets'

export async function loadWallets(): Promise<void> {
Expand All @@ -14,7 +14,7 @@ export async function loadWallets(): Promise<void> {
const loadedWallets = await Promise.all(
walletResponse?.map((accountResponse) => loadWallet(accountResponse))
)
activeAccounts.set(loadedWallets) // TODO(2.0) We can't sort this like this: sort((a, b) => a.getMetadata().index - b.getMetadata().index)
activeWallets.set(loadedWallets) // TODO(2.0) We can't sort this like this: sort((a, b) => a.getMetadata().index - b.getMetadata().index)
hasLoadedAccounts.set(true)
}
}
19 changes: 11 additions & 8 deletions packages/shared/lib/core/profile/actions/createWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { generateRandomId } from '@core/utils'
import { get } from 'svelte/store'
import { IWallet } from '../interfaces'
import { activeProfile as activeProfileStore } from '../stores'
import { getStorageDirectoryOfProfile } from '../utils'
import { getSecretManagerFromProfileType, getStorageDirectoryOfProfile } from '../utils'
import { WalletOptions } from '@iota/sdk'

// TODO(2.0): Fix and finish this method
/* - __storage__/
Expand All @@ -15,20 +16,22 @@ import { getStorageDirectoryOfProfile } from '../utils'
export async function createWallet(activeProfile = get(activeProfileStore)): Promise<IWallet> {
const id = generateRandomId()
const storagePath = await getStorageDirectoryOfProfile(id)
const snapshotPath = ''
// const snapshotPath = ''

const walletOptions = {
const walletOptions: WalletOptions = {
clientOptions: activeProfile.clientOptions,
secretManager: {
stronghold: {
snapshotPath,
},
secretManager: getSecretManagerFromProfileType(activeProfile.type, storagePath),
bipPath: {
coinType: activeProfile.network.coinType,
account: 0,
addressIndex: 0
},
coinType: activeProfile.network.coinType
}
console.log("walletOptions", walletOptions);
const wallet = await api.createWallet(id, {
...walletOptions,
storagePath,
})

return wallet
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { USE_LEDGER_SIMULATOR } from '@core/ledger'
import { ProfileType } from '@core/profile'

// TODO(2.0) Fix all usages
export function getSecretManagerFromProfileType(type: ProfileType, storagePath?: string): SecretManagerType {
export function getSecretManagerFromProfileType(type?: ProfileType, storagePath?: string): SecretManagerType {
const strongholdSecretManager = {
stronghold: { snapshotPath: `${storagePath}/wallet.stronghold`, password: 'mellamobego' },
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { onboardingProfileSecretManager } from 'shared/lib/contexts/onboarding'
import { get } from 'svelte/store'
import { activeProfileSecretManager } from '../stores'

export async function storeMnemonic(mnemonic: string): Promise<void> {
const secretManager = get(activeProfileSecretManager)
// TODO(2.0) There are two secret managers, but we might only actually need one to store the mnemonic.
const secretManager = get(onboardingProfileSecretManager)

console.log("storeMnemonic", mnemonic, "secretManager", secretManager);


if (!secretManager) {
return undefined
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/lib/core/wallet/actions/buildWalletState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export async function buildWalletState(
basic: BigInt(0),
foundry: BigInt(0),
nft: BigInt(0),
delegation: BigInt(0)
},
nativeTokens: [],
nfts: [],
foundries: [],
potentiallyLockedOutputs: {},
accounts: [],
delegations: []
}

let depositAddress = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export async function buildWalletStateAndPersistedData(
shouldRevote: false,
walletOptions: {}
}
console.log("persistedWalletData", persistedWalletData);

const accountState = await buildWalletState(wallet, persistedWalletData)
console.log("accountState", accountState);
return [accountState, persistedWalletData]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { IWallet } from '@core/profile/interfaces'
import { Address } from '@iota/sdk/out/types'

// TODO(2.0) Fix all usages
export async function getAddressesWithOutputs(account: IWallet | IWalletState): Promise<AddressWithOutputs[]> {
export async function getAddressesWithOutputs(wallet: IWallet | IWalletState): Promise<AddressWithOutputs[]> {
let addressesWithOutputs: AddressWithOutputs[] = []
const addresses: Address[] = [] // await account.accounts
const outputs = await account.outputs()
const outputs = await wallet.outputs()

const outputMapped: AddressWithOutputs[] = outputs.reduce((acc: AddressWithOutputs[], output) => {
const address = getBech32AddressFromAddressTypes(output.address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get } from 'svelte/store'
import { selectedWallet } from '../stores/selected-wallet.store'

export async function setStrongholdPassword(password: string): Promise<void> {
console.log(password)
const wallet = get(selectedWallet)
// Otherwise error is thrown, if password is still present in memory
await wallet?.clearStrongholdPassword()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { IWrappedOutput } from '@core/wallet/interfaces'
import { getBech32AddressFromAddressTypes } from '../getBech32AddressFromAddressTypes'
import {
AddressUnlockCondition,
AliasAddress,
AliasOutput,
AccountOutput,
CommonOutput,
ExpirationUnlockCondition,
UnlockConditionType,
AccountAddress,
} from '@iota/sdk/out/types'

export function getSenderAddressFromInputs(inputs: IWrappedOutput[]): string | undefined {
Expand Down Expand Up @@ -38,10 +38,10 @@ export function getSenderAddressFromInputs(inputs: IWrappedOutput[]): string | u
}

// TODO: if additional metadata is added to an aliasOutput, we could use it to determine the EVM Sender.
const aliasId = (output as AliasOutput)?.aliasId
const accountId = (output as AccountOutput)?.accountId

if (aliasId) {
return getBech32AddressFromAddressTypes(new AliasAddress(aliasId))
if (accountId) {
return getBech32AddressFromAddressTypes(new AccountAddress(accountId))
}
}

Expand Down
Loading