最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - web3 gives " Error: Returned error: invalid sender" when broadcasting transaction - Stack Overflo

programmeradmin0浏览0评论

I am new to web3 and ethereum blockchain. I tried broadcasting a transaction using the following code but it always gives me an invalid sender error.The sample code is shown below.

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

const Web3 = require('web3');
var url = '';
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)
        })
    });

The error occurs at the below line

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

ERROR:
err: Error: Returned error: invalid sender
    at 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
    at XMLHttpRequest.request.onreadystatechange (/home/vishnu/node_modules/web3-providers-http/src/index.js:96:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) txHash: undefined

Please help me know how to fix this problem. Thanks.

I am new to web3 and ethereum blockchain. I tried broadcasting a transaction using the following code but it always gives me an invalid sender error.The sample code is shown below.

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)
        })
    });

The error occurs at the below line

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

ERROR:
err: Error: Returned error: invalid sender
    at 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
    at XMLHttpRequest.request.onreadystatechange (/home/vishnu/node_modules/web3-providers-http/src/index.js:96:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/vishnu/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) txHash: undefined

Please help me know how to fix this problem. Thanks.

Share Improve this question edited Oct 1, 2021 at 7:27 kigawas 1,25816 silver badges29 bronze badges asked Aug 2, 2019 at 5:17 pacmanpacman 8351 gold badge11 silver badges40 bronze badges 1
  • Where you able to resolve this? @pacman – Abel Agoi Commented Apr 12, 2020 at 18:41
Add a comment  | 

1 Answer 1

Reset to default 17

You can try changing const tx = new Tx(txObject); to const tx = new Tx(txObject, { chain: 'ropsten' });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论