Accounts
Accounts are core primitives in Cosmos SDK that represent user identities and store state. Every transaction must be signed by an account, and accounts hold balances, sequence numbers, and public keys.Account Types
Cosmos SDK provides several account types to support different use cases.BaseAccount
TheBaseAccount is the fundamental account type that implements the AccountI interface. It contains the essential fields required for transaction processing.
types/address.go
- Address: Bech32-encoded string representation of the account address
- PubKey: Protobuf
Anywrapping the account’s public key (set on first transaction) - AccountNumber: Globally unique identifier assigned when account is created
- Sequence: Transaction counter for replay protection (increments with each tx)
Module Accounts
Module accounts are special accounts controlled by modules rather than external users. They’re used for holding tokens in escrow, collecting fees, and other module-specific operations.x/auth/types/account.go
- fee_collector: Collects transaction fees
- distribution: Holds tokens for validator rewards
- bonded_tokens_pool: Holds bonded staking tokens
- not_bonded_tokens_pool: Holds unbonding tokens
Address Formats
Cosmos SDK uses Bech32 encoding for human-readable addresses with different prefixes for different address types.Address Types
types/address.go
Bech32 Prefixes
Default prefixes (can be customized per chain):types/address.go
Address Derivation
HD Wallet Path (BIP-44):types/address.go
- Derive private key from mnemonic using HD path
- Generate public key from private key
- Hash public key with SHA-256, then RIPEMD-160 (20 bytes)
- Encode with Bech32 using appropriate prefix
Working with Addresses
Validator Addresses
Address Validation
The SDK validates addresses to prevent errors:types/address.go
- Cannot be empty
- Maximum length: 255 bytes (typically 20 bytes for standard accounts)
- Must use valid Bech32 encoding
- Prefix must match expected type
Account Management
Creating Accounts
Accounts are created implicitly when they receive tokens:Account Numbers and Sequences
Account Number:- Globally unique identifier
- Assigned when account is first created
- Never changes
- Used in transaction signing
- Transaction counter (nonce)
- Starts at 0
- Increments with each successful transaction
- Prevents replay attacks
- Must be included correctly in transaction signatures
Account Retrieval
Address Caching
The SDK caches Bech32 address string conversions for performance:types/address.go
Custom Address Derivation
Chains can customize address prefixes:Best Practices
- Always validate addresses before using them in transactions
- Cache address strings when performing repeated operations
- Use type-safe address types (AccAddress, ValAddress, ConsAddress)
- Handle nil public keys gracefully (accounts may not have sent transactions yet)
- Never hardcode address prefixes - use the config system
- Verify sequence numbers to prevent replay attacks
- Use module accounts for holding tokens on behalf of modules
Related Concepts
- Gas and Fees - Transaction costs and fee payment
- Transactions - Creating and signing transactions
- Authentication - Account authentication and management