Created
March 11, 2022 06:35
-
-
Save oussamahamdaoui/fb56c1a27c466a2b4bdbf9322f14dc0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bech = require('bech32'); | |
const { Tendermint34Client } = require('@cosmjs/tendermint-rpc'); | |
const { setupBankExtension } = require('@cosmjs/stargate'); | |
// create a rpc client to fetch data from a blockchain | |
const tendermintClient = QueryClient.withExtensions( | |
(await Tendermint34Client.connect("rpc-url")), | |
setupBankExtension ); | |
// use this to get the balances | |
const balances = await tendermintClient.client.bank.allBalances('cosmos...'); | |
// to change from one address to another you could use those functions | |
const hexToBech = (str, prefix) => { | |
const words = bech.bech32.toWords(Buffer.from(str.replace(/0x/, ''), 'hex')); | |
return bech.bech32.encode(prefix, words); | |
}; | |
const bechToHex = (str = '') => { | |
const y = bech.bech32.decode(str); | |
return Buffer.from(bech.bech32.fromWords(y.words)).toString('hex'); | |
}; | |
// this gives you an eth like address | |
bechToHex('cosmos.....'); | |
// this will convert your cosmos address to osmosis | |
hexToBech(bechToHex('cosmos.....'), 'osmo'); | |
// this will convert your cosmos address to akash | |
hexToBech(bechToHex('cosmos.....'), 'akash'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment