module Ciri::RLP::Serializable
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 `{attr_name: 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 `{attr_name: [type]}`, for example: `{bills: [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
Constants
- TYPES
nil represent
RLP
raw value(string or array of string)
Attributes
serializable_attributes[R]
Public Class Methods
included(base)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 215 def included(base) base.send :extend, ClassMethods end
new(**data)
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 237 def ==(other) self.class == other.class && serializable_attributes == other.serializable_attributes end
initialize_copy(orig)
click to toggle source
Calls superclass method
# 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)
click to toggle source
Encode
object to rlp encoding string
# 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