Skip to content

Converter

List of commonly used convert functions

Base64 to Hex

After getting the Cosmos Block information from the Rest API, the information is encoded using base64, but normally the explorers display the information in hex format.

To convert from the API response to the encoding that is usually seen in explorers and wallets, the Base64ToHexString function can be used.

proposer, _ := converter.Base64ToHexString(val.Block.Header.ProposerAddress)
hash, _ := converter.Base64ToHexString(val.BlockID.Hash)

Generate Transaction Hash

When reading the API response for a Cosmos Block, you will get every transaction included the block.

The transaction list is just the content of the transaction encoded using base64.

If you want to use the API endpoint to get the transaction logs, you will need the transaction hash.

If you already have the transactions bytes, the GenerateCosmosTxHash function can be used.

hash := converter.GenerateCosmosTxHash(txBytes)

When the transaction is in base64 format, you can use the function GenerateCosmosTxHashWithBase64

hash, err := converter.GenerateCosmosTxHashWithBase64(txBytes)

Generate Ethereum Transaction Hash

The GenerateEthTxHash hashes the given bytes (signed Ethereum transaction) and returns the transaction hash.

hash, err := converter.GenerateEthTxHash(txBytes)

When the Ethereum transaction was received using the cosmos endpoints, it will be wrapped inside a protobuf any object.

To get the Ethereum transaction hash, the GenerateEthTxHashFromEvmosTx function will unmarshall the protobuf object and return the hash:

txHash, err := converter.GenerateEthTxHashFromEvmosTx(txBase64)

Remove 0x Prefix from Address

Sometimes when encoding addresses to interact with contracts, you need the value without the 0x prefix.

The RemoveHexPrefixFromAddress function removes the prefix if it's needed.

converter.RemoveHexPrefixFromAddress("0xc5e00d3b04563950941f7137b5afa3a534f0d6d6")

Cosmos and Ethereum addresses

Bech32 to Hex

Bech32ToHex converts from cosmos address to Ethereum addresses

addr, err := converter.Bech32ToHex(bech32addr)

Hex to Bech32

HexToBech32 converts from Ethereum addresses to cosmos address

bech32, err := converter.Bech32ToHex(addr, prefix)

Normalize To Hex

NormalizeAddressToHex converts from Bech32 if the address does not have the 0x prefix

hexAddress, err := converter.NormalizeAddressToHex(addr)

Numbers converter

Hex String to Decimal

The HexStringToDecimal assumes that the value is a valid hex string, with or without the prefix and returns the decimal number in string format.

decimal := converter.HexStringToDecimal("0x1234")

Decimal to Hex String

DecimalStringToHex is the inverse process to HexStringToDecimal

hex := converter.DecimalStringToHex("1234")