Decode Tendermint b64 encoded transactions in Golang

298 views Asked by At

I execute the Tendermint request /unconfirmed_txs to get pending transactions data and I get a list of b64 encoded transactions like this:

CsQECpAECh8vZXRoZXJtaW50LmV2bS52MS5Nc2dFdGhlcmV1bVR4EuwDCqUDChovZXRoZXJtaW50LmV2bS52MS5MZWdhY3lUeBKGAwgCEg01MDc3OTQ2NTQxMzY0GPbZDCIqMHgxNDU4NjNFYjQyQ2Y2Mjg0N0E2Q2E3ODRlNjQxNkMxNjgyYjFiMkFlKhU0MDAwMDAwMDAwMDAwMDAwMDAwMDAy5AF/82q1AAAAAAAAAAAAAAAAAAAAAAAAAAAAfvEOe1XqTQ9Ynq8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAK5tNYq1CNfuvRh/6NqF6Zzkg6PtAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMd7WUoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAFx/ilcNV47YTmP9+nse5y3q4a4jAAAAAAAAAAAAAAAA3XPeoQq8K/+ZxgiC7FsrgbsdxbI6AVZCIBX0RwQW5LaC1LZG0W5mHid2Dsx/8FQn0r9IhHrWBAVxSiBLOhtk5OAdcCGBHaLUHK8rKEpL1bgtBRYxG/if+UesIxpCMHhlZGRjOWMxMWVkY2Q1NTY4YWZlOWM0Y2ZmYWNjMmU4ZjBhMGNlNjFiMDczMDE4OTQ1NzEyZDMwNTU3ZDBjMTUw+j8uCiwvZXRoZXJtaW50LmV2bS52MS5FeHRlbnNpb25PcHRpb25zRXRoZXJldW1UeBImEiQKHgoHYmFzZWNybxITMTA1NjgxMjA3ODI5NTU5Mjk1MhD22Qw=

How can I decode these transactions in Golang to get all the transaction data ( if possible in the Transaction struct of go-ethereum ) ?

After some research I understood that I have to use some codec and register them with some functions with the lib but I don't found a good code exemple for my use case and really don't understand how it works.

I also tried the library github.com/calvinlauyh/cosmosutils to decode transactions but I get the error: "unable to resolve type URL /ethermint.evm.v1.MsgEthereumTx: tx parse error"

Thanks !

1

There are 1 answers

0
mukesh prajapati On

transactions data is encode in base64, check the below code for decoding.

const { sha256 } = require("@cosmjs/crypto")
const { toHex} = require("@cosmjs/encoding")

const base64EncodedTrx ="CpIBCo8BChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEm8KLWNvc21vczEyeHQ0eDQ5cDk2bjlhdzR1bWp3eXAzaHVjdDI3bndyMmc0cjZwMhItY29zbW9zMXV0MncwbTN4YTd6MnJ2bmR2MjNwdGh2OXFjN2hrc3g2dGtmOXVxGg8KBXVhdG9tEgYyNTAwMDASaQpRCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAyhu2k5/x5GSSsNDGaahT1pMDK7Yk65LnRJo81IH166REgQKAggBGKMdEhQKDgoFdWF0b20SBTczMzU1EIu9BBpAM6HTxgkO1dRiuVU3TD23MzfKcyWOxhM4VlZKulLmFy4dcbpkaauXeuOlptAl9sbRKGozVr0Z87VPT/LIQCBrDw==";

const sha256v= sha256(Buffer.from(base64EncodedTrx,'base64'));
const txHash = toHex(sha256v)

//trx signature
console.log(txHash)