How to convert between different aspects of data for
Token Denominations
In Cosmos, every denomination amount is formatted as an unsigned integer. With this, the chain does not have to deal with fractions. For an EVM chain this amount is typically 10**18 power, while Juno and other native Cosmos chains use the 10**6 power. This means if I want to send you 1 JUNO, I am actually sending 1,000,000 of the smaller token.
You can figure out which power a token uses by its prefix character in the denomination. In the case of JUNO, the actual denomination is shown as ujuno. This u signals that it is using any amount times 10**6 to get the human readable amount.
10JUNO = 10,000,000ujuno
0.5 JUNO = 500,000ujuno
0.00001 JUNO = 10ujuno
This means the smallest amount anyone can send is 0.000001 JUNO
Address Conversions
Valoper -> Base
Convert the validator operator wallet to a standard base address
// npm i @cosmjs/encodingimport{toBech32,fromBech32}from'@cosmjs/encoding'lettoPrefix="juno"letinitial="junovaloper196ax4vc0lwpxndu9dyhvca7jhxp70rmcqcnylw"letconverted=toBech32(toPrefix,fromBech32(initial).data)console.log(converted)// juno196ax4vc0lwpxndu9dyhvca7jhxp70rmcl99tyh
Juno -> Other Chain
You can only convert between the same cointype, so converting a JUNO (118) to EVM address such as Terra's 330 will not properly convert. This is not possible to do without their private key
// npm i @cosmjs/encoding
import {fromHex, toBech32} from '@cosmjs/encoding'
// where junovalcons is the wallet prefix for the chain + valcons
const prefix = "junovalcons"
let addr = toBech32(prefix, fromHex("1470B9237056641663CB4DFDEC86B064578B29BF"))
console.log(addr)
// This outputs junovalcons1z3ctjgms2ejpvc7tfh77ep4sv3tck2dl30r3mx
// which matches their page
// https://ping.pub/juno/staking/junovaloper196ax4vc0lwpxndu9dyhvca7jhxp70rmcqcnylw
You can get the Public Key from the REST/LCD endpoint:
cosmos/staking/v1beta1/validators
https://api.juno.strange.love/cosmos/staking/v1beta1/validators
&
https://.../cosmos/staking/v1beta1/validators/<junovaloper...>
// npm i @cosmjs/encoding
import {fromBase64, toBech32} from '@cosmjs/encoding'
// npm i @cosmjs/crypto
import { sha256 } from '@cosmjs/crypto'
let prefix = "junovalcons"
// Chain Format:
// {
// "@type":"/cosmos.crypto.ed25519.PubKey",
// "key":"/O7BtNW0pafwfvomgR4ZnfldwPXiFfJs9mHg3gwfv5Q="
// }
// we just need the .key string from the object
let pubKey = "/O7BtNW0pafwfvomgR4ZnfldwPXiFfJs9mHg3gwfv5Q="
const addr = toBech32(prefix, sha256(fromBase64(pubKey)).slice(0, 20))
console.log(addr)
// junovalcons1z3ctjgms2ejpvc7tfh77ep4sv3tck2dl30r3mx