How to create Raw Transactions in Ethereum ? — Part 1

Prateek Reddy
Blockchain Musings
Published in
3 min readJun 2, 2017

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

eth.getTransactionCount(<account>)

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

const keythereum = require('keythereum');
const address = '0x9e378d2365b7657ebb0f72ae402bc08812022211';
const datadir = '/home/administrator/ethereum/data';
const password = 'password';
let str;
keythereum.importFromFile(address, datadir, function (keyObject) {
keythereum.recover(password, keyObject, function (privateKey) {
console.log(privateKey.toString('hex'));
//05a20149c1c76ae9da8457435bf0224a4f81801da1d8204cb81608abe8c112ca
});
});

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

const ethTx = require('ethereumjs-tx');

const txParams = {
nonce: '0x6', // Replace by nonce for your account on geth node
gasPrice: '0x09184e72a000',
gasLimit: '0x30000',
to: '0xfa3caabc8eefec2b5e2895e5afbf79379e7268a7',
value: '0x00'
};
// Transaction is created
const tx = new ethTx(txParams);
const privKey = Buffer.from('05a20149c1c76ae9da8457435bf0224a4f81801da1d8204cb81608abe8c112ca', 'hex');
// Transaction is signed
tx.sign(privKey);
const serializedTx = tx.serialize();
const rawTx = '0x' + serializedTx.toString('hex');
console.log(rawTx)

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.

eth.sendRawTransaction(<rawTx string>)

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

--

--