Skip to content

Commit a65f871

Browse files
authored
Merge pull request #90 from palladians/fix/restructurize-vault-stores
fix(app): start restructurization
2 parents cfde9f7 + 0323b89 commit a65f871

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1241
-851
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ This is a monorepo for all the Pallad-related code.
5151
- `key-management` - Blockchain agnostic key management.
5252
- `mina-core` - Core Mina Package SDK.
5353
- `mina-graphql` - GraphQL API client for the Mina Protocol.
54-
- `mina-wallet` - Wrapper package for Mina Protocol wallets.
5554
- `offchain-data` - Client for fetching off-chain data like fiat price.
5655
- `persistence` - Persistence logic for wallet related data.
5756
- `ui` - UI framework based on shadcn/ui and Tailwind.

packages/features/.ladle/utils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { useEffect } from 'react'
22
import { MinaPayload, Network } from '@palladxyz/key-management'
3-
import { useWallet } from '../src/common/hooks/useWallet'
43
import { Mina } from '@palladxyz/mina-core'
4+
import { useVault } from '@palladxyz/vault'
55

66
const MNEMONIC =
77
'habit hope tip crystal because grunt nation idea electric witness alert like'
88

99
export const useStoriesWallet = () => {
10-
const { wallet } = useWallet()
10+
const restoreWallet = useVault((state) => state.restoreWallet)
11+
const switchNetwork = useVault((state) => state.switchNetwork)
1112
useEffect(() => {
1213
const restore = async () => {
13-
await wallet.restoreWallet(
14+
await restoreWallet(
1415
new MinaPayload(),
1516
{
1617
network: Network.Mina,
@@ -24,8 +25,8 @@ export const useStoriesWallet = () => {
2425
getPassphrase: async () => Buffer.from('passphrase')
2526
}
2627
)
27-
await wallet.switchNetwork(Mina.Networks.DEVNET)
28-
restore()
28+
await switchNetwork(Mina.Networks.DEVNET)
2929
}
30+
restore()
3031
}, [])
3132
}

packages/features/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@palladxyz/key-management": "*",
2626
"@palladxyz/mina-core": "*",
2727
"@palladxyz/mina-graphql": "*",
28-
"@palladxyz/mina-wallet": "*",
2928
"@palladxyz/offchain-data": "*",
3029
"@palladxyz/persistence": "*",
3130
"@palladxyz/ui": "*",

packages/features/src/common/components/CommandMenu.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ import {
2121
import React from 'react'
2222
import { useNavigate } from 'react-router-dom'
2323

24-
import { useWalletUi } from '../hooks/useWalletUi'
25-
2624
interface CommandMenuProps {
2725
open: boolean
2826
setOpen: (open: boolean) => void
2927
}
3028

3129
export const CommandMenu = ({ open, setOpen }: CommandMenuProps) => {
32-
const { lockWallet } = useWalletUi()
3330
const navigate = useNavigate()
3431

3532
const COMMAND_GROUPS = [
@@ -108,7 +105,7 @@ export const CommandMenu = ({ open, setOpen }: CommandMenuProps) => {
108105
{
109106
name: 'Lock Wallet',
110107
icon: LockIcon,
111-
onSelect: lockWallet,
108+
onSelect: () => console.log('not yet'),
112109
testId: 'commandMenu__lockWallet'
113110
}
114111
]
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
import { Mina } from '@palladxyz/mina-core'
2+
import { useToast } from '@palladxyz/ui'
3+
import { useVault } from '@palladxyz/vault'
4+
import easyMeshGradient from 'easy-mesh-gradient'
5+
import { useMemo } from 'react'
26
import useSWR from 'swr'
37

48
import { useAppStore } from '../store/app'
5-
import { useWalletUi } from './useWalletUi'
69

710
export const useAccount = () => {
8-
const { credentialAddress, getWalletAccountInfo } = useWalletUi()
11+
const { toast } = useToast()
12+
const currentWallet = useVault((state) => state.getCurrentWallet())
13+
const getAccountInfo = useVault((state) => state.getAccountInfo)
914
const network = useAppStore((state) => state.network)
15+
const { publicKey } = currentWallet.accountInfo
1016
const swr = useSWR(
11-
credentialAddress
17+
publicKey
1218
? [
13-
credentialAddress,
19+
publicKey,
1420
'account',
1521
Mina.Networks[network.toUpperCase() as keyof typeof Mina.Networks]
1622
]
1723
: null,
18-
async () => await getWalletAccountInfo()
24+
async () => await getAccountInfo(network, publicKey)
1925
)
20-
const rawMinaBalance = swr.isLoading ? 0 : swr.data?.balance?.total || 0
26+
const rawMinaBalance = swr.isLoading
27+
? 0
28+
: swr.data?.accountInfo.balance.total || 0
2129
const minaBalance =
2230
rawMinaBalance && BigInt(rawMinaBalance) / BigInt(1_000_000_000)
23-
return { ...swr, minaBalance }
31+
const gradientBackground = useMemo(
32+
() =>
33+
publicKey &&
34+
easyMeshGradient({
35+
seed: publicKey,
36+
hueRange: [180, 240]
37+
}),
38+
[publicKey]
39+
)
40+
const copyWalletAddress = async () => {
41+
await navigator.clipboard.writeText(publicKey || '')
42+
toast({
43+
title: 'Wallet address was copied.'
44+
})
45+
}
46+
return {
47+
...swr,
48+
minaBalance,
49+
gradientBackground,
50+
copyWalletAddress,
51+
publicKey
52+
}
2453
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { Mina } from '@palladxyz/mina-core'
2+
import { useVault } from '@palladxyz/vault'
23
import useSWR from 'swr'
34

45
import { useAppStore } from '../store/app'
5-
import { useWalletUi } from './useWalletUi'
66

77
export const useTransaction = ({ hash }: { hash: string }) => {
8-
const { credentialAddress } = useWalletUi()
8+
const currentWallet = useVault((state) => state.getCurrentWallet())
9+
const getTransaction = useVault((state) => state.getTransaction)
10+
const { publicKey } = currentWallet.accountInfo
911
const network = useAppStore((state) => state.network)
1012
return useSWR(
11-
credentialAddress
13+
publicKey
1214
? [
1315
'transaction',
1416
hash,
1517
Mina.Networks[network.toUpperCase() as keyof typeof Mina.Networks]
1618
]
1719
: null,
18-
async () => []
20+
() => getTransaction(network, publicKey, hash)
1921
)
2022
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { Mina } from '@palladxyz/mina-core'
2+
import { useVault } from '@palladxyz/vault'
23
import useSWR from 'swr'
34

45
import { useAppStore } from '../store/app'
5-
import { useWalletUi } from './useWalletUi'
66

77
export const useTransactions = () => {
8-
const { credentialAddress, getWalletTransactions } = useWalletUi()
8+
const currentWallet = useVault((state) => state.getCurrentWallet())
9+
const getTransactions = useVault((state) => state.getTransactions)
10+
const { publicKey } = currentWallet.accountInfo
911
const network = useAppStore((state) => state.network)
1012
return useSWR(
11-
credentialAddress
13+
publicKey
1214
? [
13-
credentialAddress,
15+
publicKey,
1416
'transactions',
1517
Mina.Networks[network.toUpperCase() as keyof typeof Mina.Networks]
1618
]
1719
: null,
18-
async () => await getWalletTransactions()
20+
() => getTransactions(network, publicKey)
1921
)
2022
}

packages/features/src/common/hooks/useWalletUi.ts

-50
This file was deleted.

packages/features/src/lock/views/UnlockWallet.tsx

+17-9
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ import {
99
TooltipContent,
1010
TooltipTrigger
1111
} from '@palladxyz/ui'
12-
import { useKeyAgentStore } from '@palladxyz/vault'
12+
import { useVault } from '@palladxyz/vault'
1313
import { AlertCircleIcon, EyeIcon, EyeOffIcon } from 'lucide-react'
14-
import { useState } from 'react'
14+
import { useEffect, useState } from 'react'
1515
import { useForm } from 'react-hook-form'
1616
import { useNavigate } from 'react-router-dom'
1717

1818
import { WizardLayout } from '../../common/components'
1919
import { ViewHeading } from '../../common/components/ViewHeading'
20-
import { useWalletUi } from '../../common/hooks/useWalletUi'
2120

2221
export const UnlockWalletView = () => {
2322
const [showPassword, setShowPassword] = useState(false)
24-
const { currentWallet } = useWalletUi()
23+
const currentWallet = useVault((state) => state.getCurrentWallet())
2524
const [passwordError, setPasswordError] = useState(false)
2625
const navigate = useNavigate()
27-
const { register, handleSubmit } = useForm({
26+
const {
27+
register,
28+
handleSubmit,
29+
formState: { isDirty }
30+
} = useForm({
2831
defaultValues: {
2932
spendingPassword: ''
3033
}
@@ -39,11 +42,16 @@ export const UnlockWalletView = () => {
3942
spendingPassword: string
4043
}) => {
4144
await getSessionPersistence().setItem('spendingPassword', spendingPassword)
42-
useKeyAgentStore.destroy()
43-
useKeyAgentStore.persist.rehydrate()
44-
if (!currentWallet) return await onError()
45-
return navigate('/dashboard')
45+
await useVault.persist.rehydrate()
4646
}
47+
useEffect(() => {
48+
const unsub = useVault.persist.onFinishHydration(async () => {
49+
if (!isDirty) return
50+
if (!currentWallet?.accountInfo?.publicKey) return await onError()
51+
navigate('/dashboard')
52+
})
53+
return () => unsub()
54+
})
4755
return (
4856
<WizardLayout
4957
footer={

packages/features/src/onboarding/views/MnemonicConfirmation.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import {
66
import { Mina } from '@palladxyz/mina-core'
77
import { getSessionPersistence } from '@palladxyz/persistence'
88
import { Button, cn, Input, Label } from '@palladxyz/ui'
9-
import { useKeyAgentStore } from '@palladxyz/vault'
9+
import { KeyAgents, useVault } from '@palladxyz/vault'
1010
import { useMemo, useState } from 'react'
1111
import { useForm } from 'react-hook-form'
1212
import { useNavigate } from 'react-router-dom'
1313

1414
import { WizardLayout } from '../../common/components'
1515
import { ViewHeading } from '../../common/components/ViewHeading'
16-
import { useWalletUi } from '../../common/hooks/useWalletUi'
1716
import { useAppStore } from '../../common/store/app'
1817
import { useOnboardingStore } from '../../common/store/onboarding'
1918

@@ -22,7 +21,7 @@ const getConfirmationIndex = () => {
2221
}
2322

2423
export const MnemonicConfirmationView = () => {
25-
const { restoreWallet } = useWalletUi()
24+
const restoreWallet = useVault((state) => state.restoreWallet)
2625
const [confirmationIndex] = useState(getConfirmationIndex())
2726
const setVaultStateInitialized = useAppStore(
2827
(state) => state.setVaultStateInitialized
@@ -51,8 +50,7 @@ export const MnemonicConfirmationView = () => {
5150
if (!spendingPassword) return
5251
if (!mnemonic) return
5352
getSessionPersistence().setItem('spendingPassword', spendingPassword)
54-
useKeyAgentStore.destroy()
55-
useKeyAgentStore.persist.rehydrate()
53+
await useVault.persist.rehydrate()
5654
// TODO: Add await in UI when user clicks restore wallet
5755
const restoreArgs: MinaSpecificArgs = {
5856
network: Network.Mina,
@@ -63,12 +61,14 @@ export const MnemonicConfirmationView = () => {
6361
await restoreWallet(
6462
new MinaPayload(),
6563
restoreArgs,
66-
Mina.Networks.MAINNET,
64+
Mina.Networks.DEVNET,
6765
{
6866
mnemonicWords: mnemonic.split(' '),
6967
getPassphrase: async () => Buffer.from(spendingPassword)
7068
},
71-
walletName //this is the keyAgentName
69+
walletName,
70+
KeyAgents.InMemory,
71+
'Test'
7272
)
7373
setVaultStateInitialized()
7474
return navigate('/onboarding/finish')

packages/features/src/onboarding/views/MnemonicInput.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ import {
88
import { Mina } from '@palladxyz/mina-core'
99
import { getSessionPersistence } from '@palladxyz/persistence'
1010
import { Button, cn, Label, Textarea } from '@palladxyz/ui'
11-
import { useKeyAgentStore } from '@palladxyz/vault'
11+
import { KeyAgents, useVault } from '@palladxyz/vault'
1212
import { useMemo, useState } from 'react'
1313
import { useForm } from 'react-hook-form'
1414
import { useNavigate } from 'react-router-dom'
1515
import { shallow } from 'zustand/shallow'
1616

1717
import { WizardLayout } from '../../common/components'
1818
import { ViewHeading } from '../../common/components/ViewHeading'
19-
import { useWalletUi } from '../../common/hooks/useWalletUi'
2019
import { useAppStore } from '../../common/store/app'
2120
import { useOnboardingStore } from '../../common/store/onboarding'
2221

2322
export const MnemonicInputView = () => {
24-
const { restoreWallet } = useWalletUi()
23+
const restoreWallet = useVault((state) => state.restoreWallet)
2524
const navigate = useNavigate()
2625
const { walletName, spendingPassword } = useOnboardingStore(
2726
(state) => ({
@@ -48,8 +47,7 @@ export const MnemonicInputView = () => {
4847
if (!walletName) return
4948
if (!spendingPassword) return
5049
getSessionPersistence().setItem('spendingPassword', spendingPassword)
51-
useKeyAgentStore.destroy()
52-
useKeyAgentStore.persist.rehydrate()
50+
await useVault.persist.rehydrate()
5351
const restoreArgs: MinaSpecificArgs = {
5452
network: Network.Mina,
5553
accountIndex: 0,
@@ -59,12 +57,14 @@ export const MnemonicInputView = () => {
5957
await restoreWallet(
6058
new MinaPayload(),
6159
restoreArgs,
62-
Mina.Networks.MAINNET,
60+
Mina.Networks.DEVNET,
6361
{
6462
mnemonicWords: mnemonic.split(' '),
6563
getPassphrase: async () => Buffer.from(spendingPassword)
6664
},
67-
walletName //this is the keyAgentName
65+
walletName,
66+
KeyAgents.InMemory,
67+
'Test'
6868
)
6969
setVaultStateInitialized()
7070
return navigate('/onboarding/finish')

0 commit comments

Comments
 (0)