diff --git a/components/password-prompt.js b/components/password-prompt.js index f9e63ee..dec0b68 100644 --- a/components/password-prompt.js +++ b/components/password-prompt.js @@ -24,22 +24,19 @@ export default class PasswordPrompt extends Component { } async tryToOpenDb() { - try { - await openDb({ persistConnection: true }) - } catch (err) { + const connected = await openDb() + if (!connected) { this.setState({ showPasswordPrompt: true }) await saveEncryptionFlag(true) return } - await saveEncryptionFlag(false) this.props.showApp() } passHashToDb = async hash => { - try { - await openDb({ hash, persistConnection: true }) - } catch (err) { + const connected = await openDb(hash) + if (!connected) { Alert.alert( shared.incorrectPassword, shared.incorrectPasswordMessage, diff --git a/components/settings/password/check-current-password.js b/components/settings/password/check-current-password.js index b177de4..7d60308 100644 --- a/components/settings/password/check-current-password.js +++ b/components/settings/password/check-current-password.js @@ -3,21 +3,18 @@ import { openDb } from '../../../db' import { shared } from '../../labels' export default async function checkPassword({hash, onCancel, onTryAgain }) { - try { - await openDb({ hash, persistConnection: false }) - return true - } catch (err) { - Alert.alert( - shared.incorrectPassword, - shared.incorrectPasswordMessage, - [{ - text: shared.cancel, - onPress: onCancel - }, { - text: shared.tryAgain, - onPress: onTryAgain - }] - ) - return false - } + const connected = await openDb(hash) + if (connected) return true + Alert.alert( + shared.incorrectPassword, + shared.incorrectPasswordMessage, + [{ + text: shared.cancel, + onPress: onCancel + }, { + text: shared.tryAgain, + onPress: onTryAgain + }] + ) + return false } \ No newline at end of file diff --git a/db/index.js b/db/index.js index fe8c269..7722ec9 100644 --- a/db/index.js +++ b/db/index.js @@ -10,14 +10,28 @@ let db let isMensesStart let getMensesDaysRightAfter -export async function openDb ({ hash, persistConnection }) { +export async function openDb (hash) { const realmConfig = {} if (hash) { realmConfig.encryptionKey = hashToInt8Array(hash) } // perform migrations if necessary, see https://realm.io/docs/javascript/2.8.0/#migrations + // we open the db temporarily, to get the schema version even if the db is encrypted + let tempConnection + try { + tempConnection = await Realm.open(realmConfig) + } catch(err) { + // wrong password provided + if (hash && err.toString().includes('decrypt')) return false + // tried to open without password, but is encrypted + if (!hash && err.toString().includes('Invalid mnemonic')) return false + + throw err + } + let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath) + tempConnection.close() while (nextSchemaIndex < schemas.length - 1) { const tempConfig = Object.assign( realmConfig, @@ -34,10 +48,11 @@ export async function openDb ({ hash, persistConnection }) { schemas[schemas.length - 1] )) - if (persistConnection) db = connection + db = connection const cycle = cycleModule() isMensesStart = cycle.isMensesStart getMensesDaysRightAfter = cycle.getMensesDaysRightAfter + return true } export function getBleedingDaysSortedByDate() { @@ -212,7 +227,7 @@ export async function changeEncryptionAndRestartApp(hash) { export async function deleteDbAndOpenNew() { const exists = await fs.exists(Realm.defaultPath) if (exists) await fs.unlink(Realm.defaultPath) - await openDb({ persistConnection: true }) + await openDb() } function hashToInt8Array(hash) {