openassets-ruby 0.5.1 → 0.5.2

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: bd0918ad4bb75cb8cbc1a3cd13fcedffde7d97a9
4
- data.tar.gz: ca7fc189f43ea44adf304ab3a2f3e4df831a936f
3
+ metadata.gz: feddc0050a29a509975ae29fdd81e8f464612c99
4
+ data.tar.gz: e8c6b8bb501b03f12833613d3fd8cb12c29e9d9f
5
5
  SHA512:
6
- metadata.gz: 927f9c392e633ff2ce0f621b792283c5c6e3705dd9e8d83a561a4b946e0b575ec342e4512e72ceb8de1726963de72817ef924e5b481913e983cfdb0862578e5f
7
- data.tar.gz: 0662166b353f3e54a6514d49440ff4c2fbaf82ee61d899e46107f4f3f325f6ae3ad10d7b8ae242e2e02f268cfbe0809bcff5dcb5737bd35cf66ec27dcc83816c
6
+ metadata.gz: 82c528c39eaa375f6a888164cb2e8edbaa4524bfd13dfd8d24fa5dd73c44225d6ff72f41a4503a69ef60cae193e1dda8c7d87d69f001b22b8d2233a1110dcea3
7
+ data.tar.gz: 30b2d8a9e9e60b425f7fb8ffcc863c5dd150405ffcb647c7b5626bb22e2950ba6f64a6cf860c0107da6adb788cb02af7ba4e179112770ee99cdec82eeed0f53e
data/README.md CHANGED
@@ -38,7 +38,7 @@ The configuration options are as follows:
38
38
  |---|---|---|
39
39
  |**network**|The using network. "mainnet" or "testnet" or "regtest" |mainnet|
40
40
  |**provider**|The RPC server. Specify possible now only "bitcoind".|bitcoind|
41
- |**cache**|The path to the database file. If you want to use in-memory database, specify ':memory:'.|cache.db|
41
+ |**cache**|The path to the database file. If you want to use in-memory database, specify ':memory:'. If do not use cache, specify :none .|cache.db|
42
42
  |**dust_limit**|The amount of Bitcoin, which is set to the each output of the Open Assets Protocol(issue or transfer).|600 (satoshi)|
43
43
  |**default_fees**|The default transaction fee in satoshi. If you want to use auto fee settings, specify ':auto'. (used by issue_asset and send_asset, send_bitcoin )|10000 (satoshi)|
44
44
  |**min_confirmation**|The minimum number of confirmations the transaction containing an output that used to get UTXO.|1|
@@ -28,8 +28,10 @@ module OpenAssets
28
28
  else
29
29
  raise OpenAssets::Error, 'specified unsupported provider.'
30
30
  end
31
- @tx_cache = Cache::TransactionCache.new(@config[:cache])
32
- @output_cache = Cache::OutputCache.new(@config[:cache])
31
+ unless @config[:cache] == :none
32
+ @tx_cache = Cache::TransactionCache.new(@config[:cache])
33
+ @output_cache = Cache::OutputCache.new(@config[:cache])
34
+ end
33
35
  change_network
34
36
  end
35
37
 
@@ -209,12 +211,14 @@ module OpenAssets
209
211
  end
210
212
 
211
213
  def get_output(txid, output_index)
212
- cached = output_cache.get(txid, output_index)
213
- return cached unless cached.nil?
214
+ if output_cache
215
+ cached = output_cache.get(txid, output_index)
216
+ return cached unless cached.nil?
217
+ end
214
218
  decode_tx = load_cached_tx(txid)
215
219
  tx = Bitcoin::Protocol::Tx.new(decode_tx.htb)
216
220
  colored_outputs = get_color_outputs_from_tx(tx)
217
- colored_outputs.each_with_index { |o, index| output_cache.put(txid, index, o)}
221
+ colored_outputs.each_with_index { |o, index| output_cache.put(txid, index, o)} if output_cache
218
222
  colored_outputs[output_index]
219
223
  end
220
224
 
@@ -387,6 +391,7 @@ module OpenAssets
387
391
  end
388
392
 
389
393
  def load_cached_tx(txid)
394
+ return load_tx(txid) unless tx_cache
390
395
  decode_tx = tx_cache.get(txid)
391
396
  if decode_tx.nil?
392
397
  decode_tx = load_tx(txid)
@@ -3,7 +3,7 @@ module OpenAssets
3
3
  class SSLCertificateCache < SQLiteBase
4
4
 
5
5
  def initialize
6
- path = OpenAssets.configuration ? OpenAssets.configuration[:cache] : ':memory:'
6
+ path = OpenAssets.configuration && OpenAssets.configuration[:cache] != :none ? OpenAssets.configuration[:cache] : ':memory:'
7
7
  super(path)
8
8
  end
9
9
 
