# Identity changes and safety numbers (https://docs.open-e2ee.dev/build/identity-change-safety-numbers.md)

- Status: stable
- Applies to: 15.0.0
- Platforms: Expo · Browser · Node
- Prereqs: A working client from Start → Quickstart
- Reading time: 12 min

Trust-on-first-use gets you a working session against a key you have never
checked. It detects a later key change. It proves nothing about who was present
at first contact. This page closes that gap, where the hard part is outside the cryptography.

## Generating comparison data [#generating-comparison-data]

The client method takes a user ID and returns the comparison object. It also
returns an immutable token that confirms the exact displayed value:

```ts
const safetyNumber = await signal.verify(remoteUserId);
showVerificationDialog(safetyNumber);
await signal.confirmSafetyNumber(safetyNumber.confirmation);
```

The two steps are separate on purpose. A generated safety number never promotes
trust. Only `confirmSafetyNumber(confirmation)` records a decision. It takes
evidence for the value that the user saw. This token prevents accidental
confirmation if the key rotates between render and tap.

For the full set of representations, including the scannable form, use the
standalone module:

```ts
import { generateCompositeSafetyNumber } from '@open-e2ee/signal-protocol-sdk/safety';

const safetyNumber = generateCompositeSafetyNumber(
  localCompositeIdentity,
  remoteCompositeIdentity,
  localUserId,
  remoteUserId,
);

console.log(safetyNumber.numeric); // 60 digits, for reading aloud
console.log(safetyNumber.emojis);  // for comparing on a screen

const comparison = safetyNumber.scannable.compare(scannedQrBytes);
if (comparison === 'match') {
  // record the user's decision
}
```

Each party has its own 30-digit string. The SDK orders the two strings as libsignal
does. The string that is lower by byte comparison comes first, in the 60 digits, the
emoji, and the hex. The user identifiers do not set the order. `compare()` returns
`'match'`, `'no_match'`, or `'version_mismatch'`. The composite QR profile is
version 4, so a QR code from an 11.x client reports `'version_mismatch'`. Ask the
user to update the other device, then scan again.

Trust starts at `UNVERIFIED_TOFU`. Authenticated safety-number comparison covers
both peers' complete composite tuples. It promotes only the exact current tuple to
`VERIFIED`. "Exact current tuple" defines the scope. Trust applies to one specific
set of keys at one moment, not permanently to a contact.

> The SDK creates comparison data; the application owns QR rendering, scanning,
> the verification user experience, and storage of the user's trust decision.

## When an identity changes [#when-an-identity-changes]

The contact reinstalls, their new device generates a new identity, and the relay
starts serving a tuple you never pinned. Two errors surface from that, on two
different calls, and neither of them is an event the SDK hands you. There is no
identity-change hook: registering every entry in `SignalProtocolClientHooks` and
then changing an identity underneath a live session fires none of them.

### The session path fails closed, but not at your call site [#the-session-path-fails-closed-but-not-at-your-call-site]

`UntrustedIdentityError` (`.code` `UNTRUSTED_IDENTITY`) is the identity error the
SDK raises. It is constructed in five places: two on the
sending side — building a session against a fetched prekey bundle, and
encrypting on an established one — and three inside `decrypt`. It carries the
address it refused and the tuple it refused for it:

```ts
import { isUntrustedIdentityError } from '@open-e2ee/signal-protocol-sdk/types';

try {
  await signal.send(remoteUserId, body);
} catch (error) {
  if (isUntrustedIdentityError(error)) {
    // .untrustedAddress — who. .identity — the tuple the SDK would not use.
    await queueForUserReview(error.untrustedAddress, error.identity);
    return;
  }
  throw error;
}
```

Keep that handler, and know what it is for. Both sending-side throws sit behind
`isTrustedIdentity(…, SENDING, …)`, and a contact reinstalling does not on its
own make that answer false. On `0.1.0-alpha.12` the send path was probed in four
configurations after the far device had rebuilt — an established ratchet, a
deleted session forcing the prekey-bundle path, a `VERIFIED` trust state promoted
through `confirmSafetyNumber`, and `VERIFIED` with the session deleted — and
every one resolved with nothing thrown. So the handler is defense in depth for
the paths that do reach a caller, not the branch a reinstall takes. Do not build
the reinstall experience on it; build it on the section below.

