A Hab (Habitat) is a keystore data structure in KERI implementations that manages cryptographic key material and state for a single Autonomic Identifier (AID), implemented in KERIpy using LMDB for persistent storage of private keys, public keys, key event logs, and all associated identifier data.
Related Concepts
No related concepts available
Comprehensive Explanation
hab
Structure Definition
A Hab (short for "Habitat") is a fundamental data structure in KERI implementations that serves as the keystore and state manager for a single Autonomic Identifier (AID). The term "habitat" metaphorically represents the environment where an identifier "lives" - containing all the cryptographic material, event history, and operational state necessary for that identifier to function.
Purpose and Role
The Hab serves three critical functions in KERI architecture:
Cryptographic Key Management: Securely stores private keys, public keys, and key pairs associated with an AID, including both current signing keys and pre-rotated keys for future rotations.
Event Log Storage: Maintains the complete Key Event Log (KEL) for the identifier, including inception events, rotation events, interaction events, and all associated receipts from witnesses.
State Tracking: Manages the current key state of the identifier, including active key pairs, witness configurations, thresholds for multi-signature operations, and delegation relationships.
The Hab abstraction provides a unified interface for all operations on a single identifier, encapsulating the complexity of key management, event processing, and state transitions behind a coherent API.
Structural Organization
In the KERIpy reference implementation (used by both standalone KERIpy and the KERIA cloud agent), a Hab is implemented as a Python object that wraps an LMDB (Lightning Memory-Mapped Database) backend. This design choice provides:
ACID transaction support: Ensuring atomic operations on key material and event logs
Implementation Notes
Critical Implementation Details
Keystore Security
Passcode-Based Encryption: Habs in KERIpy use a passcode-derived encryption key to protect private key material. The passcode is never stored; instead, a salt is stored and used with the passcode to derive the encryption key via a key derivation function (KDF). This means:
Lost passcodes cannot be recovered: There is no "forgot password" mechanism
Passcode strength is critical: Weak passcodes compromise all key material
Salt must be preserved: Losing the salt makes the keystore unrecoverable
LMDB Configuration
Memory-Mapped Database: LMDB uses memory-mapped files, which means:
File system permissions matter: The LMDB files must have appropriate read/write permissions
Disk space is required: LMDB allocates a maximum database size upfront (default 1GB)
Concurrent access is supported: Multiple processes can read simultaneously, but only one can write
Crash recovery is automatic: LMDB transactions are ACID-compliant
Multi-Signature Coordination
Partial Rotation Support: For multi-sig Habs, KERI supports partial rotation where only a subset of keys are rotated. Implementation must:
Track which keys are being rotated vs. kept
Collect signatures from all participants
Ensure threshold requirements are met
Handle asynchronous signature collection
Witness Configuration
Witness List Management: Habs must maintain witness configurations that:
Specify witness AIDs and endpoints (OOBIs)
Define the TOAD (Threshold of Accountable Duplicity) - minimum witnesses required
Support witness rotation through rotation events
Handle witness unavailability gracefully
Delegation Handling
Delegated Identifier Creation: When creating a delegated Hab:
The delegate creates an inception event with a delegation seal
The delegator must approve by anchoring a delegation seal in their KEL
The delegate's inception is not valid until the delegator's seal is confirmed
Witnesses must see both the delegate's event and the delegator's seal
Memory-mapped file access: Enabling high-performance reads without loading entire databases into RAM
Compact storage: Efficient binary encoding of cryptographic primitives and event data
Crash recovery: Durable persistence with automatic recovery from system failures
The Hab structure maintains several logical data stores within the LMDB backend:
Key material store: Private keys encrypted with the keystore passcode
KEL store: Ordered sequence of key events for the identifier
Receipt store: Witness receipts and watcher confirmations
State cache: Current key state derived from the KEL
Credential store: ACDCs (Authentic Chained Data Containers) issued to or by this identifier
Configuration metadata: Witness lists, delegation relationships, and operational parameters
Key Components
A Hab contains several essential components:
Prefix (AID): The unique identifier string that this Hab manages. This is typically a self-certifying identifier derived from the inception key material.
Keystore: The encrypted storage for private key material. In KERIpy, this uses the Salter primitive to derive encryption keys from a user-provided passcode, ensuring that private keys are never stored in plaintext.
Kever (Key Event Verifier): The component responsible for processing and validating key events, maintaining the authoritative key state for the identifier.
Kevery (Key Event Registry): The collection of all Kevers managed by this Hab, enabling verification of events from other identifiers that this Hab interacts with.
Seqner: Tracks the sequence numbers of events in the KEL, ensuring proper ordering and detecting gaps or duplicates.
Tholder: Manages threshold structures for multi-signature operations, including both current signing thresholds and next rotation thresholds.
Data Format
Field Definitions
While the Hab itself is a runtime object rather than a serialized data structure, it manages several persistent data formats:
Key Material Storage:
Private keys: Stored as encrypted binary blobs using the keystore passcode-derived encryption key
Public keys: Stored as CESR-encoded qualified primitives with derivation codes
Key digests: Pre-rotated key commitments stored as CESR-encoded digests
KEL Storage:
Events: Serialized as JSON, CBOR, or MessagePack with CESR-encoded attachments
Signatures: CESR-encoded indexed signatures attached to events
Receipts: CESR-encoded witness signatures with timestamps
State Cache:
Current keys: List of active public key verifiers
Next key digests: Commitments to pre-rotated keys
Thresholds: Current and next signing thresholds
Witness configuration: List of designated witnesses and witness threshold
Sequence number: Current event sequence in the KEL
Encoding Format
The Hab uses CESR (Composable Event Streaming Representation) for all cryptographic primitives, providing:
Self-framing: Each primitive includes its type and size in the encoding
Composability: Primitives can be concatenated and converted between text and binary domains
Compactness: Minimal overhead for encoding cryptographic material
Key events within the KEL are serialized using one of three formats:
JSON: Human-readable text format for debugging and interoperability
CBOR: Compact binary format for efficient storage and transmission
MessagePack: Alternative binary format with broad language support
All three formats maintain insertion-ordered field maps to ensure deterministic serialization for SAID computation and signature verification.
Size and Constraints
The size of a Hab's persistent storage depends on:
Number of key events: Each rotation or interaction adds to the KEL
Number of witnesses: More witnesses mean more receipts to store
Credential volume: ACDCs issued to or by the identifier
High-volume identifier: >1 MB (extensive event history, many credentials)
LMDB's memory-mapped architecture means that Habs can efficiently manage large datasets without loading everything into RAM, making the system scalable to thousands of identifiers per Habery.
Event Signing: Sign the inception event with the inception key(s)
KEL Initialization: Store the signed inception event as the first entry in the KEL
State Initialization: Derive the initial key state from the inception event
For multi-signature identifiers, the inception process involves coordination between multiple participants, each contributing their key material and signatures to create a group identifier.
For delegated identifiers, the inception event includes a reference to the delegating identifier's prefix and requires approval from the delegator through a delegation seal in the delegator's KEL.
A Kever (Key Event Verifier) is the component within a Hab that processes and validates key events. While a Hab manages one identifier's complete state, the Kever specifically handles:
Event validation logic
State derivation from events
Duplicity detection
Witness receipt processing
Kevery:
A Kevery is the registry of all Kevers (both local Habs and remote identifiers) that a given Hab interacts with. This enables:
Verification of events from other identifiers
Multi-party protocol coordination
Delegation relationship management
Seqner:
A Seqner tracks event sequence numbers within a Hab's KEL, ensuring:
Monotonic sequence progression
Gap detection
Replay attack prevention
Tholder:
A Tholder manages threshold structures for multi-signature operations, including:
Current signing thresholds
Next rotation thresholds
Fractional weight calculations
Threshold satisfaction verification
Implementation Patterns
Single-Sig Hab:
The simplest Hab manages a single-signature identifier:
The Hab abstraction is fundamental to KERI's architecture, providing a clean separation between identifier management and protocol operations, enabling scalable, secure, and flexible identity systems.
Performance Considerations
KEL Verification: As KELs grow, verification time increases. Implementations should:
Cache verified key states to avoid re-verifying the entire KEL
Use incremental verification for new events
Implement efficient event indexing in LMDB
Consider KEL compaction for very long histories
Error Handling
Duplicity Detection: Habs must handle duplicity (conflicting events) by:
Maintaining a duplicitous event log (DEL) separate from the KEL
Alerting controllers to duplicity events
Refusing to process events after duplicity is detected
Providing mechanisms for duplicity resolution
Backup and Recovery
Keystore Backup: Habs should support:
Exporting encrypted keystore backups
Importing keystore backups with passcode verification
Exporting KELs for archival purposes
Reconstructing state from KEL backups
Integration with KERIA
Cloud Agent Architecture: In KERIA, each Hab runs as an agent instance: