3

Is it possible to get the senders address using golang when all you have is the raw transaction string. I can decode it and get the nonce, hash, to etc as these are all exported functions from the types.Transactions package but I cant seem to find a way to get the actual senders address (using golang) Javascript has some functions for doing so - I should also state that I need to do this in a standalone script rather than being connected to a node (if possible)

1

Here's a full working example of how to decode a raw transaction and read the sender (from) address:

package main

import (
    "encoding/hex"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/rlp"
)

func main() {
    rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772"

    tx := new(types.Transaction)
    rawTxBytes, err := hex.DecodeString(rawTx)
    rlp.DecodeBytes(rawTxBytes, &tx)

    msg, err := tx.AsMessage(types.NewEIP155Signer(tx.ChainId()))
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(msg.From().Hex())
    // 0x96216849c49358B10257cb55b28eA603c874b05E
}

See this transaction in etherscan

| improve this answer | |
0

we can use go to pick out the senders' address from raw tx as below sample code

        if msg, err := tx.AsMessage(types.NewEIP155Signer(big.NewInt(1))); err == nil {
            fmt.Println(msg.From().Hex())
        } else {
            fmt.Println(err.Error())
        }

and depend on the chainId get out from the raw tx, then you can decide which signer need to use

// Depending on the presence of the chain ID, sign with EIP155 or homestead
if chainID != nil {
    return types.SignTx(tx, types.NewEIP155Signer(chainID), key.PrivateKey)
}
return types.SignTx(tx, types.HomesteadSigner{}, key.PrivateKey)

-- or, we can simple get sender from tx as below code

        signer := types.NewEIP155Signer(tx.ChainId())
        sender, err := signer.Sender(tx)
        if err != nil {
            fmt.Printf("sender: %v", sender.Hex())
        }

Hope this help!

| improve this answer | |
  • It does help indeed. I had been looking into this myself but wasnt sure what to put as the eip signer field. – Kravitz Feb 22 '19 at 9:17
  • okay, I updated the answer for the signer part. PTAL – Ha ĐANG Feb 26 '19 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.