Skip to content

Commit

Permalink
Fix failed auth causing wallet state loss
Browse files Browse the repository at this point in the history
  • Loading branch information
minibits-cash committed Jan 4, 2024
1 parent f6cb6e1 commit 09b8f58
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/models/helpers/setupRootStore.ts
Expand Up @@ -54,20 +54,21 @@ export async function setupRootStore(rootStore: RootStore) {
try {
await MMKVStorage.initEncryption()
} catch (e: any) {
log.error('[setupRootStore]', 'Encryption init failed', {error: e})

if (e && typeof e === 'object') {
const errString = JSON.stringify(e)
const errString = JSON.stringify(e)

const isCancellPressed = errString.includes('code: 10')
const isBackPressed = errString.includes('code: 13')
const isIOSCancel = 'code' in e && String(e.code) === '-128'
const isBackPressed = errString.includes('code: 10')
const isCancellPressed = errString.includes('code: 13')
const isIOSCancel = 'code' in e && String(e.code) === '-128'

// In case user cancels / fails the fingerprint auth, empty app state is loaded.
// If a user updates the empty app state, it is stored unencrypted and returned after restart as primary one,
// making encrypted data inaccessible or later overwritten.
// Therefore this ugly app exit on unsuccessful auth.

if(isCancellPressed || isBackPressed || isIOSCancel) {
log.error('[setupRootStore]', 'Exiting app on failed auth', {error: e})
if(isCancellPressed || isBackPressed || isIOSCancel) {
RNExitApp.exitApp()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/screens/ContactsScreen.tsx
Expand Up @@ -34,7 +34,7 @@ export const ContactsScreen: FC<ContactsScreenProps> = observer(function Contact
try {
log.trace(walletProfileStore)

if(!walletProfileStore.pubkey || !walletProfileStore.picture) { // pic needed
if(!walletProfileStore.pubkey || !walletProfileStore.picture) { // pic check needed to be sure profile does not exists on the server
// create random name, NIP05 identifier, random picture and sharable profile
// announce new profile to the added default public and minibits relays
const walletId = userSettingsStore.walletId
Expand All @@ -44,11 +44,11 @@ export const ContactsScreen: FC<ContactsScreenProps> = observer(function Contact
} catch(e: any) {
log.error(e.name, e.message)

// in case we somehow hit the existing name we silently retry
if(e.name && e.name === Err.ALREADY_EXISTS_ERROR) {
// in case we somehow hit the existing name we silently retry TBD
/* if(e.name && e.name === Err.ALREADY_EXISTS_ERROR) {
const randomName = getRandomUsername()
await walletProfileStore.create(randomName as string)
}
} */

return false // silent
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/keyChain.ts
Expand Up @@ -359,7 +359,7 @@ const loadMmkvEncryptionKey = async function (): Promise<string | undefined> {
log.trace('[loadMmkvEncryptionKey]', 'Did not find existing encryptionKey in the KeyChain.')
return undefined
} catch (e: any) {
throw new AppError(Err.KEYCHAIN_ERROR, e.message)
throw new AppError(Err.KEYCHAIN_ERROR, e.message, {caller: 'loadMmkvEncryptionKey', message: e.message})
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/services/mmkvStorage.ts
Expand Up @@ -8,8 +8,12 @@ let _encryptionKey: string | undefined

const initEncryption = async function (): Promise<void> {
if (!_encryptionKey) {
_encryptionKey = await getOrCreateEncryptionKey()
getInstance() // Instantiate the MMKV instance with the encryption key
try {
_encryptionKey = await getOrCreateEncryptionKey()
getInstance() // Instantiate the MMKV instance with the encryption key
} catch(e: any) {
throw e
}
}
}

Expand Down Expand Up @@ -38,7 +42,11 @@ const getOrCreateEncryptionKey = async function (): Promise<string> {

let key: string | null = null

key = (await KeyChain.loadMmkvEncryptionKey()) as string
try {
key = (await KeyChain.loadMmkvEncryptionKey()) as string
} catch (e: any) {
throw e
}

if (!key) {
key = KeyChain.generateMmkvEncryptionKey() as string
Expand Down

0 comments on commit 09b8f58

Please sign in to comment.