Crypto 1.x is a release candidate. npm's latest tag still installs 0.x, so install these docs' version from the rc tag:
npm i @adonis-agora/crypto@rc
node ace configure @adonis-agora/cryptoThe same goes for every @adonis-agora/* package these docs install: add @rc, and where a page says node ace add, install with @rc first and then run node ace configure. On 0.x? Read the 0.x docs.
Sharing data with a person
Patient and document sharing where the recipient holds the private key and the server cannot read the payload.
When the recipient is a person (a doctor, a patient, another clinic), the right model is not a shared key: give each person an X25519 key pair, keep the private key with them, and seal the payload to their public key. The crypto server holds the public half, the audit and the operations — never the plaintext after the write, and never the private key.
This is the shape of an AccessGrant-style flow: patient shares exams with a doctor, doctor opens
them, everybody can audit who opened what.
1. One key pair per recipient
node ace crypto:keys:create doctor-42 --type x25519 --description "Dr. Ana — compartilhamento"
node ace crypto:keys:public-key doctor-42 # or GET /_crypto/v1/keys/doctor-42/public{ "kty": "OKP", "crv": "X25519", "x": "…", "kid": "…", "use": "enc", "alg": "ECDH-ES" }The public JWK is what the sharing side needs; it is also served as a JWKS
(GET /_crypto/v1/keys/doctor-42/jwks) with every version, so a rotation does not break a share
made before it.
2. Seal the payload to that key
On the side that has the data (the patient's session, or a worker that already holds it):
// The crypto kit is copied into every Agora lib and into the app that seals:
import { sealTo } from '#lib/crypto-kit/index.js'
const shared = sealTo(publicJwk, JSON.stringify({ examId, pdfUrl, notes }), {
aad: `grant:${grant.id}`,
})shared is agora:pk1:<kid>:<ephemeral>:<iv>:<tag>:<ciphertext>: an ephemeral X25519 exchange with
the recipient's public key, HKDF-SHA256, AES-256-GCM. Store that string in the grant row. The
database, a backup, the crypto server and every log see ciphertext only.
3. Only the recipient opens it
import { openWith } from '#lib/crypto-kit/index.js'
const payload = JSON.parse(openWith(recipientPrivateJwk, grant.shared, { aad: `grant:${grant.id}` }))The private key lives where the person is: sealed with their password in the app, in their own
x25519 key file, or in the crypto server only if you accept that the server can read it (the
convenience mode: crypto.decrypt({ key: 'doctor-42', ciphertext }) with a token holding
crypto:decrypt:doctor-42).
| Mode | Who can read | When |
|---|---|---|
| recipient-held (this page) | only the recipient's device | patient data, LGPD-sensitive shares |
| server-held | anyone with crypto:decrypt:<recipient> | audit-friendly internal shares, no device key |
4. Audit who opened what
Both modes are audited by the crypto server (recipient-held shares are audited at the app level,
since the server never sees them). Record the grant, the kid used, and the moment the recipient
opened it:
await db.insertInto('access_grant_events').values({
grantId: grant.id,
kid,
openedAt: new Date(),
actor: doctor.id,
})Revoking is a database action (the grant stops being served); the key can be retired separately
(crypto:keys:policy doctor-42 --min-decryption-version N only affects the server-held mode — a
recipient-held ciphertext stays openable by whoever has the private key, which is the point).
Who audits the open
| Mode | Who performs the open | Who audits it |
|---|---|---|
server-held (crypto.decrypt) | the crypto server | the crypto server, automatically (decrypt with key, version, actor, and the context the app sent) |
recipient-held (sealTo/openWith) | the recipient's device | the app: the server never sees it, so record the event yourself (grant id, kid, who, when) |
For server-held, pass context on the decrypt (user:42, grant:7) and the audit answers "who opened
what". For recipient-held there is no server-side trace at all: if the business needs "who viewed this
exam", the app must emit the event.
The event shape is the same everywhere, so Telescope shows one identity in both models:
import { SEALED_OPEN_EVENT, sealedOpenEvent } from '@adonis-agora/crypto-integrations/config'
import { openWithAudit } from '@adonis-agora/crypto-react'
const opened = openWithAudit(identity.privateKey, grant.shared, { aad: `grant:${grant.id}` })
emitter.emit(
SEALED_OPEN_EVENT,
sealedOpenEvent({
actor: ctx.identity.actor, // RFC 8693 `act`, when an agent acts for a person
subject: ctx.identity.sub,
key: 'doctor-42',
kid: opened.kid,
resource: `grant:${grant.id}`,
at: opened.at,
}),
){
"event": "crypto:sealed.open",
"v": 1,
"actor": "user:42",
"subject": "svc:agent",
"key": "doctor-42",
"kid": "LqFIiDUU…",
"resource": "grant:7",
"at": "2026-03-03T03:03:03.000Z",
"result": "ok"
}actorisactor ?? sub, the same rule as the crypto server's audit — an agent opening a share for a doctor shows the doctor, withsubjectkeeping the client that ran it.kidis the key version that opened it (from theagora:pk1header), so a rotation is visible.result: 'error'for a refused/failed open; an open with no identity at all throws instead of emitting an anonymous row.- The payload names the fact (
event: 'crypto:sealed.open', product-neutral); the bus channel names who emitted it:agora:<lib>:sealed.openfromsealedOpenChannel(lib). Config emits onagora:config:sealed.open— notagora:crypto:sealed.open, which would read as if the crypto lib had seen the open (it cannot: recipient-held is opened on the device) — and callstenant.onSealedOpen(event)with the same payload.
Why not just a shared symmetric key?
A symmetric share means the server, every backup and every admin can open it, and revoking means re-encrypting everything. Asymmetric shares survive a server compromise: an attacker with the database gets ciphertext bound to a key they do not have.