Skip to content

Commit

Permalink
fix(suite-native): handle error on SecureStore.getItemAsync
Browse files Browse the repository at this point in the history
- For some users on some phones, sometimes... SecureStore is not wiped correctly on app uninstall so it failed to loaded when app is installed again. See expo/expo#23426

- This is a workaround - deleting the key in that case and genereting new one.
  • Loading branch information
matejkriz committed Mar 28, 2024
1 parent 6b7cba5 commit 1c2acbb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions suite-native/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@reduxjs/toolkit": "1.9.5",
"@sentry/react-native": "5.19.1",
"@suite-common/wallet-config": "workspace:*",
"@suite-common/wallet-types": "workspace:*",
"@trezor/utxo-lib": "workspace:*",
Expand Down
33 changes: 20 additions & 13 deletions suite-native/storage/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Alert } from 'react-native';
import { MMKV } from 'react-native-mmkv';
import RNRestart from 'react-native-restart';

import { captureException } from '@sentry/react-native';
import * as Random from 'expo-random';
import * as SecureStore from 'expo-secure-store';
import * as SplashScreen from 'expo-splash-screen';
Expand All @@ -12,19 +13,26 @@ import { unecryptedJotaiStorage } from './atomWithUnecryptedStorage';
export const ENCRYPTION_KEY = 'STORAGE_ENCRYPTION_KEY';
export const ENCRYPTED_STORAGE_ID = 'trezorSuite-app-storage';

export const retrieveStorageEncryptionKey = async () => {
let secureKey = await SecureStore.getItemAsync(ENCRYPTION_KEY);
export let encryptedStorage: MMKV;

const retrieveStorageEncryptionKey = async () => {
try {
const secureKey = await SecureStore.getItemAsync(ENCRYPTION_KEY);

if (secureKey == null) {
secureKey = Buffer.from(Random.getRandomBytes(16)).toString('hex');
await SecureStore.setItemAsync(ENCRYPTION_KEY, secureKey);
if (secureKey) return secureKey;
} catch (error) {
// Some users are facing an error when they uninstall the app and then reinstall it,
// see https://github.com/expo/expo/issues/23426
await SecureStore.deleteItemAsync(ENCRYPTION_KEY);
captureException(error);
}

const secureKey = Buffer.from(Random.getRandomBytes(16)).toString('hex');
await SecureStore.setItemAsync(ENCRYPTION_KEY, secureKey);

return secureKey;
};

export let encryptedStorage: MMKV;

export const clearStorage = () => {
unecryptedJotaiStorage.clearAll();
encryptedStorage?.clearAll();
Expand All @@ -35,9 +43,6 @@ export const clearStorage = () => {
// If someone will mess with encryptionKey it can corrupt storage and app will crash on startup.
// Then app will hang on splashscreen indefinitely so we at least want to show some error message.
const tryInitStorage = (encryptionKey: string) => {
// storage may be already initialized (for example in dev useEffect fire twice)
if (encryptedStorage) return encryptedStorage;

try {
return new MMKV({
id: ENCRYPTED_STORAGE_ID,
Expand Down Expand Up @@ -67,9 +72,11 @@ const tryInitStorage = (encryptionKey: string) => {
};

export const initMmkvStorage = async (): Promise<Storage> => {
const encryptionKey = await retrieveStorageEncryptionKey();

encryptedStorage = tryInitStorage(encryptionKey);
// storage may be already initialized (for example in dev useEffect fire twice)
if (!encryptedStorage) {
const encryptionKey = await retrieveStorageEncryptionKey();
encryptedStorage = tryInitStorage(encryptionKey);
}

return {
setItem: (key, value) => {
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9652,6 +9652,7 @@ __metadata:
resolution: "@suite-native/storage@workspace:suite-native/storage"
dependencies:
"@reduxjs/toolkit": "npm:1.9.5"
"@sentry/react-native": "npm:5.19.1"
"@suite-common/wallet-config": "workspace:*"
"@suite-common/wallet-types": "workspace:*"
"@trezor/utxo-lib": "workspace:*"
Expand Down

0 comments on commit 1c2acbb

Please sign in to comment.