onchain 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee92d471f0e4b5a367352fb5d99179688521efff
4
- data.tar.gz: bb5df5cf76b8bb11613852bd7dcbba4cb8b61842
3
+ metadata.gz: 3d1afa67290306977f3fdbdcdbf501dc39941f09
4
+ data.tar.gz: 2d4c4c3a88040b23e9830930330e986515486f3d
5
5
  SHA512:
6
- metadata.gz: a3ed132d84404a44f1c02de448d70b148b5b716155ee657058724254cabddd83979afd9073668c93afa474f5f513cbb15954eed6a5fa552f53c648c7e17969e5
7
- data.tar.gz: 01feb04e55515ef58e7ec890d88d8c17b05ce401ebb85e850f163ad0b65ae9656f8bff9b618448f73d123e9546ff1a8128be67b5f3afaffd8e778d2a4f44dcd6
6
+ metadata.gz: 7afe5e34552e43ce01496792bc1785e1e5e7c7757ffa8ad49d51abcfdcc6f71826830860ef05988861802053e6ae7c721a729a4bc40102b9fdb0c716e3004a5b
7
+ data.tar.gz: 6428a5b15ec8671dfa812c2c49a57e93c6e245f0bf7214d177771fa354f1c1170311ee87a7130f0655f1eb9c5e574d8189c44e08d81917b5dad60a9094f05f78
@@ -12,6 +12,16 @@ require 'json'
12
12
  # get_transactions(address)
13
13
  #
14
14
  class OnChain
15
+ class << self
16
+
17
+ def bin_to_hex(bin)
18
+ return bin.unpack("H*")[0]
19
+ end
20
+
21
+ def hex_to_bin(hex)
22
+ return hex.scan(/../).map { |x| x.hex }.pack('c*')
23
+ end
24
+ end
15
25
  end
16
26
 
17
27
  class OnChain::BlockChain
@@ -5,16 +5,15 @@ class OnChain::Payments
5
5
 
6
6
  def get_address_from_redemption_script(redemption_script)
7
7
 
8
- sbin = redemption_script.scan(/../).map { |x| x.hex }.pack('c*')
9
- hex = sbin.unpack("H*")[0]
8
+ sbin = OnChain.hex_to_bin(redemption_script)
9
+ hex = OnChain.bin_to_hex(sbin)
10
10
  fund_address = Bitcoin.hash160_to_p2sh_address(Bitcoin.hash160(hex))
11
11
 
12
12
  return fund_address
13
13
  end
14
14
 
15
15
  def hex_to_script(hex)
16
- sbin = hex.scan(/../).map { |x| x.hex }.pack('c*')
17
- return Bitcoin::Script.new(sbin)
16
+ return Bitcoin::Script.new(OnChain::hex_to_bin(hex))
18
17
  end
19
18
 
20
19
  # With a bunch of HD wallet paths, build a transaction
@@ -1,39 +1,78 @@
1
1
  class OnChain::Sweeper
2
2
  class << self
3
3
 
4
-
5
- # With a bunch of HD wallet paths, build a transaction
6
- # That pays all the coins to a certain address
7
- def sweep(paths, mpk, destination_address)
4
+ # Turn a bunch of master keys into a redemption scriopt
5
+ # i.e. derive the path.
6
+ def multi_sig_address_from_mpks(mpks, path)
8
7
 
9
- master = MoneyTree::Node.from_serialized_address(mpk)
10
-
11
- tx = Bitcoin::Protocol::Tx.new
8
+ addresses = []
9
+ mpks.each do |mpk|
10
+ master = MoneyTree::Node.from_serialized_address(mpk)
11
+ m = master.node_for_path(path)
12
+ addresses << m.public_key.to_hex
13
+ end
12
14
 
13
- amount = 0
15
+ rs = generate_redemption_script(addresses)
14
16
 
15
- paths.each do |path|
17
+ return generate_address_of_redemption_script(rs)
18
+ end
19
+
20
+ def generate_redemption_script(addresses)
21
+
22
+ rs = (80 + addresses.length).to_s(16)
23
+
24
+ addresses.each do |address|
25
+ rs = rs + (address.length / 2).to_s(16)
26
+ rs = rs + address
27
+ end
28
+
29
+ rs = rs + (80 + addresses.length).to_s(16)
30
+
31
+ rs = rs + 'ae'
32
+
33
+ return rs
34
+ end
35
+
36
+ def generate_address_of_redemption_script(redemption_script)
37
+ hash160 = Bitcoin.hash160(redemption_script)
38
+
39
+ return Bitcoin.hash160_to_p2sh_address(hash160)
40
+ end
41
+
42
+ def get_block_height
43
+ return Chain.get_latest_block["height"].to_i
44
+ end
45
+
46
+ # With a bunch of HD wallet paths, build a transaction
47
+ # That pays all the coins to a certain address
48
+ def sweep(mpks, path, limit, last_block_checked)
16
49
 
17
- address = master.node_for_path(path).to_address
50
+ to_sweep = {}
51
+ # Get all the addresses we are interested in.
52
+ for i in 0..limit do
53
+ r = path.sub('#{index}', i.to_s)
54
+ a = multi_sig_address_from_mpks(mpks, r)
55
+ # store address as lookup for path.
56
+ to_sweep[a] = r
57
+ end
18
58
 
19
- unspent = OnChain::BlockChain.get_unspent_outs(address)
59
+ incoming_coins = []
20
60
 
21
- unspent.each do |spent|
22
- txin = Bitcoin::Protocol::TxIn.new
23
-
24
- txin.prev_out = spent[0]
25
- txin.prev_out_index = spent[1]
26
- amount += spent[3].to_i
61
+ to_sweep.each do |address, path|
62
+ txs = Chain.get_address_transactions(address)
27
63
 
28
- tx.add_in(txin)
64
+ txs.each do |tx|
65
+
66
+ if tx["block_height"].to_i > last_block_checked
67
+ addresses = tx["outputs"]["addresses"]
68
+
69
+ else
70
+ break
71
+ end
72
+
29
73
  end
30
74
  end
31
-
32
- txout = Bitcoin::Protocol::TxOut.new(amount, Bitcoin::Script.from_string(
33
- "OP_DUP OP_HASH160 #{destination_address} " +
34
- "OP_EQUALVERIFY OP_CHECKSIG").to_payload)
35
75
 
36
- tx.add_out(txout)
37
76
  end
38
77
 
39
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onchain
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Purton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake