rlp-lite 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +60 -2
- data/Rakefile +8 -0
- data/lib/rlp-lite/decoder.rb +6 -5
- data/lib/rlp-lite/encoder.rb +20 -13
- data/lib/rlp-lite/sedes/big_endian_int.rb +2 -2
- data/lib/rlp-lite/sedes/binary.rb +18 -40
- data/lib/rlp-lite/sedes/list.rb +5 -5
- data/lib/rlp-lite/sedes.rb +18 -33
- data/lib/rlp-lite/util.rb +24 -5
- data/lib/rlp-lite/version.rb +1 -1
- data/lib/rlp-lite.rb +14 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ebdf026e59948e13d720bef7940d7b7235cd9f03ae9e8327431c0cbb71cf041
|
4
|
+
data.tar.gz: 49f5ea7e8a42793686d997919dd546f91578be192da69c85ac57558591309997
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56e0042122a25d23770570cb7837d67d31acc511957bd8c8e42e6b3d9e491dc52e7691909cc50d1d286118080b8429ab933f966ca8cb49116a2f9bce7070d9cd
|
7
|
+
data.tar.gz: 42f3bb460973d8cb111f1bcb3d7b10a36eb5ca1921a8eebbf575f90c3ed4f6c66d82146887675c81f09e1a292ff98657389d2db966ac770b38032f9d8221a225
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Recursive Length Prefix (RLP) Lite
|
2
2
|
|
3
3
|
|
4
|
-
rlp-lite - light-weight machinery to serialize / deserialze via rlp
|
4
|
+
rlp-lite - light-weight machinery to serialize / deserialze via rlp (recursive length prefix)
|
5
5
|
|
6
6
|
|
7
7
|
|
@@ -15,9 +15,67 @@ rlp-lite - light-weight machinery to serialize / deserialze via rlp
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
-
to be done
|
19
18
|
|
19
|
+
``` ruby
|
20
|
+
require 'rlp-lite'
|
20
21
|
|
22
|
+
######
|
23
|
+
## encode
|
24
|
+
|
25
|
+
list = ['ruby', 'rlp', 255]
|
26
|
+
|
27
|
+
encoded = Rlp.encode( list )
|
28
|
+
#=> "\xCB\x84ruby\x83rlp\x81\xFF".b
|
29
|
+
|
30
|
+
#######
|
31
|
+
## decode
|
32
|
+
|
33
|
+
decoded = Rlp.decode( "\xCB\x84ruby\x83rlp\x81\xFF".b )
|
34
|
+
#=> ["ruby", "rlp", "\xFF".b]
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
Note: All integers get returned (decoded) as big integers in binary buffers (that is, string with binary "ASCII-8BIT" encoding)
|
40
|
+
e.g. `"\xFF".b` and not `255`.
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
More examples from the official ethereum rlp tests,
|
45
|
+
see [test/data/rlptest.json](test/data/rlptest.json).
|
46
|
+
|
47
|
+
|
48
|
+
``` ruby
|
49
|
+
######
|
50
|
+
# lists of lists
|
51
|
+
obj = [ [ [], [] ], [] ]
|
52
|
+
encoded = Rlp.encode( obj )
|
53
|
+
#=> "\xC4\xC2\xC0\xC0\xC0".b
|
54
|
+
|
55
|
+
decoded = Rlp.decode( "\xC4\xC2\xC0\xC0\xC0".b )
|
56
|
+
#=> [[[], []], []]
|
57
|
+
|
58
|
+
# or using a hex string (not a binary buffer)
|
59
|
+
decoded = Rlp.decode( "0xc4c2c0c0c0" )
|
60
|
+
#=> [[[], []], []]
|
61
|
+
|
62
|
+
|
63
|
+
#####
|
64
|
+
# dict(onary)
|
65
|
+
obj = [["key1", "val1"],
|
66
|
+
["key2", "val2"],
|
67
|
+
["key3", "val3"],
|
68
|
+
["key4", "val4"]]
|
69
|
+
encoded = Rlp.encode( obj )
|
70
|
+
#=> "\xEC\xCA\x84key1\x84val1\xCA\x84key2\x84val2\xCA\x84key3\x84val3\xCA\x84key4\x84val4".b
|
71
|
+
|
72
|
+
decoded = Rlp.decode( "\xEC\xCA\x84key1\x84val1\xCA\x84key2\x84val2\xCA\x84key3\x84val3\xCA\x84key4\x84val4".b )
|
73
|
+
#=> [["key1", "val1"], ["key2", "val2"], ["key3", "val3"], ["key4", "val4"]]
|
74
|
+
|
75
|
+
# or using a hex string (not a binary buffer)
|
76
|
+
decoded = Rlp.decode( "0xecca846b6579318476616c31ca846b6579328476616c32ca846b6579338476616c33ca846b6579348476616c34" )
|
77
|
+
#=> [["key1", "val1"], ["key2", "val2"], ["key3", "val3"], ["key4", "val4"]]
|
78
|
+
```
|
21
79
|
|
22
80
|
|
23
81
|
## License
|
data/Rakefile
CHANGED
data/lib/rlp-lite/decoder.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module Rlp
|
2
2
|
|
3
|
+
|
3
4
|
# Provides an RLP-decoder.
|
4
|
-
|
5
|
-
extend self
|
5
|
+
class Decoder
|
6
6
|
|
7
7
|
# Decodes an RLP-encoded object.
|
8
8
|
#
|
9
9
|
# @param rlp [String] an RLP-encoded object.
|
10
10
|
# @return [Object] the decoded and maybe deserialized object.
|
11
|
-
# @raise [
|
11
|
+
# @raise [Rlp::DecodingError] if the input string does not end after
|
12
12
|
# the root item.
|
13
|
-
def perform(rlp)
|
14
|
-
rlp = Util.hex_to_bin( rlp )
|
13
|
+
def perform( rlp )
|
14
|
+
rlp = Util.hex_to_bin( rlp ) if Util.is_hex?( rlp )
|
15
15
|
rlp = Util.str_to_bytes( rlp )
|
16
16
|
begin
|
17
17
|
item, next_start = consume_item( rlp, 0 )
|
@@ -23,6 +23,7 @@
|
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
+
|
26
27
|
private
|
27
28
|
|
28
29
|
# Consume an RLP-encoded item from the given start.
|
data/lib/rlp-lite/encoder.rb
CHANGED
@@ -1,33 +1,40 @@
|
|
1
1
|
|
2
|
-
# Provides an recursive-length prefix (RLP) encoder and decoder.
|
3
2
|
module Rlp
|
4
3
|
|
4
|
+
|
5
5
|
# Provides an RLP-encoder.
|
6
|
-
|
7
|
-
|
6
|
+
class Encoder
|
7
|
+
|
8
8
|
|
9
9
|
# Encodes a Ruby object in RLP format.
|
10
10
|
#
|
11
11
|
# @param obj [Object] a Ruby object.
|
12
12
|
# @return [String] the RLP encoded item.
|
13
|
-
# @raise [
|
13
|
+
# @raise [Rlp::EncodingError] in the rather unlikely case that the item
|
14
14
|
# is too big to encode (will not happen).
|
15
|
-
# @raise [
|
16
|
-
def perform(obj)
|
15
|
+
# @raise [Rlp::SerializationError] if the serialization fails.
|
16
|
+
def perform( obj )
|
17
17
|
item = Sedes.infer(obj).serialize(obj)
|
18
18
|
result = encode_raw( item )
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
|
22
|
+
private
|
22
23
|
|
23
24
|
# Encodes the raw item.
|
24
|
-
def encode_raw(item)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
def encode_raw( item )
|
26
|
+
if item.instance_of?( Rlp::Data )
|
27
|
+
item
|
28
|
+
elsif Util.is_primitive?( item )
|
29
|
+
encode_primitive item
|
30
|
+
elsif Util.is_list?( item )
|
31
|
+
encode_list item
|
32
|
+
else
|
33
|
+
raise EncodingError "Cannot encode object of type #{item.class.name}"
|
34
|
+
end
|
29
35
|
end
|
30
36
|
|
37
|
+
|
31
38
|
# Encodes a single primitive.
|
32
39
|
def encode_primitive(item)
|
33
40
|
return Util.str_to_bytes item if item.size == 1 && item.ord < PRIMITIVE_PREFIX_OFFSET
|
@@ -56,4 +63,4 @@
|
|
56
63
|
end
|
57
64
|
end
|
58
65
|
end
|
59
|
-
|
66
|
+
end # module Rlp
|
@@ -24,7 +24,7 @@
|
|
24
24
|
raise SerializationError, "Can only serialize integers" unless obj.is_a?(Integer)
|
25
25
|
raise SerializationError, "Cannot serialize negative integers" if obj < 0
|
26
26
|
raise SerializationError, "Integer too large (does not fit in #{@size} bytes)" if @size && obj >= 256 ** @size
|
27
|
-
s = obj == 0 ? BYTE_EMPTY : _int_to_big_endian(obj)
|
27
|
+
s = obj == 0 ? BYTE_EMPTY : _int_to_big_endian( obj )
|
28
28
|
@size ? "#{BYTE_ZERO * [0, @size - s.size].max}#{s}" : s
|
29
29
|
end
|
30
30
|
|
@@ -38,7 +38,7 @@
|
|
38
38
|
raise DeserializationError, "Invalid serialization (wrong size)" if @size && serial.size != @size
|
39
39
|
raise DeserializationError, "Invalid serialization (not minimal length)" if !@size && serial.size > 0 && serial[0] == BYTE_ZERO
|
40
40
|
serial = serial || BYTE_ZERO
|
41
|
-
_big_endian_to_int(serial)
|
41
|
+
_big_endian_to_int( serial )
|
42
42
|
end
|
43
43
|
|
44
44
|
|
@@ -9,16 +9,18 @@
|
|
9
9
|
# @param l [Integer] the fixed size of the binary.
|
10
10
|
# @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
|
11
11
|
# @return [Eth::Rlp::Sedes::Binary] a serializable binary of fixed size.
|
12
|
-
def self.fixed_length(l, allow_empty: false)
|
13
|
-
new(min_length: l,
|
12
|
+
def self.fixed_length( l, allow_empty: false )
|
13
|
+
new( min_length: l,
|
14
|
+
max_length: l,
|
15
|
+
allow_empty: allow_empty )
|
14
16
|
end
|
15
17
|
|
16
18
|
# Checks wether the given object is of a valid binary type.
|
17
19
|
#
|
18
20
|
# @param obj [Object] the supposed binary item to check.
|
19
21
|
# @return [Boolean] true if valid.
|
20
|
-
def self.valid_type?(obj)
|
21
|
-
obj.instance_of? String
|
22
|
+
def self.valid_type?( obj )
|
23
|
+
obj.instance_of?( String )
|
22
24
|
end
|
23
25
|
|
24
26
|
|
@@ -27,7 +29,7 @@
|
|
27
29
|
# @param min_length [Integer] the minimum size of the binary.
|
28
30
|
# @param max_length [Integer] the maximum size of the binary.
|
29
31
|
# @param allow_empty [Boolean] indicator wether empty binaries should be allowed.
|
30
|
-
def initialize(min_length: 0, max_length: INFINITY, allow_empty: false)
|
32
|
+
def initialize( min_length: 0, max_length: INFINITY, allow_empty: false )
|
31
33
|
@min_length = min_length
|
32
34
|
@max_length = max_length
|
33
35
|
@allow_empty = allow_empty
|
@@ -39,13 +41,17 @@
|
|
39
41
|
# @return [Object] a serialized binary.
|
40
42
|
# @raise [SerializationError] if provided object is of invalid type.
|
41
43
|
# @raise [SerializationError] if provided binary is of invalid length.
|
42
|
-
def serialize(obj)
|
43
|
-
raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type? obj
|
44
|
-
|
45
|
-
|
44
|
+
def serialize( obj )
|
45
|
+
raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type?( obj )
|
46
|
+
|
47
|
+
## make sure string is with binary encoding (ASCII-8BIT)
|
48
|
+
## note: was Util.str_to_bytes( obj )
|
49
|
+
serial = obj.encoding.name == 'ASCII-8BIT' ? obj : obj.b
|
50
|
+
raise SerializationError, "Object has invalid length" unless valid_length?( serial.size )
|
46
51
|
serial
|
47
52
|
end
|
48
53
|
|
54
|
+
|
49
55
|
# Deserializes a binary.
|
50
56
|
#
|
51
57
|
# @param serial [Object] the serialized binary.
|
@@ -53,8 +59,8 @@
|
|
53
59
|
# @raise [DeserializationError] if provided serial is of wrong type.
|
54
60
|
# @raise [DeserializationError] if provided serial is of wrong length.
|
55
61
|
def deserialize(serial)
|
56
|
-
raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless
|
57
|
-
raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
|
62
|
+
raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless serial.instance_of?(String)
|
63
|
+
raise DeserializationError, "#{serial.class} has invalid length" unless valid_length?( serial.size )
|
58
64
|
serial
|
59
65
|
end
|
60
66
|
|
@@ -63,39 +69,11 @@
|
|
63
69
|
#
|
64
70
|
# @param length [Integer] the supposed length of the binary item.
|
65
71
|
# @return [Boolean] true if valid.
|
66
|
-
def valid_length?(length)
|
72
|
+
def valid_length?( length )
|
67
73
|
(@min_length <= length && length <= @max_length) ||
|
68
74
|
(@allow_empty && length == 0)
|
69
75
|
end
|
70
76
|
|
71
|
-
#######
|
72
|
-
# private helpers
|
73
|
-
|
74
|
-
# Converts a binary string to bytes.
|
75
|
-
#
|
76
|
-
# @param str [String] binary string to be converted.
|
77
|
-
# @return [Object] the string bytes.
|
78
|
-
def _str_to_bytes(str)
|
79
|
-
_is_bytes?(str) ? str : str.b
|
80
|
-
end
|
81
|
-
|
82
|
-
# Checks if a string is a byte-string.
|
83
|
-
#
|
84
|
-
# @param str [String] a string to check.
|
85
|
-
# @return [Boolean] true if it's an ASCII-8bit encoded byte-string.
|
86
|
-
def _is_bytes?(str)
|
87
|
-
str && str.instance_of?(String) && str.encoding.name == 'ASCII-8BIT'
|
88
|
-
end
|
89
|
-
|
90
|
-
# Checks if the given item is a string primitive.
|
91
|
-
#
|
92
|
-
# @param item [Object] the item to check.
|
93
|
-
# @return [Boolean] true if it's a string primitive.
|
94
|
-
def _is_primitive?(item)
|
95
|
-
item.instance_of?(String)
|
96
|
-
end
|
97
|
-
|
98
|
-
|
99
77
|
end # class Binary
|
100
78
|
|
101
79
|
|
data/lib/rlp-lite/sedes/list.rb
CHANGED
@@ -9,14 +9,14 @@
|
|
9
9
|
#
|
10
10
|
# @param elements [Array] an array indicating the structure of the list.
|
11
11
|
# @param strict [Boolean] an option to enforce the given structure.
|
12
|
-
def initialize(elements: [], strict: true)
|
12
|
+
def initialize( elements: [], strict: true )
|
13
13
|
super()
|
14
14
|
|
15
15
|
@strict = strict
|
16
16
|
elements.each do |e|
|
17
17
|
if Sedes.is_sedes?(e)
|
18
18
|
push e
|
19
|
-
elsif
|
19
|
+
elsif Util.is_list?(e)
|
20
20
|
push List.new(elements: e)
|
21
21
|
else
|
22
22
|
raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof."
|
@@ -30,8 +30,8 @@
|
|
30
30
|
# @return [Array] a serialized list.
|
31
31
|
# @raise [SerializationError] if provided array is not a sequence.
|
32
32
|
# @raise [SerializationError] if provided array is of wrong length.
|
33
|
-
def serialize(obj)
|
34
|
-
raise SerializationError, "Can only serialize sequences" unless
|
33
|
+
def serialize( obj )
|
34
|
+
raise SerializationError, "Can only serialize sequences" unless Util.is_list?(obj)
|
35
35
|
raise SerializationError, "List has wrong length" if (@strict && self.size != obj.size) || self.size < obj.size
|
36
36
|
result = []
|
37
37
|
obj.zip(self).each_with_index do |(element, sedes), i|
|
@@ -47,7 +47,7 @@
|
|
47
47
|
# @raise [DeserializationError] if provided serial is not a sequence.
|
48
48
|
# @raise [DeserializationError] if provided serial is of wrong length.
|
49
49
|
def deserialize(serial)
|
50
|
-
raise DeserializationError, "Can only deserialize sequences" unless
|
50
|
+
raise DeserializationError, "Can only deserialize sequences" unless Util.is_list?(serial)
|
51
51
|
raise DeserializationError, "List has wrong length" if @strict && serial.size != self.size
|
52
52
|
result = []
|
53
53
|
len = [serial.size, self.size].min
|
data/lib/rlp-lite/sedes.rb
CHANGED
@@ -3,8 +3,7 @@
|
|
3
3
|
module Rlp
|
4
4
|
module Sedes
|
5
5
|
|
6
|
-
# Provides a
|
7
|
-
class << self
|
6
|
+
# Provides a {Rlp::Sedes} module to infer objects and types.
|
8
7
|
|
9
8
|
# Tries to find a sedes objects suitable for a given Ruby object.
|
10
9
|
#
|
@@ -14,12 +13,18 @@
|
|
14
13
|
#
|
15
14
|
# @param obj [Object] the Ruby object for which to find a sedes object.
|
16
15
|
# @raise [TypeError] if no appropriate sedes could be found.
|
17
|
-
def infer(obj)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def self.infer( obj )
|
17
|
+
if is_sedes?( obj.class )
|
18
|
+
obj.class
|
19
|
+
elsif obj.is_a?(Integer) && obj >= 0
|
20
|
+
big_endian_int
|
21
|
+
elsif Binary.valid_type?( obj ) ## note: same as obj.is_a?( String )
|
22
|
+
binary
|
23
|
+
elsif Util.is_list?( obj )
|
24
|
+
List.new( elements: obj.map { |item| infer( item ) } )
|
25
|
+
else
|
26
|
+
raise TypeError, "Did not find sedes handling type #{obj.class.name}"
|
27
|
+
end
|
23
28
|
end
|
24
29
|
|
25
30
|
|
@@ -27,7 +32,7 @@
|
|
27
32
|
#
|
28
33
|
# @param obj [Object] the object to check.
|
29
34
|
# @return [Boolean] true if it's serializable and deserializable.
|
30
|
-
def is_sedes?(obj)
|
35
|
+
def self.is_sedes?(obj)
|
31
36
|
obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
|
32
37
|
end
|
33
38
|
|
@@ -35,39 +40,19 @@
|
|
35
40
|
# unspecified length.
|
36
41
|
#
|
37
42
|
# @return [Rlp::Sedes::BigEndianInt] a big-endian, unsigned integer sedes.
|
38
|
-
def big_endian_int
|
43
|
+
def self.big_endian_int
|
39
44
|
@big_endian_int ||= BigEndianInt.new
|
40
45
|
end
|
41
46
|
|
42
47
|
# A utility to use a binary sedes type.
|
43
48
|
#
|
44
49
|
# @return [Rlp::Sedes::Binary] a binary sedes.
|
45
|
-
def binary
|
50
|
+
def self.binary
|
46
51
|
@binary ||= Binary.new
|
47
52
|
end
|
48
53
|
|
49
|
-
##############################
|
50
|
-
### more helpers
|
51
54
|
|
52
|
-
# Checks if the given item is a string primitive.
|
53
|
-
#
|
54
|
-
# @param item [Object] the item to check.
|
55
|
-
# @return [Boolean] true if it's a string primitive.
|
56
|
-
def is_primitive?(item)
|
57
|
-
item.instance_of?(String)
|
58
|
-
end
|
59
55
|
|
60
|
-
# Checks if the given item is a list.
|
61
|
-
#
|
62
|
-
# @param item [Object] the item to check.
|
63
|
-
# @return [Boolean] true if it's a list.
|
64
|
-
def is_list?(item)
|
65
|
-
!is_primitive?(item) && item.respond_to?(:each)
|
66
|
-
end
|
67
56
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end # module Rlp
|
57
|
+
end # module Sedes
|
58
|
+
end # module Rlp
|
data/lib/rlp-lite/util.rb
CHANGED
@@ -11,8 +11,8 @@ module Util
|
|
11
11
|
# @param str [String] a string to be checked.
|
12
12
|
# @return [String] a match if true; `nil` if not.
|
13
13
|
def is_hex?(str)
|
14
|
-
return false unless str.is_a? String
|
15
|
-
str = remove_hex_prefix str
|
14
|
+
return false unless str.is_a?( String )
|
15
|
+
str = remove_hex_prefix( str )
|
16
16
|
str.match /\A[0-9a-fA-F]*\z/
|
17
17
|
end
|
18
18
|
|
@@ -21,7 +21,7 @@ module Util
|
|
21
21
|
# @param hex [String] a prefixed hex-string.
|
22
22
|
# @return [String] an unprefixed hex-string.
|
23
23
|
def remove_hex_prefix(hex)
|
24
|
-
return hex[2..-1] if is_prefixed? hex
|
24
|
+
return hex[2..-1] if is_prefixed?( hex )
|
25
25
|
return hex
|
26
26
|
end
|
27
27
|
|
@@ -39,9 +39,9 @@ module Util
|
|
39
39
|
# @param hex [String] a hexa-decimal string to be packed.
|
40
40
|
# @return [String] a packed binary string.
|
41
41
|
# @raise [TypeError] if value is not a string or string is not hex.
|
42
|
-
def hex_to_bin(hex)
|
42
|
+
def hex_to_bin( hex )
|
43
43
|
raise TypeError, "Value must be an instance of String" unless hex.instance_of? String
|
44
|
-
hex = remove_hex_prefix hex
|
44
|
+
hex = remove_hex_prefix( hex )
|
45
45
|
raise TypeError, "Non-hexadecimal digit found" unless is_hex? hex
|
46
46
|
[hex].pack("H*")
|
47
47
|
end
|
@@ -104,5 +104,24 @@ module Util
|
|
104
104
|
end
|
105
105
|
|
106
106
|
|
107
|
+
# Checks if the given item is a string primitive.
|
108
|
+
#
|
109
|
+
# @param item [Object] the item to check.
|
110
|
+
# @return [Boolean] true if it's a string primitive.
|
111
|
+
def is_primitive?( item )
|
112
|
+
item.instance_of?(String)
|
113
|
+
end
|
114
|
+
|
115
|
+
# Checks if the given item is a list.
|
116
|
+
#
|
117
|
+
# @param item [Object] the item to check.
|
118
|
+
# @return [Boolean] true if it's a list.
|
119
|
+
def is_list?( item )
|
120
|
+
!is_primitive?(item) && item.respond_to?(:each)
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
107
126
|
end # module Util
|
108
127
|
end # module Rlp
|
data/lib/rlp-lite/version.rb
CHANGED
data/lib/rlp-lite.rb
CHANGED
@@ -22,23 +22,14 @@ module Rlp
|
|
22
22
|
## todo/check - use encoding -ascii-8bit for source file or ? - why? why not?
|
23
23
|
## use #b/.b to ensure binary encoding? - why? why not?
|
24
24
|
BYTE_EMPTY = "".freeze # The empty byte is defined as "".
|
25
|
-
BYTE_ZERO
|
26
|
-
BYTE_ONE
|
25
|
+
BYTE_ZERO = "\x00".freeze # The zero byte is 0x00.
|
26
|
+
BYTE_ONE = "\x01".freeze # The byte one is 0x01.
|
27
27
|
|
28
28
|
|
29
|
-
|
30
|
-
# The RLP
|
31
|
-
|
32
|
-
|
33
|
-
# The RLP long length limit.
|
34
|
-
LONG_LENGTH_LIMIT = (256 ** 8)
|
35
|
-
|
36
|
-
# The RLP primitive type offset.
|
37
|
-
PRIMITIVE_PREFIX_OFFSET = 0x80
|
38
|
-
|
39
|
-
# The RLP array type offset.
|
40
|
-
LIST_PREFIX_OFFSET = 0xc0
|
41
|
-
|
29
|
+
SHORT_LENGTH_LIMIT = 56 # The RLP short length limit.
|
30
|
+
LONG_LENGTH_LIMIT = (256 ** 8) # The RLP long length limit.
|
31
|
+
PRIMITIVE_PREFIX_OFFSET = 0x80 # The RLP primitive type offset.
|
32
|
+
LIST_PREFIX_OFFSET = 0xc0 # The RLP array type offset.
|
42
33
|
|
43
34
|
# Infinity as constant for convenience.
|
44
35
|
INFINITY = (1.0 / 0.0)
|
@@ -63,18 +54,24 @@ module Rlp
|
|
63
54
|
class Data < String; end
|
64
55
|
|
65
56
|
|
57
|
+
def self.encoder() @encoder ||= Encoder.new; end
|
58
|
+
def self.decoder() @decoder ||= Decoder.new; end
|
59
|
+
|
60
|
+
|
66
61
|
# Performes an {Rlp::Encoder} on any ruby object.
|
67
62
|
#
|
68
63
|
# @param obj [Object] any ruby object.
|
69
64
|
# @return [String] a packed, RLP-encoded item.
|
70
|
-
def self.encode(obj)
|
65
|
+
def self.encode(obj) encoder.perform( obj ); end
|
71
66
|
|
72
67
|
|
73
68
|
# Performes an {Rlp::Decoder} on any RLP-encoded item.
|
74
69
|
#
|
75
70
|
# @param rlp [String] a packed, RLP-encoded item.
|
76
71
|
# @return [Object] a decoded ruby object.
|
77
|
-
def self.decode(rlp)
|
72
|
+
def self.decode(rlp) decoder.perform( rlp ); end
|
73
|
+
|
74
|
+
|
78
75
|
end # module Rlp
|
79
76
|
|
80
77
|
|