Raw-Transaction

需要幫助使用“ethereumjs-tx”簽署原始交易

  • January 15, 2021

這是一個 NodeJS 腳本,我試圖在其中發送原始交易。我嘗試過以很多不同的方式更改參數,但總是得到相同的錯誤。我錯過了什麼?我從這裡跟隨一個例子。

腳本:

const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const config = require('./config');

const web3 = new Web3(new Web3.providers.HttpProvider(config.provider));
web3.eth.defaultAccount = "0x15568faf1cd21041acbb9f99d94dbe40e7f1a479";

const rawTx = {
 nonce: '0x00',                                     // 0
 gasPrice: '0x6FC23AC00',                           //30 GWei | 30 000 000 000 wei
 gasLimit: '0xF4240',                               //1 000 000
 to: '0x203D17B4a1725E001426b7Ab3193E6657b0dBcc6',
 value: '0x00',                                     // 0
 data: '0x00',                                      // 0
 chainId: '0x03'                                    // Ropsten
}

const tx = new Tx(rawTx);
const privateKey = Buffer.from("f7ef8432857eb502f9406282b6cb86219ea4973f5f9bb0605099cfc2c63a516a", 'hex');
tx.sign(privateKey);

const serializedTx = tx.serialize();
console.log(serializedTx.toString('hex')); //f865808506fc23ac00830f424094203d17b4a1725e001426b7ab3193e6657b0dbcc680002aa070f37febb101867fe6b5abb4b4a0bf6daadba43736164a08cd0413c4b29d4e05a0204248c3e9a826cac72e7be29a8cacce924e33b36a0a7e7096f6a86684884f18

web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
   console.log('Error:', err);
   console.log('Hash:', hash);
});

錯誤:

Error: Error: Invalid params
   at Object.InvalidResponse (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/errors.js:35:16)
   at /home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/requestmanager.js:86:36
   at XMLHttpRequest.request.onreadystatechange (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/httpprovider.js:118:13)
   at XMLHttpRequestEventTarget.dispatchEvent (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:64:18)
   at XMLHttpRequest._setReadyState (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:354:12)
   at XMLHttpRequest._onHttpResponseEnd (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:509:12)
   at IncomingMessage.<anonymous> (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:469:24)
   at emitNone (events.js:91:20)
   at IncomingMessage.emit (events.js:185:7)
   at endReadableNT (_stream_readable.js:974:12)

當我嘗試使用etherscan.io/pushTx廣播此交易時,我收到以下錯誤

Error! Unable to broadcast Tx : {"jsonrpc":"2.0","error":{"code":-32010,"message":"Invalid network id.","data":null},"id":1}.

問題是否可能在於我使用為主網生成的私鑰並將 ropsten 指定為chainId?我使用 keythereum 來生成密鑰。主網和 ropsten 的密鑰對生成是否有區別?

當我將chainId欄位更改為“0x01”時,出現錯誤:

Error! Unable to broadcast Tx : {"jsonrpc":"2.0","error":{"code":-32010,"message":"Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 30000000000000000 and got: 0.","data":null},"id":1}

這個錯誤是有道理的,因為這個地址只有在 ropsten 上有餘額。

一些快速的觀察:

  • nonce是硬編碼的

    • 應該計算:

    let nonce = web3.eth.getTransactionCount(fromAccount) - fromAccount從 與用於簽署原始交易publicKey的 配對的地址在哪裡privateKey

    • 在 testnet 上,起始 nonce 值是:1048576

    所以你需要增加這個偏移量:
    if (rawTx.chainId && Number(rawTx.chainId) > 1) nonce += 1048576

    • 然後,轉換為帶有 ‘0x’ 前綴的十六進制編碼字元串:
      nonce = web3.toHex(nonce)
  • 你的chainId是:0x03..這是testnet

  • web3.eth.defaultAccount沒關係..您privateKey確定發件人的“發件人”地址

  • 只是間接相關,但您可能還想查看另一個類似的庫:ethereumjs-tx-sign

更新

更正的測試網連結:

鏈 ID 值:

您正在嘗試通過與主網同步的節點發送 Ropsten 交易。

在主網中,您的“發件人”地址0x15568faf1cd21041acbb9f99d94dbe40e7f1a479沒有任何乙太幣,這就是為什麼更改 chainId 沒有多大幫助的原因。

您可能需要使用另一個節點,或使用--testnet命令行 option.d重新啟動此節點

引用自:https://ethereum.stackexchange.com/questions/12810