How to create Raw Transactions in Ethereum ? — Part 1

Prateek Reddy
Jun 2, 2017 · 3 min read

In this post, we will find out how to create, sign and send raw transactions between accounts using NodeJs.

Raw transactions are a way by which we can separate the process of creating, signing and sending raw transactions, which gives us more control over how a transaction is to be done on Ethereum. Raw transactions helps us to create transactions outside the geth console.

What is a transaction made up of ?

When we send a transaction between two accounts from a geth console, we need 3 parameters i.e from account, to account and value. But, there are some more parameters underneath, which geth console fills for us, they are gasPrice, gasLimit and nonce.

  • gasPrice is the maximum price of gas you are willing to pay for this transaction.
  • gasLimit is the maximum gas you are willing to pay for this transaction.
  • nonce which you see here is not to be confused with the nonce that is used in the mining process. As it turns out, nonce for a transaction is a parameter that is used to maintain the order in which transactions are processed. So if you send a transaction with a nonce which is not one more then the previous transaction from that same account then that transaction will be rejected.

So we have to take care that nonce value is correctly sent, without which the transaction fails.

Raw transactions between accounts

We will use a node module called ethereum-js to create raw transactions outside geth console. From what we talked above, we need correct value of nonce to send the transaction, so let us the find out the nonce from the geth node we will be sending the transaction from, which can be found using

We should also know the private key of the account we want to send the transaction from. Easiest way to get the private key is by using recover feature of keythereum module to find out the private key from the geth keystore file. Below is the code to get the private key from keyfile

We can create a raw transaction from one account to another using the following code

We will now get a raw transaction string as output of the program. This can be submitted to Ethereum Network using following command in geth console.

You will now be able to see the transaction hash of the transaction.

In the next post we will find out how to send raw transactions to a contract.

Note:

  • Use this method while working with Ethereum mainnet, only if you know what you are doing as this could have security implications as your private key is extracted here.
  • Another way to create a raw Transaction in Go in mentioned here.

References

Blockchain Musings

Our learning experiences with Blockchain, Microservices…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store