facebook

Signing Ethereum Transactions with ethereumjstx Module

Posted By : Amit

Oct 30, 2020

The most basic example for sending an ethereum transaction is through the web3js library itself using the following code.

 

// using the promise
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.then(function(receipt){
    ...
});

 

This requires the 'from' account to be present on the Ethereum node you are interacting with, and then the account should be unlocked to submit the transaction.

But sometimes you may not have the account existing on the node itself and may want to transact through the private key itself so as to not check for unlocking the transactions every time you do a transaction.

The ethereumjs-tx library allows us to do the same.

Here are the steps involved to use the above-mentioned library, from installation to transaction signing and then broadcasting the signed tx with web3

 

a) Install ethereumjs-tx

 

npm install ethereumjs-tx
const EthereumTx = require('ethereumjs-tx').Transaction

 

b) Set up your blockchain network details and your private key and address details in your environment config

 

const privateKey = config.get("blockchain.privateKey")
const fromAddress = config.get("blockchain.address")

 

c) Set up your blockchain network details

 

If it is one of the major forks then the following changes may be not be required, in that case, only the chain name and hardfork details are needed only.
But for a fork of Ethereum, you will need to provide the details through an ethereumjs-common instance, using the following

 

const Common = require("ethereumjs-common")

const customCommon = Common.default.forCustomChain(
  'mainnet',
  {
    name : "yourNetwork",
    chainId: config.get("blockchain.networkId"),
    networkId : config.get("blockchain.networkId")
  },
  "petersburg",
)

 

The above-mentioned values are required in the Transaction constructor.

 

d) Creating a signed transaction

 

This involves, the sendContractTx which needs the contract address passed as toAddress, a contract instance created through web3 and the parameter to the function you need to execute on the contract whose address is given by toAddress.


This function calls the signTx method which actually uses the ethereumjs-tx library to sign the transaction via our private key

 

async function sendContractTx(toAddress, candidateContract, param){
  let nonce = await web3HttpInstance.eth.getTransactionCount(fromAddress,'pending')
  let extraData =  await candidateContract.methods.contractMethod(param)
  extraData = extraData.encodeABI()
  let gas = await web3HttpInstance.eth.estimateGas({
      to : toAddress,
      data : extraData 
  })
  let gasPrice= await web3HttpInstance.eth.getGasPrice();
  logger.debug("DistributeReward Tx details gas %s gasPrice %s nonce %d , txPrice is %s", gas, gasPrice, nonce, 
  web3HttpInstance.utils.fromWei((parseInt(gas)*parseInt(gasPrice)).toString(10)))
  txObj = {
    from: fromAddress,
    to: toAddress,
    data : extraData,
    value: '0',
    gas: gas,
    gasPrice: gasPrice,
    privKey: privateKey,
    nonce
  }

  let signedTx = await signTx(txObj)
  signedTx = "0x" + signedTx.serialize().toString('hex')
  logger.debug("signed tx is %O", signedTx)
  return signedTx
}

 

In the above function, the signTx function is called which makes use of the ethereumjs-tx library, creates a new EthereumTx instance with the transaction parameters and the network config object.


Here privKey as also used in the function above corresponds to the private key and is required for the sign method on the tx object.

 

async function signTx(payload) {
    let { from, to, data, value, gas, gasPrice, privKey, nonce } = payload
    let txParams = {
      to,
      data,
      value: web3HttpInstance.utils.toHex(value),
      gasPrice: web3HttpInstance.utils.toHex(gasPrice),
      gas: web3HttpInstance.utils.toHex(gas),
      nonce: web3HttpInstance.utils.toHex(nonce)
    }
    var tx = new EthereumTx(txParams,{common:customCommon})
    privKey = new Buffer(privKey, 'hex')
    tx.sign(privKey)
    return tx
}

 

e) Submit the signed transaction

For submitting the transaction we are using web3.eth.sendSignedTransaction method, passing in the signed transaction created above as the parameter, and then we listen to the "receipt" event for the transaction receipt or the "error" event in case of any errors

 

let signedTx = await sendContractTx(candidate.hash,candidateContract,stakerAddress);
let signedTxObject = web3HttpInstance.eth.sendSignedTransaction(signedTx)

//On fetching receipt of the transaction
signedTxObject.on("receipt",async function(receipt){})

//On any error with the transaction
signedTxObject.on("error",function(error,receipt){})

 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

May 8, 2025 at 08:21 pm

Your comment is awaiting moderation.

bg bg

What's Trending in Tech

bg

Our Offices

India

INDIA

DG-18-009, Tower B,
Emaar Digital Greens, Sector 61,
Gurugram, Haryana
122011.
Unit- 117-120, First Floor,
Welldone Tech Park,
Sector 48, Sohna road,
Gurugram, Haryana
122018.
USA

USA

30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
Singapore

SINGAPORE

10 Anson Road, #13-09, International Plaza Singapore 079903.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us