openassets-ruby 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38a7d1efd4f24d1f1ebee399588b9f5bed87046c
4
- data.tar.gz: aba53db228cc09483dcfc5b13c06e7b1f92dc46c
3
+ metadata.gz: b9d348974ed51f85bc9e72c5eb5a38bd0a184238
4
+ data.tar.gz: a693b29a1e8d88cb25b6babd4cabcfd42640502d
5
5
  SHA512:
6
- metadata.gz: 3f290f1e69367ad04133c7c43787c9e65b552a5b51a2ef59e91912edf640c4e8a51c438c47dd18d0a14dd90eb6c052000f5ca527da2d97f3d873647452518e52
7
- data.tar.gz: 31e8c748e7a9267e15c2e9489cfc017976515a2c1434c667fe156c7d08eebfa0fc2bca910310e1da67d2d3cbd2f2a81f591a03a8a75f29ae9b1bd4e8c70d8437
6
+ metadata.gz: 742fe44913fa3c9fe836ee659719236bc024cd6b655a5fcae74affdb22f0f07cf2ec7c7d4f72e47119e88030368b4069f8333f799f963348795411dbf0a0c72b
7
+ data.tar.gz: 1d0f68e09c1c0f61c78d130d211949f52822cc0617f20314e55515910cc3625f80c92917a8697d44b846c7cb8fe4f43033741f2f39042a80b00c363bcf49a26c
data/README.md CHANGED
@@ -217,7 +217,25 @@ Creates a transaction for sending **multiple** asset from the open asset address
217
217
  params << OpenAssets::SendAssetParam.new('oGu4VXx2TU97d9LmPP8PMCkHckkcPqC5RY', 50, to)
218
218
  params << OpenAssets::SendAssetParam.new('oUygwarZqNGrjDvcZUpZdvEc7es6dcs1vs', 4, to)
219
219
  tx = api.send_assets(from, params)
220
- ```
220
+ ```
221
+
222
+ * **send_bitcoins**
223
+ Creates a transaction for sending **multiple** bitcoins from an address to others.
224
+ This transaction inputs use only uncolored outputs.
225
+ ```ruby
226
+ # send bitcoins
227
+ # api.send_bitcoins(<from btc address>, <The array of send bitcoin information(see OpenAssets::SendBitcoinParam).>, <fees (The fess in satoshis for the transaction. use 10000 satoshi if specified nil)>, <mode=('broadcast', 'signed', 'unsigned')>, <output_qty default value is 1.>)
228
+
229
+ # example
230
+ from = 'mrxpeizRrF8ymNx5FrvcGGZVecZjtUFVP3'
231
+ to1 = 'n4MEsSUN8GktDFZzU3V55mP3jWGMN7e4wE'
232
+ to2 = 'mvYbB238p3rFYFjM56cHhNNHeQb5ypQJ3T'
233
+ params = []
234
+ params << OpenAssets::SendBitcoinParam.new(50000, to1)
235
+ params << OpenAssets::SendBitcoinParam.new(3000, to2)
236
+ tx = api.send_bitcoins(from, params)
237
+ ```
238
+
221
239
 
222
240
  * **burn_asset**
223
241
  Creates a transaction for burn asset.
@@ -7,7 +7,7 @@ module OpenAssets
7
7
  include Util
8
8
  include MethodFilter
9
9
 
10
- before_filter :change_network, {:include => [:list_unspent, :get_balance, :issue_asset, :send_asset, :send_assets, :send_bitcoin]}
10
+ before_filter :change_network, {:include => [:list_unspent, :get_balance, :issue_asset, :send_asset, :send_assets, :send_bitcoin, :send_bitcoins]}
11
11
 
12
12
  attr_reader :config
13
13
  attr_reader :provider
@@ -159,6 +159,25 @@ module OpenAssets
159
159
  process_transaction(tx, mode)
160
160
  end
161
161
 
162
+ # Creates a transaction for sending multiple bitcoins from an address to others.
163
+ # @param[String] from The address to send the bitcoins from.
164
+ # @param[Array[OpenAssets::SendBitcoinParam]] send_params The send information(amount of satoshis and to).
165
+ # @param[Integer] fees The fees in satoshis for the transaction.
166
+ # @param[String] mode 'broadcast' (default) for signing and broadcasting the transaction,
167
+ # 'signed' for signing the transaction without broadcasting,
168
+ # 'unsigned' for getting the raw unsigned transaction without broadcasting"""='broadcast'
169
+ # @return[Bitcoin::Protocol:Tx] The resulting transaction.
170
+ def send_bitcoins(from, send_params, fees = nil, mode = 'broadcast')
171
+ colored_outputs = get_unspent_outputs([from])
172
+ btc_transfer_specs = send_params.map{|param|
173
+ OpenAssets::Transaction::TransferParameters.new(colored_outputs, param.to, from, param.amount)
174
+ }
175
+ tx = create_tx_builder.transfer_btcs(btc_transfer_specs, fees.nil? ? @config[:default_fees]: fees)
176
+ tx = process_transaction(tx, mode)
177
+ tx
178
+ end
179
+
180
+
162
181
  # Creates a transaction for burn asset.
