以太坊错误 {“code”:-32000,”message”:”未知账户”}

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

Ethereum error {"code":-32000,"message":"unknown account"}

问题

我正在尝试使用eth.sendTransaction发送原始交易,但是我收到一个错误,错误信息是{"code":-32000,"message":"unknown account"}。我不确定是什么原因导致的,而且在互联网上找不到答案。有人可以帮我解决吗?这是我的代码:

func ExecuteSignedTransaction(rawTransaction string) {
	var hash web3.Hash
	data := make(map[string]interface{})
	data["data"] = rawTransaction
	err := Web3HTTPClient.Call("eth_sendTransaction", &hash, data)

    if err != nil{
        fmt.Println(err)
        Os.Exit(1)
     }

	fmt.Println("Sent tx hash:", hash)
}
英文:

I am trying to send a raw transaction with eth.sendTransaction but I am getting an error that says {"code":-32000,"message":"unknown account"}. I am not sure what is causing this and I cant seem to find an answer on the internet. Can anyone help me figure it out? Here is my code:

func ExecuteSignedTransaction(rawTransaction string) {
	var hash web3.Hash
	data := make(map[string]interface{})
	data["data"] = rawTransaction
	err := Web3HTTPClient.Call("eth_sendTransaction", &hash, data)

    if err != nil{
        fmt.Println(err)
        Os.Exit(1)
     }

	fmt.Println("Sent tx hash:", hash)
}

答案1

得分: 1

所以,在这里我可能会做以下操作:

import (
  "strings"
  "crypto/ecdsa"
  "math/big"
  "github.com/ethereum/go-ethereum/ethclient"
  "github.com/ethereum/go-ethereum/crypto"
  "github.com/ethereum/go-ethereum/accounts/abi/bind"
)

var chainId = big.NewInt(1) // 以太坊主网的链ID,根据需要进行更改

func ecdsaPrivateKeyFromHex(privKeyHex string) *ecdsa.PrivateKey {
  ecdsaKey, err := crypto.HexToECDSA(privKeyHex)
  if err != nil { panic(err) }

  return ecdsaKey
}

func newTransactOpts(privKey *ecdsa.PrivateKey) *bind.TransactOpts {
  transactOpts, err := bind.NewKeyedTransactorWithChainID(privKey, chainId)
  if err != nil { panic(err) }

  return transactOpts
}

func newRpcClient() *ethclient.Client {
  c, err := ethclient.Dial("在此处插入RPC URL")
  if err != nil { panic(err) }
  
  return c
}

// 注意:构建*types.Transaction对象留给读者作为练习
func ExecuteTransaction(rawTransaction *types.Transaction) {
  privKeyHex := "0xblahblahblahblahblah" // 使用您自己账户的私钥
  transactOpts := newTransactOpts(ecdsaPrivateKeyFromHex(privKeyHex))

  signedTxn, err := transactOpts.Signer(transactOpts.From, rawTransaction)
  if err != nil { panic(err) }
  
  rpcClient := newRpcClient()
  if err := rpcClient.SendTransaction(context.Background(), signedTxn); err != nil { panic(err) }

  // 做其他操作
}
英文:

So, what I might do here:

import (
  "strings"
  "crypto/ecdsa"
  "math/big"
  "github.com/ethereum/go-ethereum/ethclient"
  "github.com/ethereum/go-ethereum/crypto"
  "github.com/ethereum/go-ethereum/accounts/abi/bind"
)

var chainId = big.NewInt(1) // chain id for the ethereum mainnet, change according to needs

func ecdsaPrivateKeyFromHex(privKeyHex string) *ecdsa.PrivateKey {
  ecdsaKey, err := crypto.HexToECDSA(privKeyHex)
  if err != nil { panic(err) }

  return ecdsaKey
}

func newTransactOpts(privKey *ecdsa.PrivateKey) *bind.TransactOpts {
  transactOpts, err := bind.NewKeyedTransactorWithChainID(privKey, chainId)
  if err != nil { panic(err) }

  return transactOpts
}

func newRpcClient() *ethclient.Client {
  c, err := ethclient.Dial("insert rpc url here")
  if err != nil { panic(err) }
  
  return c
}

// note: constructing the *types.Transaction object left as 
// an exercise to the reader
func ExecuteTransaction(rawTransaction *types.Transaction) {
  privKeyHex := "0xblahblahblahblahblah" // use your own account's private key
  transactOpts := newTransactOpts(ecdsaPrivateKeyFromHex(privKeyHex))

  signedTxn, err := transactOpts.Signer(transactOpts.From, rawTransaction)
  if err != nil { panic(err) }
  
  rpcClient := newRpcClient()
  if err := rpcClient.SendTransaction(context.Background(), signedTxn); err != nil { panic(err) }

  // do whatever
}

huangapple
  • 本文由 发表于 2021年11月2日 08:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/69804291.html
匿名

发表评论

匿名网友

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

确定