Skip to content

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

mobile: add async storage & encryption

ammarahm-edc4f894f8d0df
diff --git a/apps/mobile/app/common/database/encryption.js b/apps/mobile/app/common/database/encryption.js
index bf629f4e0..05ac18df2 100644
--- a/apps/mobile/app/common/database/encryption.js
+++ b/apps/mobile/app/common/database/encryption.js
@@ -126,6 +126,18 @@ export async function decrypt(password, data) {
return await Sodium.decrypt(password, _data);
}
+export async function decryptMulti(password, data) {
+ if (!password.password && !password.key) return undefined;
+ if (password.password && password.password === "" && !password.key)
+ return undefined;
+
+ data = data.map((d) => {
+ d.output = "plain";
+ return d;
+ });
+ return await Sodium.decryptMulti(password, data);
+}
+
export function parseAlgorithm(alg) {
if (!alg) return {};
const [enc, kdf, compressed, compressionAlg, base64variant] = alg.split("-");
diff --git a/apps/mobile/app/common/database/storage.js b/apps/mobile/app/common/database/storage.js
index b42da636e..47e71679c 100644
--- a/apps/mobile/app/common/database/storage.js
+++ b/apps/mobile/app/common/database/storage.js
@@ -27,11 +27,15 @@ import {
getCryptoKey,
getRandomBytes,
hash,
- removeCryptoKey
+ removeCryptoKey,
+ decryptMulti
} from "./encryption";
import { MMKV } from "./mmkv";
export class KV {
+ /**
+ * @type {typeof MMKV}
+ */
storage = null;
constructor(storage) {
this.storage = storage;
@@ -60,18 +64,24 @@ export class KV {
if (keys.length <= 0) {
return [];
} else {
- let data = await this.storage.getMultipleItemsAsync(keys.slice());
-
- return data.map(([key, value]) => {
- let obj;
- try {
- obj = JSON.parse(value);
- } catch (e) {
- obj = value;
- }
-
- return [key, obj];
- });
+ try {
+ let data = await this.storage.getMultipleItemsAsync(
+ keys.slice(),
+ "object"
+ );
+ return data.map(([key, value]) => {
+ let obj;
+ try {
+ obj = JSON.parse(value);
+ } catch (e) {
+ obj = value;
+ }
+
+ return [key, obj];
+ });
+ } catch (e) {
+ console.log(e);
+ }
}
}
@@ -96,6 +106,10 @@ export class KV {
);
return keys;
}
+
+ async writeMulti(items) {
+ return this.storage.setMultipleItemsAsync(items, "object");
+ }
}
const DefaultStorage = new KV(MMKV);
@@ -129,8 +143,10 @@ export default {
remove: (key) => DefaultStorage.remove(key),
clear: () => DefaultStorage.clear(),
getAllKeys: () => DefaultStorage.getAllKeys(),
+ writeMulti: (items) => DefaultStorage.writeMulti(items),
encrypt,
decrypt,
+ decryptMulti,
getRandomBytes,
checkAndCreateDir,
requestPermission,