Skip to content

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

vault: add jsdoc and reorder methods

thecodrrff94fc428ea5
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index ef5b1fa32..69ec68c5a 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -18,7 +18,7 @@ class Database {
async init() {
this.user = new User(this.context);
this.syncer = new Sync(this);
- this.vault = new Vault(this, this.context);
+ this.vault = new Vault(this);
this.conflicts = new Conflicts(this);
this.lookup = new Lookup(this);
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index 83eda3175..c3aa62f72 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -3,11 +3,11 @@ import getId from "../utils/id";
export default class Vault {
/**
*
- * @param {import('./index').default} database
+ * @param {import('./index').default} db
*/
- constructor(database, context) {
- this._db = database;
- this._context = context;
+ constructor(db) {
+ this._db = db;
+ this._context = db.context;
this._key = "Notesnook";
this._password = "";
this.ERRORS = {
@@ -17,15 +17,11 @@ export default class Vault {
};
}
- async _getKey() {
- if (await this._exists()) return await this._context.read("vaultKey");
- }
-
- async _setKey(vaultKey) {
- if (!vaultKey) return;
- await this._context.write("vaultKey", vaultKey);
- }
-
+ /**
+ * Creates a new vault (replacing if any older exists)
+ * @param {string} password The password
+ * @returns {Boolean}
+ */
async create(password) {
const vaultKey = await this._context.read("vaultKey");
if (!vaultKey || !vaultKey.cipher || !vaultKey.iv) {
@@ -39,9 +35,15 @@ export default class Vault {
return true;
}
+ /**
+ * Unlocks the vault with the given password
+ * @param {string} password The password
+ * @throws ERR_NO_VAULT | ERR_WRONG_PASSWORD
+ * @returns {Boolean}
+ */
async unlock(password) {
const vaultKey = await this._context.read("vaultKey");
- if (!(await this._exists(vaultKey))) throw new Error("ERR_NO_VAULT");
+ if (!(await this._exists(vaultKey))) throw new Error(this.ERRORS.noVault);
var data;
try {
data = await this._context.decrypt({ password }, vaultKey);
@@ -55,45 +57,44 @@ export default class Vault {
return true;
}
- async _exists(vaultKey) {
- if (!vaultKey) vaultKey = await this._context.read("vaultKey");
- return vaultKey && vaultKey.cipher && vaultKey.iv;
- }
-
- async _locked() {
- return !this._password || !this._password.length;
- }
-
- async _check() {
- if (!(await this._exists())) {
- throw new Error(this.ERRORS.noVault);
- }
-
- if (await this._locked()) {
- throw new Error(this.ERRORS.vaultLocked);
- }
- }
-
- async add(id) {
+ /**
+ * Locks (add to vault) a note
+ * @param {string} noteId The id of the note to lock
+ */
+ async add(noteId) {
await this._check();
- const note = this._db.notes.note(id).data;
- await this._lockNote(id, note);
+ const note = this._db.notes.note(noteId).data;
+ await this._lockNote(noteId, note);
}
- async remove(id, password) {
+ /**
+ * Permanently unlocks (remove from vault) a note
+ * @param {string} noteId The note id
+ * @param {string} password The password to unlock note with
+ */
+ async remove(noteId, password) {
if (await this.unlock(password)) {
- const note = this._db.notes.note(id).data;
+ const note = this._db.notes.note(noteId).data;
await this._unlockNote(note, true);
}
}
- async open(id, password) {
+ /**
+ * Temporarily unlock (open) a note
+ * @param {string} noteId The note id
+ * @param {string} password The password to open note with
+ */
+ async open(noteId, password) {
if (await this.unlock(password)) {
- const note = this._db.notes.note(id).data;
+ const note = this._db.notes.note(noteId).data;
return this._unlockNote(note, false);
}
}
+ /**
+ * Saves a note into the vault
+ * @param {{Object}} note The note to save into the vault
+ */
async save(note) {
if (!note) return;
await this._check();
@@ -101,6 +102,31 @@ export default class Vault {
return await this._lockNote(id, note);
}
+ // Private & internal methods
+
+ /** @private */
+ async _exists(vaultKey) {
+ if (!vaultKey) vaultKey = await this._context.read("vaultKey");
+ return vaultKey && vaultKey.cipher && vaultKey.iv;
+ }
+
+ /** @private */
+ async _locked() {
+ return !this._password || !this._password.length;
+ }
+
+ /** @private */
+ async _check() {
+ if (!(await this._exists())) {
+ throw new Error(this.ERRORS.noVault);
+ }
+
+ if (await this._locked()) {
+ throw new Error(this.ERRORS.vaultLocked);
+ }
+ }
+
+ /** @private */
async _encryptContent(content, ids) {
let { text, delta } = { ...content };
let { deltaId, textId } = ids;
@@ -118,6 +144,7 @@ export default class Vault {
await this._db.delta.add({ id: deltaId, data: delta });
}
+ /** @private */
async _decryptContent(content) {
let { text, delta } = { ...content };
@@ -135,6 +162,7 @@ export default class Vault {
};
}
+ /** @private */
async _lockNote(id, note) {
if (!note) return;
let oldNote = this._db.notes.note(id);
@@ -156,6 +184,7 @@ export default class Vault {
});
}
+ /** @private */
async _unlockNote(note, perm = false) {
if (!note.locked) return;
@@ -176,4 +205,15 @@ export default class Vault {
content: { delta, text },
};
}
+
+ /** @inner */
+ async _getKey() {
+ if (await this._exists()) return await this._context.read("vaultKey");
+ }
+
+ /** @inner */
+ async _setKey(vaultKey) {
+ if (!vaultKey) return;
+ await this._context.write("vaultKey", vaultKey);
+ }
}