Class: OnChain::Sweeper

Inherits:
Object
  • Object
show all
Defined in:
lib/onchain/sweeper.rb

Class Method Summary collapse

Class Method Details

.sweep(paths, mpk, destination_address) ⇒ Object

With a bunch of HD wallet paths, build a transaction That pays all the coins to a certain address



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/onchain/sweeper.rb', line 7

def sweep(paths, mpk, destination_address)
  
  master = MoneyTree::Node.from_serialized_address(mpk)
  
  tx = Bitcoin::Protocol::Tx.new
  
  amount = 0
  
  paths.each do |path|
  
    address = master.node_for_path(path).to_address
  
    unspent = OnChain::BlockChain.get_unspent_outs(address)
  
    unspent.each do |spent|
      txin = Bitcoin::Protocol::TxIn.new

      txin.prev_out = spent[0]
      txin.prev_out_index = spent[1]
      amount += spent[3].to_i
    
      tx.add_in(txin)
    end
  end

  txout = Bitcoin::Protocol::TxOut.new(amount, Bitcoin::Script.from_string(
    "OP_DUP OP_HASH160 #{destination_address} " + 
    "OP_EQUALVERIFY OP_CHECKSIG").to_payload)
  
  tx.add_out(txout)
end