Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: restore backup with encryption key

Ammar Ahmedb84cc747cb64
diff --git a/apps/mobile/app/components/dialog/index.js b/apps/mobile/app/components/dialog/index.js
index b36d4ed5e..7f878ebe4 100644
--- a/apps/mobile/app/components/dialog/index.js
+++ b/apps/mobile/app/components/dialog/index.js
@@ -40,7 +40,10 @@ import { Button } from "../ui/button";
export const Dialog = ({ context = "global" }) => {
const { colors } = useThemeColors();
const [visible, setVisible] = useState(false);
- const [inputValue, setInputValue] = useState(null);
+ const [checked, setChecked] = useState(false);
+ const values = useRef({
+ inputValue: undefined
+ });
const inputRef = useRef();
const [dialogInfo, setDialogInfo] = useState({
title: "",
@@ -76,7 +79,8 @@ export const Dialog = ({ context = "global" }) => {
if (dialogInfo.positivePress) {
inputRef.current?.blur();
let result = await dialogInfo.positivePress(
- inputValue || dialogInfo.defaultValue
+ values.current.inputValue || dialogInfo.defaultValue,
+ checked
);
if (result === false) {
return;
@@ -91,14 +95,16 @@ export const Dialog = ({ context = "global" }) => {
if (!data.context) data.context = "global";
if (data.context !== context) return;
setDialogInfo(data);
+ setChecked(false);
+ values.current.inputValue = data.defaultValue;
setVisible(true);
- setInputValue(data.defaultValue);
},
[context]
);
const hide = () => {
- setInputValue(null);
+ setChecked(false);
+ values.current.inputValue = undefined;
setVisible(false);
};
@@ -106,7 +112,6 @@ export const Dialog = ({ context = "global" }) => {
if (dialogInfo.onClose) {
await dialogInfo.onClose();
}
-
hide();
};
@@ -159,7 +164,7 @@ export const Dialog = ({ context = "global" }) => {
fwdRef={inputRef}
autoCapitalize="none"
onChangeText={(value) => {
- setInputValue(value);
+ values.current.inputValue = value;
}}
testID="input-value"
secureTextEntry={dialogInfo.secureTextEntry}
@@ -176,10 +181,10 @@ export const Dialog = ({ context = "global" }) => {
<>
<Button
onPress={() => {
- setInputValue(!inputValue);
+ setChecked(!checked);
}}
icon={
- inputValue
+ checked
? "check-circle-outline"
: "checkbox-blank-circle-outline"
}
@@ -187,9 +192,10 @@ export const Dialog = ({ context = "global" }) => {
justifyContent: "flex-start"
}}
height={35}
+ iconSize={20}
width="100%"
title={dialogInfo.check.info}
- type={inputValue ? dialogInfo.check.type : "gray"}
+ type={checked ? dialogInfo.check.type : "gray"}
/>
</>
) : null}
diff --git a/apps/mobile/app/components/sheets/restore-data/index.js b/apps/mobile/app/components/sheets/restore-data/index.js
index 94ee21375..e5dd3382e 100644
--- a/apps/mobile/app/components/sheets/restore-data/index.js
+++ b/apps/mobile/app/components/sheets/restore-data/index.js
@@ -17,17 +17,19 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { getFormattedDate } from "@notesnook/common";
import { EVENTS } from "@notesnook/core/dist/common";
+import { useThemeColors } from "@notesnook/theme";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { ActivityIndicator, Platform, View } from "react-native";
import { FlatList } from "react-native-actions-sheet";
import RNFetchBlob from "react-native-blob-util";
-import DocumentPicker, {
- DocumentPickerResponse
-} from "react-native-document-picker";
+import DocumentPicker from "react-native-document-picker";
import * as ScopedStorage from "react-native-scoped-storage";
+import { unzip } from "react-native-zip-archive";
import { db } from "../../../common/database";
import storage from "../../../common/database/storage";
+import { cacheDir, copyFileAsync } from "../../../common/filesystem/utils";
import {
ToastEvent,
eSubscribeEvent,
@@ -35,7 +37,6 @@ import {
} from "../../../services/event-manager";
import SettingsService from "../../../services/settings";
import { initialize } from "../../../stores";
-import { useThemeColors } from "@notesnook/theme";
import { eCloseRestoreDialog, eOpenRestoreDialog } from "../../../utils/events";
import { SIZE } from "../../../utils/size";
import { Dialog } from "../../dialog";
@@ -46,9 +47,6 @@ import { Button } from "../../ui/button";
import Seperator from "../../ui/seperator";
import SheetWrapper from "../../ui/sheet";
import Paragraph from "../../ui/typography/paragraph";
-import { getFormattedDate } from "@notesnook/common";
-import { unzip } from "react-native-zip-archive";
-import { cacheDir, copyFileAsync } from "../../../common/filesystem/utils";
const RestoreDataSheet = () => {
const [visible, setVisible] = useState(false);
@@ -184,7 +182,7 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
title: "Encrypted backup",
input: true,
inputPlaceholder: "Password",
- paragraph: "Please enter password of this backup file to restore it",
+ paragraph: "Please enter password of this backup file",
positiveText: "Restore",
secureTextEntry: true,
onClose: () => {
@@ -192,10 +190,17 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
resolve(undefined);
},
negativeText: "Cancel",
- positivePress: async (password) => {
- resolve(password);
+ positivePress: async (password, isEncryptionKey) => {
+ resolve({
+ value: password,
+ isEncryptionKey
+ });
resolved = true;
return true;
+ },
+ check: {
+ info: "Use encryption key",
+ type: "transparent"
}
});
});
@@ -235,8 +240,8 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
}
};
- const restoreBackup = async (backup, password) => {
- await db.backup.import(backup, password);
+ const restoreBackup = async (backup, password, key) => {
+ await db.backup.import(backup, password, key);
await db.initCollections();
initialize();
ToastEvent.show({
@@ -279,6 +284,7 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
}
let password;
+ let key;
console.log(`Found ${backupFiles?.length} files to restore from backup`);
for (const path of backupFiles) {
@@ -289,10 +295,16 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
if (parsed.encrypted && !password) {
console.log("Backup is encrypted...", "requesting password");
- password = await withPassword();
- if (!password) throw new Error("Failed to decrypt backup");
+ const { value, isEncryptionKey } = await withPassword();
+
+ if (isEncryptionKey) {
+ key = value;
+ } else {
+ password = value;
+ }
+ if (!password && !key) throw new Error("Failed to decrypt backup");
}
- await db.backup.import(parsed, password);
+ await db.backup.import(parsed, password, key);
console.log("Imported", path);
}
// Remove files from cache
@@ -323,10 +335,19 @@ const RestoreDataComponent = ({ close, setRestoring, restoring }) => {
async function restoreFromNNBackup(backup) {
try {
if (backup.data.iv && backup.data.salt) {
- const password = await withPassword();
- if (password) {
+ const { value, isEncryptionKey } = await withPassword();
+
+ let key;
+ let password;
+ if (isEncryptionKey) {
+ key = value;
+ } else {
+ password = value;
+ }
+
+ if (key || password) {
try {
- await restoreBackup(backup, password);
+ await restoreBackup(backup, password, key);
close();
setRestoring(false);
} catch (e) {
diff --git a/apps/mobile/app/utils/functions.js b/apps/mobile/app/utils/functions.js
index 1b27dcff1..1568c7145 100644
--- a/apps/mobile/app/utils/functions.js
+++ b/apps/mobile/app/utils/functions.js
@@ -36,7 +36,7 @@ function confirmDeleteAllNotes(items, type, context) {
}?`,
positiveText: "Delete",
negativeText: "Cancel",
- positivePress: (value) => {
+ positivePress: (_inputValue, value) => {
setTimeout(() => {
resolve({ delete: true, deleteNotes: value });
});