go - 类型 types.Transactions 没有字段或方法 GetRlp

标签 go go-ethereum

我正在尝试在 go-ethereum 中创建一个原始交易,并找到了一些我正在修改的教程代码。 错误是:

./transaction_raw_create.go:65:18: ts.GetRlp undefined (type types.Transactions has no field or method GetRlp)

代码:

package main

import (
    "context"
    "crypto/ecdsa"
    "encoding/hex"
    "fmt"
    "math/big"

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

func createRawTransaction() {
    client, err := ethclient.Dial(infura.URL + infura.ID)
    if err != nil {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }

    privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
    if err != nil {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        fmt.Println("error casting public key to ECDSA@" + whereami.WhereAmI())
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }

    value := big.NewInt(1000000000000000000) // in wei (1 eth)
    gasLimit := uint64(21000)                // in units
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }

    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 {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }

    signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
    if err != nil {
        fmt.Println(err.Error() + "@" + whereami.WhereAmI())
    }
    fmt.Print("signedTx: ")
    fmt.Println(signedTx)

    /*  */
    ts := types.Transactions{signedTx}
    rawTxBytes := ts.GetRlp(0)
    rawTxHex := hex.EncodeToString(rawTxBytes)
    fmt.Printf(rawTxHex) // f86...772
}

我正在尝试从已签名的交易中提取交易十六进制值。

关于如何替换最后一部分有什么建议吗?

    ts := types.Transactions{signedTx}
    rawTxBytes := ts.GetRlp(0)  //<-- ts.GetRlp undefined 
    rawTxHex := hex.EncodeToString(rawTxBytes)
    fmt.Printf(rawTxHex) // f86...772

最佳答案

不确定它何时被删除,但是 the code previously was

import (
    // ...
    "github.com/ethereum/go-ethereum/rlp"
    // ...
)

// GetRlp implements Rlpable and returns the i'th element of s in rlp.
func (s Transactions) GetRlp(i int) []byte {
    enc, _ := rlp.EncodeToBytes(s[i])
    return enc
}

rlp.EncodeToBytes仍然存在,所以按理说你可以做

ts := types.Transactions{signedTx}
rawTxBytes, _ := rlp.EncodeToBytes(ts[0])  //<-- ts.GetRlp undefined 
rawTxHex := hex.EncodeToString(rawTxBytes)
fmt.Printf(rawTxHex) // f86...772

话虽这么说,但似乎有一个新的功能

func (s Transactions) EncodeIndex(i int, w *bytes.Buffer) {
    tx := s[i]
    if tx.Type() == LegacyTxType {
        rlp.Encode(w, tx.inner)
    } else {
        tx.encodeTyped(w)
    }
}

所以我可能会尝试做

b := new(bytes.Buffer)
ts.EncodeIndex(0, b)
rawTxBytes := b.Bytes()

关于go - 类型 types.Transactions 没有字段或方法 GetRlp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70762025/

相关文章:

go - 在 Go 中将任意函数作为参数传递

go - 如何在 Go SDK 中部署以太坊智能合约

javascript - 当结构具有两个以上参数时,带有 array.push() 的 Solidity 函数不起作用

go - 如何从 R || 中的哈希消息和签名正确恢复 ECDSA 公钥S || V格式?

javascript - 调用 web3.eth.personal.unlockAccount 会抛出错误

go - 获取已知结构字段的名称

go - kubectl get with template string 失败,不兼容的类型进行比较

google-app-engine - AppEngine/数据存储 : Best way to retrieve since ancestor query returns everything recursively

go - 自定义结构的 UnmarshalYAML 接口(interface)的实现