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
address
to 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.