Always persist connection. Throw all unknown errors.
This commit is contained in:
@@ -24,7 +24,7 @@ export default class PasswordPrompt extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async tryToOpenDb() {
|
async tryToOpenDb() {
|
||||||
const connected = await openDb({ persistConnection: true })
|
const connected = await openDb()
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
this.setState({ showPasswordPrompt: true })
|
this.setState({ showPasswordPrompt: true })
|
||||||
await saveEncryptionFlag(true)
|
await saveEncryptionFlag(true)
|
||||||
@@ -35,7 +35,7 @@ export default class PasswordPrompt extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
passHashToDb = async hash => {
|
passHashToDb = async hash => {
|
||||||
const connected = await openDb({ hash, persistConnection: true })
|
const connected = await openDb(hash)
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
shared.incorrectPassword,
|
shared.incorrectPassword,
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { openDb } from '../../../db'
|
|||||||
import { shared } from '../../labels'
|
import { shared } from '../../labels'
|
||||||
|
|
||||||
export default async function checkPassword({hash, onCancel, onTryAgain }) {
|
export default async function checkPassword({hash, onCancel, onTryAgain }) {
|
||||||
const connected = await openDb({ hash, persistConnection: false })
|
const connected = await openDb(hash)
|
||||||
if (!connected) {
|
if (connected) return true
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
shared.incorrectPassword,
|
shared.incorrectPassword,
|
||||||
shared.incorrectPasswordMessage,
|
shared.incorrectPasswordMessage,
|
||||||
@@ -18,4 +18,3 @@ export default async function checkPassword({hash, onCancel, onTryAgain }) {
|
|||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
+18
-11
@@ -10,14 +10,28 @@ let db
|
|||||||
let isMensesStart
|
let isMensesStart
|
||||||
let getMensesDaysRightAfter
|
let getMensesDaysRightAfter
|
||||||
|
|
||||||
export async function openDb ({ hash, persistConnection }) {
|
export async function openDb (hash) {
|
||||||
const realmConfig = {}
|
const realmConfig = {}
|
||||||
if (hash) {
|
if (hash) {
|
||||||
realmConfig.encryptionKey = hashToInt8Array(hash)
|
realmConfig.encryptionKey = hashToInt8Array(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform migrations if necessary, see https://realm.io/docs/javascript/2.8.0/#migrations
|
// 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)
|
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath)
|
||||||
|
tempConnection.close()
|
||||||
while (nextSchemaIndex < schemas.length - 1) {
|
while (nextSchemaIndex < schemas.length - 1) {
|
||||||
const tempConfig = Object.assign(
|
const tempConfig = Object.assign(
|
||||||
realmConfig,
|
realmConfig,
|
||||||
@@ -29,23 +43,15 @@ export async function openDb ({ hash, persistConnection }) {
|
|||||||
|
|
||||||
// open the Realm with the latest schema
|
// open the Realm with the latest schema
|
||||||
realmConfig.schema = schemas[schemas.length - 1]
|
realmConfig.schema = schemas[schemas.length - 1]
|
||||||
let connection
|
const connection = await Realm.open(Object.assign(
|
||||||
try {
|
|
||||||
connection = await Realm.open(Object.assign(
|
|
||||||
realmConfig,
|
realmConfig,
|
||||||
schemas[schemas.length - 1]
|
schemas[schemas.length - 1]
|
||||||
))
|
))
|
||||||
} catch(err) {
|
|
||||||
if (!err.toString().includes('decrypt')) throw err
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (persistConnection) {
|
|
||||||
db = connection
|
db = connection
|
||||||
const cycle = cycleModule()
|
const cycle = cycleModule()
|
||||||
isMensesStart = cycle.isMensesStart
|
isMensesStart = cycle.isMensesStart
|
||||||
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +203,7 @@ export function requestHash(type, pw) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function changeEncryptionAndRestartApp(hash) {
|
export async function changeEncryptionAndRestartApp(hash) {
|
||||||
|
console.log('this runs')
|
||||||
let key
|
let key
|
||||||
if (hash) key = hashToInt8Array(hash)
|
if (hash) key = hashToInt8Array(hash)
|
||||||
const defaultPath = db.path
|
const defaultPath = db.path
|
||||||
@@ -221,7 +228,7 @@ export async function changeEncryptionAndRestartApp(hash) {
|
|||||||
export async function deleteDbAndOpenNew() {
|
export async function deleteDbAndOpenNew() {
|
||||||
const exists = await fs.exists(Realm.defaultPath)
|
const exists = await fs.exists(Realm.defaultPath)
|
||||||
if (exists) await fs.unlink(Realm.defaultPath)
|
if (exists) await fs.unlink(Realm.defaultPath)
|
||||||
await openDb({ persistConnection: true })
|
await openDb()
|
||||||
}
|
}
|
||||||
|
|
||||||
function hashToInt8Array(hash) {
|
function hashToInt8Array(hash) {
|
||||||
|
|||||||
Reference in New Issue
Block a user