Skip to content

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

feat: use abstract lock/unlock functions

thecodrr2d3b4379086f
diff --git a/packages/core/__mocks__/storage.mock.js b/packages/core/__mocks__/storage.mock.js
index d661c82da..4e25b2b92 100644
--- a/packages/core/__mocks__/storage.mock.js
+++ b/packages/core/__mocks__/storage.mock.js
@@ -13,9 +13,17 @@ function clear() {
storage = {};
}
+function encrypt(password, data) {
+ return new Promise((resolve, reject) => resolve({ iv: "", cipher: data }));
+}
+function decrypt(password, data) {
+ return new Promise((resolve, reject) => resolve(data.cipher));
+}
module.exports = {
read,
write,
remove,
- clear
+ clear,
+ encrypt,
+ decrypt
};
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.js
index 3dce1ab72..24e1a6f24 100644
--- a/packages/core/__tests__/database.test.js
+++ b/packages/core/__tests__/database.test.js
@@ -1,7 +1,6 @@
import Database from "../api/database";
import StorageInterface from "../__mocks__/storage.mock";
import { getLastWeekTimestamp } from "../utils/date";
-const sjcl = require("sjcl");
function databaseTest() {
let db = new Database(StorageInterface);
@@ -359,10 +358,11 @@ test("deletion of invalid topic from notebook should return false", () =>
expect(res).toBe(false);
}));
-test("moving note with wrong id should return false", () =>
+test("moving note with wrong id should throw", () =>
databaseTest().then(async db => {
- let res = await db.moveNote(0, undefined, undefined);
- expect(res).toBe(false);
+ db.moveNote(0, undefined, undefined).catch(err =>
+ expect(err.message).toContain("Failed to move note.")
+ );
}));
test("moving note to non-existent notebook should return false", () =>
@@ -473,7 +473,7 @@ test("lock and unlock note", () =>
expect(await db.lockNote(timestamp, "password123")).toBe(true);
let note = db.getNote(timestamp);
expect(note.locked).toBe(true);
- expect(note.content).toContain("iv");
+ expect(note.content.iv).toBeDefined();
note = await db.unlockNote(timestamp, "password123");
expect(note.dateCreated).toBe(timestamp);
expect(note.content.text).toBe(TEST_NOTE.content.text);
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index 79e5ac16b..ec3eccc64 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -8,7 +8,6 @@ import {
months,
getLastWeekTimestamp
} from "../utils/date";
-import Encryptor from "sjcl";
const KEYS = {
notes: "notes",
@@ -197,10 +196,10 @@ class Database {
if (!this.notes[noteId]) {
throw new Error(`Cannot lock note. Invalid ID: ${noteId} given.`);
}
- this.notes[noteId].content = Encryptor.encrypt(
+
+ this.notes[noteId].content = await this.storage.encrypt(
password,
- JSON.stringify(this.notes[noteId].content),
- { ks: 256 }
+ JSON.stringify(this.notes[noteId].content)
);
this.notes[noteId].locked = true;
await this.storage.write(KEYS.notes, this.notes);
@@ -211,7 +210,10 @@ class Database {
if (!this.notes[noteId]) {
throw new Error(`Cannot unlock note. Invalid ID: ${noteId} given.`);
}
- let decrypted = Encryptor.decrypt(password, this.notes[noteId].content);
+ let decrypted = await this.storage.decrypt(
+ password,
+ this.notes[noteId].content
+ );
if (perm) {
this.notes[noteId].locked = false;
await this.storage.write(KEYS.notes, this.notes);
@@ -355,20 +357,19 @@ class Database {
}
async moveNote(noteId, from, to) {
- if (
- !noteId ||
- (from && (!from.notebook || !from.topic)) ||
- !to ||
- !to.notebook ||
- !to.topic ||
- (from.notebook === to.notebook && from.topic === to.topic) //moving to same notebook and topic shouldn't be possible
- )
- return false;
- if (!from) {
+ if (!noteId || !to || !to.notebook || !to.topic) {
+ throw new Error(`Error: Failed to move note.`);
+ }
+ if (!from.notebook && !from.topic) {
return await this.addNoteToTopic(to.notebook, to.topic, noteId);
} else if (
await this.deleteNoteFromTopic(from.notebook, from.topic, noteId)
) {
+ if (from.notebook === to.notebook && from.topic === to.topic) {
+ throw new Error(
+ "Moving to the same notebook and topic is not possible."
+ );
+ }
return await this.addNoteToTopic(to.notebook, to.topic, noteId);
}
return false;
diff --git a/packages/core/helpers/storage.js b/packages/core/helpers/storage.js
index 0ff4a5a09..b3393c389 100644
--- a/packages/core/helpers/storage.js
+++ b/packages/core/helpers/storage.js
@@ -17,6 +17,12 @@ class Storage {
remove(key) {
this.storage.remove(key); //TODO add test
}
+ encrypt(password, data) {
+ return this.storage.encrypt(password, data);
+ }
+ decrypt(password, data) {
+ return this.storage.decrypt(password, data);
+ }
}
export default Storage;
diff --git a/packages/core/package.json b/packages/core/package.json
index ca21ddcdb..d34e02fdf 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -17,7 +17,6 @@
},
"dependencies": {
"fuzzysearch": "^1.0.3",
- "sjcl": "^1.0.8",
"transfun": "^1.0.2"
}
}
diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock
index 1aaa5b027..ca399a1e7 100644
--- a/packages/core/yarn.lock
+++ b/packages/core/yarn.lock
@@ -4157,11 +4157,6 @@ sisteransi@^1.0.3:
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.4.tgz#386713f1ef688c7c0304dc4c0632898941cad2e3"
integrity sha512-/ekMoM4NJ59ivGSfKapeG+FWtrmWvA1p6FBZwXrqojw90vJu8lBmrTxCMuBCydKtkaUe2zt4PlxeTKpjwMbyig==
-sjcl@^1.0.8:
- version "1.0.8"
- resolved "https://registry.yarnpkg.com/sjcl/-/sjcl-1.0.8.tgz#f2ec8d7dc1f0f21b069b8914a41a8f236b0e252a"
- integrity sha512-LzIjEQ0S0DpIgnxMEayM1rq9aGwGRG4OnZhCdjx7glTaJtf4zRfpg87ImfjSJjoW9vKpagd82McDOwbRT5kQKQ==
-
slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"