1

in many standard documentation and many blogs, I have found the example for list with two strings like below

[“cat”, “dog” ] -> C8 83 636174 83 646F67

and thats how it is decoded 1) Retrieve the first byte : 0xc8 2) Find the closest Byte Range which the first byte falls within; In this case, the First byte falls within the range [0xc0 .. 0xf7] hence the data is a List. 3) Create an empty List [] to accumulate 4) Length of the list, [0xc8 -0xc0] = 8 5) Start deciphering after c8 to get the encoded data from the List until end of List. C8 83 63 61 74 83 64 6F 67

We know we have a list [ and it has eight elements], now let’s look at what’s inside the List.

6) Figure out length which is: first byte-range, 0x83–0x80 = 3 7) The data type is String, and its length is 3 8) Parse and decipher the next 3 bytes 0x63, 0x61, 0x74 9) Repeat Steps 6 through 8 until end of the list for the next set of bytes resulting in 0x83, 0x64, 0x6f, 0x67

10) Calculate the Length and interpret the data, in this case, the data is 0x64,0x6f,0x67.

Every thing looks good and as per the RLP documentation But

I was wondering how we will decode if the example will be something like below

[131, “dog”, “cat”]

1) It is a list so start 0xc0 2) 131 → 0x83 , 3) dog to byte string → [0x80+3, 0x63, 0x61, 0x74] 4) cat to byte string → [0x80+3, 0x64, 0x6F, 0x67]

Final encoding → [0xc7, 0x83, 0x83, 0x63, 0x61, 0x74, 0x83, 0x64, 0x6F, 0x67]

Now during decoding → 0xc7 — indicate that it’s a list → list length is 7 (0xc7–0xc0) → Now what 0x83 should be assumed it could indicate that its is a byte string with 3 length or is it a number 131 which has a hex value 0x83 →

2

131 can't be encoded as just 0x83. From https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP:

For a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding.

0x83 is outside of that range, so this "self encoding" can't be used.

Otherwise, if a string is 0-55 bytes long, the RLP encoding consists of a single byte with value 0x80 plus the length of the string followed by the string. The range of the first byte is thus [0x80, 0xb7].

So we need to encode as 0x81 (string consisting of 1 byte) followed by 0x83 (that 1 byte).

So the correct RLP encoding is:

0xca818383646f6783636174

Note the 8183, which is how the item 131 is encoded.

EDIT

In the original question, either "dog" and "cat" are reversed in the list to encode, or some of the output is reversed instead. Above I used [131, "dog", "cat"], but now I think maybe it was meant to be the other way around. :-)

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.