From 207b5504e77d14989cf9a6aa89a398a371026980 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 30 Oct 2018 09:14:46 +0100 Subject: [PATCH 1/3] Only throw unexpected errors from openDb --- components/password-prompt.js | 11 ++++------- .../settings/password/check-current-password.js | 6 ++---- db/index.js | 13 +++++++++++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/components/password-prompt.js b/components/password-prompt.js index f9e63ee..c2a246e 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({ persistConnection: true }) + 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, persistConnection: true }) + 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..0a27487 100644 --- a/components/settings/password/check-current-password.js +++ b/components/settings/password/check-current-password.js @@ -3,10 +3,8 @@ 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) { + const connected = await openDb({ hash, persistConnection: false }) + if (!connected) { Alert.alert( shared.incorrectPassword, shared.incorrectPasswordMessage, diff --git a/db/index.js b/db/index.js index fe8c269..15523f6 100644 --- a/db/index.js +++ b/db/index.js @@ -29,16 +29,25 @@ export async function openDb ({ hash, persistConnection }) { // open the Realm with the latest schema realmConfig.schema = schemas[schemas.length - 1] - const connection = await Realm.open(Object.assign( + let connection + try { + connection = await Realm.open(Object.assign( realmConfig, schemas[schemas.length - 1] )) + } catch(err) { + if (!err.toString().includes('decrypt')) throw err + return false + } - if (persistConnection) db = connection + if (persistConnection) { + db = connection const cycle = cycleModule() isMensesStart = cycle.isMensesStart getMensesDaysRightAfter = cycle.getMensesDaysRightAfter } + return true +} export function getBleedingDaysSortedByDate() { return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) From fa85f321c33ace4144db56a2de12ab99d5517fd7 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 30 Oct 2018 09:46:42 +0100 Subject: [PATCH 2/3] Always persist connection. Throw all unknown errors. --- components/password-prompt.js | 4 +-- .../password/check-current-password.js | 29 +++++++++-------- db/index.js | 31 ++++++++++++------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/components/password-prompt.js b/components/password-prompt.js index c2a246e..dec0b68 100644 --- a/components/password-prompt.js +++ b/components/password-prompt.js @@ -24,7 +24,7 @@ export default class PasswordPrompt extends Component { } async tryToOpenDb() { - const connected = await openDb({ persistConnection: true }) + const connected = await openDb() if (!connected) { this.setState({ showPasswordPrompt: true }) await saveEncryptionFlag(true) @@ -35,7 +35,7 @@ export default class PasswordPrompt extends Component { } passHashToDb = async hash => { - const connected = await openDb({ hash, persistConnection: true }) + const connected = await openDb(hash) if (!connected) { Alert.alert( shared.incorrectPassword, diff --git a/components/settings/password/check-current-password.js b/components/settings/password/check-current-password.js index 0a27487..7d60308 100644 --- a/components/settings/password/check-current-password.js +++ b/components/settings/password/check-current-password.js @@ -3,19 +3,18 @@ import { openDb } from '../../../db' import { shared } from '../../labels' export default async function checkPassword({hash, onCancel, onTryAgain }) { - const connected = await openDb({ hash, persistConnection: false }) - if (!connected) { - 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 15523f6..2fcdc5a 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, @@ -29,23 +43,15 @@ export async function openDb ({ hash, persistConnection }) { // open the Realm with the latest schema realmConfig.schema = schemas[schemas.length - 1] - let connection - try { - connection = await Realm.open(Object.assign( + const connection = await Realm.open(Object.assign( realmConfig, schemas[schemas.length - 1] )) - } catch(err) { - if (!err.toString().includes('decrypt')) throw err - return false - } - if (persistConnection) { - db = connection + db = connection const cycle = cycleModule() isMensesStart = cycle.isMensesStart getMensesDaysRightAfter = cycle.getMensesDaysRightAfter -} return true } @@ -197,6 +203,7 @@ export function requestHash(type, pw) { } export async function changeEncryptionAndRestartApp(hash) { + console.log('this runs') let key if (hash) key = hashToInt8Array(hash) const defaultPath = db.path @@ -221,7 +228,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) { From 2552a01efc13b1a3216eb823bc9eefef6c2c7edc Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 2 Nov 2018 15:25:40 +0100 Subject: [PATCH 3/3] Remove debug log --- db/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/db/index.js b/db/index.js index 2fcdc5a..7722ec9 100644 --- a/db/index.js +++ b/db/index.js @@ -203,7 +203,6 @@ export function requestHash(type, pw) { } export async function changeEncryptionAndRestartApp(hash) { - console.log('this runs') let key if (hash) key = hashToInt8Array(hash) const defaultPath = db.path