Skip to content

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

refactor: vault api

thecodrrcff0bef0b700
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index 4ece9096a..45a94d181 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -4,52 +4,51 @@ 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 = "";
+ this._db = database;
+ this._context = context;
+ this._key = "Notesnook";
+ this._password = "";
}
async create(password) {
- const lockKey = await this.context.read("lockKey");
+ 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;
+ 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");
+ 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);
+ data = await this._context.decrypt(password, lockKey);
} catch (e) {
throw new Error("ERR_WRNG_PWD");
}
- if (data !== this.key) {
+ if (data !== this._key) {
throw new Error("ERR_WRNG_PWD");
}
- this.password = password;
+ this._password = password;
return true;
}
async _exists(lockKey) {
- if (!lockKey) lockKey = await this.context.read("lockKey");
+ if (!lockKey) lockKey = await this._context.read("lockKey");
return lockKey && lockKey.cipher && lockKey.iv;
}
async _locked() {
- return !this.password || !this.password.length;
+ return !this._password || !this._password.length;
}
async _check() {
@@ -64,18 +63,93 @@ export default class Vault {
async add(id) {
await this._check();
- await this.db.notes.note(id)._lock(this.password);
+ const note = this._db.notes.note(id).data;
+ await this._lockNote(id, note);
}
async remove(id, password) {
if (await this.unlock(password)) {
- await this.db.notes.note(id)._unlock(this.password, true);
+ const note = this._db.notes.note(id).data;
+ await this._unlockNote(note, true);
}
}
async open(id, password) {
if (await this.unlock(password)) {
- return this.db.notes.note(id)._unlock(password);
+ const note = this._db.notes.note(id).data;
+ return this._unlockNote(note, false);
+ }
+ }
+
+ async save(note) {
+ if (!note) return;
+ await this._check();
+ let id = note.id || Date.now().toString() + "_note";
+ return await this._lockNote(id, note);
+ }
+
+ _encryptText(text) {
+ return this._context.encrypt(this._password, JSON.stringify({ text }));
+ }
+ async _decryptText(text) {
+ const decrypted = await this._context.decrypt(this._password, text);
+ return JSON.parse(decrypted);
+ }
+
+ async _encryptDelta(id, deltaArg) {
+ if (!deltaArg) return;
+ const delta = await this._context.encrypt(
+ this._password,
+ JSON.stringify(deltaArg)
+ );
+ console.log(delta);
+ await this._context.write(this._deltaId(id), delta);
+ }
+
+ async _decryptDelta(id) {
+ const delta = await this._context.read(this._deltaId(id));
+ const decrypted = await this._context.decrypt(this._password, delta);
+ return JSON.parse(decrypted);
+ }
+
+ _deltaId(id) {
+ return id + "_delta";
+ }
+
+ async _lockNote(id, note) {
+ if (!note) return;
+
+ let delta = note.content.delta;
+ if (!delta) delta = await this._context.read(this._deltaId(id));
+ await this._encryptDelta(id, delta);
+
+ const content = await this._encryptText(note.content.text);
+
+ return await this._db.notes.add({
+ id,
+ content,
+ locked: true
+ });
+ }
+
+ async _unlockNote(note, perm = false) {
+ if (!note.locked) return;
+
+ let decrypted = await this._decryptText(note.content);
+ let delta = await this._decryptDelta(note.id);
+
+ if (perm) {
+ await this._db.notes.add({
+ id: note.id,
+ content: decrypted,
+ locked: false
+ });
+ return await this._context.write(this._deltaId(note.id), delta);
}
+
+ return {
+ ...note,
+ content: { ...decrypted, delta }
+ };
}
}
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 8effb5a6f..9701a6c7c 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -51,7 +51,7 @@ export default class Notes {
return;
}
- if (note.content.delta.ops) {
+ if (note.content.delta && note.content.delta.ops) {
await this._deltaStorage.write(id + "_delta", note.content.delta);
}
@@ -59,7 +59,7 @@ export default class Notes {
id,
type: "note",
title: getNoteTitle(note),
- content: getNoteContent(note, id),
+ content: getNoteContent(note),
pinned: !!note.pinned,
locked: !!note.locked,
notebook: note.notebook || {},
@@ -200,12 +200,12 @@ export default class Notes {
}
function isNoteEmpty(note) {
+ const isTitleEmpty = !note.title || note.title.trim().length <= 0;
+ const isTextEmpty =
+ !note.content.text || note.content.text.trim().length <= 0;
+ const hasDelta = note.content.delta !== undefined;
return (
- !note.content ||
- !note.content.delta ||
- (!note.locked &&
- (!note.title || note.title.trim().length <= 0) &&
- (!note.content.text || note.content.text.trim().length <= 0))
+ !note.content || (!note.locked && isTitleEmpty && isTextEmpty && !hasDelta)
);
}
@@ -226,13 +226,12 @@ function getNoteTitle(note) {
.trim();
}
-function getNoteContent(note, id) {
+function getNoteContent(note) {
if (note.locked) {
return note.content;
}
return {
- text: note.content.text.trim(),
- delta: id + "_delta"
+ text: note.content.text.trim()
};
}
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index 6e90ee778..0a39b29ba 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -73,50 +73,6 @@ export default class Note {
pin() {
return this._toggle("pinned");
}
-
- async _lock(password) {
- let delta = qclone(await this.delta());
- if (delta) {
- delta = await this._notes._collection.indexer.encrypt(
- password,
- JSON.stringify(delta)
- );
- await this._notes._deltaStorage.write(this._note.id + "_delta", delta);
- }
- const note = { ...this._note };
- note.content = await this._notes._collection.indexer.encrypt(
- password,
- JSON.stringify(this._note.content)
- );
- note.locked = true;
- return await this._notes._collection.addItem(note);
- }
-
- async _unlock(password, perm = false) {
- let decrypted = JSON.parse(
- await this._notes._collection.indexer.decrypt(
- password,
- this._note.content
- )
- );
- let delta = JSON.parse(
- await this._notes._collection.indexer.decrypt(
- password,
- await this.delta()
- )
- );
- if (perm) {
- const note = { ...this._note, locked: false, content: decrypted };
- note.locked = false;
- note.content = decrypted;
- await this._notes._collection.addItem(note);
- await this._notes._deltaStorage.write(note.id + "_delta", delta);
- }
- return {
- ...this._note,
- content: { ...decrypted, delta }
- };
- }
}
async function addTag(tag, collection, array) {