Module: Ciri::RLP

Defined in:
lib/ciri/rlp.rb,
lib/ciri/rlp/decode.rb,
lib/ciri/rlp/encode.rb,
lib/ciri/rlp/serializable.rb

Defined Under Namespace

Modules: Decode, Encode, Serializable Classes: Bool, InvalidValueError

Class Method Summary collapse

Class Method Details

.decode(input, type = nil) ⇒ Object

Decode input from rlp encoding, only produce string or array

Examples:

Ciri::RLP.decode(input)

41
42
43
44
45
46
47
# File 'lib/ciri/rlp.rb', line 41

def decode(input, type = nil)
  output = Decode.decode(input)
  if type
    output = decode_with_type(output, type)
  end
  output
end

.decode_with_type(item, type) ⇒ Object

Use this method after RLP.decode, decode values from string or array to specific types see Ciri::RLP::Serializable::TYPES for supported types

Examples:

item = Ciri::RLP.decode(encoded_text)
number = Ciri::RLP.decode_with_type(item, :int)

82
83
84
# File 'lib/ciri/rlp.rb', line 82

def decode_with_type(item, type)
  Serializable.decode_with_type(item, type)
end

.encode(input, type = nil) ⇒ Object

Encode input to rlp encoding, only allow string or array

Examples:

Ciri::RLP.encode("hello world")

55
56
57
58
59
60
# File 'lib/ciri/rlp.rb', line 55

def encode(input, type = nil)
  if type
    input = encode_with_type(input, type)
  end
  Encode.encode(input)
end

.encode_with_type(item, type, zero: '') ⇒ Object

Use this method before RLP.encode, this method encode ruby objects to rlp friendly format, string or array. see Ciri::RLP::Serializable::TYPES for supported types

Examples:

item = Ciri::RLP.encode_with_type(number, :int, zero: "\x00".b)
encoded_text = Ciri::RLP.encode(item)

70
71
72
# File 'lib/ciri/rlp.rb', line 70

def encode_with_type(item, type, zero: '')
  Serializable.encode_with_type(item, type, zero: zero)
end