@nopeek/chat
v0.2.9
Published
NoPeek client SDK — E2EE chat your servers can't read.
Readme
@nopeek/chat
Official client SDK for NoPeek — end-to-end-encrypted messaging. The server only ever sees ciphertext; message bodies, attachments, and key backups are all encrypted on-device (MLS-style per-channel AES-256-GCM).
import { NoPeek } from "@nopeek/chat";
const np = await NoPeek.connect({ apiUrl, appId, userId, sessionToken });
const ch = await np.channels.create({ channelTypeKey: "direct", memberIds: [userId, peerId] });
ch.on?.("message", (m) => console.log(m.body?.text));
await ch.send({ text: "encrypted before it leaves this device" });See the NoPeek API reference for the full surface (channels, messages, recovery,
backups, bots). Used by the NoPeek messenger and by @nopeek/agent-bridge.
Storage durability (read this if you run inside a WebView)
connect({ storage }) defaults to localStorage. Inside a WebView - Capacitor,
React Native, or Electron pointed at a remote URL - that store is evictable:
iOS clears WKWebView local storage under pressure and after prolonged disuse,
and Android's "Clear data" wipes it.
Device identity and channel keys live in that store. With escrow OFF, losing it
makes the user's history permanently undecryptable through no action of their
own. If you are in a WebView, pass a storage backed by native preferences:
import { Preferences } from '@capacitor/preferences'
// Hydrate into memory first so get() can stay synchronous, THEN connect().
// Connecting with an empty store looks like a brand-new device to the SDK and
// it will register a second one, orphaning the original identity.
const np = await NoPeek.connect({ ...session, storage: nativeBackedStore })Also take a backup (np.backup()) before the user accumulates history worth
losing.
Verifying from Node or CI
connect() opens a WebSocket by default, so a Node script will not exit. Pass
autoConnectWs: false, and supply a Map-backed store since localStorage
does not exist in Node:
const m = new Map<string, string>()
const np = await NoPeek.connect({
...session,
autoConnectWs: false,
storage: { get: (k) => m.get(k) ?? null, set: (k, v) => { m.set(k, v) } },
})If your app owns the password, you MUST handle these three events
The backup is unlocked by a key derived from a secret YOUR app holds. NoPeek cannot see it and cannot help you if it changes without you re-wrapping. Every integration has to handle all three, and skipping one silently breaks restore for real users - usually months later, on a new device, when it is too late.
1. Login - the only moment the password exists in memory.
const np = await NoPeek.connect({ ...session, deferDeviceRegistration: true })
if (await np.hasBackup()) {
await np.restore({ password }) // adopts the existing device identity
} else {
await np.registerDevice()
const recoveryCode = generateRecoveryCode()
await np.backup({ password, recoveryCode }) // show the code ONCE
}Use deferDeviceRegistration: true. Registering before you know whether a
backup exists creates one ORPHAN device per login: it cannot read existing
history, and its stale key packages degrade peers' claims.
2. Password change - re-wrap, and pass the previous password.
await np.backup({ password: newPassword, previousPassword: oldPassword })Without this the user's own new password cannot unlock their history. Passing
previousPassword also deletes the superseded envelopes, which is what actually
revokes the old password - restore() tries every stored envelope, so leaving
them means the old password still works.
3. Password RESET (forgotten) - you cannot re-wrap, so say so.
The wrap was derived from a password nobody has. With escrow off there is no server-side recovery: existing history is readable ONLY with the recovery code. Seed a fresh backup under the new password so future devices can restore, and tell the user plainly that older history needs their code. Failing silently here is the worst option - the user finds out on their next device.
try {
await np.restore({ password: newPassword }) // occasionally works
} catch {
await np.registerDevice()
const recoveryCode = generateRecoveryCode()
await np.backup({ password: newPassword, recoveryCode })
// TELL THE USER older history needs their previous recovery code.
}