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.isCreate 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.isLogged 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))
// 10How it works
Each side only needs the other's public keys:
secret()uses the other account'sepuband your ownepriv. Every account'sepubis stored publicly with it, so in an application each side looks up the other's withuser.get([publicKey, "epub"]), without exchanging anything first.Keyed by the user's public key: the host stores each value under the user's
pub, so a user can find their own value without the host telling them where it is.Readable by anyone, useful to one: the encrypted values can be read by every client, but only the user they're for can decrypt theirs. Only the host can change them, since it owns the account they're stored in.
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.