How can I decode a raw transaction using Go / go-ethereum ? There is no method/function to read the raw bytes or the hex into a transaction https://godoc.org/github.com/ethereum/go-ethereum/core/types . Basically I just want to extract the destination address and the amount sent.
-
Possible duplicate of How can I easily parse a raw transaction? – Ajoy Bhatia Jan 24 '18 at 17:05
-
I voted to close this as a duplicate of ethereum.stackexchange.com/questions/4196/…. If you think it is not a duplicate or are for some reason not satisfied with the answers to that question, please edit question explaining why it should be reopened. – Ajoy Bhatia Jan 24 '18 at 17:07
-
@AjoyBhatia This question seems to specifically be about how to do this using go-ethereum. – user19510 Jan 24 '18 at 19:20
-
@smarx - That is correct. I retracted my Close vote – Ajoy Bhatia Jan 24 '18 at 19:48
Using official ethereum-go:
// import packages:
// "github.com/ethereum/go-ethereum/rlp"
// "github.com/ethereum/go-ethereum/core/types"
var tx *types.Transaction
rawtx,err := hex.DecodeString(rawTXstring)
rlp.DecodeBytes(rawtx, &tx)
fmt.Println(tx)
// amount *big.Int
tx.Value()
// destination address *common.Address
tx.To()
To access data in tx object look for methods in file go-ethereum/core/types/transaction.go
Here's a working code on github
-
may I ask what are you doing in that loop to decode the public key ??? What are the sig1 and sig2 and why do you need a public key at all? – Nulik Jun 21 '18 at 2:32
-
@Nulik For normal using of blockchain you don't need a public key. But if you want to research or to know deeper about EC cryptography you might be eager to find a public key. In my code sig1 and sig2 is a signature of the message. It consists of R+S+V (learn more about elliptic curves). R and S are known. V can be 00 or 01, so we just check both options. – Tarik Jun 22 '18 at 10:47
Here is a package I found out about from this answer:
https://github.com/ConsenSys/abi-decoder
See the code block titled Decode Tx data. I think this should do what you want.
-
-
Ok. Ok. I should read questions more carefully. go-ethereum also has a Javascript API to communicate with the node using IPC or RPC. That is what I have always used because I programmed in Javascript. Perhaps, when you said Go / go-ethereum, you meant an application in Go (i.e. not Javascript) interacting with the go-ethereum client? – Ajoy Bhatia Jan 24 '18 at 20:32
Here's a full example of how to read transaction values (ie, from address, eth value, etc..) in Go.
package main
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://mainnet.infura.io")
if err != nil {
log.Fatal(err)
}
blockNumber := big.NewInt(5671744)
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatal(err)
}
for _, tx := range block.Transactions() {
fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
fmt.Println(tx.Value().String()) // 10000000000000000
fmt.Println(tx.Gas()) // 105000
fmt.Println(tx.GasPrice().Uint64()) // 102000000000
fmt.Println(tx.Nonce()) // 110644
fmt.Println(tx.Data()) // []
fmt.Println(tx.To().Hex()) // 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e
if msg, err := tx.AsMessage(types.HomesteadSigner{}); err != nil {
fmt.Println(msg.From().Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258
}
receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
log.Fatal(err)
}
fmt.Println(receipt.Status) // 1
}
}
If you have the raw tx hex, here's how you can decode it into a tx type:
package main
import (
"encoding/hex"
"log"
"github.com/c3systems/vendor.bak/github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
)
func main() {
client, err := ethclient.Dial("https://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772"
var tx *types.Transaction
rawTxBytes, err := hex.DecodeString(rawTx)
rlp.DecodeBytes(rawTxBytes, &tx)
spew.Dump(tx)
}