module Rlp::Util

Overview

Exposes a set of utilities to ease the handling of different data types. It comes in handy when building protocols further decoding RLP byte-streams.

decoded = Rlp.decode Bytes[197, 42, 131, 101, 116, 104]
pp decoded
# => [Bytes[42], Bytes[101, 116, 104]]

protocol = [] of String | Int32 | BigInt
protocol << Rlp::Util.bin_to_int decoded[0]
protocol << Rlp::Util.bin_to_str decoded[1]
pp protocol
# => [42, "eth"]

Defined in:

util.cr

Class Method Summary

Class Method Detail

def self.bin_to_hex(b : Bytes) #

Converts binary Bytes to a hex-encoded String.

Parameters:

  • b (Bytes): the binary Bytes data to convert.
Rlp::Util.bin_to_hex Bytes[4, 200, 29]
# => "04c81d"

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

Converts binary Bytes to a BigInt.

Parameters:

  • b (Bytes): the binary Bytes data to convert.
Rlp::Util.bin_to_int Bytes[15, 66, 64]
# => 1000000

[View source]
def self.bin_to_int(a) #

Overloads .bin_to_int with arbitrary data types and raises if input data is not binary.

NOTE Raises in any case if a actually contains non-binary or nested data. Shouldn't be used if decoded Rlp data could contain nested data structures.


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

Converts binary Bytes to a String literal.

Parameters:

  • b (Bytes): the binary Bytes data to convert.
Rlp::Util.bin_to_str Bytes[97, 98, 99]
# => "abc"

[View source]
def self.bin_to_str(a) #

Overloads .bin_to_str with arbitrary data types and raises if input data is not binary.

NOTE Raises in any case if a actually contains non-binary or nested data. Shouldn't be used if decoded Rlp data could contain nested data structures.


[View source]
def self.binary_add(a : Bytes, b : Bytes) #

Concatenates two Bytes slices of UInt8.

a = Bytes[131]
b = Bytes[97, 98, 99]
Rlp::Util.binary_add a, b
# => Bytes[131, 97, 98, 99]

[View source]
def self.hex_to_bin(h : String) #

Converts hex-encoded Strings to binary Bytes data.

Parameters:

  • h (String): the hex-encoded String to convert.
Rlp::Util.hex_to_bin "04c81d"
# => Bytes[4, 200, 29]

[View source]
def self.hex_to_int(h : String) #

Converts hex-encoded Strings to BigInts.

Parameters:

  • h (String): the hex-encoded String to convert.
Rlp::Util.hex_to_int "04c81d"
# => 313373

[View source]
def self.hex_to_str(h : String) #

Converts hex-encoded Strings to String literals.

Parameters:

  • h (String): the hex-encoded String to convert.
Rlp::Util.hex_to_str "646f67"
# => "dog"

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

Converts integers to binary Bytes.

Parameters:

  • i (Int): the integer to convert.
Rlp::Util.int_to_bin 1_000_000
# => Bytes[15, 66, 64]

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

Converts integers to hex-encoded Strings.

Parameters:

  • i (Int): the integer to convert.
Rlp::Util.int_to_hex 313_373
# => "04c81d"

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

Converts String literals to binary Bytes data.

Parameters:

  • s (String): the String literal to convert.
Rlp::Util.str_to_bin "abc"
# => Bytes[97, 98, 99]

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

Converts String literals to hex-encoded Strings.

Parameters:

  • s (String): the String literal to convert.
Rlp::Util.str_to_hex "dog"
# => "646f67"

[View source]