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.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime