0

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.

Questions:

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:
   '9809fdb7aeb339f416acc6c79ce422e05b4980a18f622492cd0097162276b5b0',
  v: '0x29',
  r:
   '0x3be21ae87e7733af7226d12c9aa4579d80fbe3cdd577844ddabc7b7b806016db',
  s:
   '0x17d1a6749c746e74903e0a0f07718fcd5cabb5ddabf998580ca03c1785518f66',
  rawTransaction:
   '0xf86a820431843b9aca00831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008029a03be21ae87e7733af7226d12c9aa4579d80fbe3cdd577844ddabc7b7b806016dba017d1a6749c746e74903e0a0f07718fcd5cabb5ddabf998580ca03c1785518f66',
  transactionHash:
   '0x3011a0d0b2b223e69994c55487b553d3a2b9cdb073d6301505568469b04d8afc' }
  • 1
    Take it into a string via JSON.stringify(obj), then replace every character in that string with its hexadecimal enoding. – goodvibration Jun 18 at 17:30
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, privacy policy and cookie policy

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