Skip to content

Commit

Permalink
fix: request unlock during login when it has 0 accounts, so we can cr…
Browse files Browse the repository at this point in the history
…eate a new account (#7623)

* chore: Add testnet-2

* fix: Ask for password when profile has 0 accounts created

* fixes

* fix

---------

Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
marc2332 and begonaalvarezd authored Oct 19, 2023
1 parent 6bc4782 commit d3b2a43
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
13 changes: 9 additions & 4 deletions packages/shared/lib/core/ledger/actions/checkOrConnectLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import { get } from 'svelte/store'
import { LedgerConnectionState } from '../interfaces'
import { ledgerConnectionState } from '../stores'
import { handleError } from '@core/error/handlers/handleError'
import { IError } from '@core/error'

export function checkOrConnectLedger(
export async function checkOrConnectLedger(
callback: () => Promise<unknown> = async (): Promise<void> => {},
reopenPopup?: boolean
reopenPopup?: boolean,
cancelledCallback?: () => unknown
): Promise<unknown> {
const previousPopup = get(popupState)
function _callback(): Promise<unknown> {

function _callback(): Promise<unknown> | void {
if (reopenPopup) {
openPopup({ ...previousPopup, props: { ...previousPopup.props, _onMount: callback } })
} else {
return callback()
}
}

try {
const ledgerConnected = get(ledgerConnectionState) === LedgerConnectionState.CorrectAppOpen
if (ledgerConnected) {
Expand All @@ -26,10 +30,11 @@ export function checkOrConnectLedger(
hideClose: true,
props: {
onContinue: _callback,
onCancel: cancelledCallback,
},
})
}
} catch (err) {
handleError(err)
handleError(err as unknown as IError)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { get } from 'svelte/store'

export function checkActiveProfileAuth(
callback: () => Promise<unknown> = async () => {},
reopenPopup?: { stronghold?: boolean; ledger?: boolean }
reopenPopup?: { stronghold?: boolean; ledger?: boolean },
cancelledCallback?: () => unknown
): Promise<unknown> {
if (get(isSoftwareProfile)) {
return checkOrUnlockStronghold(callback, reopenPopup?.stronghold)
return checkOrUnlockStronghold(callback, reopenPopup?.stronghold, cancelledCallback)
} else if (get(isActiveLedgerProfile)) {
return checkOrConnectLedger(callback, reopenPopup?.ledger)
return checkOrConnectLedger(callback, reopenPopup?.ledger, cancelledCallback)
} else {
return Promise.resolve()
}
}
18 changes: 17 additions & 1 deletion packages/shared/lib/core/profile/actions/active-profile/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { logout } from './logout'
import { subscribeToWalletApiEventsForActiveProfile } from './subscribeToWalletApiEventsForActiveProfile'
import { checkAndUpdateActiveProfileNetwork } from './checkAndUpdateActiveProfileNetwork'
import { checkAndRemoveProfilePicture } from './checkAndRemoveProfilePicture'
import { checkActiveProfileAuth } from '@core/profile'

export async function login(loginOptions?: ILoginOptions): Promise<void> {
const loginRouter = get(routerManager).getRouterForAppContext(AppContext.Login)
Expand Down Expand Up @@ -92,7 +93,22 @@ export async function login(loginOptions?: ILoginOptions): Promise<void> {
* create one for the new profile.
*/
if (accounts?.length === 0) {
await createNewAccount()
const onUnlocked = new Promise<boolean>((resolve) => {
const onSuccess = () => {
resolve(true)
return Promise.resolve()
}
const onCancel = () => resolve(false)
const config = { stronghold: false, ledger: false }
checkActiveProfileAuth(onSuccess, config, onCancel)
})
const success = await onUnlocked
if (success) {
await createNewAccount()
} else {
resetLoginProgress()
return loginRouter.previous()
}
}

// Step 4: load accounts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import { isStrongholdUnlocked } from '@core/profile-manager'
import { closePopup, openPopup, PopupId, popupState } from '@auxiliary/popup'
import { get } from 'svelte/store'
import { handleError } from '@core/error/handlers/handleError'
import { IError } from '@core/error'

export async function checkOrUnlockStronghold(
callback: () => Promise<unknown> = async (): Promise<void> => {},
reopenPopup?: boolean
reopenPopup?: boolean,
cancelledCallback?: () => unknown
): Promise<unknown> {
const previousPopup = get(popupState)
function _callback(): Promise<unknown> {

function _callback(): Promise<unknown> | void {
if (reopenPopup) {
openPopup({ ...previousPopup, props: { ...previousPopup.props, _onMount: callback } })
} else {
return callback()
}
}

try {
const strongholdUnlocked = await isStrongholdUnlocked()
if (strongholdUnlocked) {
Expand All @@ -25,10 +29,11 @@ export async function checkOrUnlockStronghold(
id: PopupId.UnlockStronghold,
props: {
onSuccess: _callback,
onCancelled: cancelledCallback,
},
})
}
} catch (err) {
handleError(err)
handleError(err as unknown as IError)
}
}

0 comments on commit d3b2a43

Please sign in to comment.