diff --git a/components/app-wrapper.js b/components/app-wrapper.js
index 889161c..cd50bb7 100644
--- a/components/app-wrapper.js
+++ b/components/app-wrapper.js
@@ -17,7 +17,9 @@ export default class AppWrapper extends Component {
:
this.setState({showApp: true})}
+ showApp={() => {
+ this.setState({showApp: true})
+ }}
/>
}
diff --git a/components/home.js b/components/home.js
index 799f845..658f809 100644
--- a/components/home.js
+++ b/components/home.js
@@ -9,7 +9,7 @@ import { LocalDate, ChronoUnit } from 'js-joda'
import nodejs from 'nodejs-mobile-react-native'
import styles from '../styles/index'
import cycleModule from '../lib/cycle'
-import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, encryptAndRestartApp } from '../db'
+import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, changeEncryptionAndRestartApp } from '../db'
import {bleedingPrediction as labels} from './labels'
export default class Home extends Component {
@@ -38,18 +38,9 @@ export default class Home extends Component {
this.startEncryption = async (msg) => {
msg = JSON.parse(msg)
- if (msg.type === 'sha512') {
- const hash = msg.message
- const key = new Uint8Array(64)
- for (let i = 0; i < key.length; i++) {
- const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
- key[i] = parseInt(twoDigitHex, 16)
- }
- encryptAndRestartApp(key)
- }
+ changeEncryptionAndRestartApp(msg.message)
}
- nodejs.start('main.js')
nodejs.channel.addListener(
'message',
this.startEncryption,
diff --git a/components/password-prompt.js b/components/password-prompt.js
index d2e7b36..dd43489 100644
--- a/components/password-prompt.js
+++ b/components/password-prompt.js
@@ -19,8 +19,7 @@ export default class PasswordPrompt extends Component {
this.setState({showPasswordPrompt: true})
} else {
await openDb({persistConnection: true})
- console.log(this.props)
- this.props.onCorrectPassword()
+ this.props.showApp()
}
})
@@ -35,7 +34,6 @@ export default class PasswordPrompt extends Component {
msg = JSON.parse(msg)
if (msg.type != 'sha512') return
try {
- console.log('password prompt opening db')
await openDb({hash: msg.message, persistConnection: true })
} catch (err) {
Alert.alert(
@@ -48,7 +46,7 @@ export default class PasswordPrompt extends Component {
)
return
}
- this.setState({ showApp: true })
+ this.props.showApp()
}
componentWillUnmount() {
diff --git a/components/settings/password-setting.js b/components/settings/password-setting.js
index 210fadb..56859ac 100644
--- a/components/settings/password-setting.js
+++ b/components/settings/password-setting.js
@@ -12,7 +12,7 @@ import {
} from '../../local-storage'
import styles from '../../styles/index'
import { settings as labels, shared } from '../labels'
-import { requestHash, openDb } from '../../db'
+import { requestHash, openDb, changeEncryptionAndRestartApp } from '../../db'
export default class PasswordSetting extends Component {
constructor(props) {
@@ -40,9 +40,6 @@ export default class PasswordSetting extends Component {
if (msg.type != 'sha512') return
try {
await openDb({ hash: msg.message, persistConnection: false })
- this.setState({
- enteringCurrentPassword: false
- })
} catch (err) {
Alert.alert(
shared.incorrectPassword,
@@ -60,7 +57,9 @@ export default class PasswordSetting extends Component {
onPress: () => this.setState({currentPassword: null})
}]
)
+ return
}
+ await changeEncryptionAndRestartApp()
}
render() {
diff --git a/db/index.js b/db/index.js
index ceac742..f12b53f 100644
--- a/db/index.js
+++ b/db/index.js
@@ -170,13 +170,7 @@ function requestHash(pw) {
export async function openDb ({ hash, persistConnection }) {
if (hash) {
- const key = new Uint8Array(64)
- for (let i = 0; i < key.length; i++) {
- const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
- key[i] = parseInt(twoDigitHex, 16)
- }
-
- realmConfig.encryptionKey = key
+ realmConfig.encryptionKey = hashToInt8Array(hash)
}
const connection = await Realm.open(realmConfig)
@@ -184,37 +178,26 @@ export async function openDb ({ hash, persistConnection }) {
if (persistConnection) db = connection
}
-async function encryptAndRestartApp(key) {
- const oldPath = db.path
+export async function changeEncryptionAndRestartApp(hash) {
+ let key
+ if (hash) key = hashToInt8Array(hash)
+ const defaultPath = db.path
const dir = db.path.split('/')
dir.pop()
dir.push('copied.realm')
const copyPath = dir.join('/')
const exists = await fs.exists(copyPath)
if (exists) await fs.unlink(copyPath)
- db.writeCopyTo(copyPath)
+ // for some reason, realm complains if we give it a key with value undefined
+ if (key) {
+ db.writeCopyTo(copyPath, key)
+ } else {
+ db.writeCopyTo(copyPath)
+ }
db.close()
- await fs.unlink(oldPath)
- realmConfig.encryptionKey = key
- db = new Realm(realmConfig)
- await saveEncryptionFlag(true)
- restart.Restart()
-}
-
-export async function removeDbEncryptionAndRestartApp(key) {
- const oldPath = db.path
- const dir = db.path.split('/')
- dir.pop()
- dir.push('copied.realm')
- const copyPath = dir.join('/')
- const exists = await fs.exists(copyPath)
- if (exists) await fs.unlink(copyPath)
- db.writeCopyTo(copyPath)
- db.close()
- await fs.unlink(oldPath)
- realmConfig.encryptionKey = key
- db = new Realm(realmConfig)
- await saveEncryptionFlag(true)
+ await fs.unlink(defaultPath)
+ await fs.moveFile(copyPath, defaultPath)
+ await saveEncryptionFlag(key ? true : false)
restart.Restart()
}
@@ -222,6 +205,16 @@ async function deleteDbAndOpenNew() {
const exists = await fs.exists(Realm.defaultPath)
if (exists) await fs.unlink(Realm.defaultPath)
await openDb({ persistConnection: true })
+ await saveEncryptionFlag(false)
+}
+
+function hashToInt8Array(hash) {
+ const key = new Uint8Array(64)
+ for (let i = 0; i < key.length; i++) {
+ const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
+ key[i] = parseInt(twoDigitHex, 16)
+ }
+ return key
}
export {
@@ -240,6 +233,5 @@ export {
tryToImportWithDelete,
tryToImportWithoutDelete,
requestHash,
- encryptAndRestartApp,
deleteDbAndOpenNew
}