diff --git a/components/home.js b/components/home.js index b7156d9..799f845 100644 --- a/components/home.js +++ b/components/home.js @@ -6,9 +6,10 @@ import { ScrollView } from 'react-native' 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 } from '../db' +import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, encryptAndRestartApp } from '../db' import {bleedingPrediction as labels} from './labels' export default class Home extends Component { @@ -34,10 +35,31 @@ export default class Home extends Component { })(this) getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) + + 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) + } + } + + nodejs.start('main.js') + nodejs.channel.addListener( + 'message', + this.startEncryption, + this + ) } componentWillUnmount() { getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) + nodejs.channel.removeListener('message', this.startEncryption) } passTodayToDayView() { diff --git a/components/password-prompt.js b/components/password-prompt.js index 5ecc5cb..f3dcc97 100644 --- a/components/password-prompt.js +++ b/components/password-prompt.js @@ -20,30 +20,37 @@ export default class PasswordPrompt extends Component { this.setState({showApp: true}) } }) + + this.tryToOpenDb = 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) + } + try { + await openDbConnection(key) + } catch (err) { + console.log(err) + this.setState({ wrongPassword: true }) + } + } + } + nodejs.start('main.js') nodejs.channel.addListener( 'message', - 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) - } - try { - await openDbConnection(key) - } catch(err) { - console.log(err) - this.setState({wrongPassword: true}) - } - } - }, + this.tryToOpenDb, this ) } + componentWillUnmount() { + nodejs.channel.removeListener('message', this.tryToOpenDb) + } + render() { return ( diff --git a/db/index.js b/db/index.js index d68d46b..74a7852 100644 --- a/db/index.js +++ b/db/index.js @@ -174,7 +174,7 @@ function requestHash(pw) { })) } -async function encrypt(hash) { +async function encryptAndRestartApp(key) { const oldPath = db.path const dir = db.path.split('/') dir.pop() @@ -185,11 +185,6 @@ async function encrypt(hash) { db.writeCopyTo(copyPath) db.close() await fs.unlink(oldPath) - const key = new Array(64).map((_, i) => { - let code = hash.charCodeAt(i) - if (isNaN(code)) code = 0 - return code - }) realmConfig.encryptionKey = key db = new Realm(realmConfig) await saveEncryptionFlag(true) @@ -212,5 +207,5 @@ export { tryToImportWithDelete, tryToImportWithoutDelete, requestHash, - encrypt + encryptAndRestartApp }