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
address
to a Bech32 string with a desired prefix
(e.g. cosmos
).
- Convert any Bech32 address string back into an EVM
address
.
It is a thin wrapper around the Cosmos address‐conversion library and lives at 0x0000000000000000000000000000000000000400
.
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.
import { ethers } from "ethers";
// Connect to the network
const provider = new ethers.JsonRpcProvider("<RPC_URL>");
// Precompile address and ABI
const precompileAddress = "0x0000000000000000000000000000000000000400";
const precompileAbi = [
"function hexToBech32(address addr, string memory prefix) view returns (string memory)"
];
// Create a contract instance
const contract = new ethers.Contract(precompileAddress, precompileAbi, provider);
// Inputs
const ethAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; // Placeholder
const prefix = "cosmos";
async function convertToBech32() {
try {
const bech32Address = await contract.hexToBech32(ethAddress, prefix);
console.log("Bech32 Address:", bech32Address);
} catch (error) {
console.error("Error converting to Bech32:", error);
}
}
convertToBech32();
Parameters:
addr
(address): The Ethereum hex address to convert
prefix
(string): The bech32 prefix to use (e.g., “cosmos”)
Returns: String containing the bech32 formatted address
bech32ToHex
Signature: bech32ToHex(string memory bech32Address) → address
Description: Converts a Cosmos bech32 address to Ethereum hex format.
import { ethers } from "ethers";
// Connect to the network
const provider = new ethers.JsonRpcProvider("<RPC_URL>");
// Precompile address and ABI
const precompileAddress = "0x0000000000000000000000000000000000000400";
const precompileAbi = [
"function bech32ToHex(string memory bech32Address) view returns (address)"
];
// Create a contract instance
const contract = new ethers.Contract(precompileAddress, precompileAbi, provider);
// Input
const bech32Address = "cosmos1qql8ag4cluz6r4dz22pdenrmwliufqss8c9j0e"; // Placeholder
async function convertToHex() {
try {
const hexAddress = await contract.bech32ToHex(bech32Address);
console.log("Hex Address:", hexAddress);
} catch (error) {
console.error("Error converting to Hex:", error);
}
}
convertToHex();
Parameters:
bech32Address
(string): The bech32 formatted address to convert
Returns: Ethereum address in hex format
Full Interface & ABI
Bech32 Solidity Interface
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.17;
/// @dev The Bech32I contract's address.
address constant Bech32_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000400;
/// @dev The Bech32I contract's instance.
Bech32I constant BECH32_CONTRACT = Bech32I(Bech32_PRECOMPILE_ADDRESS);
interface Bech32I {
function hexToBech32(address addr,string memory prefix) external view returns (string memory bech32Address);
function bech32ToHex(string memory bech32Address) external view returns (address addr);
}
{
"_format": "hh-sol-artifact-1",
"contractName": "Bech32I",
"sourceName": "solidity/precompiles/bech32/Bech32I.sol",
"abi": [
{
"inputs": [
{ "internalType": "string", "name": "bech32Address", "type": "string" }
],
"name": "bech32ToHex",
"outputs": [
{ "internalType": "address", "name": "addr", "type": "address" }
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "addr", "type": "address" },
{ "internalType": "string", "name": "prefix", "type": "string" }
],
"name": "hexToBech32",
"outputs": [
{ "internalType": "string", "name": "bech32Address", "type": "string" }
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}