0

I'm trying to sign and push a transaction using ethereumjs and Infura. It seems to be working and I'm getting a transaction hash back, but the transaction is not visible on Etherscan, and when I'm trying to push the hash through a service like this I get an error:

Error! Unable to broadcast Tx : {"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid RLP.","data":"RlpExpectedToBeList"},"id":1}

This is the code I'm using:

var Web3 = require('web3');
var web3 = new Web3(new 
Web3.providers.HttpProvider('https://ropsten.infura.io/v3/my_api_key'));
var util = require('ethereumjs-util');
var tx = require('ethereumjs-tx');

var rawTx = {
    nonce: web3.toHex(1),
    gasPrice: web3.toHex(20000000000),
    gasLimit: web3.toHex(30000000000),
    to:... 
    value: web3.toHex(1000000),
    data: '0xc0de',
    networkId: 3
};

var privateKey = '0x...';
var p = new Buffer.from(privateKey.substring(2), 'hex');
var transaction = new tx(rawTx);
transaction.sign(p);
console.log("tx hash = " + util.bufferToHex(transaction.hash(true)));

What am I missing?

0
+50

It's

let p = new Buffer(privateKey, "hex");

and of course don't forget to serialize:

var serializedTx = "0x" + transaction.serialize().toString('hex'); 

and the serialized transaction is what you feed into web3.eth.sendRawTransaction.

  • Thanks. Looks like the transaction goes through on Ropsten but it seems to be stuck on pending for over an hour now, and I cannot see it in the address transactions either. https://ropsten.etherscan.io/tx/0xd5b53084af1b76d38848630b52add44e3aaaaa52bfa790285615f909329567ce. Any idea why? – Eddy Jul 30 '18 at 11:58
  • for that particular one the gas limit seems a bit high: 9484321 is what etherscan tells me. – Nikita Fuchs Jul 30 '18 at 12:41
  • So the transaction did not go though? Which gas limit should I specify? – Eddy Jul 30 '18 at 13:39
  • technically, it's not being mined. I'm seeing ropsten accepting higher gas limits than mainnet. Either way, it's still a bit above average: "( Warning: The GAS for this Txn exceeds the Avg GasLimit of 9,470,229 for the last 50 Blocks )" says etherscan. so go with 9000000 or something smaller, but big enough to execute whatever you're planning to ;) – Nikita Fuchs Jul 30 '18 at 13:46
  • 1
    Would have helped you out even without a bounty ;) It's more a thing of how poorly these things are documented and how few people are out there who mess around with these things. – Nikita Fuchs Aug 1 '18 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.