// Private messages between two users: the server stores and forwards boxes it cannot open.
//
//   quillo run crypto_box.qwe      then open http://localhost:8080/message

use "crypto_box.qwer"

// Each user's key pair is made on their own device; the server knows only the public keys.
let ada = cryptobox_keypair()
let grace = cryptobox_keypair()
let mallory = cryptobox_keypair()

route GET /message {
    let boxed = cryptobox_encrypt("Lunch at 1? The usual place.", grace.public, ada.secret)?
    {
        stored_by_server: boxed,
        grace_reads: cryptobox_decrypt(boxed, ada.public, grace.secret)?,
        mallory_cannot: is_err(cryptobox_decrypt(boxed, ada.public, mallory.secret)),
        not_from_mallory: is_err(cryptobox_decrypt(boxed, mallory.public, grace.secret)),
    }
}

// An anonymous tip, sealed in the browser (libsodium.js) or here, readable only by the newsroom.
route GET /tip {
    let sealed = cryptobox_seal("The invoices are in folder 7.", grace.public)?
    { opened: cryptobox_unseal(sealed, grace.secret)?, others: is_err(cryptobox_unseal(sealed, ada.secret)) }
}
