A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: impl vault for encryption/decryption of notes
thecodrr1146bdf5ae94
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.jsindex 162383cea..242c983b1 100644--- a/packages/core/__tests__/notes.test.js+++ b/packages/core/__tests__/notes.test.js@@ -230,7 +230,7 @@ test("favorite note", () =>note = db.notes.note(id);expect(note.data.favorite).toBe(true);}));-+/*test("lock a note", () =>noteTest().then(async ({ db, id }) => {let note = db.notes.note(id);@@ -261,7 +261,7 @@ test("permanently unlock a locked note", () =>note = db.notes.note(id);expect((await note.delta()).ops).toBeDefined();expect(note.data.locked).toBe(false);- }));+ })); */test("add note to topic", () =>noteTest().then(async ({ db, id }) => {diff --git a/packages/core/api/index.js b/packages/core/api/index.jsindex 74fab33d3..53676d337 100644--- a/packages/core/api/index.js+++ b/packages/core/api/index.js@@ -4,6 +4,7 @@ import Trash from "../collections/trash";import Tags from "../collections/tags";import User from "../models/user";import Sync from "./sync";+import Vault from "./vault";class Database {constructor(context) {@@ -22,6 +23,7 @@ class Database {await this.notebooks.init(this.notes, this.trash);await this.trash.init(this.notes, this.notebooks);this.syncer = new Sync(this);+ this.vault = new Vault(this, this.context);}sync() {diff --git a/packages/core/api/vault.js b/packages/core/api/vault.jsnew file mode 100644index 000000000..4ece9096a--- /dev/null+++ b/packages/core/api/vault.js@@ -0,0 +1,81 @@+import Database from "./index";++const ERRORS = {+ noVault: "ERR_NO_VAULT",+ vaultLocked: "ERR_VAULT_LOCKED"+};++export default class Vault {+ /**+ *+ * @param {Database} database+ */+ constructor(database, context) {+ this.db = database;+ this.context = context;+ this.key = "Notesnook";+ this.password = "";+ }++ async create(password) {+ const lockKey = await this.context.read("lockKey");+ if (!lockKey || !lockKey.cipher || !lockKey.iv) {+ const encryptedData = await this.context.encrypt(password, this.key);+ await this.context.write("lockKey", encryptedData);+ this.password = password;+ }+ return true;+ }++ async unlock(password) {+ const lockKey = await this.context.read("lockKey");+ if (!this._exists(lockKey)) throw new Error("ERR_NO_VAULT");+ var data;+ try {+ data = await this.context.decrypt(password, lockKey);+ } catch (e) {+ throw new Error("ERR_WRNG_PWD");+ }+ if (data !== this.key) {+ throw new Error("ERR_WRNG_PWD");+ }+ this.password = password;+ return true;+ }++ async _exists(lockKey) {+ if (!lockKey) lockKey = await this.context.read("lockKey");+ return lockKey && lockKey.cipher && lockKey.iv;+ }++ async _locked() {+ return !this.password || !this.password.length;+ }++ async _check() {+ if (!(await this._exists())) {+ throw new Error(ERRORS.noVault);+ }++ if (await this._locked()) {+ throw new Error(ERRORS.vaultLocked);+ }+ }++ async add(id) {+ await this._check();+ await this.db.notes.note(id)._lock(this.password);+ }++ async remove(id, password) {+ if (await this.unlock(password)) {+ await this.db.notes.note(id)._unlock(this.password, true);+ }+ }++ async open(id, password) {+ if (await this.unlock(password)) {+ return this.db.notes.note(id)._unlock(password);+ }+ }+}diff --git a/packages/core/models/note.js b/packages/core/models/note.jsindex 191128931..6e90ee778 100644--- a/packages/core/models/note.js+++ b/packages/core/models/note.js@@ -74,7 +74,7 @@ export default class Note {return this._toggle("pinned");}- async lock(password) {+ async _lock(password) {let delta = qclone(await this.delta());if (delta) {delta = await this._notes._collection.indexer.encrypt(@@ -92,7 +92,7 @@ export default class Note {return await this._notes._collection.addItem(note);}- async unlock(password, perm = false) {+ async _unlock(password, perm = false) {let decrypted = JSON.parse(await this._notes._collection.indexer.decrypt(password,