module Rlp

Overview

The Rlp module implementing Ethereum's Recursive Length Prefix for arbitrary data encoding and decoding.

Defined in:

array.cr
constants.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

Class Method Detail

def self.decode(rlp : Bytes) #

Decodes arbitrary data structures from a given binary recursive length prefix data stream.

Parameters:

  • rlp (Bytes): the encoded Rlp 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.


[View source]
def self.decode(hex : String) #

Decodes arbitrary data structures from a given hex-encoded recursive length prefix data stream.

Parameters:

  • hex (String): the encoded Rlp 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.


[View source]
def self.encode(b : Bytes) #

RLP-encodes binary Bytes data.

Parameters:

  • b (Bytes): the binary Bytes data to encode.
Rlp.encode Bytes[15, 66, 64]
# => Bytes[131, 15, 66, 64]

[View source]
def self.encode(l : Array) #

RLP-encodes nested Array data.

Parameters:

  • l (Array): the nested Array data to encode.
Rlp.encode [[""], [""]]
# => Bytes[196, 193, 128, 193, 128]

[View source]
def self.encode(s : String) #

RLP-encodes String literals.

Parameters:

  • s (String): the String literal to encode.
Rlp.encode "dog"
# => Bytes[131, 100, 111, 103]

[View source]
def self.encode(i : Int) #

RLP-encodes scalar Int numbers.

Parameters:

  • i (Int): the scalar Int number to encode.
Rlp.encode 1_000_000
# => Bytes[131, 15, 66, 64]

[View source]
def self.encode(c : Char) #

RLP-encodes Char characters.

Parameters:

  • c (Char): the Char character to encode.
Rlp.encode 'x'
# => Bytes[120]

[View source]
def self.encode(o : Bool) #

RLP-encodes boolean Bool values.

Parameters:

  • o (Bool): the boolean Bool value to encode.
Rlp.encode true
# => Bytes[1]

[View source]