If you don't want to pull your head off with arg encoding please note that there is now in web3JS vs 1.0 a method called
encodeParameters
Check also encodeFunctionCall that will encode the whole function + arguments
If we take a look at the solidity doc Use of dynamic types
we use the offset in bytes to the start of their data area, measured from the start of the value encoding (i.e. not counting the first four bytes containing the hash of the function signature).
The following types are called “dynamic”:
bytes string T[] for any T
T[k] for any dynamic T and any k > 0
(T1,...,Tk) if any Ti is dynamic for 1 <= i <= k
All other types are called “static”.
NB: Address is the equivalent of a uint160
so in your example you will encode
_address the first parameter, a uint160 value padded to 32 bytes
_name
will be 0x0000000000000000000000000000000000000000000000000000000000000080
(offset to start of data part of second parameter, 4*32 bytes, exactly the size of the head part)
- _choices
(offset to start of data part of third parameter = offset to start of data part of first dynamic parameter + size of data part of first dynamic parameter = 4*32 + ???*32 )
- _blocknumber the uint256 value padded to 32 bytes
After this, the data part of the first dynamic argument
- number of bytes of _name
- _name value padded to 32 bytes on the right
Finally, we encode the data part of the second dynamic argument _choices
- number of elements of the array
- value of first element
- value of second element
- etc ...
Your input is not ABI encoded. The correct ABI encoding of those values is as follows:
0xff00000000000000000000000000000000000000000000000000000000000000ffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000006bc84f6a0fabbd7102be338c048fe0ae54948c2e0000000000000000000000006bc84f6a0fabbd7102be338c048fe0ae54948c2e
In ABI encoding, pretty much everything is encoded to length 32 bytes. Fixed-length byte sequences (like byte
and bytes5
) are right-padded with zeros, and numeric types (like address
) are left-padded with zeros. Array types are a bit more complicated, but you don't have any of those in this example.
Best Answer
I don't know if there is any native library available in Solidity, but there are node-js libraries available will help you do that. I also found this solidity code when doing a search on RLP, have a look at the code snippet below:
https://github.com/Giveth/milestonetracker/blob/master/contracts/RLP.sol
Libraries:
https://www.npmjs.com/package/rlp
articles:
http://wanderer.github.io/ethereum/nodejs/code/2014/05/21/using-ethereums-tries-with-node/
https://medium.com/@phansnt/data-structure-in-ethereum-episode-1-recursive-length-prefix-rlp-encoding-decoding-d1016832f919
Here is a thread on RLP Encoding:
RLP decoding question
Hope this helps.