question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sendRawTransaction doesn't produce valid RPC request

See original GitHub issue
  • Version: 4.0.0-beta.1
  • Python: 3.5
  • OS: osx

What was wrong?

Trying to sign and send a raw transaction produces an error indicating the RPC request is invalid.

from web3 import Web3, HTTPProvider

w3 = Web3(HTTPProvider('https://mainnet.infura.io'))
transaction = {
    'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    'value': 1000000000,
    'gas': 2000000,
    'gasPrice': 234567897654321,
    'nonce': 0,
    'chainId': 1
}
key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
signed = w3.eth.account.signTransaction(transaction, key)
print(Web3.toHex(signed.rawTransaction))
tx = w3.eth.sendRawTransaction(signed.rawTransaction)
  • The full output of the error
Traceback (most recent call last):
  File "justtesting.py", line 14, in <module>
    tx = w3.eth.sendRawTransaction(signed.rawTransaction)
  File "/Users/monokh/Code/web3.py/web3/eth.py", line 222, in sendRawTransaction
    [raw_transaction],
  File "/Users/monokh/Code/web3.py/web3/manager.py", line 92, in request_blocking
    raise ValueError(response["error"])
ValueError: {'message': 'invalid argument 0: json: cannot unmarshal invalid hex string into Go value of type hexutil.Bytes', 'code': -32602}
  • What type of node you were connecting to. https://mainnet.infura.io testrpc produces similar error: ValueError: {'code': -32000, 'message': 'Error: Invalid Signature

How can it be fixed?

The web3py version produces the signed message: 0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428 The web3.js version produces an identical signed message.

Here’s the web3.js version: ``

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"));

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318', 'hex')

var rawTx = {
    'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    'value': web3.utils.toHex(1000000000),
    'gas': web3.utils.toHex(2000000),
    'gasPrice': web3.utils.toHex(234567897654321),
    'nonce': web3.utils.toHex(0),
    'chainId': 1
}

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

var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
  if (!err) {
    console.log(hash);
  } else {
    console.log(err); 
  }
});

So it seems the transaction signing is working correctly but something in the process of encoding that for the RPC request is going wrong.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
carvercommented, Nov 19, 2017

@monokh sendRawTransaction should accept either bytes or a hex string. signTransaction is working as intended.

0reactions
carvercommented, Nov 22, 2017

Released in v4.0.0-beta2

Read more comments on GitHub >

github_iconTop Results From Across the Web

SOLVED - sendRawTransaction (no) errors, valid tx, not found ...
I have a method that is supposed to create a transaction, send some wei and validate. When looking around I red that you...
Read more >
Retrying Transactions - Solana Cookbook
sendTransaction is only responsible for relaying a transaction from a client to an RPC node. If the node receives the transaction, ...
Read more >
Error occurred when trying to send rpc requests(s)
I have the same issue, and this code working for me var privateKey = _user.PrivateKey; var account = new Account(privateKey); var web3 =...
Read more >
Raw Transaction RPCs - Dash Core
This subsection covers all the raw transaction RPCs in Dash. ... SendRawTransaction: validates a transaction and broadcasts it to the peer-to-peer network.
Read more >
web3.eth API — Web3.py 5.31.3 documentation - Read the Docs
BSC apparently does not support these newer transaction types. ... The default block number that will be used for any RPC methods that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found