ethereum / aleth

Aleth – Ethereum C++ client, tools and libraries
GNU General Public License v3.0
3.96k stars 2.18k forks source link

How to RLP encode a JSON String for a raw transaction? #5884

Open Bowfish opened 4 years ago

Bowfish commented 4 years ago

I'm new to RLP encoding. I'm writing a C++ application in Qt which should send a raw transaction. I installed and compiled the aleth framework. What are the steps to get an RLP encoded string of a JSON?

My JSON looks like this:

{
  nonce: '0x6',
  gasPrice: '0x09184e72a000', 
  gasLimit: '0x30000',
  to: '0xfa3caabc8eefec2b5e2895e5afbf79379e7268a7', 
  value: '0x00'
};

I've tried the following:

dev::RLP *m_rlp;

m_rlp = new dev::RLP(json, dev::RLP::VeryStrict);

If I compile and run the application I get the following error:

terminate called after throwing an instance of 'boost::wrapexcept<dev::OversizeRLP>'
  what():  /home/damian/src/aleth/libdevcore/RLP.cpp(28): Throw in function dev::RLP::RLP(dev::bytesConstRef, dev::RLP::Strictness)
Dynamic exception type: boost::wrapexcept<dev::OversizeRLP>
[dev::tag_comment*] = Actual size: 1, data size: 10
gumb0 commented 4 years ago

You'll have to parse your JSON first with some library, then use RLPStream class to encode the data.