openassets-ruby 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/openassets/api.rb +21 -0
- data/lib/openassets/protocol/marker_output.rb +1 -0
- data/lib/openassets/transaction/transaction_builder.rb +69 -48
- data/lib/openassets/version.rb +1 -1
- 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: d3804b292002c1e288151b5f5e9b17e271e9a223
|
4
|
+
data.tar.gz: 7ca178d7e5556ddbc997f09cf05a9dcdb87f7112
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 066a91d9c71bc20a8241279ff9f8319c0220d42cc7748847f9bbefabad54e2304f64b7974fc23a27588a05f6075303120c84e7781d35ea80dfba6a5e88b8c537
|
7
|
+
data.tar.gz: 568042474c3efa701a00672b695dec08dbea170eeda6108a63653a32e61fc40ebd052a548a8eef53ee439fc19599dd3c95dcece6e8436a0ea4b2b1d1545cea06
|
data/README.md
CHANGED
@@ -69,6 +69,21 @@ Creates a transaction for sending an asset from the open asset address to anothe
|
|
69
69
|
api.send_asset(from, 'AWo3R89p5REmoSyMWB8AeUmud8456bRxZL', 100, to, 10000, 'broadcast')
|
70
70
|
```
|
71
71
|
|
72
|
+
* **send_bitcoin**
|
73
|
+
Creates a transaction for sending bitcoins from an address to another.
|
74
|
+
```ruby
|
75
|
+
# send bitcoin
|
76
|
+
# api.send_bitcoin(<from btc address>, <amount (satoshi)>, <to btc address>, <fees (The fess in satoshis for the transaction. use 10000 satoshi if specified nil)>, <mode=('broadcast', 'signed', 'unsigned')>, <output_qty default value is 1.>)
|
77
|
+
|
78
|
+
# example
|
79
|
+
from = '14M4kbAtn71P1nnNYuhBDFTNYxa19t1XP6'
|
80
|
+
to = '1MFW7BTwiNbAkmVz4SzAMQXboKYKGSzkq2'
|
81
|
+
api.send_bitcoin(from, 60000, to)
|
82
|
+
```
|
83
|
+
|
84
|
+
If specified output_qty, the send output is divided by the number of output_qty.
|
85
|
+
Ex, amount = 60000 and output_qty = 2, send TxOut is two (each value is 30000, 30000) and change TxOut one.
|
86
|
+
|
72
87
|
## License
|
73
88
|
|
74
89
|
The MIT License (MIT)
|
data/lib/openassets/api.rb
CHANGED
@@ -118,6 +118,7 @@ module OpenAssets
|
|
118
118
|
# @param[String] mode 'broadcast' (default) for signing and broadcasting the transaction,
|
119
119
|
# 'signed' for signing the transaction without broadcasting,
|
120
120
|
# 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
|
121
|
+
# @return[Bitcoin::Protocol:Tx] The resulting transaction.
|
121
122
|
def send_asset(from, asset_id, amount, to, fees = nil, mode = 'broadcast')
|
122
123
|
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
123
124
|
colored_outputs = get_unspent_outputs([oa_address_to_address(from)])
|
@@ -127,6 +128,26 @@ module OpenAssets
|
|
127
128
|
tx
|
128
129
|
end
|
129
130
|
|
131
|
+
# Creates a transaction for sending bitcoins from an address to another.
|
132
|
+
# @param[String] from The address to send the bitcoins from.
|
133
|
+
# @param[Integer] amount The amount of satoshis to send.
|
134
|
+
# @param[String] to The address to send the bitcoins to.
|
135
|
+
# @param[Integer] fees The fess in satoshis for the transaction.
|
136
|
+
# @param[String] mode 'broadcast' (default) for signing and broadcasting the transaction,
|
137
|
+
# 'signed' for signing the transaction without broadcasting,
|
138
|
+
# 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
|
139
|
+
# @param [Integer] output_qty The number of divides the issue output. Default value is 1.
|
140
|
+
# Ex. amount = 125 and output_qty = 2, asset quantity = [62, 63] and issue TxOut is two.
|
141
|
+
# @return[Bitcoin::Protocol:Tx] The resulting transaction.
|
142
|
+
def send_bitcoin(from, amount, to, fees = nil, mode = 'broadcast', output_qty = 1)
|
143
|
+
validate_address([from, to])
|
144
|
+
builder = OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
145
|
+
colored_outputs = get_unspent_outputs([from])
|
146
|
+
send_param = OpenAssets::Transaction::TransferParameters.new(colored_outputs, to, from, amount)
|
147
|
+
tx = builder.transfer_btc(send_param, fees.nil? ? @config[:default_fees]: fees, output_qty)
|
148
|
+
process_transaction(tx, mode)
|
149
|
+
end
|
150
|
+
|
130
151
|
# Get unspent outputs.
|
131
152
|
# @param [Array] addresses The array of Bitcoin address.
|
132
153
|
# @return [Array[OpenAssets::Transaction::SpendableOutput]] The array of unspent outputs.
|
@@ -28,6 +28,7 @@ module OpenAssets
|
|
28
28
|
payload = [OAP_MARKER, VERSION]
|
29
29
|
payload << Bitcoin::Protocol.pack_var_int(@asset_quantities.length).unpack("H*")
|
30
30
|
@asset_quantities.map{|q|payload << encode_leb128(q)}
|
31
|
+
@metadata ||= ''
|
31
32
|
payload << Bitcoin::Protocol.pack_var_int(@metadata.length).unpack("H*")
|
32
33
|
tmp = []
|
33
34
|
@metadata.bytes{|b| tmp << b.to_s(16)}
|
@@ -50,56 +50,17 @@ module OpenAssets
|
|
50
50
|
# @param[Integer] fees The fees to include in the transaction.
|
51
51
|
# @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
|
52
52
|
def transfer_assets(asset_id, transfer_spec, btc_change_script, fees)
|
53
|
-
btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(
|
54
|
-
|
55
|
-
|
56
|
-
inputs, total_amount = TransactionBuilder.collect_colored_outputs(transfer_spec.unspent_outputs, asset_id, transfer_spec.amount)
|
57
|
-
|
58
|
-
# add asset transfer outpu
|
59
|
-
outputs << create_colored_output(oa_address_to_address(transfer_spec.to_script))
|
60
|
-
asset_quantities << transfer_spec.amount
|
61
|
-
|
62
|
-
# add the rest of the asset to the origin address
|
63
|
-
if total_amount > transfer_spec.amount
|
64
|
-
outputs << create_colored_output(oa_address_to_address(transfer_spec.change_script))
|
65
|
-
asset_quantities << (total_amount - transfer_spec.amount)
|
66
|
-
end
|
67
|
-
|
68
|
-
btc_excess = inputs.inject(0) { |sum, i| sum + i.output.value } - outputs.inject(0){|sum, o| sum + o.value}
|
69
|
-
if btc_excess < btc_transfer_spec.amount + fees
|
70
|
-
uncolored_outputs, uncolored_amount =
|
71
|
-
TransactionBuilder.collect_uncolored_outputs(btc_transfer_spec.unspent_outputs, btc_transfer_spec.amount + fees - btc_excess)
|
72
|
-
inputs << uncolored_outputs
|
73
|
-
btc_excess += uncolored_amount
|
74
|
-
end
|
75
|
-
|
76
|
-
change = btc_excess - btc_transfer_spec.amount - fees
|
77
|
-
if change > 0
|
78
|
-
outputs << create_uncolored_output(oa_address_to_address(btc_transfer_spec.change_script), change)
|
79
|
-
end
|
80
|
-
|
81
|
-
if btc_transfer_spec.amount > 0
|
82
|
-
outputs << create_uncolored_output(oa_address_to_address(btc_transfer_spec.to_script), btc_transfer_spec.amount)
|
83
|
-
end
|
84
|
-
|
85
|
-
# add marker output to outputs first.
|
86
|
-
unless asset_quantities.empty?
|
87
|
-
outputs.unshift(create_marker_output(asset_quantities))
|
88
|
-
end
|
89
|
-
|
90
|
-
tx = Bitcoin::Protocol::Tx.new
|
91
|
-
inputs.flatten.each{|i|
|
92
|
-
script_sig = i.output.script.to_binary
|
93
|
-
tx_in = Bitcoin::Protocol::TxIn.from_hex_hash(i.out_point.hash, i.out_point.index)
|
94
|
-
tx_in.script_sig = script_sig
|
95
|
-
tx.add_in(tx_in)
|
96
|
-
}
|
97
|
-
outputs.each{|o|tx.add_out(o)}
|
98
|
-
tx
|
53
|
+
btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(
|
54
|
+
transfer_spec.unspent_outputs, nil, oa_address_to_address(btc_change_script), 0)
|
55
|
+
transfer([[asset_id, transfer_spec]], btc_transfer_spec, fees)
|
99
56
|
end
|
100
57
|
|
101
|
-
|
102
|
-
|
58
|
+
# Creates a transaction for sending bitcoins.
|
59
|
+
# @param[OpenAssets::Transaction::TransferParameters] btc_transfer_spec The parameters of the bitcoins being transferred.
|
60
|
+
# @param[Integer] fees The fees to include in the transaction.
|
61
|
+
# @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
|
62
|
+
def transfer_btc(btc_transfer_spec, fees, output_qty = 1)
|
63
|
+
transfer([], btc_transfer_spec, fees, output_qty)
|
103
64
|
end
|
104
65
|
|
105
66
|
# collect uncolored outputs in unspent outputs(contains colored output).
|
@@ -166,6 +127,66 @@ module OpenAssets
|
|
166
127
|
Bitcoin::Protocol::TxOut.new(value, Bitcoin::Script.new(Bitcoin::Script.to_hash160_script(hash160)).to_payload)
|
167
128
|
end
|
168
129
|
|
130
|
+
def transfer(asset_transfer_specs, btc_transfer_spec, fees, btc_output_qty = 1)
|
131
|
+
inputs = []
|
132
|
+
outputs = []
|
133
|
+
asset_quantities = []
|
134
|
+
asset_transfer_specs.each{|asset_id, transfer_spec|
|
135
|
+
colored_outputs, total_amount = TransactionBuilder.collect_colored_outputs(transfer_spec.unspent_outputs, asset_id, transfer_spec.amount)
|
136
|
+
inputs = inputs + colored_outputs
|
137
|
+
|
138
|
+
# add asset transfer output
|
139
|
+
outputs << create_colored_output(oa_address_to_address(transfer_spec.to_script))
|
140
|
+
asset_quantities << transfer_spec.amount
|
141
|
+
|
142
|
+
# add the rest of the asset to the origin address
|
143
|
+
if total_amount > transfer_spec.amount
|
144
|
+
outputs << create_colored_output(oa_address_to_address(transfer_spec.change_script))
|
145
|
+
asset_quantities << (total_amount - transfer_spec.amount)
|
146
|
+
end
|
147
|
+
}
|
148
|
+
|
149
|
+
# btc_excess = inputs(colored) total satoshi - outputs(transfer) total satoshi
|
150
|
+
btc_excess = inputs.inject(0) { |sum, i| sum + i.output.value } - outputs.inject(0){|sum, o| sum + o.value}
|
151
|
+
if btc_excess < btc_transfer_spec.amount + fees
|
152
|
+
uncolored_outputs, uncolored_amount =
|
153
|
+
TransactionBuilder.collect_uncolored_outputs(btc_transfer_spec.unspent_outputs, btc_transfer_spec.amount + fees - btc_excess)
|
154
|
+
inputs << uncolored_outputs
|
155
|
+
btc_excess += uncolored_amount
|
156
|
+
end
|
157
|
+
|
158
|
+
otsuri = btc_excess - btc_transfer_spec.amount - fees
|
159
|
+
if otsuri > 0
|
160
|
+
outputs << create_uncolored_output(btc_transfer_spec.change_script, otsuri)
|
161
|
+
end
|
162
|
+
|
163
|
+
if btc_transfer_spec.amount > 0
|
164
|
+
btc_output_qty.times {|index|
|
165
|
+
if index == btc_output_qty - 1
|
166
|
+
amount = btc_transfer_spec.amount / btc_output_qty + btc_transfer_spec.amount % btc_output_qty
|
167
|
+
else
|
168
|
+
amount = btc_transfer_spec.amount / btc_output_qty
|
169
|
+
end
|
170
|
+
outputs << create_uncolored_output(btc_transfer_spec.to_script, amount)
|
171
|
+
}
|
172
|
+
end
|
173
|
+
|
174
|
+
# add marker output to outputs first.
|
175
|
+
unless asset_quantities.empty?
|
176
|
+
outputs.unshift(create_marker_output(asset_quantities))
|
177
|
+
end
|
178
|
+
|
179
|
+
tx = Bitcoin::Protocol::Tx.new
|
180
|
+
inputs.flatten.each{|i|
|
181
|
+
script_sig = i.output.script.to_binary
|
182
|
+
tx_in = Bitcoin::Protocol::TxIn.from_hex_hash(i.out_point.hash, i.out_point.index)
|
183
|
+
tx_in.script_sig = script_sig
|
184
|
+
tx.add_in(tx_in)
|
185
|
+
}
|
186
|
+
outputs.each{|o|tx.add_out(o)}
|
187
|
+
tx
|
188
|
+
end
|
189
|
+
|
169
190
|
end
|
170
191
|
|
171
192
|
end
|
data/lib/openassets/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openassets-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- azuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bitcoin-ruby
|