Class: Dropzone::BitcoinConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/dropzone/connection.rb

Defined Under Namespace

Classes: WalletFundsTooLowOrNoUTXOs

Constant Summary collapse

SATOSHIS_IN_BTC =
100_000_000
TXO_DUST =

0.0000543 BTC

5430
PREFIX =
'DZ'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, options = {}) ⇒ BitcoinConnection



13
14
15
16
17
18
# File 'lib/dropzone/connection.rb', line 13

def initialize(network, options = {})
  @network = network
  @bitcoin = options[:bitcoin] if options.has_key? :bitcoin
  @is_testing = (/\Atestnet/.match network.to_s) ? true : false
  @bitcoin ||= BlockrIo.new is_testing?
end

Class Attribute Details

.cacheObject

This is a hook to speed up some operations via a local cache



310
311
312
# File 'lib/dropzone/connection.rb', line 310

def cache
  @cache
end

Instance Attribute Details

#bitcoinObject (readonly)

Returns the value of attribute bitcoin.



11
12
13
# File 'lib/dropzone/connection.rb', line 11

def bitcoin
  @bitcoin
end

Instance Method Details

#block_heightObject



191
192
193
# File 'lib/dropzone/connection.rb', line 191

def block_height
  bitcoin.getblockinfo('last')['nb']
end

#hash160_from_address(addr) ⇒ Object



32
33
34
35
# File 'lib/dropzone/connection.rb', line 32

def hash160_from_address(addr)
  set_network_mode!
  Bitcoin.hash160_from_address addr
end

#hash160_to_address(hash160) ⇒ Object



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

def hash160_to_address(hash160) 
  set_network_mode!
  Bitcoin.hash160_to_address hash160
end

#is_testing?Boolean



20
# File 'lib/dropzone/connection.rb', line 20

def is_testing?; @is_testing; end

#messages_by_addr(addr, options = {}) ⇒ Object

NOTE:

- This needs to return the messages in Descending order by block
  In the case that two transactions are in the same block, it goes by time
- This should only 'valid' return messages. Not transactions


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

def messages_by_addr(addr, options = {})
  ret = cache.listtransactions addr, true, is_testing? if cache

  unless ret
    ret = bitcoin.listtransactions addr, true
    cache.listtransactions! addr, true, ret, is_testing? if cache
  end

  ret = ret.collect{ |tx_h|
    begin
      msg = Dropzone::MessageBase.new_message_from tx_by_id(tx_h['tx'])

      (msg && msg.valid?) ? msg : nil
    rescue Counterparty::TxDecode::InvalidOutput,
      Counterparty::TxDecode::MultisigUnsupported,
      Counterparty::TxDecode::UndefinedBehavior
      next
    end
  }.compact

  filter_messages ret, options
end

#messages_in_block(at_height, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dropzone/connection.rb', line 69

def messages_in_block(at_height, options = {})
  ret = bitcoin.getblock(at_height).collect{ |tx_h|

    # This is a speed hack that drastically reduces query times:
    next if options[:type] == 'ITCRTE' && !tx_h_create?(tx_h)

    begin
      msg = Dropzone::MessageBase.new_message_from tx_by_id( tx_h['tx'], 
        block_height: at_height )

      (msg && msg.valid?) ? msg : nil
    rescue Counterparty::TxDecode::InvalidOutput,
      Counterparty::TxDecode::MultisigUnsupported,
      Counterparty::TxDecode::UndefinedBehavior,
      Counterparty::TxDecode::InvalidOpReturn
      next
    end
  }.compact

  filter_messages ret, options
end

#privkey_to_addr(key) ⇒ Object



22
23
24
25
# File 'lib/dropzone/connection.rb', line 22

def privkey_to_addr(key) 
  set_network_mode!
  Bitcoin::Key.from_base58(key).addr
end

#save!(data, private_key_wif) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/dropzone/connection.rb', line 156

