Skip to content

Commit

Permalink
fix: Migrate error
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Jan 23, 2024
1 parent a555e32 commit 88025c3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
19 changes: 1 addition & 18 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
import { computed, Ref, inject } from 'vue'
import passwordManager from '@/lib/password'
import { API_NET, API_TARGET, Wallet } from 'meta-contract'
// setup vue-query
import { useQueryClient } from '@tanstack/vue-query'
Expand All @@ -14,24 +13,8 @@ import BgHueImg from './assets/images/bg-hue.png?url'
import TheFooter from './components/the-footer/Index.vue'
import TheHeader from './components/headers/TheHeader.vue'
import SecondaryHeader from './components/headers/SecondaryHeader.vue'
import { getLastLockTime } from './lib/lock'
const route = useRoute()
const router = useRouter()
// check password
passwordManager.has().then(hasPassword => {
const following = hasPassword ? '/wallet' : '/wallet/set-password'
router.push(following)
})
// check lock time
getLastLockTime().then(time => {
console.log({ time });
if (time === -1 || Date.now() - time > 1000 * 60 * 60) {
router.push('/lock')
}
})
const queryClient = useQueryClient()
queryClient.setDefaultOptions({
Expand Down
8 changes: 6 additions & 2 deletions src/lib/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export async function connectAccount(accountId: string) {
return true
}

export async function setV2Accounts(accountsMap: Map<string, Account>): Promise<void> {
await setStorage(ACCOUNT_STORAGE_HISTORY_KEYS[1], serializeAccountMap(accountsMap))
}

export async function setAccounts(accountsMap: Map<string, Account>): Promise<void> {
await setStorage(ACCOUNT_STORAGE_CURRENT_KEY, serializeAccountMap(accountsMap))
}
Expand Down Expand Up @@ -382,7 +386,7 @@ export async function needsMigrationV2(): Promise<boolean> {
}

export async function getLegacyAccounts(): Promise<V1Account[]> {
const legacyAccounts = await getStorage(ACCOUNT_STORAGE_HISTORY_KEYS[0])
const legacyAccounts = await getStorage(ACCOUNT_STORAGE_HISTORY_KEYS[0], { isParse: true })
if (!legacyAccounts) {
return []
}
Expand Down Expand Up @@ -456,7 +460,7 @@ export async function migrateV2(): Promise<void> {
}

// set new accounts map
await setAccounts(v2Accounts)
await setV2Accounts(v2Accounts)
}

export async function migrateV3(): Promise<void> {
Expand Down
20 changes: 18 additions & 2 deletions src/pages/wallet/Index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
<script lang="ts" setup>
import { ref } from 'vue'
import passwordManager from '@/lib/password'
import { getCurrentAccount, type Account } from '@/lib/account'
import { useRouter } from 'vue-router'
import Balance from './components/Balance.vue'
import AssetList from './components/AssetList.vue'
import AccountItem from '../accounts/components/Item.vue'
import { getLastLockTime } from '@/lib/lock'
const router = useRouter()
// check password
passwordManager.has().then(async hasPassword => {
if (!hasPassword) {
router.push('/wallet/set-password')
return
}
// check lock time
const time = await getLastLockTime()
if (time === -1 || Date.now() - time > 1000 * 60 * 60) {
router.push('/lock')
}
})
const currentAccount = ref<Account>()
getCurrentAccount().then((acc) => {
Expand Down
21 changes: 10 additions & 11 deletions src/pages/welcome/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { getAccounts, getCurrentAccount, migrateV2, needsMigrationV2 ,migrateV3} from '@/lib/account'
import { getAccounts, getCurrentAccount, migrateV2, needsMigrationV2, migrateV3 } from '@/lib/account'
import { sleep } from '@/lib/helpers'
import MetaletLogoImg from '@/assets/images/metalet-logo.png?url'
import { getStorage, setStorage } from '@/lib/storage'
const router = useRouter()
Expand All @@ -20,16 +21,19 @@ getAccounts().then((accounts) => {
const showingMigrationCover = ref(true)
needsMigrationV2().then(async (needsMigration: boolean) => {
if(await getStorage('needsMigration')){
return
}
if (!needsMigration) {
showingMigrationCover.value = false
return
}
if (needsMigration) {
showingMigrationCover.value = true
await migrateV2()
await migrateV3()
await setStorage("needsMigration", false)
await sleep(1000)
showingMigrationCover.value = false
Expand Down Expand Up @@ -68,22 +72,17 @@ needsMigrationV2().then(async (needsMigration: boolean) => {
</div>

<div class="flex flex-col items-stretch gap-y-4">
<button
class="gradient-bg rounded-md py-4 text-base leading-none text-white"
@click="() => $router.push('/accounts')"
v-if="accountsCount"
>
<button class="gradient-bg rounded-md py-4 text-base leading-none text-white"
@click="() => $router.push('/accounts')" v-if="accountsCount">
Connect
</button>

<button class="gradient-bg rounded-md py-4 text-base leading-none text-white" @click="createWallet">
Create Wallet
</button>

<button
class="rounded-md border border-primary-blue py-4 text-base leading-none"
@click="router.push('/wallet/import')"
>
<button class="rounded-md border border-primary-blue py-4 text-base leading-none"
@click="router.push('/wallet/import')">
Restore Wallet
</button>
</div>
Expand Down
18 changes: 9 additions & 9 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ const router = VueRouter.createRouter({
})

// 检查锁定状态;如果锁定,跳转到锁定页面
router.beforeEach(async (to, from) => {
if (to.path !== '/lock') {
const locked = await getStorage('locked')
if (locked) {
return '/lock'
}
}
})
// router.beforeEach(async (to, from) => {
// if (to.path !== '/lock') {
// const locked = await getStorage('locked')
// if (locked) {
// return '/lock'
// }
// }
// })

// 检查账号状态;如果没有当前账号,跳转到账号页面
router.beforeEach(async (to, from) => {
Expand All @@ -334,7 +334,7 @@ router.beforeEach(async (to, from) => {
}
}

if (await needsMigrationV2()) {
if (await needsMigrationV2() && await getStorage('needsMigration')) {
if (to.path !== '/welcome') {
return '/welcome'
}
Expand Down

0 comments on commit 88025c3

Please sign in to comment.