@@ -3,6 +3,8 @@ module OpenAssets
3
3
  autoload :MarkerOutput, 'openassets/protocol/marker_output'
4
4
  autoload :TransactionOutput, 'openassets/protocol/transaction_output'
5
5
  autoload :OutputType, 'openassets/protocol/output_type'
6
+ autoload :AssetDefinitionLoader, 'openassets/protocol/asset_definition_loader'
7
+ autoload :HttpAssetDefinitionLoader, 'openassets/protocol/http_asset_definition_loader'
6
8
  autoload :AssetDefinition, 'openassets/protocol/asset_definition'
7
9
  end
8
10
  end
@@ -53,19 +53,6 @@ module OpenAssets
53
53
  definition
54
54
  end
55
55
 
56
- # Parse the JSON obtained from the URL, and create a AssetDefinition object.
57
- # @param[String] url The URL of Asset Definition.
58
- def self.parse_url(url)
59
- begin
60
- definition = parse_json(RestClient::Request.execute(:method => :get, :url => url, :timeout => 10, :open_timeout => 10))
61
- definition.asset_definition_url = url
62
- definition
63
- rescue => e
64
- puts e
65
- nil
66
- end
67
- end
68
-
69
56
  def include_asset_id?(asset_id)
70
57
  return false if asset_ids.nil? || asset_ids.empty?
71
58
  asset_ids.include?(asset_id)
@@ -0,0 +1,22 @@
1
+ module OpenAssets
2
+ module Protocol
3
+
4
+ class AssetDefinitionLoader
5
+
6
+ attr_reader :loader
7
+
8
+ def initialize(metadata)
9
+ if metadata.start_with?('http://') || metadata.start_with?('https://')
10
+ @loader = HttpAssetDefinitionLoader.new(metadata)
11
+ end
12
+ end
13
+
14
+ # load Asset Definition File
15
+ # @return[OpenAssets::Protocol::AssetDefinition] loaded asset definition object
16
+ def load_definition
17
+ @loader.load
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ module OpenAssets
2
+ module Protocol
3
+
4
+ # Asset Definition loader for http or https uri scheme
5
+ class HttpAssetDefinitionLoader
6
+
7
+ attr_reader :url
8
+
9
+ def initialize(url)
10
+ @url = url
11
+ end
12
+
13
+ # load asset definition
14
+ def load
15
+ begin
16
+ definition = AssetDefinition.parse_json(RestClient::Request.execute(:method => :get, :url => url, :timeout => 10, :open_timeout => 10))
17
+ definition.asset_definition_url = url
18
+ definition
19
+ rescue => e
20
+ puts e
21
+ nil
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -43,6 +43,7 @@ module OpenAssets
43
43
  # @param [String] payload The Open Assets Payload.
44
44
  # @return [OpenAssets::Protocol::MarkerOutput] The marker output object.
45
45
  def self.deserialize_payload(payload)
46
+ return nil unless valid?(payload)
46
47
  payload = payload[8..-1] # exclude OAP_MARKER,VERSION
47
48
  asset_quantity, payload = parse_asset_qty(payload)
48
49
  list = to_bytes(payload).map{|x|(x.to_i(16)>=128 ? x : x+"|")}.join.split('|')[0..(asset_quantity - 1)].join
@@ -57,24 +58,7 @@ module OpenAssets
57
58
  # @return [String] The byte string of the marker output payload if the output fits the pattern, nil otherwise.
58
59
  def self.parse_script(output_script)
59
60
  data = Bitcoin::Script.new(output_script).get_op_return_data
60
- return data if data.nil?
61
- # check open assets marker
62
- return nil unless data.start_with?(OAP_MARKER + VERSION)
63
- # check asset quantity
64
- offset = [OAP_MARKER + VERSION].pack('H*').length
65
- count, offset = read_var_integer(data, offset)
66
- return nil unless count
67
- # check metadata
68
- count.times do
69
- quantity, length = read_leb128(data, offset)
70
- return nil if quantity.nil? || (length - offset) > 9
71
- offset = length
72
- end
73
- # check metadata
74
- length, offset = read_var_integer(data, offset)
75
- return nil unless length
76
- return nil if [data].pack('H*').bytes.length < length + offset
77
- data
61
+ return data if valid?(data)
78
62
  end
79
63
 
80
64
  # Creates an output script containing an OP_RETURN and a PUSHDATA from payload.
@@ -117,6 +101,29 @@ module OpenAssets
117
101
  end
118
102
  end
119
103
 