163
182
  # @param[String] oa_address The open asset address to burn asset.
164
183
  # @param[String] asset_id The asset ID identifying the asset to burn.
@@ -0,0 +1,13 @@
1
+ module OpenAssets
2
+
3
+ class SendBitcoinParam
4
+ attr_accessor :amount
5
+ attr_accessor :to
6
+
7
+ def initialize(amount, to)
8
+ @amount = amount
9
+ @to = to
10
+ end
11
+ end
12
+
13
+ end
@@ -48,13 +48,13 @@ module OpenAssets
48
48
  def transfer_asset(asset_id, asset_transfer_spec, btc_change_script, fees)
49
49
  btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(
50
50
  asset_transfer_spec.unspent_outputs, nil, oa_address_to_address(btc_change_script), 0)
51
- transfer([[asset_id, asset_transfer_spec]], btc_transfer_spec, fees)
51
+ transfer([[asset_id, asset_transfer_spec]], [btc_transfer_spec], fees)
52
52
  end
53
53
 
54
54
  def transfer_assets(transfer_specs, btc_change_script, fees)
55
55
  btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(
56
56
  transfer_specs[0][1].unspent_outputs, nil, oa_address_to_address(btc_change_script), 0)
57
- transfer(transfer_specs, btc_transfer_spec, fees)
57
+ transfer(transfer_specs, [btc_transfer_spec], fees)
58
58
  end
59
59
 
60
60
  # Creates a transaction for sending bitcoins.
@@ -62,9 +62,18 @@ module OpenAssets
62
62
  # @param[Integer] fees The fees to include in the transaction.
63
63
  # @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
64
64
  def transfer_btc(btc_transfer_spec, fees)
65
- transfer([], btc_transfer_spec, fees)
65
+ transfer_btcs([btc_transfer_spec], fees)
66
66
  end
67
67
 
68
+ # Creates a transaction for sending bitcoins to many.
69
+ # @param[Array[OpenAssets::Transaction::TransferParameters]] btc_transfer_specs The parameters of the bitcoins being transferred.
70
+ # @param[Integer] fees The fees to include in the transaction.
71
+ # @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
72
+ def transfer_btcs(btc_transfer_specs, fees)
73
+ transfer([], btc_transfer_specs, fees)
74
+ end
75
+
76
+
68
77
  # Create a transaction for burn asset
69
78
  def burn_asset(unspents, asset_id, fee)
70
79
  tx = Bitcoin::Protocol::Tx.new
@@ -149,11 +158,18 @@ module OpenAssets
149
158
  Bitcoin::Protocol::TxOut.new(value, Bitcoin::Script.new(Bitcoin::Script.to_address_script(address)).to_payload)
150
159
  end
151
160
 
152
- def transfer(asset_transfer_specs, btc_transfer_spec, fees)
153
- inputs = []
154
- outputs = []
161
+
162
+ # create a transaction
163
+ # @param[Array[OpenAssets::Transaction::TransferParameters]] asset_transfer_specs The parameters of the assets being transferred.
164
+ # @param[Array[OpenAssets::Transaction::TransferParameters]] btc_transfer_specs The parameters of the bitcoins being transferred.
165
+ # @param[Integer] fees The fees to include in the transaction.
166
+ # @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
167
+ def transfer(asset_transfer_specs, btc_transfer_specs, fees)
168
+ inputs = [] # vin field
169
+ outputs = [] # vout field
155
170
  asset_quantities = []
156
171
 
172
+ # Only when assets are transferred
157
173
  asset_based_specs = {}
158
174
  asset_transfer_specs.each{|asset_id, transfer_spec|
159
175
  asset_based_specs[asset_id] = [] unless asset_based_specs.has_key?(asset_id)
@@ -178,19 +194,35 @@ module OpenAssets
178
194
  end
179
195
  }
180
196
 
197
+ # End of asset settings
198
+
199
+ ## For bitcoins transfer
200
+ # Assume that there is one address from
201
+ utxo = btc_transfer_specs[0].unspent_outputs.dup
202
+
203
+ # Calculate rest of bitcoins in asset settings
181
204
  # btc_excess = inputs(colored) total satoshi - outputs(transfer) total satoshi
182
- utxo = btc_transfer_spec.unspent_outputs.dup
183
205
  btc_excess = inputs.inject(0) { |sum, i| sum + i.output.value } - outputs.inject(0){|sum, o| sum + o.value}
184
- if btc_excess < btc_transfer_spec.amount + fees
206
+
207
+ # Calculate total amount of bitcoins to send
208
+ btc_transfer_total_amount = btc_transfer_specs.inject(0) {|sum, b| sum + b.amount}
209
+
210
+ if btc_excess < btc_transfer_total_amount + fees
211
+ # When there does not exist enough bitcoins to send in the inputs
212
+ # assign new address (utxo) to the inputs (does not include output coins)
213
+ # CREATING INPUT (if needed)
185
214
  uncolored_outputs, uncolored_amount =
186
- TransactionBuilder.collect_uncolored_outputs(utxo, btc_transfer_spec.amount + fees - btc_excess)
215
+ TransactionBuilder.collect_uncolored_outputs(utxo, btc_transfer_total_amount + fees - btc_excess)
187
216
  utxo = utxo - uncolored_outputs
188
217
  inputs << uncolored_outputs
189
218
  btc_excess += uncolored_amount
190
219
  end
191
220
 
192
- otsuri = btc_excess - btc_transfer_spec.amount - fees
221
+ otsuri = btc_excess - btc_transfer_total_amount - fees
193
222
  if otsuri > 0 && otsuri < @amount
223
+ # When there exists otsuri, but it is smaller than @amount (default is 600 satoshis)
224
+ # assign new address (utxo) to the input (does not include @amount - otsuri)
225
+ # CREATING INPUT (if needed)
194
226
  uncolored_outputs, uncolored_amount =
195
227
  TransactionBuilder.collect_uncolored_outputs(utxo, @amount - otsuri)
196
228
  inputs << uncolored_outputs
@@ -198,28 +230,38 @@ module OpenAssets
198
230
  end
199
231
 
200
232
  if otsuri > 0
201
- outputs << create_uncolored_output(btc_transfer_spec.change_script, otsuri)
233
+ # When there exists otsuri, write it to outputs
234
+ # CREATING OUTPUT
235
+ outputs << create_uncolored_output(btc_transfer_specs[0].change_script, otsuri)
202
236
  end
203
237
 
204
- if btc_transfer_spec.amount > 0
205
- btc_transfer_spec.split_output_amount.each {|amount|
206
- outputs << create_uncolored_output(btc_transfer_spec.to_script, amount)
207
- }
208
- end
238
+ btc_transfer_specs.each{|btc_transfer_spec|
239
+ if btc_transfer_spec.amount > 0
240
+ # Write output for bitcoin transfer by specifics of the argument
241
+ # CREATING OUTPUT
242
+ btc_transfer_spec.split_output_amount.each {|amount|
243
+ outputs << create_uncolored_output(btc_transfer_spec.to_script, amount)
244
+ }
245
+ end
246
+ }
209
247
 
210
248
  # add marker output to outputs first.
211
249
  unless asset_quantities.empty?
212
250
  outputs.unshift(create_marker_output(asset_quantities))
213
251
  end
214
252
 
253
+ # create a bitcoin transaction (assembling inputs and output into one transaction)
215
254
  tx = Bitcoin::Protocol::Tx.new
255
+ # for all inputs (vin fields), add sigs to the same transaction
216
256
  inputs.flatten.each{|i|
217
257
  script_sig = i.output.script.to_binary
218
258
  tx_in = Bitcoin::Protocol::TxIn.from_hex_hash(i.out_point.hash, i.out_point.index)
219
259
  tx_in.script_sig = script_sig
220
260
  tx.add_in(tx_in)
221
261
  }
262
+
222
263
  outputs.each{|o|tx.add_out(o)}
264
+
223
265
  tx
224
266
  end
225
267
 
@@ -1,3 +1,3 @@
1
1
  module OpenAssets
2
- VERSION = '0.4.5'
2
+ VERSION = '0.4.6'
3
3
  end
data/lib/openassets.rb CHANGED
@@ -10,6 +10,7 @@ module OpenAssets
10
10
  autoload :Provider, 'openassets/provider'
11
11
  autoload :Error, 'openassets/error'
12
12
  autoload :SendAssetParam, 'openassets/send_asset_param'
13
+ autoload :SendBitcoinParam, 'openassets/send_bitcoin_param'
13
14
  autoload :Cache, 'openassets/cache'
14
15
 
15
16
  class << self
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.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bitcoin-ruby
@@ -176,6 +176,7 @@ files:
176
176
  - lib/openassets/provider/bitcoin_core_provider.rb
177
177
  - lib/openassets/provider/block_chain_provider_base.rb
178
178
  - lib/openassets/send_asset_param.rb
179
+ - lib/openassets/send_bitcoin_param.rb
179
180
  - lib/openassets/transaction.rb
180
181
  - lib/openassets/transaction/dust_output_error.rb
181
182
  - lib/openassets/transaction/insufficient_asset_quantity_error.rb