Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript web3在广播事务时给出“错误:返回的错误:无效发件人”_Javascript_Ethereum_Web3js - Fatal编程技术网

Javascript web3在广播事务时给出“错误:返回的错误:无效发件人”

Javascript web3在广播事务时给出“错误:返回的错误:无效发件人”,javascript,ethereum,web3js,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

我不熟悉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)
        })
    });
错误: 错误:错误:返回错误:发件人无效 在Object.ErrorResponse/home/vishnu/node_modules/web3 core helpers/src/errors.js:29:16 at/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请求事件target.js:34:22 在XMLHttpRequest.\u setReadyState/home/vishnu/node\u modules/xhr2 cookies/dist/xml http request.js:208:14 在XMLHttpRequest.\u onhttpresponseed/home/vishnu/node\u modules/xhr2 cookies/dist/xml http request.js:318:14 在收到消息时/home/vishnu/node_modules/xhr2 cookies/dist/xml http request.js:289:61 在emitNone事件中。js:111:20 在IncomingMessage.emit events.js:208:7 在endReadableNT _stream _readable.js:1064:12 在_combinedtickcallbackinternal/process/next_tick.js:138:11 在进程中。_tickCallbackinternal/process/next_tick.js:180:9 txHash:undefined


请帮助我知道如何解决这个问题。谢谢。

您在使用之前是否创建了帐户?因为您使用的是硬编码的帐户地址。 私钥也是这样。通常,您应该以某种方式将其从帐户中提取出来,而不是作为硬编码明文提供。

您可以尝试更改 const tx=新的TxtxObject; 到
const tx=new TxtxObject,{chain:'ropsten'}

在哪里可以解决这个问题@吃豆人