Skip to content

Instantly share code, notes, and snippets.

@blockpane
Last active February 25, 2021 06:27
Show Gist options
  • Save blockpane/c052a0de9316d41d80e50dce44c62e71 to your computer and use it in GitHub Desktop.
Save blockpane/c052a0de9316d41d80e50dce44c62e71 to your computer and use it in GitHub Desktop.
Get Ethereum address from transaction
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
/*
Ethereum doesn't actually include the address of a sender in a transaction. This is derived from the V, R, S values,
hashed, and truncated to get the address. Fortunately this is made easy via a member function (Sender) from the
EIP155Signer structure.
*/
func main() {
var (
url = "http://ropsten:18545"
txid = "0xc79d64e7f75ddc0042c4de386e2fa7a1f742e5cf68651fa580297b16277f9801"
tx *types.Transaction
sender common.Address
)
// connect
client, err := ethclient.Dial(url)
if err != nil {
panic(err)
}
// get a tx
tx, _, err = client.TransactionByHash(context.Background(), common.HexToHash(txid))
if err != nil {
panic(err)
}
// get the sender
sender, err = types.NewEIP155Signer(tx.ChainId()).Sender(tx)
if err != nil {
panic(err)
}
// TX 0xc79d64e7f75ddc0042c4de386e2fa7a1f742e5cf68651fa580297b16277f9801 was sent from 0xcDa3283e436807a798FC2f8B07Be20F2b3B4D155
fmt.Printf("TX %s was sent from %s\n", txid, sender.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment