I'm trying to generate a signature for a raw transaction using an ECDSA library implemented in C.

Here are the transaction parameters:

const txParams = {
    "nonce": "0x05",
    "gasPrice": "0x0ba43b7400",
    "gasLimit": "0x5208",
    "to": "0x9fbad6ab4b240d7512f863753ad3449f9ad6a530",
    "value": "0x0de0b6b3a7640000",
    "data": "0x",
    "chainId": 4

Then I'm generating the RLP encoded Keccak256 hash of the transaction parameters. Below is the RLP hash:


All of the steps upto here are done using the ethereumjs-tx library.

In the C program I convert the RLP Hash to byte format and store in a byte array.

static uint8_t m_hash[] =

This is then signed using the private key.

The output signature is always different each time I run the program, and below are a few samples.




I then use the sign function of ethereumjs-util to parse this signature and encode it into v, r, s values.

To calculate v, I've tried out different values of the recovery id in the range 0 - 3 which I directly enter in the sign function of ethereumjs-util, which looks like this.

exports.ecsign = function (msgHash, privateKey) {

  // -------------------------------------------------
  // Original Implementation

  // var sig = secp256k1.sign(msgHash, privateKey);

  // --------------------------------------------------

  // --------------------------------------------------
  // My changes

  var sig = {
    signature: "",
    recovery: 0

  var signature = "29C1C06C3E60D5BD51F9F803AEE5193ACABFFD6B45FAC48C7DDE737427FB7F2C643B35FABB633E6A6B6ABC52F7907D245457ECAE5F2372D3D6A85BC2B68A4DF0"

  sig.signature = Buffer.from(signature, 'hex');

  // --------------------------------------------------

  var ret = {};
  ret.r = sig.signature.slice(0, 32);
  ret.s = sig.signature.slice(32, 64);
  ret.v = sig.recovery + 27;
  return ret;

This returns the signed transaction which I then serialize and try to broadcast to the ethereum blockchain


At this point I get an error, stating "Invalid sender".

Can someone help me with what I'm doing wrong here?

1 Answer 1


Sorry, I was trying to post the transaction to the Main network, instead of the Rinkeby test network. It's working fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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