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 throws "Invalid Sender" #1040

Closed
skkiran-pro opened this issue Sep 14, 2017 · 13 comments
Closed

sendSignedTransaction throws "Invalid Sender" #1040

skkiran-pro opened this issue Sep 14, 2017 · 13 comments

Comments

@skkiran-pro
Copy link

skkiran-pro commented Sep 14, 2017

sample code:

var ethNodeUrl = 'http://localhost:8545';

var provider = new Web3.providers.HttpProvider(ethNodeUrl);
var web3 = new Web3(provider);
var accountString = '{............encrypted account.........}';
var account = web3.eth.accounts.decrypt(JSON.parse(accountString), 'password');
account.signTransaction({
  from: account.address, 
  to: '<some target>', 
  value: 8704500000000000, 
  gas: 200000
}).then((tx) => {
  web3.eth.sendSignedTransaction(tx.rawTransaction).then((response) => {
     console.log(response);
  });
});

throws "Unhandled rejection Error: Returned error: invalid sender".

genesis.json has non-zero chainId. FROM account has sufficient balance. Works fine if i "unlock the account" in geth and then use sendTransaction(). dont want to do that as that has security issues (even if unlock timeout is very small).

@lukehedger
Copy link

@skkiran - this could be related to #932?

@skkiran-pro
Copy link
Author

@lukehedger does not seem to be related. signTransaction is working fine, and im even able to retrieve the signer address using web3.eth.accounts.recoverTransaction(tx.rawTransaction).

decoded tx.rawTransaction using ethereumjs-tx, and it seems to match the transaction RLP spec.

@skkiran-pro
Copy link
Author

Switched to "testrpc" and got same error as #932. Worked around it by using "ethereumjs-tx" for signing transaction and then sending it using sendSignedTransaction. sample code that works with testrpc:

var ethNodeUrl = 'http://localhost:8545';

var provider = new Web3.providers.HttpProvider(ethNodeUrl);
var web3 = new Web3(provider);
var accountString = '{............encrypted account.........}';
var account = web3.eth.accounts.decrypt(JSON.parse(accountString), 'password');
web3.eth.getTransactionCount(account.address).then((nonce) => {
  var tx = new Tx({
    nonce: nonce,
    from: account.address, 
    to: '<some target>', 
    value: 8704500000000000, 
    gasLimit: 200000
  });
  tx.sign(new Buffer(account.privateKey.substr(2), 'hex'));
  web3.eth.sendSignedTransaction('0x'+tx.serialize().toString('hex')).then((response) => {
     console.log(response);
  });
});

not sure why it is not working only with geth. you can close this if you feel it is not a problem with web3.

@drgorb
Copy link

drgorb commented Sep 19, 2017

I get the same error in the browser while the tests with node work fine.

In order to debug, I tried using eth.accounts.recoverTransaction(rawTransaction) again, this works fine on node but fails in the browser with: TypeError: Cannot read property 'redMul' of null

@robert-zaremba
Copy link

the problem is with EIP155 and matching ChainID and NetworkID.
If you set the two values above to the same value it will work. In case of geth --dev ... the networkID should be 1337.

@skkiran-pro
Copy link
Author

Tried with geth again and it works fine with ChainID and NetworkID set to 10. With ChainID 1337, i get #932. I will close this for now.

@ghost
Copy link

ghost commented Feb 24, 2018

solved by change the ChainID.

0: Olympic, Ethereum public pre-release testnet
1: Frontier, Homestead, Metropolis, the Ethereum public main network
1: Classic, the (un)forked public Ethereum Classic main network, chain ID 61
1: Expanse, an alternative Ethereum implementation, chain ID 2
2: Morden, the public Ethereum testnet, now Ethereum Classic testnet
3: Ropsten, the public cross-client Ethereum testnet
4: Rinkeby, the public Geth PoA testnet
8: Ubiq, the public Gubiq main network with flux difficulty chain ID 8
42: Kovan, the public Parity PoA testnet
77: Sokol, the public POA Network testnet
99: Core, the public POA Network main network
7762959: Musicoin, the music blockchain
[Other]: Could indicate that your connected to a local development test network.

Ref -> https://ethereum.stackexchange.com/questions/17051/how-to-select-a-network-id-or-is-there-a-list-of-network-ids/17101#17101

@kyriediculous
Copy link

I ran into this issue.

Make sure if you supply networkid when starting geth that it is the same in all places (ABIs, Provider, Genesis, Geth)

Eg. I was using 6660001 everywhere
My genesis had 666001 (missing 1 '0')

And i got the invalid sender error.

@agnelvishal
Copy link

agnelvishal commented Oct 24, 2019

Change
let tx = new Tx(rawTx);
to
let tx = new Tx(rawTx, { chain: 'ropsten', hardfork: 'petersburg' });

Source: #2915

@fsobh
Copy link

fsobh commented Oct 30, 2020

still facing this issues {"jsonrpc":"2.0","id":0,"error":{"code":-32000,"message":"invalid sender"}

@naftalimurgor
Copy link

@drgorb @robert-zaremba @lukehedger still facing this issue

@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
Tagging @drgorb @robert-zaremba @lukehedger since you are facing the issue.

@GEEKSUMAN
Copy link

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 Tagging @drgorb @robert-zaremba @lukehedger since you are facing the issue.

Thanks , Its completely working

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

10 participants