Search
Duplicate
Try Notion
📙
Cosmos Transactions
For this workshop, we are going to be working with just cosmos transactions because if we want to deploy any solidity contract we can just follow any Ethereum guide.
The solidity contracts and Metamask wallet integration will work the same as Ethereum without any change needed.
Message Send
We are going to center the workshop around the Message Send, it’s the simplest message to work with because we just need:
Sender wallet
Receiver wallet
Amount to send
Coin denomination
Considerations
To create transactions we need to know the wallet's public key, account number and sequence.
Most of the values can be obtained using the blockchain’s API. But first, we need to be sure to send coins to the address that we want to use.
Sending coins to a wallet will register it to the blockchain.
Calling the account’s endpoint will return the following information:
curl https://rest.bd.evmos.org:1317/cosmos/auth/v1beta1/accounts/evmos1dgpv4leszpeg2jusx2xgyfnhdzghf3rfzw06t3
Bash
Response:
{ "account": { "@type": "/ethermint.types.v1.EthAccount", "base_account": { "address": "evmos1dgpv4leszpeg2jusx2xgyfnhdzghf3rfzw06t3", "pub_key": { "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", "key": "AhTMDUk9pBj+OBkBbSPXbR/Eka+YDC+Kh//uZhSkk5fA" }, "account_number": "2004488", "sequence": "154" }, "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" } }
JSON
NOTE: something important to note is that the pub_key values are optional, they are registered in the blockchain after that wallet sent a transaction to the Evmos network.
If the user didn’t send any transaction to the network we need to generate the public key.
Programmatic Signing
We are going to use a lib that I created called evmos-ts-wallet that handles the public key generation in case the value is not returned by the accounts’ endpoint.
It works by signing with the wallet a string message generate_pubkey and then converting the signature to a public key. In the library, this is automatically called when using getSender().
export async function generatePubkey(wallet: Wallet) { // Sign the personal message `generate_pubkey` and generate the pubkey from that signature const signature = await wallet.signMessage("generate_pubkey"); return signatureToPubkey( signature, Buffer.from([ 50, 215, 18, 245, 169, 63, 252, 16, 225, 169, 71, 95, 254, 165, 146, 216, 40, 162, 115, 78, 147, 125, 80, 182, 25, 69, 136, 250, 65, 200, 94, 178, ]) ); }
TypeScript
Metamask Signing
Metamask doesn’t expose a function to get the public key in the format that we need it, so we are going to do something similar to the previous step but using the Metamask interface.
export async function generatePubkeyFromSignature(wallet: string) { const signature = await window.ethereum.request({ method: "personal_sign", params: [wallet, "generate_pubkey"], }); const message = Buffer.from([ 50, 215, 18, 245, 169, 63, 252, 16, 225, 169, 71, 95, 254, 165, 146, 216, 40, 162, 115, 78, 147, 125, 80, 182, 25, 69, 136, 250, 65, 200, 94, 178, ]); return signatureToPubkey(signature, message); }
TypeScript
Keplr Signing
Keplr wallet was created with cosmos chains in mind, so the wallet provides us with the public key in the correct format.
const offlineSigner = window.getOfflineSigner("evmos_9001-2"); const wallets = await offlineSigner.getAccounts(); const pubkey = Buffer.from(wallets[0].pubkey).toString("base64");
TypeScript