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, bread

Change 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))
}
// Groceries

How it works

Live lists continues from this example to keep the list up to date as notes change.