2020-03-07 12:29:55 +05:00
|
|
|
import Database from "./index";
|
2020-03-19 11:30:05 +05:00
|
|
|
import getId from "../utils/id";
|
2020-03-07 12:29:55 +05:00
|
|
|
export default class Vault {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Database} database
|
|
|
|
|
*/
|
|
|
|
|
constructor(database, context) {
|
2020-03-08 11:33:55 +05:00
|
|
|
this._db = database;
|
|
|
|
|
this._context = context;
|
|
|
|
|
this._key = "Notesnook";
|
|
|
|
|
this._password = "";
|
2020-03-10 13:54:34 +05:00
|
|
|
this.ERRORS = {
|
|
|
|
|
noVault: "ERR_NO_VAULT",
|
|
|
|
|
vaultLocked: "ERR_VAULT_LOCKED",
|
2020-04-11 17:20:37 +05:00
|
|
|
wrongPassword: "ERR_WRONG_PASSWORD",
|
2020-03-10 13:54:34 +05:00
|
|
|
};
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(password) {
|
2020-03-08 11:33:55 +05:00
|
|
|
const lockKey = await this._context.read("lockKey");
|
2020-03-07 12:29:55 +05:00
|
|
|
if (!lockKey || !lockKey.cipher || !lockKey.iv) {
|
2020-04-11 17:20:37 +05:00
|
|
|
const encryptedData = await this._db.crypto.encrypt(password, this._key);
|
2020-03-08 11:33:55 +05:00
|
|
|
await this._context.write("lockKey", encryptedData);
|
|
|
|
|
this._password = password;
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unlock(password) {
|
2020-03-08 11:33:55 +05:00
|
|
|
const lockKey = await this._context.read("lockKey");
|
2020-03-09 10:43:32 +05:00
|
|
|
if (!(await this._exists(lockKey))) throw new Error("ERR_NO_VAULT");
|
2020-03-07 12:29:55 +05:00
|
|
|
var data;
|
|
|
|
|
try {
|
2020-04-11 17:20:37 +05:00
|
|
|
data = this._db.crypto.decrypt(password, lockKey);
|
2020-03-07 12:29:55 +05:00
|
|
|
} catch (e) {
|
2020-03-10 13:52:20 +05:00
|
|
|
throw new Error(this.ERRORS.wrongPassword);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
2020-03-08 11:33:55 +05:00
|
|
|
if (data !== this._key) {
|
2020-03-10 13:52:20 +05:00
|
|
|
throw new Error(this.ERRORS.wrongPassword);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
2020-03-08 11:33:55 +05:00
|
|
|
this._password = password;
|
2020-03-07 12:29:55 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _exists(lockKey) {
|
2020-03-08 11:33:55 +05:00
|
|
|
if (!lockKey) lockKey = await this._context.read("lockKey");
|
2020-03-07 12:29:55 +05:00
|
|
|
return lockKey && lockKey.cipher && lockKey.iv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _locked() {
|
2020-03-08 11:33:55 +05:00
|
|
|
return !this._password || !this._password.length;
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _check() {
|
|
|
|
|
if (!(await this._exists())) {
|
2020-03-10 13:52:20 +05:00
|
|
|
throw new Error(this.ERRORS.noVault);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (await this._locked()) {
|
2020-03-10 13:52:20 +05:00
|
|
|
throw new Error(this.ERRORS.vaultLocked);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(id) {
|
|
|
|
|
await this._check();
|
2020-03-08 11:33:55 +05:00
|
|
|
const note = this._db.notes.note(id).data;
|
|
|
|
|
await this._lockNote(id, note);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async remove(id, password) {
|
|
|
|
|
if (await this.unlock(password)) {
|
2020-03-08 11:33:55 +05:00
|
|
|
const note = this._db.notes.note(id).data;
|
|
|
|
|
await this._unlockNote(note, true);
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async open(id, password) {
|
|
|
|
|
if (await this.unlock(password)) {
|
2020-03-08 11:33:55 +05:00
|
|
|
const note = this._db.notes.note(id).data;
|
|
|
|
|
return this._unlockNote(note, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async save(note) {
|
|
|
|
|
if (!note) return;
|
|
|
|
|
await this._check();
|
2020-03-19 11:30:05 +05:00
|
|
|
let id = note.id || getId();
|
2020-03-08 11:33:55 +05:00
|
|
|
return await this._lockNote(id, note);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
async _encryptContent(content, ids) {
|
|
|
|
|
let { text, delta } = { ...content };
|
|
|
|
|
let { deltaId, textId } = ids;
|
2020-03-08 11:33:55 +05:00
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
if (!delta.ops) delta = await this._db.delta.get(deltaId);
|
|
|
|
|
if (text === textId) text = await this._db.text.get(textId);
|
2020-03-08 11:33:55 +05:00
|
|
|
|
2020-04-11 17:20:37 +05:00
|
|
|
text = this._db.crypto.encrypt(this._password, text);
|
|
|
|
|
delta = this._db.crypto.encrypt(this._password, delta);
|
2020-03-19 11:30:05 +05:00
|
|
|
|
|
|
|
|
await this._db.text.add({ id: textId, data: text });
|
|
|
|
|
await this._db.delta.add({ id: deltaId, data: delta });
|
2020-03-08 11:33:55 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
async _decryptContent(content) {
|
|
|
|
|
let { text, delta } = { ...content };
|
|
|
|
|
|
|
|
|
|
text = await this._db.text.get(text);
|
2020-04-11 17:20:37 +05:00
|
|
|
text = this._db.crypto.decrypt(this._password, text);
|
2020-03-19 11:30:05 +05:00
|
|
|
|
|
|
|
|
delta = await this._db.text.get(delta);
|
2020-04-11 17:20:37 +05:00
|
|
|
delta = JSON.parse(this._db.crypto.decrypt(this._password, delta));
|
2020-03-19 11:30:05 +05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
delta,
|
2020-04-11 17:20:37 +05:00
|
|
|
text,
|
2020-03-19 11:30:05 +05:00
|
|
|
};
|
2020-03-08 11:33:55 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _lockNote(id, note) {
|
|
|
|
|
if (!note) return;
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
let oldNote = this._db.notes.note(id);
|
|
|
|
|
|
|
|
|
|
let deltaId = 0;
|
|
|
|
|
let textId = 0;
|
|
|
|
|
|
|
|
|
|
if (oldNote && oldNote.data.content) {
|
|
|
|
|
deltaId = oldNote.data.content.delta;
|
|
|
|
|
textId = oldNote.data.content.text;
|
|
|
|
|
}
|
2020-03-08 11:33:55 +05:00
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
await this._encryptContent(note.content, { textId, deltaId });
|
2020-03-08 11:33:55 +05:00
|
|
|
|
|
|
|
|
return await this._db.notes.add({
|
|
|
|
|
id,
|
2020-04-11 17:20:37 +05:00
|
|
|
locked: true,
|
2020-03-08 11:33:55 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _unlockNote(note, perm = false) {
|
|
|
|
|
if (!note.locked) return;
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
let { delta, text } = await this._decryptContent(note.content);
|
2020-03-08 11:33:55 +05:00
|
|
|
|
|
|
|
|
if (perm) {
|
|
|
|
|
await this._db.notes.add({
|
|
|
|
|
id: note.id,
|
2020-04-11 17:20:37 +05:00
|
|
|
locked: false,
|
2020-03-08 11:33:55 +05:00
|
|
|
});
|
2020-03-19 11:30:05 +05:00
|
|
|
await this._db.delta.add({ id: note.content.delta, data: delta });
|
|
|
|
|
await this._db.text.add({ id: note.content.text, data: text });
|
|
|
|
|
return;
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
2020-03-08 11:33:55 +05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...note,
|
2020-04-11 17:20:37 +05:00
|
|
|
content: { delta },
|
2020-03-08 11:33:55 +05:00
|
|
|
};
|
2020-03-07 12:29:55 +05:00
|
|
|
}
|
|
|
|
|
}
|