Skip to content

Commit

Permalink
Merge pull request #87 from teddyjfpender/main
Browse files Browse the repository at this point in the history
Merging as is for refactoring.
  • Loading branch information
teddyjfpender authored Oct 12, 2023
2 parents 557ed5c + 65fdd79 commit cfde9f7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 508 deletions.
23 changes: 17 additions & 6 deletions packages/mina-wallet/src/Wallet/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FromBip39MnemonicWordsProps,
generateMnemonicWords,
GroupedCredentials,
InMemoryKeyAgent,
MinaSpecificArgs,
Network
} from '@palladxyz/key-management'
Expand All @@ -14,7 +15,6 @@ import { Multichain } from '@palladxyz/multi-chain-core'
import {
CredentialName,
KeyAgentName,
KeyAgents,
SearchQuery,
SingleCredentialState,
StoredCredential,
Expand Down Expand Up @@ -72,6 +72,7 @@ export const useWallet = ({ network, name }: UseWalletProps) => {
const initialiseKeyAgent = useKeyAgentStore(
(state) => state.initialiseKeyAgent
)

const searchCredentials = useCredentialStore(
(state) => state.searchCredentials
)
Expand Down Expand Up @@ -287,19 +288,25 @@ export const useWallet = ({ network, name }: UseWalletProps) => {
network: Multichain.MultiChainNetworks,
{ mnemonicWords, getPassphrase }: FromBip39MnemonicWordsProps,
keyAgentName: KeyAgentName,
keyAgentType: KeyAgents = KeyAgents.InMemory,
//keyAgentType: KeyAgents = KeyAgents.InMemory,
credentialName: CredentialName = getRandomAnimalName()
) => {
const agentArgs: FromBip39MnemonicWordsProps = {
getPassphrase: getPassphrase,
mnemonicWords: mnemonicWords,
mnemonic2ndFactorPassphrase: ''
}
await initialiseKeyAgent(keyAgentName, keyAgentType, agentArgs)
const keyAgentInitialised = await InMemoryKeyAgent.fromMnemonicWords(
agentArgs
)

//await initialiseKeyAgent(keyAgentName, keyAgentType, keyAgentInitialised)
setCurrentKeyAgentName(keyAgentName)
const keyAgent = getKeyAgent(keyAgentName)
console.log('>>>KA', keyAgent)
const derivedCredential = await keyAgent?.keyAgent?.deriveCredentials(
//const keyAgent = await getKeyAgent(keyAgentName)

console.log('>>>KAs', keyAgents)
//console.log('>>>KA', keyAgent)
const derivedCredential = await keyAgentInitialised.deriveCredentials(
payload,
args,
getPassphrase,
Expand All @@ -317,11 +324,15 @@ export const useWallet = ({ network, name }: UseWalletProps) => {
setCredential(singleCredentialState)
setCurrentWallet(singleCredentialState)
await _syncWallet(network, derivedCredential)
return { keyAgentInitialised }
}

return {
walletNetwork,
walletName,
keyAgents,
initialiseKeyAgent,
setCurrentKeyAgentName,
restoreWallet,
createWallet,
switchNetwork,
Expand Down
43 changes: 43 additions & 0 deletions packages/mina-wallet/test/Wallet/useWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
MinaSpecificArgs,
Network
} from '@palladxyz/key-management'
import { InMemoryKeyAgent } from '@palladxyz/key-management'
import { Mina } from '@palladxyz/mina-core'
import { KeyAgents } from '@palladxyz/vault'
// eslint-disable-next-line import/no-extraneous-dependencies
import { act, renderHook } from '@testing-library/react'

Expand Down Expand Up @@ -55,6 +57,47 @@ describe('useWallet', () => {
})
expect(mnemonic).toHaveLength(24)
})
it('initializes the key agent correctly', async () => {
const { result } = renderHook(() =>
useWallet({ network: Mina.Networks.DEVNET, name: 'Pallad' })
)
const agentArgs = {
mnemonicWords: PREMADE_MNEMONIC,
getPassphrase: getPassword
}
let derivedCredential
let keyAgentInitialised: InMemoryKeyAgent
const restoreArgs: MinaSpecificArgs = {
network: Network.Mina,
accountIndex: 0,
addressIndex: 0,
networkType: 'testnet'
}
const payload = new MinaPayload()
await act(async () => {
keyAgentInitialised = await result.current.restoreWallet(
payload,
restoreArgs,
Mina.Networks.DEVNET,
agentArgs,
keyAgentName
)
derivedCredential = result.current.currentWallet
})
expect(derivedCredential).toBeDefined()
// them initialise the key agent store
const keyAgentType = KeyAgents.InMemory

await act(async () => {
await result.current.initialiseKeyAgent(
keyAgentName,
keyAgentType,
keyAgentInitialised
)
result.current.setCurrentKeyAgentName(keyAgentName)
})
expect(result.current.keyAgents[keyAgentName]).toBeDefined()
})
it('restores a wallet', async () => {
const { result } = renderHook(() =>
useWallet({ network: Mina.Networks.DEVNET, name: 'Pallad' })
Expand Down
9 changes: 3 additions & 6 deletions packages/vault/src/keyAgent/keyAgentState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
* @file Represents the state definitions related to keyAgents.
*/

import {
FromBip39MnemonicWordsProps,
InMemoryKeyAgent
} from '@palladxyz/key-management'
import { InMemoryKeyAgent } from '@palladxyz/key-management'

export type KeyAgentName = string
export enum KeyAgents {
Expand Down Expand Up @@ -43,9 +40,9 @@ export type KeyAgentState = {
keyAgents: InStoreKeyAgents
ensureKeyAgent: (name: KeyAgentName) => void
initialiseKeyAgent: (
name: KeyAgentName,
keyAgentName: KeyAgentName,
keyAgentType: KeyAgents,
{ mnemonicWords, getPassphrase }: FromBip39MnemonicWordsProps
InMemoryKeyAgent: InMemoryKeyAgent
) => Promise<void>
removeKeyAgent: (name: KeyAgentName) => void
clear: () => void
Expand Down
20 changes: 3 additions & 17 deletions packages/vault/src/keyAgent/keyAgentStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
FromBip39MnemonicWordsProps,
InMemoryKeyAgent
} from '@palladxyz/key-management'
import { getSecurePersistence } from '@palladxyz/persistence'
import { produce } from 'immer'
import { create } from 'zustand'
Expand All @@ -26,23 +22,13 @@ export const useKeyAgentStore = create<KeyAgentState>()(
})
)
},
async initialiseKeyAgent(
name,
keyAgentType,
{ mnemonicWords, getPassphrase }
) {
const agentArgs: FromBip39MnemonicWordsProps = {
getPassphrase: getPassphrase,
mnemonicWords: mnemonicWords,
mnemonic2ndFactorPassphrase: ''
}
const keyAgent = await InMemoryKeyAgent.fromMnemonicWords(agentArgs)
console.log('>>>KI EJDZENT', keyAgent)
async initialiseKeyAgent(name, keyAgentType, InMemoryKeyAgent) {
console.log('>>>KI EJDZENT', InMemoryKeyAgent)
return set(
produce((state) => {
state.keyAgents[name] = {
keyAgentType: keyAgentType,
keyAgent: keyAgent,
keyAgent: InMemoryKeyAgent,
name: name
}
})
Expand Down
15 changes: 10 additions & 5 deletions packages/vault/test/keyAgent/keyAgentStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FromBip39MnemonicWordsProps,
generateMnemonicWords,
InMemoryKeyAgent,
MinaPayload,
MinaSpecificArgs,
Network
Expand Down Expand Up @@ -38,6 +39,8 @@ describe('AccountStore', () => {
let agentArgs2: FromBip39MnemonicWordsProps
const keyAgentName = 'test key agent'
const keyAgentName2 = 'test key agent 2'
let keyAgentInitialised: InMemoryKeyAgent
let keyAgentInitialised2: InMemoryKeyAgent

beforeEach(async () => {
randomMnemonic = generateMnemonicWords()
Expand All @@ -49,6 +52,8 @@ describe('AccountStore', () => {
getPassphrase: getPassphrase,
mnemonicWords: randomMnemonic
}
keyAgentInitialised = await InMemoryKeyAgent.fromMnemonicWords(agentArgs)
keyAgentInitialised2 = await InMemoryKeyAgent.fromMnemonicWords(agentArgs2)
})

afterEach(() => {
Expand All @@ -69,7 +74,7 @@ describe('AccountStore', () => {
await result.current.initialiseKeyAgent(
keyAgentName,
KeyAgents.InMemory,
agentArgs
keyAgentInitialised
)
})
expect(result.current.keyAgents[keyAgentName]).toBeDefined()
Expand All @@ -82,13 +87,13 @@ describe('AccountStore', () => {
await result.current.initialiseKeyAgent(
keyAgentName,
KeyAgents.InMemory,
agentArgs
keyAgentInitialised
)
// add second key agent
await result.current.initialiseKeyAgent(
keyAgentName2,
KeyAgents.InMemory,
agentArgs2
keyAgentInitialised2
)
})
const keyAgent1 = result.current.keyAgents[keyAgentName]
Expand All @@ -111,13 +116,13 @@ describe('AccountStore', () => {
await result.current.initialiseKeyAgent(
keyAgentName,
KeyAgents.InMemory,
agentArgs
keyAgentInitialised
)
// add second key agent
await result.current.initialiseKeyAgent(
keyAgentName2,
KeyAgents.InMemory,
agentArgs2
keyAgentInitialised2
)
})
// check that both key agents are in the store
Expand Down
Loading

0 comments on commit cfde9f7

Please sign in to comment.