Go Ethereum – How to Create and Sign a Transaction Locally Using Go

go-ethereumjson-rpc

Geth's code can be used as a library. Are there any code examples that use Geth to sign a transaction locally? I then want to POST the resulting string using eth_sendRawTransaction.

Best Answer

Richard's answer pointed me in the right direction, but I had to remove some cruft to get it to do what I wanted. Here's some code that works (just replace the hardcoded values):

package main

import (
    "bytes"
    "fmt"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
)

func main() {

    chainId := big.NewInt(3) // ropsten

    senderPrivKey, _ := crypto.HexToECDSA("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    recipientAddr := common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")

    nonce := uint64(7)
    amount := big.NewInt(1000000000000000000) // 1 ether
    gasLimit := big.NewInt(100000)
    gasPrice := big.NewInt(20000000000) // 20 gwei

    tx := types.NewTransaction(nonce, recipientAddr, amount, gasLimit, gasPrice, nil)

    signer := types.NewEIP155Signer(chainId)
    signedTx, _ := types.SignTx(tx, signer, senderPrivKey)
    fmt.Println(signedTx)

    var buff bytes.Buffer
    signedTx.EncodeRLP(&buff)
    fmt.Printf("0x%x\n", buff.Bytes())
}