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
Rlpmodule 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
Bytesdata. -
.encode(l : Array)
RLP-encodes nested
Arraydata. -
.encode(s : String)
RLP-encodes
Stringliterals. -
.encode(i : Int)
RLP-encodes scalar
Intnumbers. -
.encode(c : Char)
RLP-encodes
Charcharacters. -
.encode(o : Bool)
RLP-encodes boolean
Boolvalues.
Class Method Detail
Decodes arbitrary data structures from a given binary recursive length prefix data stream.
Parameters:
rlp(Bytes): the encodedRlpdata 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 encodedRlpdata 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 binaryBytesdata to encode.
Rlp.encode Bytes[15, 66, 64]
# => Bytes[131, 15, 66, 64]
RLP-encodes nested Array data.
Parameters:
l(Array): the nestedArraydata to encode.
Rlp.encode [[""], [""]]
# => Bytes[196, 193, 128, 193, 128]
RLP-encodes String literals.
Parameters:
s(String): theStringliteral to encode.
Rlp.encode "dog"
# => Bytes[131, 100, 111, 103]
RLP-encodes scalar Int numbers.
Parameters:
i(Int): the scalarIntnumber to encode.
Rlp.encode 1_000_000
# => Bytes[131, 15, 66, 64]
RLP-encodes Char characters.
Parameters:
c(Char): theCharcharacter to encode.
Rlp.encode 'x'
# => Bytes[120]
RLP-encodes boolean Bool values.
Parameters:
o(Bool): the booleanBoolvalue to encode.
Rlp.encode true
# => Bytes[1]