调用合同方法并手动签名时出错。 SendTransaction工作SendRawTransaction不工作

美好的一天,

即时通讯编写节点API暴露我的区块链(部署和松露testing)的方法。 我使用web3.js,ethereumjs-tx,ethereum,松露和坚实作为我的技术堆栈。

var txMethodData = masterKeyContract.myMethod.getData(myParams); 

交易参数是:

  const txParams = { nonce: web3.toHex(web3.eth.getTransactionCount(web3.eth.coinbase)), gasPrice: web3.toHex(web3.eth.gasPrice), gasLimit: web3.toHex(2000000), from: mainAccount, value: '0x00', to: targetContract.address, data: txMethodData, chainId: 3 }; 

即时通讯使用ethereumjs-TX

 const EthereumTx = require('ethereumjs-tx'); 

使用链接到我的mainAccount的私钥签署交易

 const tx = new EthereumTx(txParams); tx.sign(privateKey); const serializedTx = tx.serialize(); web3.eth.sendRawTransaction("0x" + serializedTx.toString('hex'), function (err1, resp1) { if (err1) { console.log(err1); } else { console.log(resp1); } }); 

而我得到的错误资金不足*天然气价格+价值。 我从mainAccount(从txParams的from:字段)发送此事务。 所以我login了我的mainAccount的余额

  web3.eth.getBalance(mainAccount, function (error, result) { if (!error) { console.log(web3.fromWei(result.toNumber(), "ether")); } else { console.error(error); } }); 

结果是252.12609391539726。 所以它不能没有资金。 我甚至估计了web3.eth.estimateGas(txParams)交易,它给了我97899.当前ropstein块的气体限制是4,707,806。 所以我应该有足够的。 所以问题在于为什么我的资金不足。

我怀疑的唯一原因是from:field,这是我的mainAccount实际上并不是交易的付款人。

更新:问题可能与签署,因为我刚刚testing

  web3.eth.sendTransaction(txParams, function (err1, resp1) { if (err1) { console.log(err1); } else { console.log(resp1); } }); 

它的工作,所以这个问题实际上是为什么sendRawTransaction不起作用。 可能与我签署交易的方式有关?

我检查了

 const privateKey = Buffer.from('[private_key_inserted_here]', 'hex'); 

实际上与我的mainAccount有关。 private_key_inserted_here从“密文”字段中从与我的主帐户相关的密钥库中获取。 我通过匹配keystore的“地址”字段来检查与我的mainAccount有关的信息。

提前致谢。

有些事情我可以看到

  • 它是gas而不是gasLimit
  • chainId是不需要的
  • 你应该自己pipe理这个nonce ,而不是依赖节点给你发送正确的随机数。 换句话说,不要从节点查询它,而是保持一个variables

如果你想你可以看看一个简约的钱包签名class我写了https://gist.github.com/gaiazov/e65a3e36c67eaf46fe81982cd193316d