From b47035641703cdf7900c0f6f47505c76a4df1cab Mon Sep 17 00:00:00 2001 From: AricRedemption Date: Sun, 19 May 2024 14:22:07 +0800 Subject: [PATCH] fix(wallet): resolve 'current wallet id not found' error after adding wallet --- src/lib/migrate.ts | 10 ++++---- src/lib/wallet.ts | 12 ++++++++++ src/pages/manage/Edit.vue | 2 ++ src/pages/welcome/components/Activate.vue | 17 +++++++++----- src/stores/ChainWalletsStore.ts | 10 ++++++-- src/stores/WalletStore.ts | 28 ++++++----------------- 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/lib/migrate.ts b/src/lib/migrate.ts index cd04f793..c7705460 100644 --- a/src/lib/migrate.ts +++ b/src/lib/migrate.ts @@ -4,14 +4,15 @@ import { encrypt, decrypt } from './crypto' import { toast } from '@/components/ui/toast' import { generateRandomString } from './helpers' import { type DerivedAccountDetail } from '@/lib/types' -import { AddressType as ScriptType, deriveAllAddresses } from './bip32-deriver' +import { getBackupV3Wallet, setBackupV3Wallet } from './backup' import { AddressType, Chain } from '@metalet/utxo-wallet-service' +import { AddressTypeRecord, migrateV3AddressTypeStorage } from './addressType' +import { AddressType as ScriptType, deriveAllAddresses } from './bip32-deriver' import { getV3Wallets, - hasV3Wallets, + setV3WalletsNum, getV3WalletsStorage, setV3WalletsStorage, - getCurrentWalletId, setCurrentWalletId, } from '@/lib/wallet' import { @@ -25,8 +26,6 @@ import { getLegacyAccounts, getCurrentAccountId, } from './account' -import { AddressTypeRecord, migrateV3AddressTypeStorage } from './addressType' -import { getBackupV3Wallet, setBackupV3Wallet } from './backup' export const ACCOUNT_Sync_Migrated_KEY = 'accounts_sync_migrated' export const ACCOUNT_V1_Migrated_KEY = 'accounts_v1_migrated' @@ -444,6 +443,7 @@ async function migrateV2ToV3(): Promise { } else if (successfulMigrations > 0 && failedMigrations === 0) { code = MigrateResultCode.SUCCESS } + await setV3WalletsNum(successfulMigrations) return { code, message: ` diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 45e8af49..d611433e 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -5,6 +5,7 @@ import { getCurrentAccountId } from './account' import { getV3AddressTypeStorage } from './addressType' import { Chain, MvcWallet, BtcWallet, AddressType, CoinType } from '@metalet/utxo-wallet-service' +const Wallet_Num = 'wallet_num' const CURRENT_WALLET_ID = 'currentWalletId' export const V3_WALLETS_STORAGE_KEY = 'wallets_v3' @@ -24,6 +25,17 @@ export async function getV3Wallets() { return Object.values(await getV3WalletsStorage()) } +export async function getV3WalletsNum() { + const wallets = await getV3Wallets() + return await storage.get(Wallet_Num, { + defaultValue: wallets.length, + }) +} + +export async function setV3WalletsNum(num: number) { + return await storage.set(Wallet_Num, num) +} + export async function addV3Wallet(wallet: V3Wallet) { const v3Wallet = await getV3Wallets() if (v3Wallet.find((w) => w.mnemonic === wallet.mnemonic)) { diff --git a/src/pages/manage/Edit.vue b/src/pages/manage/Edit.vue index 7f96b9e5..3dfc8dfb 100644 --- a/src/pages/manage/Edit.vue +++ b/src/pages/manage/Edit.vue @@ -6,6 +6,7 @@ import { type V3Wallet } from '@/lib/types' import Avatar from '@/components/Avatar.vue' import { goToPage, goToTab } from '@/lib/utils' import { getBackupV3Wallet } from '@/lib/backup' +import { WalletsStore } from '@/stores/WalletStore' import RemoveIcon from '@/assets/icons-v3/remove.svg' import PencilIcon from '@/assets/icons-v3/pencil.svg' import { EllipsisHorizontalIcon } from '@heroicons/vue/24/solid' @@ -78,6 +79,7 @@ const deleteWallet = async (walletId: string) => { getWallets() deleteWalletOpen.value = false } else { + WalletsStore.resetManager() goToTab('/welcome', true) } } diff --git a/src/pages/welcome/components/Activate.vue b/src/pages/welcome/components/Activate.vue index 27695b5e..8a281ff5 100644 --- a/src/pages/welcome/components/Activate.vue +++ b/src/pages/welcome/components/Activate.vue @@ -18,8 +18,8 @@ import { getBackupV3Wallet, setBackupV3Wallet } from '@/lib/backup' import { getCurrentAccountId, setCurrentAccountId } from '@/lib/account' import { genUID, formatIndex, Chain } from '@metalet/utxo-wallet-service' import { getServiceNetworkStorage, setServiceNetwork } from '@/lib/network' -import { addV3Wallet, getV3Wallets, setCurrentWalletId } from '@/lib/wallet' import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form' +import { addV3Wallet, getCurrentWalletId, getV3Wallets, getV3WalletsNum, setCurrentWalletId, setV3WalletsNum } from '@/lib/wallet' const { updateAllWallets } = useChainWalletsStore() @@ -86,7 +86,7 @@ const selectChain = (chain: Chain) => { } } -const onSubmit = handleSubmit(({ chains }) => { +const onSubmit = handleSubmit(async ({ chains }) => { updateServiceNetwork(chains as Chain[]) }) @@ -105,9 +105,10 @@ onMounted(async () => { } const walletId = genUID() const accountId = genUID() + const walletNum = await getV3WalletsNum() await addV3Wallet({ id: walletId, - name: `Wallet ${formatIndex(wallets.length + 1)}`, + name: `Wallet ${formatIndex(walletNum + 1)}`, mnemonic, mvcTypes, accounts: [ @@ -121,10 +122,14 @@ onMounted(async () => { error.value = err.message return }) - await WalletsStore.getWalletManager() - await WalletsStore.addWalletOnlyAccount(walletId, accountId) + await setV3WalletsNum(walletNum + 1) await setCurrentWalletId(walletId) await setCurrentAccountId(accountId) + if (WalletsStore.hasWalletManager()) { + await WalletsStore.addWalletOnlyAccount(walletId, accountId) + } else { + await WalletsStore.initWalletManager() + } await updateAllWallets() if (type === 'Import') { const walletIds = await getBackupV3Wallet() @@ -140,7 +145,7 @@ onMounted(async () => {
diff --git a/src/stores/ChainWalletsStore.ts b/src/stores/ChainWalletsStore.ts index add5fe08..c068b12b 100644 --- a/src/stores/ChainWalletsStore.ts +++ b/src/stores/ChainWalletsStore.ts @@ -1,7 +1,9 @@ import { FEEB } from '@/data/config' import { network } from '@/lib/network' import { createGlobalState } from '@vueuse/core' +import { getCurrentWalletId } from '@/lib/wallet' import { WalletsStore } from '@/stores/WalletStore' +import { getCurrentAccountId } from '@/lib/account' import { onMounted, ref, toRaw, computed } from 'vue' import { API_NET, API_TARGET, Wallet } from 'meta-contract' import { BtcWallet, Chain, MvcWallet } from '@metalet/utxo-wallet-service' @@ -48,8 +50,12 @@ export const useChainWalletsStore = createGlobalState(() => { } onMounted(async () => { - currentBTCWallet.value = await WalletsStore.getCurrentChainWallet(Chain.BTC) - currentMVCWallet.value = await WalletsStore.getCurrentChainWallet(Chain.MVC) + const currentWalletId = await getCurrentWalletId() + const currentAccountId = await getCurrentAccountId() + if (currentWalletId && currentAccountId) { + currentBTCWallet.value = await WalletsStore.getCurrentChainWallet(Chain.BTC) + currentMVCWallet.value = await WalletsStore.getCurrentChainWallet(Chain.MVC) + } }) return { diff --git a/src/stores/WalletStore.ts b/src/stores/WalletStore.ts index 163a9cd6..3d4c9ab4 100644 --- a/src/stores/WalletStore.ts +++ b/src/stores/WalletStore.ts @@ -1,13 +1,11 @@ import { reactive } from 'vue' import { getNet } from '@/lib/network' import { toast } from '@/components/ui/toast' -import { goToPage, goToTab } from '@/lib/utils' import { getCurrentAccountId } from '@/lib/account' import { getV3AddressTypeStorage } from '@/lib/addressType' import { WalletManager } from '@metalet/utxo-wallet-service' import { Chain, type Net } from '@metalet/utxo-wallet-service' import { - hasV3Wallets, getCurrentWalletId, getWalletOnlyAccount, getWalletOtherAccounts, @@ -39,11 +37,6 @@ const initWalletManager = async (): Promise => { return walletManager } catch (err) { toast({ title: (err as Error).message, toastType: 'fail' }) - if (await hasV3Wallets()) { - goToPage('/manage/wallets') - } else { - goToTab('/welcome', true) - } throw err } } @@ -52,6 +45,10 @@ const hasWalletManager = () => { return walletManager !== null } +const resetManager = () => { + walletManager = null +} + const getWalletManager = async (): Promise => { if (!walletManager) { walletManager = await initWalletManager() @@ -64,12 +61,7 @@ const loadActiveWalletOtherAccounts = async () => { const manager = await getWalletManager() const activeWallet = await getActiveWalletOtherAccounts() if (!activeWallet) { - if (await hasV3Wallets()) { - goToPage('/manage/wallets') - } else { - goToTab('/welcome', true) - } - return + throw new Error('No active wallet found. Please select a wallet.') } for (let account of activeWallet.accounts) { manager.addAccount(activeWallet.id, { @@ -84,12 +76,7 @@ const loadWalletOtherAccounts = async (walletId: string, accountId: string) => { const manager = await getWalletManager() const wallet = await getWalletOtherAccounts(walletId, accountId) if (!wallet) { - if (await hasV3Wallets()) { - goToPage('/manage/wallets') - } else { - goToTab('/welcome', true) - } - return + throw new Error('No wallet found. Please select a wallet.') } for (let account of wallet.accounts) { manager.addAccount(wallet.id, { @@ -104,13 +91,11 @@ const getAccountChainWallets = async () => { try { const currentWalletId = await getCurrentWalletId() if (!currentWalletId) { - goToPage('/manage/wallets') throw new Error('No current wallet found. Please select a wallet.') } const currentAccountId = await getCurrentAccountId() if (!currentAccountId) { - goToPage('/manage/wallets') throw new Error('No current account found. Please select an account.') } @@ -168,6 +153,7 @@ const loadAccount = async ( export const WalletsStore = reactive({ getWallets, loadAccount, + resetManager, getWalletManager, hasWalletManager, initWalletManager,