Prerequisites
- Go installed on your machine.
- An Ethereum client (like Infura or a local node) and an RPC URL.
- A basic understanding of Go and Ethereum transactions.
Step 1: Set Up Your Go Project
First, create a new Go project and initialize it:
mkdir go-ethereum-tx
cd go-ethereum-tx
go mod init go-ethereum-tx
Next, install the required dependencies:
go get github.com/ethereum/go-ethereum
go get github.com/joho/godotenv
Step 2: Create a .env File
Create a .env file to store your RPC URL:
touch .env
Add your RPC URL to the .env file:
RPC_URL=https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID
Step 3: Write the Go Code
Create a main.go file and add the following code:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/ethereum/go-ethereum/common"
ethGotypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
)
/**
* Get RPC URL from environment variable
*/
func getRpcUrl() string {
if err := godotenv.Load(); err != nil {
log.Fatal(err, `for getRpcUrl`)
}
rpcUrl := os.Getenv("RPC_URL")
return rpcUrl
}
/**
* Get blockchain client
*/
func getBlockchainClient() *ethclient.Client {
client, err := ethclient.Dial(getRpcUrl())
if err != nil {
log.Fatal(err, `for getBlockchainClient`)
}
return client
}
/**
* Get transaction data
*/
func getTransactionData(tx string) map[string]interface{} {
client := getBlockchainClient()
txHash := common.HexToHash(tx)
transaction, isPending, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal(err, ` for finding the transaction`)
}
if isPending {
log.Fatal(`tx pending`)
}
chainId, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err, `for chainId`)
}
sender, err := ethGotypes.Sender(ethGotypes.NewLondonSigner(chainId), transaction)
if err != nil {
log.Fatal(err, `for getting the sender`)
}
// Convert transaction data to a hex string
dataHex := common.Bytes2Hex(transaction.Data())
hash := crypto.Keccak256Hash(transaction.Data())
fmt.Println("Keccak256Hash:", hash.Hex())
fmt.Println("Data (hex):", dataHex)
return map[string]interface{}{
"Hash": transaction.Hash().Hex(),
"Value": transaction.Value().String(),
"Gas": transaction.Gas(),
"GasPrice": transaction.GasPrice().String(),
"Nonce": transaction.Nonce(),
"Data": dataHex,
"From": sender.Hex(),
"To": transaction.To().Hex(),
}
}
/**
* Main function
*/
func main() {
getTransactionData("0x1661b4bd2ce6110fa3f51c0fe76c6b5ad45e35873be9583ff0f93678eb17099b")
}
Step 4: Run the Go Code
Run the Go code with:
The output
Data (hex): de05e6a0000000000000000000000000000000000000000000000000056e62be0f8ea86100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006655feb400000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000002000000000000000000000000030c5379b31b9910dcad606a0f1baa480ae6fbe4000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
Explanation
Loading the RPC URL: We use the godotenv package to load environment variables from the .env file. The getRpcUrl function fetches the RPC URL for connecting to the Ethereum network.
Connecting to the Ethereum Client: The getBlockchainClient function establishes a connection to the Ethereum client using the RPC URL.
Fetching Transaction Data: The getTransactionData function retrieves transaction details using the transaction hash. It fetches the transaction from the Ethereum network and extracts relevant information such as the hash, value, gas, gas price, nonce, input data, sender, and recipient.
Converting Data to Hex String: The input data from the transaction is in byte format. We use the common.Bytes2Hex function to convert this byte data to a hexadecimal string, which is a readable and standard format for Ethereum transaction input data.
Printing the Results: The input data and the Keccak-256 hash of the data are printed in the console.
Conclusion
In this tutorial, you learned how to extract transaction input data from a transaction hash in Go and convert the data from UTF-8 to a hexadecimal string. This approach ensures that the data is readable and can be compared with other sources like Etherscan.
Github link
Top comments (0)