I am reading the Web3 documentation about signing transactions (see example below). As I understand it, the "transaction object" gets turned into a hex string and enveloped, this is the "message". The message is keccak256 hashed, and this hash is signed, i.e. used to make the signature. Now the message and signature are put together and RLP encoded, this is the raw transaction which is submitted to the network. Also this raw transaction is keccak256 hashed, the "transaction hash" which is used like the ID of the transaction.


Is there a stand alone function for converting a javascript object (like this transaction object) into a hex string, either in web3 or somewhere else?

How is the enveloped hex string combined (concatenated?) to the signature and RLP encoded? Is there a web3 RLP encode / decode function?

> web3.eth.accounts.signTransaction({to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',value: '1000000000',gas: 2000000},'0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318').then(console.log)
> { messageHash:
  v: '0x29',
   '0x3011a0d0b2b223e69994c55487b553d3a2b9cdb073d6301505568469b04d8afc' }
  • 1
    Take it into a string via JSON.stringify(obj), then replace every character in that string with its hexadecimal enoding. Commented Jun 18, 2019 at 17:30

1 Answer 1


Converting an object into a hexadecimal string can be done in native JS, with no web3 dependency:

const str = "0x"+[...JSON.stringify(obj)].map((c,i)=>str.charCodeAt(i).toString(16)).join("");

The JSON.stringify(obj) part will fail for circular objects, so look it on this website up if you need to.

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.