15

I created a transaction in Go, signed it with a EIP155Signer signer, but when I try to publish the transaction through the Go client to the private blockchain, I get the error Invalid sender. Any idea what's happening here?

This issue seems to have the same error: https://github.com/ethereum/web3.js/issues/566

The transaction gets signed correctly (string output below), but the From field is never populated, and I'm unable to set it in Go because it's a hidden struct field that is internally set by the EIP155Signer

tx before sign=
    TX(e694193e2f4d6dc099444ff0057456e23cafe0f8035c7c70daf688bc2bf5546a)
    Contract: false
    From:     [invalid sender: invalid sig]
    To:       b424f3bcc3da6f54f27f5f4af0c9c18d567702e6
    Nonce:    0
    GasPrice: 0x0
    GasLimit  0x0
    Value:    0x0
    Data:     0x746865626573746461746165766572
    V:        0x0
    R:        0x0
    S:        0x0
    Hex:      ec80808094b424f3bcc3da6f54f27f5f4af0c9c18d567702e6808f746865626573746461746165766572808080

signed tx=
    TX(ff6c011c7527f74e00fde94f6a4a5e523211f21f221c0b7133bf723486b3284c)
    Contract: false
    From:     [invalid sender: invalid sig]
    To:       b424f3bcc3da6f54f27f5f4af0c9c18d567702e6
    Nonce:    0
    GasPrice: 0x0
    GasLimit  0x0
    Value:    0x0
    Data:     0x746865626573746461746165766572
    V:        0x0
    R:        0x91a89e1ee55cc959572c050780301701c7f02436701db5961ccb01d556c2e708
    S:        0x699559f036a1153b2f979cd01ee9b0beb0e889c8aaab0dbd5a46dd7931842f02
    Hex:      f86c80808094b424f3bcc3da6f54f27f5f4af0c9c18d567702e6808f74686562657374646174616576657280a091a89e1ee55cc959572c050780301701c7f02436701db5961ccb01d556c2e708a0699559f036a1153b2f979cd01ee9b0beb0e889c8aaab0dbd5a46dd7931842f02

Seems that the sender can't be derived according to this source file when the transaction is printed: https://github.com/ethereum/go-ethereum/blob/8771c3061f340451d0966adcc547338a25f2231f/core/types/transaction.go#L273

thanos
  • 844
  • 1
  • 8
  • 20

3 Answers3

24

The issue was that the chain id was not being set correctly. As a result transactions were signed with the incorrect chain id, so the sender could not be derived correctly. The fix was to also declare a config in the genesis block which specified it. An example:

"config": {
    "chainID"       : 10,
    "homesteadBlock": 0,
    "eip155Block":    0,
    "eip158Block":    0
}
thanos
  • 844
  • 1
  • 8
  • 20
0

Change the below

      const Tx = require('ethereumjs-tx').Transaction;
  var transaction_data = {
    "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(transaction_data,{ 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 transaction_data = {
    "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(transaction_data, privateKey)
    .then(function(value){
        web3js.eth.sendSignedTransaction(value.transaction_data)
        .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: https://github.com/ChainSafe/web3.js/issues/1040

nameless
  • 3
  • 3
0

before signing: rawTx.v = Buffer.from([chainId]) rawTx.nonce = web3.utils.toHex(web3.eth.getTransactionCount(account))