Skip to content

Commit

Permalink
fix(wallet): resolve 'current wallet id not found' error after adding…
Browse files Browse the repository at this point in the history
… wallet
  • Loading branch information
AricRedemption committed May 19, 2024
1 parent 7569643 commit b470356
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/lib/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
Expand Down Expand Up @@ -444,6 +443,7 @@ async function migrateV2ToV3(): Promise<MigrateResult> {
} else if (successfulMigrations > 0 && failedMigrations === 0) {
code = MigrateResultCode.SUCCESS
}
await setV3WalletsNum(successfulMigrations)
return {
code,
message: `
Expand Down
12 changes: 12 additions & 0 deletions src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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<number>(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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/pages/manage/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -78,6 +79,7 @@ const deleteWallet = async (walletId: string) => {
getWallets()
deleteWalletOpen.value = false
} else {
WalletsStore.resetManager()
goToTab('/welcome', true)
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/pages/welcome/components/Activate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -86,7 +86,7 @@ const selectChain = (chain: Chain) => {
}
}
const onSubmit = handleSubmit(({ chains }) => {
const onSubmit = handleSubmit(async ({ chains }) => {
updateServiceNetwork(chains as Chain[])
})
Expand All @@ -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: [
Expand All @@ -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()
Expand All @@ -140,7 +145,7 @@ onMounted(async () => {
<!-- TODO: Add a loading screen -->
<div
v-if="loading"
class="absolute top-0 left-0 w-screen h-screen bg-black bg-opacity-50 flex items-center justify-center"
class="absolute top-0 left-0 w-screen h-screen bg-black bg-opacity-50 flex items-center justify-center z-10"
>
<div class="w-[108px] h-[108px] bg-white rounded-lg flex flex-col items-center justify-center gap-4">
<LoadingIcon class="text-blue-primary" />
Expand Down
10 changes: 8 additions & 2 deletions src/stores/ChainWalletsStore.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 7 additions & 21 deletions src/stores/WalletStore.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -39,11 +37,6 @@ const initWalletManager = async (): Promise<WalletManager> => {
return walletManager
} catch (err) {
toast({ title: (err as Error).message, toastType: 'fail' })
if (await hasV3Wallets()) {
goToPage('/manage/wallets')
} else {
goToTab('/welcome', true)
}
throw err
}
}
Expand All @@ -52,6 +45,10 @@ const hasWalletManager = () => {
return walletManager !== null
}

const resetManager = () => {
walletManager = null
}

const getWalletManager = async (): Promise<WalletManager> => {
if (!walletManager) {
walletManager = await initWalletManager()
Expand All @@ -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, {
Expand All @@ -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, {
Expand All @@ -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.')
}

Expand Down Expand Up @@ -168,6 +153,7 @@ const loadAccount = async (
export const WalletsStore = reactive({
getWallets,
loadAccount,
resetManager,
getWalletManager,
hasWalletManager,
initWalletManager,
Expand Down

0 comments on commit b470356

Please sign in to comment.