> **Failure mode — A resolved send() is not a delivery**
>
> The handler above does not fire on this path. On `0.1.0-alpha.12`, a send issued
> into the window where the far device has already rebuilt resolves normally. The
> refusal reaches the sending side afterwards, once the rebuilt device's automatic
> retry request has forced a new session — and it arrives at the logger rather than
> at the caller, because the throw happens inside the SDK's own handling of that
> retry request, which no application call is awaiting.
>
> That retry request is a `retry_request` envelope in the mailbox of the sending
> device. When the resend throws, the client keeps the envelope unacknowledged. Each
> later delivery tries the resend again. On the relay subscription, the client writes
> each failure to the logger. On a pull through `receiveIncomingEnvelopes()`, the
> client puts the envelope ID in `failedMessageIds`. This repeats until a resend
> succeeds or the sender copy of the message is more than 14 days old. Then the
> sender deletes the copy and sends no resend. On the relay subscription, the client
> then acknowledges the envelope. On a pull, the client puts the envelope ID in
> `processedMessageIds`. Your application then acknowledges it.
>
> So a product that treats a resolved `send()` as delivered will show a sent
> message that the recipient never received, with nothing thrown to catch. Whatever
> delivery evidence your product shows a user has to come from the receiving side,
> not from the send call returning.

### `verify()` refuses before you accept [#verify-refuses-before-you-accept]

Once the relay is serving an identity that is not the one you pinned, `verify()`
does not return a new safety number to show. It throws `EncryptionError` with
`.code` `IDENTITY_MISMATCH`:

```ts
import {
  EncryptionErrorCode,
  isEncryptionError,
} from '@open-e2ee/signal-protocol-sdk/types';

try {
  showVerificationDialog(await signal.verify(remoteUserId));
} catch (error) {
  if (isEncryptionError(error) && error.code === EncryptionErrorCode.IDENTITY_MISMATCH) {
    // the relay serves a tuple you have not pinned; accept it or refuse it
    return promptForIdentityChange(remoteUserId);
  }
  throw error;
}
```

The pinned tuple is the trust object, and the relay is never allowed to select
which tuple the user sees. That rule is what produces this refusal, and it
constrains the screen you can build: **there is no "here is the new safety
number, compare it" dialog available before the change has been accepted.**
Accepting comes first, comparing comes second, and a design that assumes the
other order cannot be implemented on this SDK.

`confirmSafetyNumber` raises the same code in three more places — when the
confirmation does not match the pinned identity, when it does not match the value
that was displayed, and when the relay's identity moved between display and tap.
Each is the same guard: a decision is recorded only against the exact tuple the
user actually saw.

### Accepting the change [#accepting-the-change]

Accepting is explicit, and it takes the tuple:

```ts
const record = await signal.acceptIdentityRotation(remoteUserId, error.identity);
```

The tuple is the one on the error, if you caught it. If you did not, it is the
one the relay is serving — read it from the relay you configured, with
`relay.getIdentityKey(remoteUserId)`. Supplying the tuple that is already trusted
throws.

Acceptance resets the sessions bound to the old tuple and returns the contact's
identity record. It does not mark the contact `VERIFIED`. Accepting a rotation
and verifying it are different acts. If one tap combines them, a "verified" badge
loses its meaning.

## The part nobody wants to hear [#the-part-nobody-wants-to-hear]

Users cannot do this. The evidence is not ambiguous.

In one study, 21 of 28 computer science students could not verify a public key. In
another, which explained the risks first, only 13% completed the ceremony. These
people are not the general population. They started with a reason to care, and most of them
still did not finish.

Keybase, whose users were unusually motivated, put it plainly:

> "Checking is infeasible, since it happens way too often. Checking sucks."

They reframed trust-on-first-use as **TADA, Trust After Device Additions**. In a
multi-device product, the decision does not occur only at first contact. Users repeat
it every time anyone upgrades a phone, reinstalls
an app, or links a tablet. On a fleet of any size, that is a steady drip of
identity changes with a base rate that is almost entirely benign.

> **Failure mode — The warning-fatigue machine you build by default**
>
> Bannering every identity change looks like the responsible choice. It is also the
> mechanism that trains users to dismiss the one that matters.
>
> Consider the arithmetic in a 500-person deployment. Phone replacement and
> reinstall rates alone produce identity changes every week. Each one raises a warning
> the user cannot evaluate, about a contact they know is real, in a thread they need
> to answer. Within a month the interaction is muscle memory. The genuine event
> arrives into a dismissal reflex you built.
>
> The signal is correct. `UntrustedIdentityError` reports the truth. A
> high-base-rate interrupt creates the failure.

