Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added secure store for the wallet key #21

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion apps/expo/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { AppAgent } from '@internal/agent'

import { AgentProvider, initializeAgent } from '@internal/agent'
import { useToastController } from '@internal/ui'
import { DefaultTheme, ThemeProvider } from '@react-navigation/native'
import { Provider } from 'app/provider'
import { useFonts } from 'expo-font'
import { Stack } from 'expo-router'
import * as SplashScreen from 'expo-splash-screen'
import { useEffect, useState } from 'react'

import { getSecureWalletKey } from '../utils/walletKeyStore'

void SplashScreen.preventAutoHideAsync()

export default function HomeLayout() {
Expand All @@ -22,12 +25,27 @@ export default function HomeLayout() {
InterBold: require('@tamagui/font-inter/otf/Inter-Bold.otf'),
})
const [agent, setAgent] = useState<AppAgent>()
const toast = useToastController()

// Initialize agent
useEffect(() => {
if (agent) return

void initializeAgent().then((agent) => setAgent(agent))
const startAgent = async () => {
const walletKey = await getSecureWalletKey().catch(() => {
toast.show('Could not load wallet key from secure storage.')
})
if (!walletKey) return

const agent = await initializeAgent(walletKey).catch(() => {
toast.show('Could not initialize agent.')
})
if (!agent) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a dead end. What does the UI look like? I think we should show at least a warning to the user like "Could not establish a secure environment. The current device is not supported." and show that indefinitely (as otherwise it will show a white screen or something?).

Also, in which case will secure store not be available?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The secret store should always be available on iOS and Android devices.

I added some text when the agent failed initialization:

image


setAgent(agent)
}

void startAgent()
}, [])

// Hide splash screen when agent and fonts are loaded
Expand Down
1 change: 1 addition & 0 deletions apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"expo-linear-gradient": "^12.1.2",
"expo-linking": "^4.0.1",
"expo-router": "^1.4.3",
"expo-secure-store": "^12.1.1",
"expo-splash-screen": "~0.18.2",
"expo-status-bar": "^1.4.4",
"expo-system-ui": "~2.2.1",
Expand Down
21 changes: 21 additions & 0 deletions apps/expo/utils/walletKeyStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { uuid } from '@aries-framework/core/build/utils/uuid'
import * as SecureStore from 'expo-secure-store'

const STORE_KEY = 'wallet-key' as const

const generateNewWalletKey = (): string => {
return uuid()
}

export const getSecureWalletKey = async (): Promise<string> => {
const secureStoreAvailable = await SecureStore.isAvailableAsync()
if (!secureStoreAvailable) throw new Error('SecureStore is not available on this device.')

const walletKey = await SecureStore.getItemAsync(STORE_KEY)
if (walletKey) return walletKey

const newWalletKey = generateNewWalletKey()
await SecureStore.setItemAsync(STORE_KEY, newWalletKey)

return newWalletKey
}
7 changes: 3 additions & 4 deletions packages/agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import { useAgent as useAgentLib } from '@aries-framework/react-hooks'
import { agentDependencies } from '@aries-framework/react-native'
import { ariesAskar } from '@hyperledger/aries-askar-react-native'

export const initializeAgent = async () => {
export const initializeAgent = async (walletKey: string) => {
const agent = new Agent({
dependencies: agentDependencies,
config: {
label: 'Paradym Wallet',
// FIXME: AW-58: Store wallet key in secure enclave
walletConfig: {
id: 'paradym-wallet',
key: 'a5fc4d22-5e0c-434b-abb5-c091815cf279',
id: 'paradym-wallet-secure',
key: walletKey,
},
autoUpdateStorageOnStartup: true,
logger: new ConsoleLogger(LogLevel.debug),
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8044,6 +8044,7 @@ __metadata:
expo-linear-gradient: ^12.1.2
expo-linking: ^4.0.1
expo-router: ^1.4.3
expo-secure-store: ^12.1.1
expo-splash-screen: ~0.18.2
expo-status-bar: ^1.4.4
expo-system-ui: ~2.2.1
Expand Down Expand Up @@ -8349,6 +8350,15 @@ __metadata:
languageName: node
linkType: hard

"expo-secure-store@npm:^12.1.1":
version: 12.3.0
resolution: "expo-secure-store@npm:12.3.0"
peerDependencies:
expo: "*"
checksum: 37b0b95c80731bbd17ea62e9b3c15e7ad059f23238d69cac145d2094c054efc8ae08f5d8cc48ca4107ca9e23d7c61231eb43493dff984b1a447acd34cb3ea090
languageName: node
linkType: hard

"expo-splash-screen@npm:*, expo-splash-screen@npm:~0.18.2":
version: 0.18.2
resolution: "expo-splash-screen@npm:0.18.2"
Expand Down