Data shared by the host

A server can have its own Holster account, the host, and use it to send each user private values, such as their storage limit. The values are stored publicly in the host's account, but each one is encrypted so only that user can read it.

Run it

This example needs two accounts, the host and a user. Only one account can be logged in at a time, so it logs in to each in turn and saves its keys from user.is before switching. Start a new REPL with the setup lines.

Create the user's account, and wait for it to print null:

const user = holster.user()
user.create("alice", "a strong password", console.log)

Log in as the user, wait for null, then save their keys:

user.auth("alice", "a strong password", console.log)
const alice = user.is

Create the host's account, and wait for null:

user.create("host", "a host password", console.log)

Log in as the host, wait for null, then save its keys:

user.auth("host", "a host password", console.log)
const host = user.is

Logged in as the host, create a secret shared with the user and store an encrypted value under their public key:

const toAlice = await holster.SEA.secret(alice, host)
const limit = await holster.SEA.encrypt(10, toAlice)
await user.get("limits").next(alice.pub).put(limit)

The user creates the same secret from the host's keys, and reads their value. Reading the host's data doesn't need a login, and decrypting only needs the user's own keys:

const fromHost = await holster.SEA.secret(host, alice)
const enc = await user.get([host.pub, "limits"]).next(alice.pub)
console.log(await holster.SEA.decrypt(enc, fromHost))
// 10

How it works

In an application, the host and the user are logged in on different devices, so neither needs to switch. The user would also listen for changes to their value with on() instead of reading it once, so an update from the host shows up straight away.