onchain 1.1.7 → 1.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/onchain/block_chain.rb +10 -0
- data/lib/onchain/payments.rb +3 -4
- data/lib/onchain/sweeper.rb +62 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d1afa67290306977f3fdbdcdbf501dc39941f09
|
4
|
+
data.tar.gz: 2d4c4c3a88040b23e9830930330e986515486f3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7afe5e34552e43ce01496792bc1785e1e5e7c7757ffa8ad49d51abcfdcc6f71826830860ef05988861802053e6ae7c721a729a4bc40102b9fdb0c716e3004a5b
|
7
|
+
data.tar.gz: 6428a5b15ec8671dfa812c2c49a57e93c6e245f0bf7214d177771fa354f1c1170311ee87a7130f0655f1eb9c5e574d8189c44e08d81917b5dad60a9094f05f78
|
data/lib/onchain/block_chain.rb
CHANGED
@@ -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
|
data/lib/onchain/payments.rb
CHANGED
@@ -5,16 +5,15 @@ class OnChain::Payments
|
|
5
5
|
|
6
6
|
def get_address_from_redemption_script(redemption_script)
|
7
7
|
|
8
|
-
sbin =
|
9
|
-
hex =
|
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
|
-
|
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
|
data/lib/onchain/sweeper.rb
CHANGED
@@ -1,39 +1,78 @@
|
|
1
1
|
class OnChain::Sweeper
|
2
2
|
class << self
|
3
3
|
|
4
|
-
|
5
|
-
#
|
6
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
15
|
+
rs = generate_redemption_script(addresses)
|
14
16
|
|
15
|
-
|
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
|
-
|
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
|
-
|
59
|
+
incoming_coins = []
|
20
60
|
|
21
|
-
|
22
|
-
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2014-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|