104
+ # validate marker output format
105
+ # @param[String] data marker output data with start with 4f41
106
+ def self.valid?(data)
107
+ return false if data.nil?
108
+ # check open assets marker
109
+ return false unless data.start_with?(OAP_MARKER + VERSION)
110
+ # check asset quantity
111
+ offset = [OAP_MARKER + VERSION].pack('H*').length
112
+ count, offset = read_var_integer(data, offset)
113
+ return false unless count
114
+ # check metadata
115
+ count.times do
116
+ quantity, length = read_leb128(data, offset)
117
+ return false if quantity.nil? || (length - offset) > 9
118
+ offset = length
119
+ end
120
+ # check metadata
121
+ length, offset = read_var_integer(data, offset)
122
+ return false unless length
123
+ return false if [data].pack('H*').bytes.length < length + offset
124
+ true
125
+ end
126
+
120
127
  end
121
128
 
122
129
  end
@@ -120,7 +120,10 @@ module OpenAssets
120
120
  end
121
121
 
122
122
  def load_asset_definition(url)
123
- @@definition_cache[url] = AssetDefinition.parse_url(metadata_url) unless @@definition_cache.has_key?(url)
123
+ unless @@definition_cache.has_key?(url)
124
+ loader = AssetDefinitionLoader.new(metadata_url)
125
+ @@definition_cache[url] = loader.load_definition
126
+ end
124
127
  @@definition_cache[url]
125
128
  end
126
129
  end
@@ -193,28 +193,30 @@ module OpenAssets
193
193
  # Only when assets are transferred
194
194
  asset_based_specs = {}
195
195
  asset_transfer_specs.each{|asset_id, transfer_spec|
196
- asset_based_specs[asset_id] = [] unless asset_based_specs.has_key?(asset_id)
197
- asset_based_specs[asset_id] << transfer_spec
196
+ asset_based_specs[asset_id] = {} unless asset_based_specs.has_key?(asset_id)
197
+ asset_based_specs[asset_id][transfer_spec.change_script] = [] unless asset_based_specs[asset_id].has_key?(transfer_spec.change_script)
198
+ asset_based_specs[asset_id][transfer_spec.change_script] << transfer_spec
198
199
  }
199
200
 
200
- asset_based_specs.each{|asset_id, transfer_specs|
201
- transfer_amount = transfer_specs.inject(0){|sum, s| sum + s.amount}
202
- colored_outputs, total_amount = TransactionBuilder.collect_colored_outputs(transfer_specs[0].unspent_outputs, asset_id, transfer_amount)
203
- inputs = inputs + colored_outputs
204
- transfer_specs.each{|spec|
205
- # add asset transfer output
206
- spec.split_output_amount.each {|amount|
207
- outputs << create_colored_output(oa_address_to_address(spec.to_script))
208
- asset_quantities << amount
201
+ asset_based_specs.each{|asset_id, address_based_specs|
202
+ address_based_specs.values.each{|transfer_specs|
203
+ transfer_amount = transfer_specs.inject(0){|sum, s| sum + s.amount}
204
+ colored_outputs, total_amount = TransactionBuilder.collect_colored_outputs(transfer_specs[0].unspent_outputs, asset_id, transfer_amount)
205
+ inputs = inputs + colored_outputs
206
+ transfer_specs.each{|spec|
207
+ # add asset transfer output
208
+ spec.split_output_amount.each {|amount|
209
+ outputs << create_colored_output(oa_address_to_address(spec.to_script))
210
+ asset_quantities << amount
211
+ }
209
212
  }
213
+ # add the rest of the asset to the origin address
214
+ if total_amount > transfer_amount
215
+ outputs << create_colored_output(oa_address_to_address(transfer_specs[0].change_script))
216
+ asset_quantities << (total_amount - transfer_amount)
217
+ end
210
218
  }
211
- # add the rest of the asset to the origin address
212
- if total_amount > transfer_amount
213
- outputs << create_colored_output(oa_address_to_address(transfer_specs[0].change_script))
214
- asset_quantities << (total_amount - transfer_amount)
215
- end
216
219
  }
217
-
218
220
  # End of asset settings
219
221
 
220
222
  ## For bitcoins transfer
@@ -1,3 +1,3 @@
1
1
  module OpenAssets
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
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.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bitcoin-ruby
@@ -183,6 +183,8 @@ files:
183
183
  - lib/openassets/medhod_filter.rb
184
184
  - lib/openassets/protocol.rb
185
185
  - lib/openassets/protocol/asset_definition.rb
186
+ - lib/openassets/protocol/asset_definition_loader.rb
187
+ - lib/openassets/protocol/http_asset_definition_loader.rb
186
188
  - lib/openassets/protocol/marker_output.rb
187
189
  - lib/openassets/protocol/output_type.rb
188
190
  - lib/openassets/protocol/transaction_output.rb
@@ -224,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
226
  version: '0'
225
227
  requirements: []
226
228
  rubyforge_project:
227
- rubygems_version: 2.4.6
229
+ rubygems_version: 2.4.5
228
230
  signing_key:
229
231
  specification_version: 4
230
232
  summary: The implementation of the Open Assets Protocol for Ruby.