diff --git a/packages/features/src/common/hooks/useAccount.ts b/packages/features/src/common/hooks/useAccount.ts index 0a4d92f9..94abcb33 100644 --- a/packages/features/src/common/hooks/useAccount.ts +++ b/packages/features/src/common/hooks/useAccount.ts @@ -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 @@ -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 } } diff --git a/packages/features/src/common/store/app.ts b/packages/features/src/common/store/app.ts index a53ec408..0d3b0b1a 100644 --- a/packages/features/src/common/store/app.ts +++ b/packages/features/src/common/store/app.ts @@ -23,6 +23,7 @@ type AppMutators = { setNetwork: (network: Multichain.MultiChainNetworks) => void setVaultState: (vaultState: VaultState) => void setVaultStateInitialized: () => void + setVaultStateUninitialized: () => void } type AppStore = AppState & AppMutators & AppQueries @@ -55,6 +56,10 @@ export const useAppStore = create()( setVaultStateInitialized: () => { const { setVaultState } = get() return setVaultState(VaultState.INITIALIZED) + }, + setVaultStateUninitialized: () => { + const { setVaultState } = get() + return setVaultState(VaultState.UNINITIALIZED) } }), { diff --git a/packages/features/src/lock/views/UnlockWallet.tsx b/packages/features/src/lock/views/UnlockWallet.tsx index de807980..522e1a07 100644 --- a/packages/features/src/lock/views/UnlockWallet.tsx +++ b/packages/features/src/lock/views/UnlockWallet.tsx @@ -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 { @@ -70,7 +72,7 @@ export const UnlockWalletView = () => { title="Unlock Wallet" button={{ label: 'Restart Wallet', - onClick: () => console.log('restart') + onClick: restartCurrentWallet }} /> {passwordError && ( diff --git a/packages/features/src/settings/views/Settings.tsx b/packages/features/src/settings/views/Settings.tsx index f2853fc0..7f2cecdc 100644 --- a/packages/features/src/settings/views/Settings.tsx +++ b/packages/features/src/settings/views/Settings.tsx @@ -1,5 +1,6 @@ import { Mina } from '@palladxyz/mina-core' import { + Button, Card, Label, RadioGroup, @@ -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 = () => { @@ -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 }) @@ -117,6 +120,9 @@ export const SettingsView = () => { + ) diff --git a/packages/vault/src/providers/providerStore.ts b/packages/vault/src/providers/providerStore.ts index 6fd7c58c..b7aa6d00 100644 --- a/packages/vault/src/providers/providerStore.ts +++ b/packages/vault/src/providers/providerStore.ts @@ -13,7 +13,7 @@ export const providerFactory = ( export const providerSlice: StateCreator = (set, get) => ({ providers: {}, - currentNetwork: '', + currentNetwork: 'devnet', setCurrentNetwork: (networkName) => { set( diff --git a/packages/vault/src/vault/vaultState.ts b/packages/vault/src/vault/vaultState.ts index b4abe584..26d7847c 100644 --- a/packages/vault/src/vault/vaultState.ts +++ b/packages/vault/src/vault/vaultState.ts @@ -86,6 +86,7 @@ export type GlobalVaultActions = { keyAgentType: KeyAgents, credentialName: CredentialName ) => Promise + restartWallet: () => void } export type GlobalVaultStore = GlobalVaultState & GlobalVaultActions diff --git a/packages/vault/src/vault/vaultStore.ts b/packages/vault/src/vault/vaultStore.ts index 72d8fee4..2387deb7 100644 --- a/packages/vault/src/vault/vaultStore.ts +++ b/packages/vault/src/vault/vaultStore.ts @@ -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) }