Private notes
A notes app can store each note in the user's account with its title and body encrypted, so nobody else can read them. Fields that the app needs to work with, such as when a note was created, can stay plain, so notes can be sorted without decrypting anything. The User API already makes sure only the owner can change their notes, since every update is signed.
Run it
After the setup lines, create an account, and wait for it to print null:
const user = holster.user()
user.create("alice", "a strong password", console.log)Then log in, and wait for null again:
user.auth("alice", "a strong password", console.log)Add a note, encrypting its title and body with the logged in user's own keys:
const id = crypto.randomUUID()
await user
.get("notes")
.next(id)
.put({
title: await holster.SEA.encrypt("Shopping list", user.is),
body: await holster.SEA.encrypt("Milk, bread", user.is),
created: Date.now(),
})Read it back:
const note = await user.get("notes").next(id)
console.log(await holster.SEA.decrypt(note.body, user.is))
// Milk, breadChange its title. Only the title is written, and the rest of the note stays as it was:
const title = await holster.SEA.encrypt("Groceries", user.is)
await user.get("notes").next(id).next("title").put(title)List every note's title, newest first:
const notes = await user.get("notes")
const newest = Object.values(notes)
.filter(note => note)
.sort((a, b) => b.created - a.created)
for (const note of newest) {
console.log(await holster.SEA.decrypt(note.title, user.is))
}
// GroceriesHow it works
An id chosen by the app: each note is stored under an id from
crypto.randomUUID(). The app knows the id straight away, so it can open, edit or delete the note it just created. A set also stores values under random keys, but it doesn't return the key it chose.Plain and encrypted fields side by side:
createdis stored as a plain number, so notes can be sorted by it. Anyone can read plain fields, so everyone can see how many notes there are and when they were created, but not what they say.Encrypted values are objects:
encrypt()returns an object withct,ivandsproperties. It's stored as its own node, and read back as the same object fordecrypt().Deleted notes stay as
null: deleting a note withput(null)leaves its id with anullvalue, so lists skip those, as thefilterdoes above.
Live lists continues from this example to keep the list up to date as notes change.