Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendSignedTransaction() error: invalid sender #2915

Closed
Skyge opened this issue Jun 27, 2019 · 17 comments
Closed

sendSignedTransaction() error: invalid sender #2915

Skyge opened this issue Jun 27, 2019 · 17 comments

Comments

@Skyge
Copy link
Contributor

Skyge commented Jun 27, 2019

Description

Expected behavior

Calling `web3.eth.sendSignedTransaction(data)` should succeed.

Actual behavior

Calling `web3.eth.sendSignedTransaction(data)` 
returns `error: {"code":-32000,"message":"invalid sender"}`.

Steps to reproduce the behavior

const Tx = require('ethereumjs-tx');
const privateKey = new Buffer('e33xxxxc0fd109', 'hex')

const rawTx = {
  nonce: '0x00',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

const tx = new Tx(rawTx);
tx.sign(privateKey);

const serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

Error Logs

error: {"code":-32000,"message":"invalid sender"}

Versions

  • web3.js: 1.0.0-beta.55
  • nodejs: v11.10.1
  • browser: Chrome
@Skyge
Copy link
Contributor Author

Skyge commented Jun 27, 2019

But this is not a bug of web3.js, it is due to the package ethereumjs-tx at the version 2.0.0, if you use the version 1.3.7, it will not have this error.

@Skyge
Copy link
Contributor Author

Skyge commented Jun 28, 2019

Besides above, I can not get anything from Promise Event receipt , does that means transactionHash now?

@shumailxyz
Copy link

I am having issues with receipt as well. Keeps on waiting. Works with version beta 1.0.0-beta.36.

@Skyge
Copy link
Contributor Author

Skyge commented Jul 2, 2019

@shumailxyz When I try to use a lower version: 1.0.0-beta.36, I can get a log with .on("receipt", …

@Skyge
Copy link
Contributor Author

Skyge commented Jul 4, 2019

Ohhh, I find a similar issue that mentions the event receipt: #2601

@PaulRBerg
Copy link

I can also confirm that ethereumjs-tx version 2.0.0 is broken:

error: {"code":-32000,"message":"invalid sender"}

Reverting to 1.3.7 fixes the problem.

@Skyge
Copy link
Contributor Author

Skyge commented Jul 6, 2019

@PaulRBerg If you want to use the version: 2.0.0, you should add extra parameter chain like this

var tx = new Tx(rawTx, {'chain':'ropsten'});

@nivida
Copy link
Contributor

nivida commented Jul 8, 2019

I've closed this because your problem is related to the ethereumjs-tx package.

@nivida nivida closed this as completed Jul 8, 2019
benjaminaaron added a commit to FuturICT2/FIN4Xplorer that referenced this issue Sep 25, 2019
Fixes: UnhandledPromiseRejectionWarning: Error: invalid sender
Via web3/web3.js#2915 (comment)
@sjana0
Copy link

sjana0 commented Feb 21, 2020

confirmed v1.3.7 works.

@zikyfranky
Copy link

@PaulRBerg If you want to use the version: 2.0.0, you should add extra parameter chain like this

var tx = new Tx(rawTx, {'chain':'ropsten'});

This works 100%

Thanks.

@programerr01
Copy link

var tx = new Tx(rawTx, {'chain':'ropsten'});
will it work for binance smart chain ?

const Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'))

@zikyfranky
Copy link

var tx = new Tx(rawTx, {'chain':'ropsten'});
will it work for binance smart chain ?

const Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'))

try also specifying the right chainId for Tx module using the @ethereumjs/common or deprecated version ethereumjs-common

I haven't tested this, so I don't know if it works.

import Common from 'ethereumjs-common'

const BSC_MAIN = Common.forCustomChain(
    'mainnet', {
        name: 'bnb',
        networkId: 56, 
        chainId: 56
    }, 
    'petersburg'
)

var tx = new Tx(rawTx, {common: BSC_MAIN});

@programerr01
Copy link

var tx = new Tx(rawTx, {'chain':'ropsten'});
will it work for binance smart chain ?

const Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'))

try also specifying the right chainId for Tx module using the @ethereumjs/common or deprecated version ethereumjs-common

I haven't tested this, so I don't know if it works.

import Common from 'ethereumjs-common'

const BSC_MAIN = Common.forCustomChain(
    'mainnet', {
        name: 'bnb',
        networkId: 56, 
        chainId: 56
    }, 
    'petersburg'
)

var tx = new Tx(rawTx, {common: BSC_MAIN});

thanks, i'll test it though i switched to python

@TuanLe2411
Copy link

var tx = new Tx(rawTx, {'chain':'ropsten'});
will it work for binance smart chain ?

const Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'))

try also specifying the right chainId for Tx module using the @ethereumjs/common or deprecated version ethereumjs-common
I haven't tested this, so I don't know if it works.

import Common from 'ethereumjs-common'

const BSC_MAIN = Common.forCustomChain(
    'mainnet', {
        name: 'bnb',
        networkId: 56, 
        chainId: 56
    }, 
    'petersburg'
)

var tx = new Tx(rawTx, {common: BSC_MAIN});

thanks, i'll test it though i switched to python

does it work?

@decentpay-finance
Copy link

var tx = new Tx(rawTx, {'chain':'ropsten'});
will it work for binance smart chain ?

const Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'))

try also specifying the right chainId for Tx module using the @ethereumjs/common or deprecated version ethereumjs-common
I haven't tested this, so I don't know if it works.

import Common from 'ethereumjs-common'

const BSC_MAIN = Common.forCustomChain(
    'mainnet', {
        name: 'bnb',
        networkId: 56, 
        chainId: 56
    }, 
    'petersburg'
)

var tx = new Tx(rawTx, {common: BSC_MAIN});

thanks, i'll test it though i switched to python

did it work?

@prantiknoor
Copy link

prantiknoor commented Jun 5, 2022

var Transaction = require("@ethereumjs/tx").Transaction;
var Common = require("@ethereumjs/common").default;

const BSC_MAIN = Common.custom({ name: "bnb", networkId: 56, chainId: 56 });
var tx = new Transaction(txData, { common: BSC_MAIN });

It has worked for me.

@namelessperson0
Copy link

namelessperson0 commented Jul 10, 2022

Change the below

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

      var rawTransaction = {
        "from": myAddress,
        "gasPrice": web3js.utils.toHex(20 * 1e9),
        "gasLimit": web3js.utils.toHex(210000),
        "to": contractAddress,
        "value": "0x0",
        "data": contract.methods.safeTransferFrom(myAddress, toAddress, tokenId).encodeABI(),
      }

        var privateKey = Buffer.from('abcde', 'hex')

        var transaction = new Tx(rawTransaction,{ chain: 'ropsten' , hardfork: 'petersburg' });

        transaction.sign(privateKey);

        web3js.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
            .on('transactionHash', console.log);

to the below

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

      var rawTransaction = {
        "from": myAddress,
        "gasPrice": web3js.utils.toHex(20 * 1e9),
        "gasLimit": web3js.utils.toHex(210000),
        "to": contractAddress,
        "value": "0x0",
        "data": contract.methods.safeTransferFrom(myAddress, toAddress, tokenId).encodeABI(),
      }

        var privateKey ='abcde'

        web3js.eth.accounts.signTransaction(rawTransaction, privateKey)
        .then(function(value){
            web3js.eth.sendSignedTransaction(value.rawTransaction)
            .then(function(response){
              console.log("response:" + JSON.stringify(response, null, ' '));
            })
          })

web3js.eth.accounts.signTransaction will take the burden of including the correct chainId and nonce .
Reference: #3305

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests