Skip to content

Commit

Permalink
fix: duplicated db fetch (#6426)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuanyang233 authored Jun 5, 2022
1 parent a476bfb commit a70834b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
isSecp256k1Point,
isSecp256k1PrivateKey,
} from '@masknet/shared-base'
import { INTERNAL_getPasswordRequired } from '../../plugins/Wallet/services/wallet/password'

delegatePluginBackup(backupAllPlugins)
delegatePluginRestore(async function (backup) {
Expand Down Expand Up @@ -71,12 +72,13 @@ delegateWalletBackup(async function () {
return wallet.flat()
})
delegateWalletRestore(async function (backup) {
const password = await INTERNAL_getPasswordRequired()
for (const wallet of backup) {
try {
const name = wallet.name

if (wallet.privateKey.some)
await recoverWalletFromPrivateKey(name, await JWKToKey(wallet.privateKey.val, 'private'))
await recoverWalletFromPrivateKey(name, await JWKToKey(wallet.privateKey.val, 'private'), password)
else if (wallet.mnemonic.some) {
// fix a backup bug of pre-v2.2.2 versions
const accounts = await getDerivableAccounts(wallet.mnemonic.val.words, 1, 5)
Expand All @@ -85,6 +87,7 @@ delegateWalletRestore(async function (backup) {
name,
wallet.mnemonic.val.words,
index > -1 ? `${HD_PATH_WITHOUT_INDEX_ETHEREUM}/${index}` : wallet.mnemonic.val.path,
password,
)
}
} catch (error) {
Expand Down
7 changes: 4 additions & 3 deletions packages/mask/src/plugins/Wallet/services/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ export async function recoverWalletFromMnemonic(
name: string,
mnemonic: string,
derivationPath = `${HD_PATH_WITHOUT_INDEX_ETHEREUM}/0`,
initialPassword?: string,
) {
const password_ = await password.INTERNAL_getPasswordRequired()
const password_ = initialPassword ?? (await password.INTERNAL_getPasswordRequired())
const imported = await Mask.importMnemonic({
mnemonic,
password: password_,
Expand Down Expand Up @@ -325,8 +326,8 @@ export async function recoverWalletFromMnemonic(
}
}

export async function recoverWalletFromPrivateKey(name: string, privateKey: string) {
const password_ = await password.INTERNAL_getPasswordRequired()
export async function recoverWalletFromPrivateKey(name: string, privateKey: string, initialPassword_?: string) {
const password_ = initialPassword_ ?? (await password.INTERNAL_getPasswordRequired())
const imported = await Mask.importPrivateKey({
coin: api.Coin.Ethereum,
name,
Expand Down

0 comments on commit a70834b

Please sign in to comment.