这个是我的:
[go-ethereum](https://github.com/ethereum/go-ethereum) version:1.8.20
rawTxHex:f86d808504a817c800825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a76400008081eda006d7f604cebd917db3b6bca27a2df342c837815bffa58bd86f11cdcee87f12c6a0787278c1a27d4a02608172019821323b271221382cd4597330c9c4bde5625ea5
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
//"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
//"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
//"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"math/big"
"os"
"path/filepath"
)
func main() {
client, err := ethclient.Dial("http://gateway.moac.io/testnet")//testnet
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("25be2dffe090f3e61381d2e7c9f12ed8433d8b5744751f5004fccea167517793")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(1000000000000000000) // in wei (1 eth)
gasLimit := uint64(21000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d")
var data []byte
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
fmt.Println(chainID)
ts := types.Transactions{signedTx}
rawTxBytes := ts.GetRlp(0)
rawTxHex := hex.EncodeToString(rawTxBytes)
fmt.Println(rawTxHex) // f86...772
rawTxBytess, err := hex.DecodeString(rawTxHex)
txs := new(types.Transaction)
rlp.DecodeBytes(rawTxBytess, &txs)
err = client.SendTransaction(context.Background(), txs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s", txs.Hash().Hex())
}