def save!(data, private_key_wif)
  set_network_mode!

  from_key = Bitcoin::Key.from_base58 private_key_wif
  
  # We need to know how many transactions we'll have in order to know how
  # many satoshi's to allocate. We start with 1, since that's the return
  # address of the allocated input satoshis
  data_outputs_needed = 1

  bytes_in_output = Counterparty::TxEncode::BYTES_IN_MULTISIG

  # 3 is for the two-byte DZ prefix, and the 1-byte length
  data_outputs_needed += ((data[:data].length+3) / bytes_in_output).ceil

  # We'll need a P2PSH for the destination if that applies
  data_outputs_needed += 1 if data.has_key? :receiver_addr
  tip = data[:tip] || 0

  new_tx = create_tx(from_key,data_outputs_needed * TXO_DUST+tip) do |tx, amount_allocated|
    outputs = Counterparty::TxEncode.new( 
      [tx.inputs[0].prev_out.reverse_hth].pack('H*'),
      data[:data], receiver_addr: data[:receiver_addr],
      sender_pubkey: from_key.pub, prefix: PREFIX).to_opmultisig

    outputs.each_with_index do |output,i|
      tx.add_out Bitcoin::P::TxOut.new( (i == (outputs.length-1)) ? 
        (amount_allocated - tip - TXO_DUST*(outputs.length - 1)) : TXO_DUST,
        Bitcoin::Script.binary_from_string(output) )
    end
  end

  sign_and_send new_tx, from_key
end

#send_value(from_key, to_addr, send_satoshis, tip_satoshis) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dropzone/connection.rb', line 91

def send_value(from_key, to_addr, send_satoshis, tip_satoshis)
  set_network_mode!

  new_tx = create_tx(from_key, send_satoshis+tip_satoshis){ |tx, allocated|
    [ [send_satoshis, Bitcoin.hash160_from_address(to_addr)],
      [(allocated-send_satoshis-tip_satoshis), from_key.hash160]
    ].each do |(amt, to)|
      tx.add_out Bitcoin::P::TxOut.new( amt,
        Bitcoin::Script.to_hash160_script(to) )
    end
  }

  sign_and_send new_tx, from_key
end

#sign_tx(tx, key) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dropzone/connection.rb', line 106

def sign_tx(tx, key)
  set_network_mode!

  # Sign the transaction:
  tx.inputs.length.times do |i|
    # Fetch the previous input:
    prev_out_tx_hash = tx.inputs[i].prev_out.reverse.unpack('H*').first
    prev_tx_raw = bitcoin.getrawtransaction(prev_out_tx_hash)['hex']
    prev_tx = Bitcoin::P::Tx.new [prev_tx_raw].pack('H*')
    
    # Now we actually sign
    sig = Bitcoin.sign_data Bitcoin.open_key(key.priv), 
      tx.signature_hash_for_input(i, prev_tx)
    tx.in[i].script_sig = Bitcoin::Script.to_signature_pubkey_script( sig, 
      [key.pub].pack("H*"))
  end
  tx
end

#tx_by_id(id, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dropzone/connection.rb', line 125

def tx_by_id(id, options = {})
  set_network_mode!

  ret = cache.tx_by_id id, is_testing? if cache

  unless ret
    tx_h = bitcoin.getrawtransaction(id)
    tx = Bitcoin::P::Tx.new [tx_h['hex']].pack('H*')

    if tx_h.has_key? 'blockhash'
      options[:block_height] = bitcoin.getblockinfo(tx_h['blockhash'])['nb']
    end

    record = Counterparty::TxDecode.new tx,
      prefix: Dropzone::BitcoinConnection::PREFIX

    ret = options.merge({ data: record.data, 
      receiver_addr: record.receiver_addr, 
      txid: id,
      sender_addr: record.sender_addr})

    # NOTE that in the case of a reorg, this might have incorrect block
    # heights cached. It's probable that we can/should cache these, and 
    # merely set the block height when it's confirmed, and/or set the
    # height to current_height+1
    cache.tx_by_id! id, ret, is_testing? if cache && options[:block_height]
  end

  ret
end

#valid_address?(addr) ⇒ Boolean



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

def valid_address?(addr) 
  set_network_mode!
  Bitcoin.valid_address? addr
end