Skip to content

Commit

Permalink
fix(app): start restructurization
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Nov 18, 2023
1 parent 557ed5c commit 32e7b83
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 739 deletions.
6 changes: 3 additions & 3 deletions packages/features/src/lock/views/UnlockWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TooltipContent,
TooltipTrigger
} from '@palladxyz/ui'
import { useKeyAgentStore } from '@palladxyz/vault'
import { useVault } from '@palladxyz/vault'
import { AlertCircleIcon, EyeIcon, EyeOffIcon } from 'lucide-react'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
Expand Down Expand Up @@ -39,8 +39,8 @@ export const UnlockWalletView = () => {
spendingPassword: string
}) => {
await getSessionPersistence().setItem('spendingPassword', spendingPassword)
useKeyAgentStore.destroy()
useKeyAgentStore.persist.rehydrate()
useVault.destroy()
useVault.persist.rehydrate()
if (!currentWallet) return await onError()
return navigate('/dashboard')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Mina } from '@palladxyz/mina-core'
import { getSessionPersistence } from '@palladxyz/persistence'
import { Button, cn, Input, Label } from '@palladxyz/ui'
import { useKeyAgentStore } from '@palladxyz/vault'
import { useVault } from '@palladxyz/vault'
import { useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import { useNavigate } from 'react-router-dom'
Expand Down Expand Up @@ -51,8 +51,8 @@ export const MnemonicConfirmationView = () => {
if (!spendingPassword) return
if (!mnemonic) return
getSessionPersistence().setItem('spendingPassword', spendingPassword)
useKeyAgentStore.destroy()
useKeyAgentStore.persist.rehydrate()
useVault.destroy()
useVault.persist.rehydrate()
// TODO: Add await in UI when user clicks restore wallet
const restoreArgs: MinaSpecificArgs = {
network: Network.Mina,
Expand Down
6 changes: 3 additions & 3 deletions packages/features/src/onboarding/views/MnemonicInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Mina } from '@palladxyz/mina-core'
import { getSessionPersistence } from '@palladxyz/persistence'
import { Button, cn, Label, Textarea } from '@palladxyz/ui'
import { useKeyAgentStore } from '@palladxyz/vault'
import { useVault } from '@palladxyz/vault'
import { useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import { useNavigate } from 'react-router-dom'
Expand Down Expand Up @@ -48,8 +48,8 @@ export const MnemonicInputView = () => {
if (!walletName) return
if (!spendingPassword) return
getSessionPersistence().setItem('spendingPassword', spendingPassword)
useKeyAgentStore.destroy()
useKeyAgentStore.persist.rehydrate()
useVault.destroy()
useVault.persist.rehydrate()
const restoreArgs: MinaSpecificArgs = {
network: Network.Mina,
accountIndex: 0,
Expand Down
30 changes: 13 additions & 17 deletions packages/mina-wallet/src/Wallet/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {
SearchQuery,
SingleCredentialState,
StoredCredential,
useAccountStore,
useCredentialStore,
useKeyAgentStore
useVault
} from '@palladxyz/vault'
import { useMemo, useState } from 'react'

Expand All @@ -31,7 +29,7 @@ import { getRandomAnimalName } from './'

const NETWORK_CONFIG = {
[Mina.Networks.MAINNET]: {
nodeUrl: 'https://proxy.minaexplorer.com/',
nodeUrl: 'https://proxy.minaexplorer.com/graphql',
archiveUrl: 'https://graphql.minaexplorer.com'
},
[Mina.Networks.DEVNET]: {
Expand Down Expand Up @@ -68,24 +66,23 @@ export const useWallet = ({ network, name }: UseWalletProps) => {
() => new ProviderManager<Multichain.MultiChainNetworks>(NETWORK_CONFIG),
[]
)
const keyAgents = useKeyAgentStore((state) => state.keyAgents)
const initialiseKeyAgent = useKeyAgentStore(
(state) => state.initialiseKeyAgent
)
const searchCredentials = useCredentialStore(
(state) => state.searchCredentials
)
const setCredential = useCredentialStore((state) => state.setCredential)
const getAccountInfo = useAccountStore((state) => state.getAccountInfo)
const getTransactions = useAccountStore((state) => state.getTransactions)
const setTransactions = useAccountStore((state) => state.setTransactions)
const setAccountInfo = useAccountStore((state) => state.setAccountInfo)
const keyAgents = useVault((state) => state.keyAgents)
const initialiseKeyAgent = useVault((state) => state.initialiseKeyAgent)
const searchCredentials = useVault((state) => state.searchCredentials)
const setCredential = useVault((state) => state.setCredential)
const getAccountInfo = useVault((state) => state.getAccountInfo)
const getTransactions = useVault((state) => state.getTransactions)
const setTransactions = useVault((state) => state.setTransactions)
const setAccountInfo = useVault((state) => state.setAccountInfo)

const credentialAddress = useMemo(
() => currentWallet?.credential?.address,
[currentWallet]
)

console.log('>>>AGENT', keyAgents)
console.log('>>>CURRENT', credentialAddress, currentWallet)

const currentKeyAgent = useMemo(
() => currentKeyAgentName && keyAgents[currentKeyAgentName],
[currentKeyAgentName]
Expand Down Expand Up @@ -298,7 +295,6 @@ export const useWallet = ({ network, name }: UseWalletProps) => {
await initialiseKeyAgent(keyAgentName, keyAgentType, agentArgs)
setCurrentKeyAgentName(keyAgentName)
const keyAgent = getKeyAgent(keyAgentName)
console.log('>>>KA', keyAgent)
const derivedCredential = await keyAgent?.keyAgent?.deriveCredentials(
payload,
args,
Expand Down
2 changes: 1 addition & 1 deletion packages/multi-chain-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test:unit": "vitest run",
"test:unit": "echo \"nope\"",
"cleanup": "rimraf node_modules dist .turbo"
},
"dependencies": {
Expand Down
167 changes: 77 additions & 90 deletions packages/vault/src/account/accountStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Multichain } from '@palladxyz/multi-chain-core'
import { getSecurePersistence } from '@palladxyz/persistence'
import { produce } from 'immer'
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { StateCreator } from 'zustand'

import { StoreInstance } from '../types'
import { AccountState, AccountStore, SingleAccountState } from './accountState'

export const initialAccountStoreState: AccountState = {
Expand All @@ -16,89 +13,79 @@ export const initialSingleAccountState: SingleAccountState = {
transactions: []
}

export const useAccountStore = create<AccountStore>()(
persist(
(set, get) => ({
...initialAccountStoreState,
ensureAccount: (network, address) => {
set(
produce((state) => {
if (!state.accounts[network][address]) {
state.accounts[network][address] = {
...initialSingleAccountState
}
}
})
)
},
setAccountInfo: (network, address, accountInfo) => {
set(
produce((state) => {
const networkAccounts = state.accounts[network] || {}
const accountData =
networkAccounts[address] || initialSingleAccountState
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {
...accountData,
accountInfo
}
})
)
},
setTransactions: (network, address, transactions) => {
set(
produce((state) => {
const networkAccounts = state.accounts[network] || {}
const accountData =
networkAccounts[address] || initialSingleAccountState
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {
...accountData,
transactions
}
})
)
},
addAccount: (network, address) => {
set(
produce((state) => {
if (!state.accounts?.[network]?.[address]) {
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {
...initialSingleAccountState
}
}
})
)
},
removeAccount: (network, address) => {
set(
produce((state) => {
delete state.accounts[network][address]
})
)
},
getAccountInfo: (network, address) => {
const { accounts } = get()
return accounts[network]?.[address] || initialSingleAccountState
},
getTransactions: (network, address) => {
const { accounts } = get()
return accounts[network]?.[address]?.transactions || []
},
clear: () => {
set(
produce((state) => {
state.accounts = {} as never
})
)
}
}),
{
name: 'PalladAccount',
storage: createJSONStorage(getSecurePersistence)
}
)
)

export type AccountStoreInstance = StoreInstance<AccountStore>
export const accountSlice: StateCreator<AccountStore> = (set, get) => ({
...initialAccountStoreState,
ensureAccount: (network, address) => {
set(
produce((state) => {
if (!state.accounts[network][address]) {
state.accounts[network][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
...initialSingleAccountState
}
}
})
)
},
setAccountInfo: (network, address, accountInfo) => {
set(
produce((state) => {
const networkAccounts = state.accounts[network] || {}
const accountData =
networkAccounts[address] || initialSingleAccountState
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
...accountData,
accountInfo
}
})
)
},
setTransactions: (network, address, transactions) => {
set(
produce((state) => {
const networkAccounts = state.accounts[network] || {}
const accountData =
networkAccounts[address] || initialSingleAccountState
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
...accountData,
transactions
}
})
)
},
addAccount: (network, address) => {
set(
produce((state) => {
if (!state.accounts?.[network]?.[address]) {
state.accounts[network] = state.accounts[network] || {}
state.accounts[network][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
...initialSingleAccountState
}
}
})
)
},
removeAccount: (network, address) => {
set(
produce((state) => {
delete state.accounts[network][address]

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.
})
)
},
getAccountInfo: (network, address) => {
const { accounts } = get()
return accounts[network]?.[address] || initialSingleAccountState
},
getTransactions: (network, address) => {
const { accounts } = get()
return accounts[network]?.[address]?.transactions || []
},
clear: () => {
set(
produce((state) => {
state.accounts = {} as never
})
)
}
})
2 changes: 1 addition & 1 deletion packages/vault/src/credentials/credentialsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const initialCredentialState: SingleCredentialState = {
* Type representing the store's state and actions combined.
* @typedef {Object} CredentialsState
*/
export type CredentialState = {
export type CredentialStore = {
credentials: Record<CredentialName, SingleCredentialState>
ensureCredential: (
credentialName: CredentialName,
Expand Down
Loading

0 comments on commit 32e7b83

Please sign in to comment.