module Rlp
Overview
The Rlp
module implementing Ethereum's Recursive Length Prefix
for arbitrary data encoding and decoding.
Defined in:
array.crconstants.cr
rlp.cr
version.cr
Constant Summary
-
EMPTY_ARRAY =
Bytes[OFFSET_ARRAY]
-
An empty array is defined as
0xC0
. -
EMPTY_STRING =
Bytes[OFFSET_STRING]
-
An empty string is defined as
0x80
. -
LIMIT_LONG =
(BigInt.new(256)) ** (BigInt.new(8))
-
The size limit of large data objects to be encoded is
256 ** 8
. -
LIMIT_SHORT =
56
-
The size limit of small data objects to be encoded is
56
. -
OFFSET_ARRAY =
192
-
The offset for array list encoding is
192
. -
OFFSET_STRING =
128
-
The offset for string literal encoding is
128
. -
VERSION =
"0.1.8"
-
The version of the
Rlp
module shard.
Class Method Summary
-
.decode(rlp : Bytes)
Decodes arbitrary data structures from a given binary recursive length prefix data stream.
-
.decode(hex : String)
Decodes arbitrary data structures from a given hex-encoded recursive length prefix data stream.
-
.encode(b : Bytes)
RLP-encodes binary
Bytes
data. -
.encode(l : Array)
RLP-encodes nested
Array
data. -
.encode(s : String)
RLP-encodes
String
literals. -
.encode(i : Int)
RLP-encodes scalar
Int
numbers. -
.encode(c : Char)
RLP-encodes
Char
characters. -
.encode(o : Bool)
RLP-encodes boolean
Bool
values.
Class Method Detail
Decodes arbitrary data structures from a given binary recursive length prefix data stream.
Parameters:
rlp
(Bytes
): the encodedRlp
data to decode.
Rlp.decode Bytes[195, 193, 192, 192]
# => [[[]], []]
NOTE The returned data only restores the data structure. It's up to the protocol to determine the meaning of the data as defined in Ethereum's design rationale.
Decodes arbitrary data structures from a given hex-encoded recursive length prefix data stream.
Parameters:
hex
(String
): the encodedRlp
data to decode.
Rlp.decode "c7c0c1c0c3c0c1c0"
# => [[], [[]], [[], [[]]]]
NOTE The returned data only restores the data structure. It's up to the protocol to determine the meaning of the data as defined in Ethereum's design rationale.
RLP-encodes binary Bytes
data.
Parameters:
b
(Bytes
): the binaryBytes
data to encode.
Rlp.encode Bytes[15, 66, 64]
# => Bytes[131, 15, 66, 64]
RLP-encodes nested Array
data.
Parameters:
l
(Array
): the nestedArray
data to encode.
Rlp.encode [[""], [""]]
# => Bytes[196, 193, 128, 193, 128]
RLP-encodes String
literals.
Parameters:
s
(String
): theString
literal to encode.
Rlp.encode "dog"
# => Bytes[131, 100, 111, 103]
RLP-encodes scalar Int
numbers.
Parameters:
i
(Int
): the scalarInt
number to encode.
Rlp.encode 1_000_000
# => Bytes[131, 15, 66, 64]
RLP-encodes Char
characters.
Parameters:
c
(Char
): theChar
character to encode.
Rlp.encode 'x'
# => Bytes[120]
RLP-encodes boolean Bool
values.
Parameters:
o
(Bool
): the booleanBool
value to encode.
Rlp.encode true
# => Bytes[1]