Skip to content

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

fix: Relocking a note sends delta as its 'key' (#5)

thecodrr0dc7d361e1e2
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index b2b274872..162383cea 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -231,23 +231,31 @@ test("favorite note", () =>
expect(note.data.favorite).toBe(true);
}));
-test("lock and unlock note", () =>
- noteTest({
- content: {
- delta: { ops: [{ insert: "I am a text", type: "text" }] },
- text: "I am a text"
- }
- }).then(async ({ db, id }) => {
+test("lock a note", () =>
+ noteTest().then(async ({ db, id }) => {
let note = db.notes.note(id);
await note.lock("password123");
note = db.notes.note(id);
expect(note.data.locked).toBe(true);
expect(note.data.content.iv).toBeDefined();
expect((await note.delta()).iv).toBeDefined();
+ }));
+
+test("unlock a locked note", () =>
+ noteTest().then(async ({ db, id }) => {
+ let note = db.notes.note(id);
+ await note.lock("password123");
+ note = db.notes.note(id);
note = await note.unlock("password123");
expect(note.id).toBe(id);
expect(note.content.text).toBe(TEST_NOTE.content.text);
expect(note.content.delta.ops).toBeDefined();
+ }));
+
+test("permanently unlock a locked note", () =>
+ noteTest().then(async ({ db, id }) => {
+ let note = db.notes.note(id);
+ await note.lock("password123");
note = db.notes.note(id);
await note.unlock("password123", true);
note = db.notes.note(id);
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index f7e0c3bb5..191128931 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -1,4 +1,5 @@
import Notes from "../collections/notes";
+import { qclone } from "qclone";
export default class Note {
/**
@@ -74,13 +75,13 @@ export default class Note {
}
async lock(password) {
- let delta = await this.delta();
+ 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.content.delta, delta);
+ await this._notes._deltaStorage.write(this._note.id + "_delta", delta);
}
const note = { ...this._note };
note.content = await this._notes._collection.indexer.encrypt(
@@ -109,7 +110,7 @@ export default class Note {
note.locked = false;
note.content = decrypted;
await this._notes._collection.addItem(note);
- await this._notes._deltaStorage.write(note.content.delta, delta);
+ await this._notes._deltaStorage.write(note.id + "_delta", delta);
}
return {
...this._note,