地址类型
SDK 定义了三种不同的地址类型,每种类型都有各自的 Bech32 前缀:| 地址类型 | Bech32 前缀 | 示例 | 用途 |
|---|---|---|---|
| 账户地址 | cosmos | cosmos1r5v5sr... | 用户账户、余额、交易 |
| 验证者操作员地址 | cosmosvaloper | cosmosvaloper1r5v5sr... | 验证者操作员身份、质押操作 |
| 共识地址 | cosmosvalcons | cosmosvalcons1r5v5sr... | 验证者参与共识、区块签名 |
- 账户公钥:
cosmospub - 验证者公钥:
cosmosvaloperpub - 共识公钥:
cosmosvalconspub
支持的密钥方案
Cosmos SDK 支持三种密钥方案。所选方案会影响地址长度,以及它是否可用于交易或共识:| 地址长度(字节) | 公钥长度(字节) | 用于交易认证 | 用于共识(CometBFT) | |
|---|---|---|---|---|
secp256k1 | 20 | 33 | 是 | 否 |
secp256r1 | 32 | 33 | 是 | 否 |
tm-ed25519 | — 不使用 — | 32 | 否 | 是 |
secp256k1 是用户账户的默认方案。secp256r1 作为替代方案受支持,并会生成更长的地址(32 字节)。tm-ed25519 仅用于验证者共识密钥,不会生成面向用户的地址。
地址派生
地址通过对公钥进行密码学哈希得到。具体过程因密钥算法而异:Secp256k1 密钥(账户地址)
账户地址使用类似 Bitcoin 的地址派生方式:crypto/keys/secp256k1/secp256k1.go
Ed25519 密钥(共识地址)
共识地址使用截断的 SHA-256:crypto/keys/ed25519/ed25519.go
Bech32 编码过程
在派生出地址字节后,会将其转换为 Bech32 格式: 第 1 步:从 8 位编码转换为 5 位编码types/bech32/bech32.go
配置 Bech32 前缀
每个 Cosmos SDK 应用都会在启动时通过sdk.GetConfig() 一次性设置自己的 Bech32 前缀和 SLIP-44 coin type,然后封存配置,使其在运行时无法再被修改。默认值(cosmos、cosmosvaloper 等)定义在 types/config.go 中。链开发者会在应用启动前覆盖这些值:
地址校验
SDK 会通过以下方式校验地址:- 格式校验:确保是有效的 Bech32 编码
- 前缀校验:确认地址类型对应的 HRP 正确
- 长度校验:验证解码后的地址长度恰好为 20 字节
模块地址
模块账户使用 ADR-028 中定义的确定性地址派生方式:验证者地址之间的关系
一个验证者有三个相关地址:- 操作员地址(
cosmosvaloper1...):验证者的运营身份,由操作员账户密钥派生 - 共识地址(
cosmosvalcons1...):由验证者的共识公钥(Ed25519)派生,用于区块签名 - 账户地址(
cosmos1...):操作员用于接收奖励的账户
性能:地址缓存
SDK 会缓存 Bech32 编码后的地址,以优化重复转换的开销:Address.String() 时,SDK 会:
- 检查 LRU 缓存中是否已有已编码地址
- 如果找到则直接返回缓存值
- 否则执行 Bech32 编码并缓存结果
完整示例
下面是创建账户地址的完整流程:相关概念
The Cosmos SDK uses the Bech32 address format for all user-facing addresses. Bech32 encoding provides robust integrity checks through checksums and includes a human-readable prefix (HRP) that provides contextual information about the address type.
Address Types
The SDK defines three distinct address types, each with its own Bech32 prefix:| Address Type | Bech32 Prefix | Example | Purpose |
|---|---|---|---|
| Account Address | cosmos | cosmos1r5v5sr... | User accounts, balances, transactions |
| Validator Operator Address | cosmosvaloper | cosmosvaloper1r5v5sr... | Validator operator identity, staking operations |
| Consensus Address | cosmosvalcons | cosmosvalcons1r5v5sr... | Validator consensus participation, block signing |
- Account public keys:
cosmospub - Validator public keys:
cosmosvaloperpub - Consensus public keys:
cosmosvalconspub
Supported Key Schemes
The Cosmos SDK supports three key schemes. The choice of scheme affects address length and whether it can be used for transactions or consensus:| Address length in bytes | Public key length in bytes | Used for transaction authentication | Used for consensus (CometBFT) | |
|---|---|---|---|---|
secp256k1 | 20 | 33 | yes | no |
secp256r1 | 32 | 33 | yes | no |
tm-ed25519 | — not used — | 32 | no | yes |
secp256k1 is the default for user accounts. secp256r1 is supported as an alternative and produces longer addresses (32 bytes). tm-ed25519 is used exclusively for validator consensus keys and does not produce a user-facing address.
Address Derivation
Addresses are derived from public keys through cryptographic hashing. The process differs based on the key algorithm:Secp256k1 Keys (Account Addresses)
Account addresses use Bitcoin-style address derivation:crypto/keys/secp256k1/secp256k1.go
Ed25519 Keys (Consensus Addresses)
Consensus addresses use truncated SHA-256:crypto/keys/ed25519/ed25519.go
Bech32 Encoding Process
Once address bytes are derived, they’re converted to Bech32 format: Step 1: Convert from 8-bit to 5-bit encodingtypes/bech32/bech32.go
Configuring Bech32 prefixes
Every Cosmos SDK application sets its Bech32 prefixes and SLIP-44 coin type once at startup viasdk.GetConfig(), then seals the config so it cannot be changed at runtime. The defaults (cosmos, cosmosvaloper, etc.) are defined in types/config.go. Chain developers override them before the app starts:
Address Validation
The SDK validates addresses through:- Format validation: Ensures valid Bech32 encoding
- Prefix validation: Confirms correct HRP for address type
- Length validation: Verifies address is exactly 20 bytes when decoded
Module Addresses
Module accounts use deterministic address derivation defined in ADR-028:Validator Address Relationships
A validator has three related addresses:- Operator Address (
cosmosvaloper1...): The validator’s operational identity, derived from the operator’s account key - Consensus Address (
cosmosvalcons1...): Derived from the validator’s consensus public key (Ed25519), used for block signing - Account Address (
cosmos1...): The operator’s account for receiving rewards
Performance: Address Caching
The SDK caches Bech32-encoded addresses to optimize repeated conversions:Address.String() is called, the SDK:
- Checks the LRU cache for the encoded address
- Returns cached value if found
- Otherwise, performs Bech32 encoding and caches the result
Complete Example
Here’s the full pipeline for creating an account address:Related Concepts
- Accounts - Understanding account types and management
- Store - How addresses are used as keys in state storage
- Transactions - How addresses are used in transaction signing