src/identity is the library every signing feature goes through. It keeps all private keys in one Keychain secret and hands out signers by account id. It has no sources of its own: the Me package owns the directory and exposes it to the UI, and the Vault mirrors it into encrypted state.
Files
File | Role |
|---|---|
The key map, signers, names, short ids, | |
"The one place src/identity reaches crypto": re-exports mnemonic and key functions from Crypto primitives, plus |
Storage
All keys live in the Keychain secret seed-keys (SECRET), reachable because app.ts holds the grant secret.keep seed-keys (Grants and capabilities). Its value is a JSON object:
{
"<accountId>": {"privateKeyB64": "<standard base64 of the 32-byte seed>", "name": "<label or empty>", "createdAt": 1700000000000},
"<removedAccountId>": {"removed": true}
}SecretStore is the slice of the data module's secret store used here: get(name), set(name, value), forget(name). The current account and the server are not stored here; they are persisted Contract state passed to sources as arguments (The root contract). The file header also sets a rule: "Never log a mnemonic or a key from here."
Reading and writing
Function | Behaviour |
|---|---|
Returns the | |
Account ids that carry a removal marker | |
One Keychain write of the map plus pending markers. A marker is dropped for an account that has a key again. An empty result forgets the secret | |
Drops markers once the vault has recorded them | |
Adds a key. Idempotent per account: an existing entry keeps its name and creation time | |
Deletes the key and writes its marker in the same write. | |
Returns | |
| |
|
Fail-closed loading
"Corruption must never be mistaken for an empty keychain." loadKeys throws IdentityStorageError when the secret is not a JSON object, when a key of the map is not a valid principal, or when an entry lacks a 32-byte base64 seed, a string name or a finite createdAt. The message is fixed and safe to display: Identity storage is unreadable. Restore it from a backup before making changes. It never echoes stored bytes. Because every writer calls loadKeys first, an unreadable secret is never overwritten.
The removal marker
Removing an identity leaves {removed: true} in its place. The rule the vault applies is exact:
The vault tombstones an identity (which deletes it on every device) only when this map holds the explicit marker for it.
A key that is merely absent from the map is not a deletion. The vault restores it.
The reason is in the file header: "a map that is behind the vault once made the vault delete every identity on every device". The Keychain map is a copy written after the vault state, so it can lag. removeKey therefore changes the key and the marker together, "so the vault can never see one without the other". The consuming side is described in Vault: the store and on-device format and Vault: merge and tombstones.
Names and short ids
nameError(name, keys, selfId) returns '' when a name is acceptable:
empty is allowed (no name),
otherwise it must match ^[a-zA-Z0-9_-]+$,
it must not parse as a different account's public key,
it must not equal another entry's name or id.
shortId(accountId) is the first four and last four characters joined by an ellipsis, for example z6Mk…YJ2f.
Key files
buildSeedKeyExport(privateKey, createdAtMs): the desktop .seedkey JSON (createTime, publicKey, keyB64 in unpadded base64url), indented, with a trailing newline.
parseSeedKey(text): returns {ok: true, keyPair, createdAt} or {ok: false, error}. It refuses invalid JSON, a missing keyB64, a seed that is not 32 bytes, a mismatched publicKey, and any encryption field (ENCRYPTED_SEEDKEY_ERROR, "Password-protected key exports are not supported yet."). The limit is listed in Current limits and known gaps.
exportSeedKey(store, accountId): the export for a stored key, or null if absent or mismatched.
Tests
identity.test.ts, 12 tests: derivation vectors, save, list, sign and remove (including the marker left behind), corruption, name rules, short id, key file export, import and errors, async derivation.
storage.test.ts, 4 tests: saving never overwrites an unreadable secret, malformed seeds are refused "without echoing key material", export never returns another account's key, and a name starting z6Mk is not mistaken for a principal.
Who uses it
signerFor is called by Discuss: loading and posting, Profile, the Activity notify client and the Agents sources.
User stories
The code described here exists to deliver these stories. Each story lists this page in its Trace (traceability).
US-DISC-03 Comment on a document: signerFor: the key that signs, read from the Keychain
US-DISC-04 Reply in a thread: The signer for the current identity
US-DISC-05 Be guided to create an identity before commenting: What "an identity on this device" means: a key in the Keychain
US-ACT-01 See replies and mentions for my identity: The signer for the selected identity
US-ACT-03 Mark everything read: The signer for the selected identity
US-ACT-04 Get notifications by email: The signer for the selected identity
US-AGENT-01 See my agents: The signer for the current identity
US-ID-01 Create an identity: Where the key is kept in the Keychain and the name rules
US-ID-02 Import an identity I already have: Key file parsing, name rules and Keychain storage
US-ID-03 Switch between my identities: listKeys order and short ids
US-ID-04 See and share my account ID, and name my key on this device: renameKey, nameError and where the name is stored
US-ID-05 Back up my key: exportSeedKey and the key file format
US-ID-06 Remove an identity from this device: removeKey writes the key removal and its marker in one Keychain write
US-ID-07 Edit my public profile: signerFor: the key that signs the profile
US-VAULT-02 Keep my identities in sync across devices: The Keychain map the vault publishes into, and the removal marker
US-VAULT-03 Lock my vault, switch to local, or log out: The signing key map that lock empties and unlock refills
US-VAULT-04 Trust that my keys stay safe: One Keychain secret, fail-closed loading, the removal marker
US-APP-06 See people and spaces by name: Key names and the short id
US-APP-09 Know the app only reaches what it must: One Keychain secret, fail-closed reads, export only after asking
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime