Deriving Wallet Addresses Across Cosmos Ecosystem



Recently, I built tshue.app to help myself rediscover forgotten tokens and early airdrops across the Cosmos ecosystem, so it’s a web app that scans most app chains. The first thing I needed to solve was: How can I find all of my addresses on every app chain ?

Although each chain shows a different address, they are all derived from the same public key. In other words, if you have one address, then you can derive the others. All app-chain addresses share the same three-part structure:

  • Prefix – for example, cosmos or osmo
  • Core Public Key – a Uint8Array that never changes
  • Checksum – generated by the algorithm

Once you extract the core public key, you can change the prefix to generate an address for a different chain.




Using @cosmjs/encoding to parse and generate addresses

The Cosmos team provides the @cosmjs/encoding library, which makes it easy to split an address into its prefix and core bytes.

import { fromBech32, toBech32 } from '@cosmjs/encoding'


// Parse the cosmos address
const cosmosAddress = 'cosmos…'
const { prefix, data } = fromBech32(cosmosAddress)

console.log(prefix) // prefix: cosmos
console.log(data) // core public key data

// Generate the osmosis address
const osmosisAddress = toBech32('osmo', data)

Enter fullscreen mode

Exit fullscreen mode

If you want the full set of app-chain prefixes, you can get them from the chain-registry package. Thanks for reading!



Source link