NodeJS 发送已签名事务:Quorum上的无效发件人错误

gc0ot86w  于 2022-12-18  发布在  Node.js
关注(0)|答案(3)|浏览(142)

I am trying to call a method on a Smart Contract on a node app using an account that I create from a private key using web3 on a private quorum blockchain. I am using the code below, but I keep getting the error
Error: Returned error: invalid sender
Here's the code:

const Web3 = require("web3")
const web3 = new Web3(RPC_NODE);

var contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADRESS);
var myMethod = contract.methods.myMethod(param1, param2);
var encodedABI = myMethod.encodeABI()

const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);

var tx = {
  from: account.address,
  to: CONTRACT_ADRESS,
  gas: 2000000,
  data: encodedABI
};

console.log("Account Address:", account.address);

account.signTransaction(tx).then(signed => {
  var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Raw Signed Transaction:", signed.rawTransaction);
});

The error makes sense, since if I have a look at the logs, this is what I get:

Account Address: 0xBd55e32CB2559b06511D03E9B37a1c3bfF0f35Cd
Raw Signed Transaction: '0xf901050480831e84809473341607498eb0648db01ed55c378c3ea227bf6980b8a47d8febab00000000000000000000000000000000000000000000000000000000000000401e4008f33fa1479a536072f9b06e978689c47527593b9a29853e2c708ab75293000000000000000000000000000000000000000000000000000000000000002463663038646139362d363665332d343034312d393630652d3030393838636433383865320000000000000000000000000000000000000000000000000000000078a0700c0b30a6a58016a9621033fe71974eb0cc8721841353b7aa5e42f96d122a7aa023fa6bf121c3c83de1c03674edf90c5bf9ad55165ac10a43965318cdde65e438'

And if I decode the raw signed transaction I see:

{
  "nonce": 4,
  "gasPrice": null,
  "gasLimit": 2000000,
  "to": "0x73341607498eb0648db01ed55c378c3ea227bf69",
  "value": 0,
  "data": "7d8febab00000000000000000000000000000000000000000000000000000000000000401e4008f33fa1479a536072f9b06e978689c47527593b9a29853e2c708ab75293000000000000000000000000000000000000000000000000000000000000002463343866323863322d353932622d343930622d616663322d62323262366164303035653900000000000000000000000000000000000000000000000000000000",
  "from": "0xbd55e32cb2559b06511d03e9b37a1c3bff0f35cd",
  "r": "d423912021ff81d53c63da08374f9543df1fca3b1d76f085148310d51afb64f1",
  "v": "77",
  "s": "0a212017b256986ca24f151bd920817ae106624b54c04ea702ffd03ab288205c"
}

As you can see, the "from" field from the signed raw transaction does not match the address of the account I created by using the private key. Can somebody explain this behaviour?

7z5jn7bk

7z5jn7bk1#

上一次我遇到类似的问题时,我在处理privateKey的格式时遇到了困难。
根据您使用的web3版本,此格式可能会对通过privateKeyToAccount创建的帐户产生影响。
我猜您缺少了一个0x前缀。请确保您的PRIVATE_KEY前面有一个"0x",然后再给予一次。

0mkxixxg

0mkxixxg2#

我通过将正确的chainId添加到tx对象来解决此问题

x8diyxa7

x8diyxa73#

您可以通过添加chainId来解决此问题,例如var tx = new Tx(rawTx, {'chain':'ropsten'});
对于您的代码,您可以这样做=

account.signTransaction(tx, {'chain':'ropsten'}).then(signed => {
  var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Raw Signed Transaction:", signed.rawTransaction);
});

相关问题