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

SecureStore to keep mnemonic wallet #319

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions app/api/secure_store.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Platform } from 'react-native'
import ExpoSecureStore from 'expo-secure-store'
import { ISecureStore, getKey } from './secure_store'

class NativeSecureStore implements ISecureStore {
/**
* @param key {string} of item with 'environment' and 'network' prefixed
* @return {string | null}
*/
async getItem (key: string): Promise<string | null> {
return await ExpoSecureStore.getItemAsync(await getKey(key))
}

/**
* @param key {string} of item with 'environment' and 'network' prefixed
* @param value {string} to set
*/
async setItem (key: string, value: string): Promise<void> {
return await ExpoSecureStore.setItemAsync(await getKey(key), value)
}

/**
* @param key {string} of item with 'environment' and 'network' prefixed
*/
async removeItem (key: string): Promise<void> {
await ExpoSecureStore.deleteItemAsync(await getKey(key))
}
}

console.log(Platform.OS)
export const SecureStorage = new NativeSecureStore()
42 changes: 42 additions & 0 deletions app/api/secure_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getEnvironment } from '../environment'
import { getNetwork } from './storage'
import AsyncStorage from '@react-native-async-storage/async-storage'

export async function getKey (key: string): Promise<string> {
const env = getEnvironment()
const network = await getNetwork()
return `${env.name}.${network}.${key}`
}

export interface ISecureStore {
getItem: (key: string) => Promise<string | null>
setItem: (key: string, value: string) => Promise<void>
removeItem: (key: string) => Promise<void>
}

class WebAsyncStorage implements ISecureStore {
/**
* @param key {string} of item with 'environment' and 'network' prefixed
* @return {string | null}
*/
async getItem (key: string): Promise<string | null> {
return await AsyncStorage.getItem(await getKey(key))
}

/**
* @param key {string} of item with 'environment' and 'network' prefixed
* @param value {string} to set
*/
async setItem (key: string, value: string): Promise<void> {
return await AsyncStorage.setItem(await getKey(key), value)
}

/**
* @param key {string} of item with 'environment' and 'network' prefixed
*/
async removeItem (key: string): Promise<void> {
await AsyncStorage.removeItem(await getKey(key))
}
}

export const SecureStorage = new WebAsyncStorage()
8 changes: 7 additions & 1 deletion app/api/wallet/persistence.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { EnvironmentNetwork } from "../../environment";
import { WalletPersistence, WalletType } from "./persistence";

jest.mock('react-native/Libraries/Utilities/Platform', () => ({
OS: 'web',
select: opts => opts.web
}))

// web has no SecureStore implementation, fallback to AsyncStorage
const getItem = jest.spyOn(AsyncStorage, 'getItem')
const setItem = jest.spyOn(AsyncStorage, 'setItem')

Expand Down
7 changes: 4 additions & 3 deletions app/api/wallet/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getItem, setItem } from '../storage'
// import { getItem, setItem } from '../storage'
import { SecureStorage } from '../secure_store'

const KEY = 'WALLET'

Expand All @@ -18,7 +19,7 @@ export interface WalletData {
}

async function get (): Promise<WalletData[]> {
const json = await getItem(KEY)
const json = await SecureStorage.getItem(KEY)
if (json !== null) {
return JSON.parse(json)
}
Expand All @@ -27,7 +28,7 @@ async function get (): Promise<WalletData[]> {
}

async function set (wallets: WalletData[]): Promise<void> {
await setItem(KEY, JSON.stringify(wallets))
await SecureStorage.setItem(KEY, JSON.stringify(wallets))
}

async function add (data: WalletData): Promise<void> {
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"expo-linking": "~2.3.1",
"expo-localization": "~10.2.0",
"expo-random": "~11.2.0",
"expo-secure-store": "^10.2.0",
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-updates": "~0.8.1",
Expand Down