> **Tradeoff — Block on identity change, or log it**
>
> Blocking sends until the user acts is the strict reading. No message leaves for a
> key the user has not accepted. This block interrupts the user's actual task. A
> colleague's new phone triggers it most often and turns a security decision into an obstacle.
>
> If the application logs the change and continues, the product stays usable and preserves
> evidence. However, a user who never opens the log silently accepts an unverified key.
> The SDK cannot choose either default for you. It fails closed inside the protocol
> rather than at your call site: as the section above shows, on a reinstall the
> refusal does not arrive as a rejected `send()`. A product that blocks has to
> implement the block itself, on the receiving-side evidence, rather than inherit
> it from a thrown error. The product layer must respond based on the message's
> value.

## Where the industry actually went [#where-the-industry-actually-went]

The mature answer is not a better dialog. It is to make key changes **auditable**
rather than to ask each user to adjudicate them.

Apple's Contact Key Verification and WhatsApp's Automatic Device Verification both
move in this direction. The system checks continuity in the background. A person
participates only after a failed check or when the stakes justify a real ceremony.
The human decision becomes an exception path rather than a routine one.

This SDK gives you the comparison data. It does not give you a transparency log or
an automated continuity check, and we do not claim to. What you can build with what
is here:

- Show the safety number only where it earns attention: a deliberate "verify this
  contact" flow, not an unsolicited banner.
- Use `scannable.compare(scannedQrBytes)` for in-person verification. A QR scan
  succeeds where a 60-digit read-aloud does not.
- Persist every `UntrustedIdentityError` with `.untrustedAddress` and `.identity`
  to an auditable record that your application owns — from the handler above and
  from wherever your logger sink writes, since on a reinstall the refusal reports
  there rather than to a caller. The tuple you held before it
  is the contact identity record you already have, so the record has both sides.
  A security team can use it to answer questions later. Do not make the user act
  as a verification oracle in the moment.
- Reserve the interrupt for cases your product can justify. Examples include a
  first message, a high-value action, or an account that your own signals flagged.

**Opacity ledger**

| Artifact | Stays on device | Sent to relay | In object store | Visible as metadata |
|---|---|---|---|---|
| `localCompositeIdentity` private half | yes | no | no | no |
| `localCompositeIdentity` public half | yes | yes | no | yes |
| `remoteCompositeIdentity` | yes, cached | fetched from relay | no | yes |
| `safetyNumber.numeric` | yes | no | no | no |
| `safetyNumber.emojis` | yes | no | no | no |
| `safetyNumber.scannable` bytes | yes | no | no | no — it crosses out of band, by QR |
| `safetyNumber.confirmation` | yes | no | no | no |
| Trust state (`UNVERIFIED_TOFU` / `VERIFIED`) | yes | no | no | no |
| `UntrustedIdentityError.identity` | yes | already public | no | yes |
| Your record of the user's verification decision | yes, app-owned | no | no | no |

Trust state is device-local. If a user verifies a contact on their phone, their
tablet does not know. Call `syncVerificationStateToLinkedDevices(...)` to send that
state. The SDK does not run this call in the background. See
[Multi-device](https://docs.open-e2ee.dev/build/multi-device.md).

> **Not covered — Key transparency**
>
> This SDK does not include verifiable append-only identity-key logs or the auditor
> infrastructure that supports them. If your threat model needs detection without
> user participation, build or buy that system alongside the SDK. Start with the
> [Threat model](https://docs.open-e2ee.dev/learn/threat-model.md).

The SDK is `15.0.x`. Public APIs and persisted formats follow semantic versioning.

## Next [#next]

- [Multi-device](https://docs.open-e2ee.dev/build/multi-device.md): the main reason your users' safety numbers change
- [Error handling](https://docs.open-e2ee.dev/operate/error-handling.md): the full identity and session error taxonomy
- [Threat model](https://docs.open-e2ee.dev/learn/threat-model.md): decide whether to ask for verification
- [Offline, recovery, and identity change](https://docs.open-e2ee.dev/guides/offline-recovery-identity-change.md): a worked flow end to end
