0x0000000000000000000000000000000000000400
相关模块: 地址转换工具
Bech32 预编译提供了 Ethereum 十六进制地址与 Cosmos bech32 地址之间的地址格式转换功能。
概览
Bech32 预编译公开了简单的转换辅助方法,使 Solidity 合约可以:- 将 Ethereum
address转换为带有所需prefix的 Bech32 字符串(例如cosmos)。 - 将任意 Bech32 地址字符串转换回 EVM
address。
0x0000000000000000000000000000000000000400。
Gas 成本
这两个方法都使用一个可配置的基础 gas 数量,该值在链初始化期间设置。在合理范围内,无论字符串长度如何,gas 成本都是固定的。主要方法
hexToBech32
签名: hexToBech32(address addr, string memory prefix) → string memory
说明: 使用指定前缀将 Ethereum 十六进制地址转换为 Cosmos bech32 格式。
addr(address): 要转换的 Ethereum 十六进制地址prefix(string): 要使用的 bech32 前缀(例如:"cosmos")
bech32ToHex
签名: bech32ToHex(string memory bech32Address) → address
说明: 将 Cosmos bech32 地址转换为 Ethereum 十六进制格式。
bech32Address(string): 要转换的 bech32 格式地址
完整接口与 ABI
Bech32 Solidity Interface
Bech32 ABI
实现细节
地址校验
这两个方法都会对地址格式进行校验:- hexToBech32:校验十六进制地址是否恰好为 20 字节,且前缀非空
- bech32ToHex:校验 bech32 地址是否包含分隔符字符 “1”,并能解码为 20 字节
前缀处理
对于hexToBech32:
- prefix 参数决定 bech32 地址中人类可读部分的内容
- 常见前缀包括账户地址、验证者地址和共识地址
- 当前缀为空或仅包含空白字符时,将返回错误并提示建议使用的有效前缀
bech32ToHex:
- 前缀会从 bech32 地址中自动提取
- 无需提供 prefix 参数,因为它已嵌入地址中
状态可变性
虽然这两个方法在 ABI 中都标记为nonpayable,但它们实际作为只读操作运行,不会修改区块链状态。
Address:
0x0000000000000000000000000000000000000400
Related Module: Address conversion utilities
The Bech32 precompile provides address format conversion functionality between Ethereum hex addresses and Cosmos bech32 addresses.
Overview
The Bech32 precompile exposes simple conversion helpers so Solidity contracts can:- Convert an Ethereum
addressto a Bech32 string with a desiredprefix(e.g.cosmos). - Convert any Bech32 address string back into an EVM
address.
0x0000000000000000000000000000000000000400.
Gas Costs
Both methods use a configurable base gas amount that is set during chain initialization. The gas cost is fixed regardless of string length within reasonable bounds.Primary Methods
hexToBech32
Signature: hexToBech32(address addr, string memory prefix) → string memory
Description: Converts an Ethereum hex address to Cosmos bech32 format using the specified prefix.
addr(address): The Ethereum hex address to convertprefix(string): The bech32 prefix to use (e.g., “cosmos”)
bech32ToHex
Signature: bech32ToHex(string memory bech32Address) → address
Description: Converts a Cosmos bech32 address to Ethereum hex format.
bech32Address(string): The bech32 formatted address to convert
Full Interface & ABI
Bech32 Solidity Interface
Bech32 ABI
Implementation Details
Address Validation
Both methods perform validation on the address format:- hexToBech32: Validates that the hex address is exactly 20 bytes and the prefix is non-empty
- bech32ToHex: Validates that the bech32 address contains the separator character “1” and decodes to 20 bytes
Prefix Handling
ForhexToBech32:
- The prefix parameter determines the human-readable part of the bech32 address
- Common prefixes include account addresses, validator addresses, and consensus addresses
- Empty or whitespace-only prefixes result in an error with suggested valid prefixes
bech32ToHex:
- The prefix is automatically extracted from the bech32 address
- No prefix parameter is required as it’s embedded in the address
State Mutability
While both methods are marked asnonpayable in the ABI, they function as read-only operations and do not modify blockchain state.