onchain 1.4.3 → 2.0.0
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 +4 -4
- data/lib/onchain/transaction.rb +121 -0
- data/lib/onchain.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e2d4ee4a149d7de0252138466227fdcb5c22465
|
4
|
+
data.tar.gz: 348d78dd77c7b11b3b76846f431a4e31c060c8c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f152a7df7fef3b11eaac8632aa497779f3be1f26d831dc65b87010bf0b79a82b40b04e5e82a52796d1d73c215dce50555edfd60a59501c93e8cb44f272cd630
|
7
|
+
data.tar.gz: c3c187a0e9fa71dd55d6811d3772cdb3081ef58707d4a757c394454c7a7df0ce2b1923d9e4c63b337e7fda48c02903d6318fcd19abfdc5e76b64343a7179f780
|
@@ -0,0 +1,121 @@
|
|
1
|
+
class OnChain::Transaction
|
2
|
+
class << self
|
3
|
+
|
4
|
+
# Given a send address and an amount produce a transaction
|
5
|
+
# and a list of hashes that need to be signed.
|
6
|
+
#
|
7
|
+
# The transaction will be in hex format.
|
8
|
+
#
|
9
|
+
# The list of hashes that need to be signed will be in this format
|
10
|
+
#
|
11
|
+
# [input index]{public_key => { :hash => hash} }
|
12
|
+
#
|
13
|
+
# i.e.
|
14
|
+
#
|
15
|
+
# [0][034000....][:hash => '345435345...']
|
16
|
+
# [0][02fee.....][:hash => '122133445....']
|
17
|
+
#
|
18
|
+
def create_transaction(redemption_scripts, address, amount_in_satoshi, miners_fee)
|
19
|
+
|
20
|
+
tx = Bitcoin::Protocol::Tx.new
|
21
|
+
|
22
|
+
total_amount = miners_fee
|
23
|
+
|
24
|
+
total_amount = total_amount + amount_in_satoshi
|
25
|
+
|
26
|
+
addresses = redemption_scripts.map { |rs|
|
27
|
+
OnChain::Sweeper.generate_address_of_redemption_script(rs)
|
28
|
+
}
|
29
|
+
|
30
|
+
unspents, indexes, change = OnChain::BlockChain.get_unspent_for_amount(addresses, total_amount)
|
31
|
+
|
32
|
+
# OK, let's build a transaction.
|
33
|
+
tx = Bitcoin::Protocol::Tx.new
|
34
|
+
|
35
|
+
# Process the unpsent outs.
|
36
|
+
unspents.each_with_index do |spent, index|
|
37
|
+
|
38
|
+
script = redemption_scripts[indexes[index]]
|
39
|
+
|
40
|
+
txin = Bitcoin::Protocol::TxIn.new([ spent[0] ].pack('H*').reverse, spent[1])
|
41
|
+
txin.script_sig = OnChain::hex_to_bin(script)
|
42
|
+
tx.add_in(txin)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Do we have enough in the fund.
|
46
|
+
#if(total_amount > btc_balance)
|
47
|
+
# raise 'Balance is not enough to cover payment'
|
48
|
+
#end
|
49
|
+
|
50
|
+
txout = Bitcoin::Protocol::TxOut.new(amount_in_satoshi,
|
51
|
+
Bitcoin::Script.to_address_script(address))
|
52
|
+
|
53
|
+
tx.add_out(txout)
|
54
|
+
|
55
|
+
change_address = addresses[0]
|
56
|
+
|
57
|
+
# Send the change back.
|
58
|
+
if change > 0
|
59
|
+
|
60
|
+
txout = Bitcoin::Protocol::TxOut.new(change,
|
61
|
+
Bitcoin::Script.to_address_script(change_address))
|
62
|
+
|
63
|
+
tx.add_out(txout)
|
64
|
+
end
|
65
|
+
|
66
|
+
inputs_to_sign = []
|
67
|
+
tx.in.each_with_index do |txin, index|
|
68
|
+
hash = tx.signature_hash_for_input(index, txin.script, 1)
|
69
|
+
|
70
|
+
rsscript = Bitcoin::Script.new txin.script
|
71
|
+
rsscript.get_multisig_pubkeys.each do |key|
|
72
|
+
|
73
|
+
if inputs_to_sign[index] == nil
|
74
|
+
inputs_to_sign[index] = {}
|
75
|
+
end
|
76
|
+
inputs_to_sign[index][OnChain.bin_to_hex(key)] = {:hash => OnChain::bin_to_hex(hash)}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
return OnChain::bin_to_hex(tx.to_payload), inputs_to_sign
|
81
|
+
end
|
82
|
+
|
83
|
+
# Given a transaction in hex string format, apply
|
84
|
+
# the given signature list to it.
|
85
|
+
#
|
86
|
+
# Signatures should be in the format
|
87
|
+
#
|
88
|
+
# [0]{034000.....' => {:hash => '345435345....', :sig => '435fgdf4553...'}}
|
89
|
+
# [0]{02fee.....' => {:hash => '122133445....', :sig => '435fgdf4553...'}}
|
90
|
+
#
|
91
|
+
def sign_transaction(transaction_hex, sig_list)
|
92
|
+
|
93
|
+
tx = Bitcoin::Protocol::Tx.new OnChain::hex_to_bin(transaction_hex)
|
94
|
+
|
95
|
+
tx.in.each_with_index do |txin, index|
|
96
|
+
|
97
|
+
hash = OnChain.bin_to_hex(tx.signature_hash_for_input(index, txin.script, 1))
|
98
|
+
|
99
|
+
sigs = []
|
100
|
+
|
101
|
+
rscript = Bitcoin::Script.new txin.script
|
102
|
+
rscript.get_multisig_pubkeys.each do |key|
|
103
|
+
|
104
|
+
hkey = OnChain.bin_to_hex(key)
|
105
|
+
if sig_list[index][hkey] != nil and sig_list[index][hkey][:sig] != nil
|
106
|
+
|
107
|
+
# Add the signature to the list.
|
108
|
+
sigs << OnChain.hex_to_bin(sig_list[index][hkey][:sig])
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
if sigs.count > 0
|
114
|
+
txin.script = Bitcoin::Script.to_p2sh_multisig_script_sig(rscript.to_payload, sigs)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return OnChain::bin_to_hex(tx.to_payload)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/onchain.rb
CHANGED
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:
|
4
|
+
version: 2.0.0
|
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-11-
|
11
|
+
date: 2014-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/onchain/providers/blockr_api.rb
|
109
109
|
- lib/onchain/providers/chaincom_api.rb
|
110
110
|
- lib/onchain/sweeper.rb
|
111
|
+
- lib/onchain/transaction.rb
|
111
112
|
homepage: https://github.com/onchain/onchain-gem
|
112
113
|
licenses: []
|
113
114
|
metadata: {}
|