Class: Dropzone::MessageBase

Inherits:
RecordBase show all
Defined in:
lib/dropzone/message_base.rb

Direct Known Subclasses

Buyer, Communication, Invoice, Item, Payment, Seller

Constant Summary collapse

DEFAULT_TIP =
20_000

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RecordBase

#blockchain, #errors, #valid?

Constructor Details

#initialize(attrs = {}) ⇒ MessageBase

Returns a new instance of MessageBase.



30
31
32
33
34
35
36
# File 'lib/dropzone/message_base.rb', line 30

def initialize(attrs = {})
  data = attrs.delete(:data)

  attrs.merge(data_hash_from_hex(data)).each do |attr, value|
    instance_variable_set '@%s' % attr, value
  end
end

Class Attribute Details

.default_tipObject



114
115
116
# File 'lib/dropzone/message_base.rb', line 114

def default_tip
  @default_tip || DEFAULT_TIP
end

Instance Attribute Details

#block_heightObject (readonly)

Returns the value of attribute block_height.



28
29
30
# File 'lib/dropzone/message_base.rb', line 28

def block_height
  @block_height
end

#message_typeObject (readonly)

Returns the value of attribute message_type.



28
29
30
# File 'lib/dropzone/message_base.rb', line 28

def message_type
  @message_type
end

#receiver_addrObject (readonly)

Returns the value of attribute receiver_addr.



28
29
30
# File 'lib/dropzone/message_base.rb', line 28

def receiver_addr
  @receiver_addr
end

#sender_addrObject (readonly)

Returns the value of attribute sender_addr.



28
29
30
# File 'lib/dropzone/message_base.rb', line 28

def sender_addr
  @sender_addr
end

#txidObject (readonly)

Returns the value of attribute txid.



28
29
30
# File 'lib/dropzone/message_base.rb', line 28

def txid
  @txid
end

Class Method Details

.attr_message(attribs) ⇒ Object



136
137
138
139
140
141
# File 'lib/dropzone/message_base.rb', line 136

def attr_message(attribs)
  @message_attribs ||= {}
  @message_attribs.merge! attribs 

  attribs.each{ |short_attr, full_attr| attr_reader full_attr }
end

.attr_message_int(attribs) ⇒ Object



143
144
145
146
147
148
# File 'lib/dropzone/message_base.rb', line 143

def attr_message_int(attribs)
  @message_integers ||= []
  @message_integers += attribs.keys

  attr_message attribs
end

.attr_message_pkey(attribs) ⇒ Object



150
151
152
153
154
155
# File 'lib/dropzone/message_base.rb', line 150

def attr_message_pkey(attribs)
  @message_pkeys ||= []
  @message_pkeys += attribs.keys

  attr_message attribs
end

.find(txid) ⇒ Object



157
158
159
160
# File 'lib/dropzone/message_base.rb', line 157

def find(txid)
  tx = RecordBase.blockchain.tx_by_id txid
  tx ? self.new(tx) : nil
end

.is_attr_int?(attr) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/dropzone/message_base.rb', line 122

def is_attr_int?(attr)
  @message_integers && @message_integers.include?(attr)
end

.is_attr_pkey?(attr) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/dropzone/message_base.rb', line 126

def is_attr_pkey?(attr)
  @message_pkeys && @message_pkeys.include?(attr)
end

.message_attribsObject



118
119
120
# File 'lib/dropzone/message_base.rb', line 118

def message_attribs
  @message_attribs
end

.message_type(type) ⇒ Object



130
131
132
133
134
# File 'lib/dropzone/message_base.rb', line 130

def message_type(type)
  @types_include ||= [type]

  define_method(:message_type){ type }
end

.new_message_from(tx) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/dropzone/message_base.rb', line 166

def new_message_from(tx)
  @messages ||= ObjectSpace.each_object(Class).select {|klass| 
    klass < Dropzone::MessageBase }

  if /\A([a-z0-9]{6})/i.match tx[:data]
    message_klass = @messages.find{|klass| klass.types_include? $1}
    (message_klass) ? message_klass.new(tx) : nil
  end
end

.types_include?(type) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/dropzone/message_base.rb', line 162

def types_include?(type)
  @types_include && @types_include.include?(type)
end

Instance Method Details

#data_to_hashObject



67
68
69
70
71
# File 'lib/dropzone/message_base.rb', line 67

def data_to_hash
  self.class.message_attribs.inject({}) do |ret , (short, full)|
    ret.merge(short => self.send(full))
  end
end

#data_to_hexObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dropzone/message_base.rb', line 47

def data_to_hex
  data_to_hash.inject(message_type.dup) do |ret, (key, value)|
    value_hex = case
      when value.nil?
        nil
      when self.class.is_attr_int?(key)
        Bitcoin::Protocol.pack_var_int(value.to_i)
      when self.class.is_attr_pkey?(key)
        Bitcoin::Protocol.pack_var_string(
          (value == 0) ? 0.chr : 
            [anynet_for_address(:hash160_from_address, value)].pack('H*'))
      else
        Bitcoin::Protocol.pack_var_string([value.to_s].pack('a*'))
    end 

    (value_hex.nil?) ? ret : 
      ret << Bitcoin::Protocol.pack_var_string(key.to_s) << value_hex
  end
end

#save!(private_key) ⇒ Object



38
39
40
# File 'lib/dropzone/message_base.rb', line 38

def save!(private_key)
  self.blockchain.save! to_transaction, private_key
end

#to_transactionObject



42
43
44
45
# File 'lib/dropzone/message_base.rb', line 42

def to_transaction
  {receiver_addr: receiver_addr, data: data_to_hex, 
    tip: MessageBase.default_tip }
end