Skip to content

Commit

Permalink
fix:bugfix for create wallet with lock (#10651)
Browse files Browse the repository at this point in the history
* fix: bugfix for create wallet when is locked

* refactor: await

---------

Co-authored-by: guanbinrui <52657989+guanbinrui@users.noreply.github.com>
  • Loading branch information
nuanyang233 and guanbinrui committed Aug 28, 2023
1 parent f08ef67 commit 5ac3c42
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ const CreateMnemonic = memo(function CreateMnemonic() {
}, [words.join(' '), walletName, hasPassword])

const [{ loading }, onSubmit] = useAsyncFn(async () => {
await handlePasswordAndWallets(location.state?.password, location.state?.isReset)

const result = await handlePasswordAndWallets(location.state?.password, location.state?.isReset)
if (!result) return
const address = await WalletServiceRef.value.createWalletFromMnemonicWords(walletName, words.join(' '))
await WalletServiceRef.value.resolveMaskAccount([
{
Expand Down
10 changes: 9 additions & 1 deletion packages/dashboard/src/pages/CreateMaskWallet/context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback } from 'react'
import { createContainer } from 'unstated-next'
import { useWallets } from '@masknet/web3-hooks-base'
import { getDefaultWalletPassword, CrossIsolationMessages } from '@masknet/shared-base'
import { getDefaultWalletPassword, CrossIsolationMessages, PopupRoutes } from '@masknet/shared-base'
import { Web3 } from '@masknet/web3-providers'
import { ProviderType } from '@masknet/web3-shared-evm'
import { WalletServiceRef } from '@masknet/plugin-infra/dom'
import { Services } from '../../API.js'

function useContext() {
const wallets = useWallets()
Expand All @@ -24,13 +25,20 @@ function useContext() {
const handlePasswordAndWallets = useCallback(
async (password: string | undefined, isReset: boolean | undefined) => {
const hasPassword = await WalletServiceRef.value.hasPassword()

if (!hasPassword) await WalletServiceRef.value.setDefaultPassword()
const isLocked = await WalletServiceRef.value.isLocked()

if (hasPassword && isLocked) {
await Services.Helper.openPopupWindow(PopupRoutes.Unlock, { toBeClose: true })
return false
}
if (isReset) {
await resetWallets(password, isReset)
} else if (password && !hasPassword) {
await WalletServiceRef.value.changePassword(getDefaultWalletPassword(), password)
}
return true
},
[resetWallets],
)
Expand Down
25 changes: 15 additions & 10 deletions packages/mask/src/extension/popups/pages/Wallet/Unlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,23 @@ const Unlock = memo(() => {

const [{ value: verified, loading }, handleUnlock] = useAsyncFn(async () => {
const from = params.get('from')

const toBeClose = params.get('toBeClose')
const verified = await Services.Wallet.unlockWallet(password)

if (verified)
navigate(
from
? urlcat(from, {
tab: from === PopupRoutes.Personas ? PopupHomeTabType.ConnectedWallets : undefined,
})
: PopupRoutes.Wallet,
{ replace: true },
)
if (verified) {
if (toBeClose) {
await Services.Helper.removePopupWindow()
} else {
navigate(
from
? urlcat(from, {
tab: from === PopupRoutes.Personas ? PopupHomeTabType.ConnectedWallets : undefined,
})
: PopupRoutes.Wallet,
{ replace: true },
)
}
}
return verified
}, [password, params])

Expand Down
1 change: 1 addition & 0 deletions packages/mask/src/initialization/WalletRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ WalletServiceRef.value = {
resetPassword: Services.Wallet.resetPassword,
resolveMaskAccount: Services.Wallet.resolveMaskAccount,
setDefaultPassword: Services.Wallet.setDefaultPassword,
isLocked: Services.Wallet.isLocked,
setPassword: Services.Wallet.setPassword,
verifyPassword: Services.Wallet.verifyPassword,
exportPrivateKey: Services.Wallet.exportPrivateKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-infra/src/dom/useWalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface WalletBackupProvider {
hasPasswordWithDefaultOne(): Promise<boolean>
resetPassword(newPassword: string): Promise<void>
setDefaultPassword(): Promise<void>

isLocked(): Promise<boolean>
createMnemonicWords(): Promise<string[]>
exportMnemonicWords(address: string): Promise<string>
exportPrivateKey(address: string): Promise<string>
Expand Down

0 comments on commit 5ac3c42

Please sign in to comment.