5 golang 实现ETH 交易离线签名(冷签)交易时出现错误

rlp: input string too short for common.Address, decoding into (types.Transaction)(types.txdata).Recipient

这个是我的:

[go-ethereum](https://github.com/ethereum/go-ethereum) version:1.8.20

rawTxHex:f86d808504a817c800825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a76400008081eda006d7f604cebd917db3b6bca27a2df342c837815bffa58bd86f11cdcee87f12c6a0787278c1a27d4a02608172019821323b271221382cd4597330c9c4bde5625ea5

test Code:


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())
}
请先 登录 后评论

最佳答案 2020-03-03 23:46

http://gateway.moac.io/testnet是从以太坊fork的,但修改了交易结构(增加了几个字段),所以会出现这个错误。官方有提供golang的sdk。

请先 登录 后评论

其它 1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 3 关注
  • 0 收藏,6363 浏览
  • 提出于 2020-02-13 17:40