广播事务时,web3给出“错误:返回的错误:无效的发送者”

时间:2019-08-02 05:17:59

标签: javascript ethereum web3js

我是web3和以太坊区块链的新手。我尝试使用以下代码广播事务,但它始终会给我发送人错误消息。示例代码如下所示。

const Tx = require('ethereumjs-tx').Transaction;

const Web3 = require('web3');
var url = 'https://ropsten.infura.io/v3/XXX';
const web3 = new Web3(new Web3.providers.HttpProvider(url));


const account1 = '0xaB7BXXX';
web3.eth.defaultAccount =  account1;



const privatekey1 = Buffer.from('fee069363aXXX','hex');


web3.eth.getTransactionCount(account1, (err, txCount) => {

        data = "0xXXX";
        const txObject = {
            nonce:    web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(200000), 
            gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
            data: data
                 }
        const tx = new Tx(txObject)
        tx.sign(privatekey1)

        const serializedTx = tx.serialize()
        const raw = '0x' + serializedTx.toString('hex')


        web3.eth.sendSignedTransaction(raw, (err, txHash) => {
            console.log('err:', err, 'txHash:', txHash)
        })
    });

错误发生在下面的行

web3.eth.sendSignedTransaction(raw, (err, txHash) => {
            console.log('err:', err, 'txHash:', txHash)
        })
    });

错误: err:错误:返回的错误:无效的发件人     在Object.ErrorResponse(/home/vishnu/node_modules/web3-core-helpers/src/errors.js:29:16)     在/home/vishnu/node_modules/web3-core-requestmanager/src/index.js:140:36     在XMLHttpRequest.request.onreadystatechange(/home/vishnu/node_modules/web3-providers-http/src/index.js:96:13)     在XMLHttpRequestEventTarget.dispatchEvent(/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)     在XMLHttpRequest._setReadyState(/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)     在XMLHttpRequest._onHttpResponseEnd(/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)     在IncomingMessage。 (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)     在emitNone(events.js:111:20)     在IncomingMessage.emit(events.js:208:7)     在endReadableNT(_stream_visible.js:1064:12)     在_combinedTickCallback(内部/进程/next_tick.js:138:11)     在process._tickCallback(内部/进程/next_tick.js:180:9)处txHash:未定义

请帮助我了解如何解决此问题。谢谢。

2 个答案:

答案 0 :(得分:0)

您是否在使用帐户之前创建过帐户?因为您使用的是硬编码帐户地址。 私钥也一样。通常,您应该以某种方式从帐户中提取它,而不要以硬编码明文形式提供它。

答案 1 :(得分:0)

您可以尝试更改 const tx = new Tx(txObject);const tx = new Tx(txObject, { chain: 'ropsten' });