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, schema support simple types: Integer, RLP::Bool, RLP::Bytes, RLP::List…
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
include Ciri
# define schema
schema(
signature: RLP::Bytes, # raw type: string
initiator_pubkey: MySerializableKey, # this attr is a RLP serializable object
nonce: [Integer],
version: Integer
)
# default values
default_data(version: 1)
end
msg = AuthMsgV4.new(signature: "\x00", initiator_pubkey: my_pubkey, nonce: [1, 2, 3], version: 4)
encoded = AuthMsgV4.rlp_encode(msg)
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, Bytes, List, 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.
220 221 222 |
# File 'lib/ciri/rlp/serializable.rb', line 220 def serializable_attributes @serializable_attributes end |
Class Method Details
.included(base) ⇒ Object
215 216 217 |
# File 'lib/ciri/rlp/serializable.rb', line 215 def included(base) base.send :extend, ClassMethods end |
Instance Method Details
#==(other) ⇒ Object
237 238 239 |
# File 'lib/ciri/rlp/serializable.rb', line 237 def ==(other) self.class == other.class && serializable_attributes == other.serializable_attributes end |
#initialize(**data) ⇒ Object
222 223 224 225 |
# File 'lib/ciri/rlp/serializable.rb', line 222 def initialize(**data) @serializable_attributes = (self.class.default_data || {}).merge(data) self.class.schema.validate!(@serializable_attributes) end |
#initialize_copy(orig) ⇒ Object
227 228 229 230 |
# File 'lib/ciri/rlp/serializable.rb', line 227 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
233 234 235 |
# File 'lib/ciri/rlp/serializable.rb', line 233 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 |