Module: Ciri::RLP::Serializable
- Defined in:
- lib/ciri/rlp/serializable.rb
Overview
Serializable module allow ruby objects serialize/deserialize to or from RLP encoding. See Ciri::RLP::Serializable::TYPES for supported type.
schema method define ordered data structure for class, and determine how to encoding objects.
schema follow ‘type` format, if attr is raw type(string or array of string), you can just use `:attr_name` to define it schema simple types include Integer, Bool, String, Array…
schema also support complex types: array and serializable.
array types represented as ‘[type]`, for example: `[Integer]` means value of bill attr is an array of integer serializable type represent value of attr is a RLP serializable object
Examples:
class AuthMsgV4
include Ciri::RLP::Serializable
# define schema
schema [
:signature, # raw type: string
{initiator_pubkey: MySerializableKey}, # this attr is a RLP serializable object
{nonce: [Integer]},
{version: Integer}
]
# default values
default_data(got_plain: false)
end
msg = AuthMsgV4.new(signature: "\x00", initiator_pubkey: my_pubkey, nonce: [1, 2, 3], version: 4)
encoded = msg.rlp_encode
msg2 = AuthMsgV4.rlp_decode(encoded)
msg == msg2 # true
Defined Under Namespace
Modules: ClassMethods Classes: Schema
Constant Summary collapse
- TYPES =
nil represent RLP raw value(string or array of string)
[Raw, Integer, Bool].map {|key| [key, true]}.to_h.freeze
Instance Attribute Summary collapse
-
#serializable_attributes ⇒ Object
readonly
Returns the value of attribute serializable_attributes.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #initialize(**data) ⇒ Object
- #initialize_copy(orig) ⇒ Object
-
#rlp_encode(skip_keys: nil, white_list_keys: nil) ⇒ Object
Encode object to rlp encoding string.
Instance Attribute Details
#serializable_attributes ⇒ Object (readonly)
Returns the value of attribute serializable_attributes.
218 219 220 |
# File 'lib/ciri/rlp/serializable.rb', line 218 def serializable_attributes @serializable_attributes end |
Class Method Details
.included(base) ⇒ Object
213 214 215 |
# File 'lib/ciri/rlp/serializable.rb', line 213 def included(base) base.send :extend, ClassMethods end |
Instance Method Details
#==(other) ⇒ Object
235 236 237 |
# File 'lib/ciri/rlp/serializable.rb', line 235 def ==(other) self.class == other.class && serializable_attributes == other.serializable_attributes end |
#initialize(**data) ⇒ Object
220 221 222 223 |
# File 'lib/ciri/rlp/serializable.rb', line 220 def initialize(**data) @serializable_attributes = (self.class.default_data || {}).merge(data) self.class.schema.validate!(@serializable_attributes) end |
#initialize_copy(orig) ⇒ Object
225 226 227 228 |
# File 'lib/ciri/rlp/serializable.rb', line 225 def initialize_copy(orig) super @serializable_attributes = orig.serializable_attributes.dup end |
#rlp_encode(skip_keys: nil, white_list_keys: nil) ⇒ Object
Encode object to rlp encoding string
231 232 233 |
# File 'lib/ciri/rlp/serializable.rb', line 231 def rlp_encode(skip_keys: nil, white_list_keys: nil) self.class.rlp_encode(self, skip_keys: skip_keys, white_list_keys: white_list_keys) end |