ciri-rlp 0.2.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -6
- data/lib/ciri/rlp/decode.rb +4 -4
- data/lib/ciri/rlp/encode.rb +2 -2
- data/lib/ciri/rlp/serializable.rb +15 -27
- data/lib/ciri/rlp/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d767ee2b245808b3803b1de37c0560e8180b3ca4f5346caf268e0abf90d2b85
|
4
|
+
data.tar.gz: 4f7787004fdc1d205c9667cfa1b099291948996c37eae8fe4bdae00d6dc03f6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 007ee152a9ba605c2499c8f7f55e204ce0674626de104985c300073f0ed135794cf6339616ca0fa7cfae5de80017bb2752e3dd93807ea7e8c15b969abc96ce0a
|
7
|
+
data.tar.gz: 727f98939b3c91f644cddbbcd94d5782c4fbf55659babddfe8441f997d4c0885578fb6d82acb0eba2ebc76574883ea79085b6f7cd7b162a6c8c8a075bef31ebf
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -34,17 +34,18 @@ Structure:
|
|
34
34
|
```ruby
|
35
35
|
# declare a RLP structure
|
36
36
|
class Doge
|
37
|
+
include Ciri
|
37
38
|
include Ciri::RLP::Serializable
|
38
39
|
|
39
40
|
# RLP is a low level encoding format, only support string(bytes) and list.
|
40
41
|
# so we use a list to present structure data
|
41
42
|
# we also need to declare column type clearly if column is not string
|
42
|
-
schema
|
43
|
-
:name,
|
44
|
-
|
45
|
-
:
|
46
|
-
|
47
|
-
|
43
|
+
schema(
|
44
|
+
name: RLP::Bytes, # declare name a Bytes type, represent as normal ruby string
|
45
|
+
age: Integer, # declare age as a Integer type
|
46
|
+
gender: RLP::Bytes,
|
47
|
+
has_master: Ciri::RLP::Bool # dechare bool
|
48
|
+
)
|
48
49
|
end
|
49
50
|
|
50
51
|
doge = Doge.new(name: 'neo doge', age: 5, gender: "boy", has_master: false)
|
data/lib/ciri/rlp/decode.rb
CHANGED
@@ -73,13 +73,13 @@ module Ciri
|
|
73
73
|
i += 1
|
74
74
|
end
|
75
75
|
end
|
76
|
-
elsif type ==
|
76
|
+
elsif type == Bytes
|
77
77
|
str = decode_stream(s)
|
78
|
-
raise RLP::InvalidError.new "decode #{str.class} from
|
78
|
+
raise RLP::InvalidError.new "decode #{str.class} from Bytes" unless str.is_a?(String)
|
79
79
|
str
|
80
|
-
elsif type ==
|
80
|
+
elsif type == List
|
81
81
|
list = decode_stream(s)
|
82
|
-
raise RLP::InvalidError.new "decode #{list.class} from
|
82
|
+
raise RLP::InvalidError.new "decode #{list.class} from List" unless list.is_a?(Array)
|
83
83
|
list
|
84
84
|
elsif type == Raw
|
85
85
|
decode_stream(s)
|
data/lib/ciri/rlp/encode.rb
CHANGED
@@ -74,10 +74,10 @@ module Ciri
|
|
74
74
|
end
|
75
75
|
elsif type == Raw
|
76
76
|
encode_raw(item)
|
77
|
-
elsif type ==
|
77
|
+
elsif type == Bytes
|
78
78
|
raise RLP::InvalidError.new "expect String, got #{item.class}" unless item.is_a?(String)
|
79
79
|
encode_raw(item)
|
80
|
-
elsif type ==
|
80
|
+
elsif type == List
|
81
81
|
raise RLP::InvalidError.new "expect Array, got #{item.class}" unless item.is_a?(Array)
|
82
82
|
encode_raw(item)
|
83
83
|
else
|
@@ -30,24 +30,21 @@ module Ciri
|
|
30
30
|
class Raw
|
31
31
|
end
|
32
32
|
|
33
|
+
# represent Bytes
|
33
34
|
class Bytes
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
# represent List
|
38
38
|
class List
|
39
39
|
end
|
40
40
|
|
41
|
-
RawList = List
|
42
|
-
|
43
41
|
# Serializable module allow ruby objects serialize/deserialize to or from RLP encoding.
|
44
42
|
# See Ciri::RLP::Serializable::TYPES for supported type.
|
45
43
|
#
|
46
44
|
# schema method define ordered data structure for class, and determine how to encoding objects.
|
47
45
|
#
|
48
46
|
# schema follow `{attr_name: type}` format,
|
49
|
-
#
|
50
|
-
# schema simple types include Integer, Bool, String, Array...
|
47
|
+
# schema support simple types: Integer, RLP::Bool, RLP::Bytes, RLP::List...
|
51
48
|
#
|
52
49
|
# schema also support complex types: array and serializable.
|
53
50
|
#
|
@@ -59,21 +56,22 @@ module Ciri
|
|
59
56
|
#
|
60
57
|
# class AuthMsgV4
|
61
58
|
# include Ciri::RLP::Serializable
|
59
|
+
# include Ciri
|
62
60
|
#
|
63
61
|
# # define schema
|
64
|
-
# schema
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
62
|
+
# schema(
|
63
|
+
# signature: RLP::Bytes, # raw type: string
|
64
|
+
# initiator_pubkey: MySerializableKey, # this attr is a RLP serializable object
|
65
|
+
# nonce: [Integer],
|
66
|
+
# version: Integer
|
67
|
+
# )
|
70
68
|
#
|
71
69
|
# # default values
|
72
|
-
# default_data(
|
70
|
+
# default_data(version: 1)
|
73
71
|
# end
|
74
72
|
#
|
75
73
|
# msg = AuthMsgV4.new(signature: "\x00", initiator_pubkey: my_pubkey, nonce: [1, 2, 3], version: 4)
|
76
|
-
# encoded =
|
74
|
+
# encoded = AuthMsgV4.rlp_encode(msg)
|
77
75
|
# msg2 = AuthMsgV4.rlp_decode(encoded)
|
78
76
|
# msg == msg2 # true
|
79
77
|
#
|
@@ -93,24 +91,16 @@ module Ciri
|
|
93
91
|
# keys return data columns array
|
94
92
|
attr_reader :keys
|
95
93
|
|
96
|
-
KeySchema = Struct.new(:type,
|
94
|
+
KeySchema = Struct.new(:type, keyword_init: true)
|
97
95
|
|
98
96
|
def initialize(schema)
|
99
97
|
keys = []
|
100
98
|
@_schema = {}
|
101
99
|
|
102
|
-
schema.each do |key|
|
103
|
-
if key.is_a?(Hash)
|
104
|
-
options = [:optional].map {|o| [o, key.delete(o)]}.to_h
|
105
|
-
raise InvalidSchemaError.new("include unknown options #{key}") unless key.size == 1
|
106
|
-
key, type = key.to_a[0]
|
107
|
-
else
|
108
|
-
options = {}
|
109
|
-
type = Raw
|
110
|
-
end
|
100
|
+
schema.each do |key, type|
|
111
101
|
raise InvalidSchemaError.new("incorrect type on key #{key}, #{type} is not a valid RLP class") unless check_key_type(type)
|
112
102
|
keys << key
|
113
|
-
@_schema[key] = KeySchema.new(type: type
|
103
|
+
@_schema[key] = KeySchema.new(type: type)
|
114
104
|
end
|
115
105
|
|
116
106
|
@_schema.freeze
|
@@ -141,7 +131,6 @@ module Ciri
|
|
141
131
|
data_list = []
|
142
132
|
used_keys.each do |key|
|
143
133
|
value = data[key]
|
144
|
-
next if value.nil? && self[key].options[:optional]
|
145
134
|
data_list << encode_with_type(value, self[key].type)
|
146
135
|
end
|
147
136
|
encode_list(data_list)
|
@@ -151,7 +140,6 @@ module Ciri
|
|
151
140
|
values = decode_list(input) do |list, stream|
|
152
141
|
keys.each do |key|
|
153
142
|
# decode data by type
|
154
|
-
next if stream.eof? && self[key].options[:optional]
|
155
143
|
list << decode_with_type(stream, self[key].type)
|
156
144
|
end
|
157
145
|
end
|
data/lib/ciri/rlp/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ciri-rlp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jiang Jinyang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ciri-utils
|