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

huangapple go评论118阅读模式
英文:

type types.Transactions has no field or method GetRlp

问题

我正在尝试在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
英文:

I am trying to create a raw transaction in go-ethereum and found some tutorial code that I was tinkering with.
The error is:

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

The code:

package main
import (
&quot;context&quot;
&quot;crypto/ecdsa&quot;
&quot;encoding/hex&quot;
&quot;fmt&quot;
&quot;math/big&quot;
&quot;github.com/ethereum/go-ethereum/common&quot;
&quot;github.com/ethereum/go-ethereum/core/types&quot;
&quot;github.com/ethereum/go-ethereum/crypto&quot;
&quot;github.com/ethereum/go-ethereum/ethclient&quot;
&quot;github.com/jimlawless/whereami&quot;
)
func createRawTransaction() {
client, err := ethclient.Dial(infura.URL + infura.ID)
if err != nil {
fmt.Println(err.Error() + &quot;@&quot; + whereami.WhereAmI())
}
privateKey, err := crypto.HexToECDSA(&quot;fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19&quot;)
if err != nil {
fmt.Println(err.Error() + &quot;@&quot; + whereami.WhereAmI())
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
fmt.Println(&quot;error casting public key to ECDSA@&quot; + whereami.WhereAmI())
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
fmt.Println(err.Error() + &quot;@&quot; + 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() + &quot;@&quot; + whereami.WhereAmI())
}
toAddress := common.HexToAddress(&quot;0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d&quot;)
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() + &quot;@&quot; + whereami.WhereAmI())
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
fmt.Println(err.Error() + &quot;@&quot; + whereami.WhereAmI())
}
fmt.Print(&quot;signedTx: &quot;)
fmt.Println(signedTx)
/*	*/
ts := types.Transactions{signedTx}
rawTxBytes := ts.GetRlp(0)
rawTxHex := hex.EncodeToString(rawTxBytes)
fmt.Printf(rawTxHex) // f86...772
}

I am attempting to extract the transaction Hex value from the signed transaction.

Any suggestions on how to replace the last part?

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

答案1

得分: 7

不确定它是何时被移除的,但之前的代码如下:

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

// GetRlp 实现了 Rlpable 接口,并返回 rlp 中的第 i 个元素。
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])  //&lt;-- 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()
英文:

Not sure when it was removed, but the code previously was

import (
// ...
&quot;github.com/ethereum/go-ethereum/rlp&quot;
// ...
)
// GetRlp implements Rlpable and returns the i&#39;th element of s in rlp.
func (s Transactions) GetRlp(i int) []byte {
enc, _ := rlp.EncodeToBytes(s[i])
return enc
}

And rlp.EncodeToBytes still exists, so it stands to reason that you could do

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

That being said, there seems to be a new function

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)
}
}

so I would probably try doing

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

huangapple
  • 本文由 发表于 2022年1月19日 04:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70762025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定