Address Format Conversion
This tutorial demonstrates how to convert between an EVM hex address and a Cosmos bech32 address.
Prerequisites
Project Setup
First, we need to create a new directory for our project.
Next, we will initialize the project and installed the bech32-converting
package.
We then create a new file called index.js
in the src
directory where we will write our code.
You can use any package that allows you to encode and decode bech32-prefixed addresses. However, the specific steps for using those packages may vary.
Development
For this tutorial, we will assume that we have an EVM address 0x0901aeeD1d5144987DaE3D95AB219ff75e9bA636
that we want to convert to a Cosmos address, and then convert it back to an EVM address.
First, we import the bech32-converting
package and initialize the variable for the chain’s Bech32 prefix.
EVM Address to Cosmos Address
To convert an EVM address to a Cosmos address, we will be using the bech32-converting
package’s toHex
function.
Cosmos Address to EVM Address
To convert a Cosmos address to an EVM address, we will be using the bech32-converting
package’s fromHex
function.
The full code for the script is then as follows:
If we run this script, we should see the following output:
Was this page helpful?