Skip to content

Commit

Permalink
feat(wallet): add wallet reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Dec 13, 2023
1 parent be8c281 commit 0e00298
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/features/src/common/hooks/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const useAccount = () => {
const { toast } = useToast()
const currentWallet = useVault((state) => state.getCurrentWallet())
const getAccountInfo = useVault((state) => state.getAccountInfo)
const restartWallet = useVault((state) => state.restartWallet)
const network = useAppStore((state) => state.network)
const setVaultStateUninitialized = useAppStore(
(state) => state.setVaultStateUninitialized
)
const { publicKey } = currentWallet.accountInfo
const swr = useSWR(
publicKey
Expand Down Expand Up @@ -51,12 +55,18 @@ export const useAccount = () => {
await useVault.persist.rehydrate()
return navigate('/unlock')
}
const restartCurrentWallet = () => {
restartWallet()
setVaultStateUninitialized()
return navigate('/')
}
return {
...swr,
minaBalance,
gradientBackground,
copyWalletAddress,
publicKey,
lockWallet
lockWallet,
restartCurrentWallet
}
}
5 changes: 5 additions & 0 deletions packages/features/src/common/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type AppMutators = {
setNetwork: (network: Multichain.MultiChainNetworks) => void
setVaultState: (vaultState: VaultState) => void
setVaultStateInitialized: () => void
setVaultStateUninitialized: () => void
}

type AppStore = AppState & AppMutators & AppQueries
Expand Down Expand Up @@ -55,6 +56,10 @@ export const useAppStore = create<AppStore>()(
setVaultStateInitialized: () => {
const { setVaultState } = get()
return setVaultState(VaultState.INITIALIZED)
},
setVaultStateUninitialized: () => {
const { setVaultState } = get()
return setVaultState(VaultState.UNINITIALIZED)
}
}),
{
Expand Down
4 changes: 3 additions & 1 deletion packages/features/src/lock/views/UnlockWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { useNavigate } from 'react-router-dom'

import { WizardLayout } from '../../common/components'
import { ViewHeading } from '../../common/components/ViewHeading'
import { useAccount } from '../../common/hooks/useAccount'

export const UnlockWalletView = () => {
const [showPassword, setShowPassword] = useState(false)
const currentWallet = useVault((state) => state.getCurrentWallet())
const { restartCurrentWallet } = useAccount()
const [passwordError, setPasswordError] = useState(false)
const navigate = useNavigate()
const {
Expand Down Expand Up @@ -70,7 +72,7 @@ export const UnlockWalletView = () => {
title="Unlock Wallet"
button={{
label: 'Restart Wallet',
onClick: () => console.log('restart')
onClick: restartCurrentWallet
}}
/>
{passwordError && (
Expand Down
6 changes: 6 additions & 0 deletions packages/features/src/settings/views/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mina } from '@palladxyz/mina-core'
import {
Button,
Card,
Label,
RadioGroup,
Expand All @@ -14,6 +15,7 @@ import { useSWRConfig } from 'swr'

import { AppLayout } from '../../common/components/AppLayout'
import { ViewHeading } from '../../common/components/ViewHeading'
import { useAccount } from '../../common/hooks/useAccount'
import { useAppStore } from '../../common/store/app'

export const SettingsView = () => {
Expand All @@ -26,6 +28,7 @@ export const SettingsView = () => {
setNetwork: state.setNetwork,
network: state.network
}))
const { restartCurrentWallet } = useAccount()
const handleNetworkSwitch = async (value: Mina.Networks) => {
await switchNetwork(value)
await mutate(() => true, undefined, { revalidate: false })
Expand Down Expand Up @@ -117,6 +120,9 @@ export const SettingsView = () => {
</Card>
</div>
</div>
<Button variant="destructive" onClick={restartCurrentWallet}>
Restart Wallet
</Button>
</div>
</AppLayout>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vault/src/providers/providerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const providerFactory = (

export const providerSlice: StateCreator<ProviderStore> = (set, get) => ({
providers: {},
currentNetwork: '',
currentNetwork: 'devnet',

setCurrentNetwork: (networkName) => {
set(
Expand Down
1 change: 1 addition & 0 deletions packages/vault/src/vault/vaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type GlobalVaultActions = {
keyAgentType: KeyAgents,
credentialName: CredentialName
) => Promise<void>
restartWallet: () => void
}

export type GlobalVaultStore = GlobalVaultState & GlobalVaultActions
14 changes: 14 additions & 0 deletions packages/vault/src/vault/vaultStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ export const useVault = create<
})
ensureAccount(network, derivedCredential.address)
await _syncWallet(network, derivedCredential)
},
restartWallet: () => {
const {
getCurrentWallet,
keyAgentName,
currentNetwork,
removeKeyAgent,
removeAccount,
removeCredential
} = get()
const currentWallet = getCurrentWallet()
removeAccount(currentNetwork as Networks, '')
removeKeyAgent(keyAgentName)
removeCredential(currentWallet.credential.credentialName)
}
}),
{ name: 'PalladVault', storage: createJSONStorage(getSecurePersistence) }
Expand Down

0 comments on commit 0e00298

Please sign in to comment.