openassets-ruby 0.3.5 → 0.3.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: 10d7759652e07b134eee52b630c86a5694bf9370
4
- data.tar.gz: dacced4d83bbc49aa52cf6aa2eed2a1d6e83d6a4
3
+ metadata.gz: 3ba830cb6156258e0adc278a7a520cdb5bfe1423
4
+ data.tar.gz: 6eaa0d97aa767d1aa3fbf08099e6a6dcd78df7fa
5
5
  SHA512:
6
- metadata.gz: 0ea0b2089ab5bda555d36b599e080b104b95f86587330f9836e6c07c32151d92b8545e24a2c4badb66597c24af728016b90ac96c328a35850f78562b37a6a0fb
7
- data.tar.gz: 93582e63690146c92f6794f72ea6fb590a98e7dc68c848dd4f1fa04dfad1263b9db7705ddd47961592b91362acb9a9b1217c14064317f6d97beced0442d673ce
6
+ metadata.gz: 5caf4f4fa9f8941aa84ef5b8570b682eff9d47f6c0291833ef86df47588770b5f55f9d5cd800ac886c16d97873e7fa37176bd4b5fec7459a16023c749cfbbfe7
7
+ data.tar.gz: 5e1225f806f20189d7707c2ff52d4ada3c80e8d4b17cc59e7a1a3da010e85c021191806c4e5f67a8e6a7a0f05fdb31c717996ba25d53f059901b66026c340c39
data/README.md CHANGED
@@ -91,7 +91,7 @@ Returns an array of unspent transaction outputs, argument with the asset ID and
91
91
  |asset_quantity|The asset quantity is an unsigned integer representing how many units of that asset are stored on the output.|
92
92
  |asset_amount| The asset amount is the value obtained by converting the asset quantity to the unit of divisibility that are defined in the Asset definition file. |
93
93
  |asset_definition_url|The url of asset definition file.|
94
- |proof_of_authenticity|The result of [Proof of Authenticity](https://github.com/OpenAssets/open-assets-protocol/blob/master/asset-definition-protocol.mediawiki#Proof_of_Authenticity) that is checked consistent with the subject in the SSL certificate. If the result is true, issuer is verified. If the result is false, issuer is not verified.||
94
+ |proof_of_authenticity|The result of [Proof of Authenticity](https://github.com/OpenAssets/open-assets-protocol/blob/master/asset-definition-protocol.mediawiki#Proof_of_Authenticity) that is checked consistent with the subject in the SSL certificate. If the result is true, issuer is verified. If the result is false, issuer is not verified. This verification is performed only if link_to_website that is defined in Asset Definition File is true.|
95
95
 
96
96
  * **get_balance**
97
97
  Returns the balance in both bitcoin and colored coin assets for all of the addresses available in your Bitcoin Core wallet.
@@ -137,7 +137,7 @@ Returns the balance in both bitcoin and colored coin assets for all of the addre
137
137
  |asset_quantity|The asset quantity is an unsigned integer representing how many units of that asset are stored on the output.|
138
138
  |asset_amount| The asset amount is the value obtained by converting the asset quantity to the unit of divisibility that are defined in the Asset definition file. |
139
139
  |asset_definition_url|The url of asset definition file.|
140
- |proof_of_authenticity|The result of [Proof of Authenticity](https://github.com/OpenAssets/open-assets-protocol/blob/master/asset-definition-protocol.mediawiki#Proof_of_Authenticity) that is checked consistent with the subject in the SSL certificate. If the result is true, issuer is verified. If the result is false, issuer is not verified.|
140
+ |proof_of_authenticity|The result of [Proof of Authenticity](https://github.com/OpenAssets/open-assets-protocol/blob/master/asset-definition-protocol.mediawiki#Proof_of_Authenticity) that is checked consistent with the subject in the SSL certificate. If the result is true, issuer is verified. If the result is false, issuer is not verified. This verification is performed only if link_to_website that is defined in Asset Definition File is true.|
141
141
  |account|The name of an account.|
142
142
 
143
143
  * **issue_asset**
@@ -185,6 +185,9 @@ Ex, amount = 60000 and output_qty = 2, send TxOut is two (each value is 30000, 3
185
185
  * **get_outputs_from_txid**
186
186
  Get tx outputs. (use for debug)
187
187
  ```ruby
188
+ # api.get_outputs_from_txid(<txid>, <use_cache default value is false.>)
189
+
190
+ # example
188
191
  api.get_outputs_from_txid('3fba8bfb157ae29c293d5bd65c178fec169a87f880e2e62537fcce26612a6aa3')
189
192
  > [{
190
193
  "address": "14M4kbAtn71P1nnNYuhBDFTNYxa19t1XP6",
@@ -21,6 +21,7 @@ module OpenAssets
21
21
  if config
22
22
  @config.update(config)
23
23
  end
24
+ OpenAssets.configuration = @config
24
25
  if @config[:provider] == 'bitcoind'
25
26
  @provider = Provider::BitcoinCoreProvider.new(@config[:rpc])
26
27
  else
@@ -186,12 +187,7 @@ module OpenAssets
186
187
  end
187
188
 
188
189
  def get_output(txid, output_index)
189
- decode_tx = tx_cache.get(txid)
190
- if decode_tx.nil?
191
- decode_tx = provider.get_transaction(txid, 0)
192
- raise OpenAssets::Transaction::TransactionBuildError, "txid #{txid} could not be retrieved." if decode_tx.nil?
193
- tx_cache.put(txid, decode_tx)
194
- end
190
+ decode_tx = load_cached_tx(txid)
195
191
  tx = Bitcoin::Protocol::Tx.new(decode_tx.htb)
196
192
  colored_outputs = get_color_outputs_from_tx(tx)
197
193
  colored_outputs[output_index]
@@ -214,12 +210,12 @@ module OpenAssets
214
210
  tx.outputs.map{|out| OpenAssets::Protocol::TransactionOutput.new(out.value, out.parsed_script, nil, 0, OpenAssets::Protocol::OutputType::UNCOLORED)}
215
211
  end
216
212
 
217
- # Get tx outputs. This method will always get the latest transaction without the cache.
213
+ # Get tx outputs.
218
214
  # @param[String] txid Transaction ID.
215
+ # @param[Boolean] use_cache If specified true use cache.(default value is false)
219
216
  # @return[Array] Return array of the transaction output Hash with coloring information.
220
- def get_outputs_from_txid(txid)
221
- decode_tx = provider.get_transaction(txid, 0)
222
- raise OpenAssets::Transaction::TransactionBuildError, "txid #{txid} could not be retrieved." if decode_tx.nil?
217
+ def get_outputs_from_txid(txid, use_cache = false)
218
+ decode_tx = use_cache ? load_cached_tx(txid) : load_tx(txid)
223
219
  tx = Bitcoin::Protocol::Tx.new(decode_tx.htb)
224
220
  outputs = get_color_outputs_from_tx(tx)
225
221
  outputs.map.with_index{|out, i|out.to_hash.merge({'txid' => tx.hash, 'vout' => i})}
@@ -323,6 +319,21 @@ module OpenAssets
323
319
  OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
324
320
  end
325
321
 
322
+ def load_tx(txid)
323
+ decode_tx = provider.get_transaction(txid, 0)
324
+ raise OpenAssets::Transaction::TransactionBuildError, "txid #{txid} could not be retrieved." if decode_tx.nil?
325
+ decode_tx
326
+ end
327
+
328
+ def load_cached_tx(txid)
329
+ decode_tx = tx_cache.get(txid)
330
+ if decode_tx.nil?
331
+ decode_tx = load_tx(txid)
332
+ tx_cache.put(txid, decode_tx)
333
+ end
334
+ decode_tx
335
+ end
336
+
326
337
  end
327
338
 
328
339
  end
@@ -0,0 +1,26 @@
1
+ require 'sqlite3'
2
+
3
+ module OpenAssets
4
+ module Cache
5
+
6
+ # The base class of SQLite3 cache implementation.
7
+ class SQLiteBase
8
+
9
+ attr_reader :db
10
+
11
+ # Initializes the connection to the database, and creates the table if needed.
12
+ # @param[String] path The path to the database file. Use ':memory:' for an in-memory database.
13
+ def initialize(path)
14
+ @db = SQLite3::Database.new path
15
+ setup
16
+ end
17
+
18
+ # Setup table ddl, implements by subclass.
19
+ def setup
20
+ raise StandardError.new('need setup method implementation.')
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ module OpenAssets
2
+ module Cache
3
+ class SSLCertificateCache < SQLiteBase
4
+
5
+ def initialize
6
+ path = OpenAssets.configuration ? OpenAssets.configuration[:cache] : ':memory:'
7
+ super(path)
8
+ end
9
+
10
+ def setup
11
+ db.execute <<-SQL
12
+ CREATE TABLE IF NOT EXISTS SslCertificate(
13
+ Url TEXT,
14
+ Subject TEXT,
15
+ ExpireDate TEXT,
16
+ PRIMARY KEY (Url))
17
+ SQL
18
+ end
19
+
20
+ # Return the subject value which defined by ssl certificate.
21
+ # @param[String] url The URL of asset definition file.
22
+ # @return[String] The subject value. If not found, return nil.
23
+ def get(url)
24
+ rows = db.execute('SELECT Subject,ExpireDate FROM SslCertificate WHERE Url = ?', [url])
25
+ return nil if rows.empty?
26
+ if rows[0][1].to_i < Time.now.to_i
27
+ db.execute('DELETE FROM SslCertificate where Url = ?', [url])
28
+ nil
29
+ else
30
+ rows[0][0]
31
+ end
32
+ end
33
+
34
+ # Saves a serialized transaction in cache.
35
+ # @param[String] url The URL of asset definition file.
36
+ # @param[String] subject The SSL Certificate subject value.
37
+ # @param[Time] expire_date The expire date of SSL Certificate.
38
+ def put(url, subject, expire_date)
39
+ db.execute('INSERT INTO SslCertificate (Url, Subject, ExpireDate) VALUES (?, ?, ?)', [url, subject, expire_date.to_i])
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -1,17 +1,11 @@
1
- require 'sqlite3'
2
1
  module OpenAssets
3
2
  module Cache
4
3
 
5
4
  # An object that can be used for caching serialized transaction in a Sqlite database.
6
- class TransactionCache
5
+ class TransactionCache < SQLiteBase
7
6
 
8
- attr_reader :db
9
-
10
- # Initializes the connection to the database, and creates the table if needed.
11
- # @param[String] path The path to the database file. Use ':memory:' for an in-memory database.
12
- def initialize(path)
13
- @db = SQLite3::Database.new path
14
- @db.execute <<-SQL
7
+ def setup
8
+ db.execute <<-SQL
15
9
  CREATE TABLE IF NOT EXISTS Tx(
16
10
  TransactionHash BLOB,
17
11
  SerializedTx BLOB,
@@ -1,5 +1,7 @@
1
1
  module OpenAssets
2
2
  module Cache
3
+ autoload :SQLiteBase, 'openassets/cache/sqlite_base'
3
4
  autoload :TransactionCache, 'openassets/cache/transaction_cache'
5
+ autoload :SSLCertificateCache, 'openassets/cache/ssl_certificate_cache'
4
6
  end
5
7
  end
@@ -5,6 +5,9 @@ module OpenAssets
5
5
 
6
6
  # The Definition of Open Asset
7
7
  class AssetDefinition
8
+ include OpenAssets::MethodFilter
9
+
10
+ before_filter :clear_poa_cache, {:include => [:issuer=, :asset_definition_url=, :link_to_website=]}
8
11
 
9
12
  attr_accessor :asset_definition_url
10
13
 
@@ -21,6 +24,7 @@ module OpenAssets
21
24
  attr_accessor :icon_url
22
25
  attr_accessor :image_url
23
26
  attr_accessor :version
27
+ attr_accessor :proof_of_authenticity
24
28
 
25
29
  def initialize
26
30
  @asset_ids = []
@@ -89,17 +93,33 @@ module OpenAssets
89
93
  private
90
94
  def calc_proof_of_authenticity
91
95
  result = false
92
- unless asset_definition_url.nil?
93
- client = HTTPClient.new
94
- response = client.get(asset_definition_url, :follow_redirect => true)
96
+ if !asset_definition_url.nil? && link_to_website
97
+ subject = ssl_certificate_subject
98
+ return true if !subject.nil? && subject == issuer
99
+ end
100
+ result
101
+ end
102
+
103
+ def clear_poa_cache
104
+ @proof_of_authenticity = nil
105
+ end
106
+
107
+ def ssl_certificate_subject
108
+ cache = OpenAssets::Cache::SSLCertificateCache.new
109
+ subject = cache.get(asset_definition_url)
110
+ if subject.nil?
111
+ response = HTTPClient.new.get(asset_definition_url, :follow_redirect => true)
95
112
  cert = response.peer_cert
96
113
  unless cert.nil?
97
- subject = response.peer_cert.subject.to_a
98
- o = subject.find{|x|x[0] == 'O'}
99
- result = true if !o.nil? && o.length > 2 && o[1] == issuer
114
+ subjects = cert.subject.to_a
115
+ o = subjects.find{|x|x[0] == 'O'}
116
+ if !o.nil? && o.length > 2
117
+ subject = o[1]
118
+ cache.put(asset_definition_url, subject, cert.not_after)
119
+ end
100
120
  end
101
121
  end
102
- result
122
+ subject
103
123
  end
104
124
  end
105
125
 
@@ -48,6 +48,7 @@ module OpenAssets
48
48
  @asset_definition.divisibility
49
49
  end
50
50
 
51
+ # Verify proof of authenticity.
51
52
  def proof_of_authenticity
52
53
  valid_asset_definition? ? @asset_definition.proof_of_authenticity : false
53
54
  end
@@ -1,3 +1,3 @@
1
1
  module OpenAssets
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.6'
3
3
  end
data/lib/openassets.rb CHANGED
@@ -12,4 +12,8 @@ module OpenAssets
12
12
  autoload :SendAssetParam, 'openassets/send_asset_param'
13
13
  autoload :Cache, 'openassets/cache'
14
14
 
15
+ class << self
16
+ attr_accessor :configuration
17
+ end
18
+
15
19
  end
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.9"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec"
29
-
29
+ spec.add_development_dependency "timecop"
30
30
  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.3.5
4
+ version: 0.3.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-02-13 00:00:00.000000000 Z
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bitcoin-ruby
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: The implementation of the Open Assets Protocol for Ruby.
126
140
  email:
127
141
  - azuchi@haw.co.jp
@@ -146,6 +160,8 @@ files:
146
160
  - lib/openassets.rb
147
161
  - lib/openassets/api.rb
148
162
  - lib/openassets/cache.rb
163
+ - lib/openassets/cache/sqlite_base.rb
164
+ - lib/openassets/cache/ssl_certificate_cache.rb
149
165
  - lib/openassets/cache/transaction_cache.rb
150
166
  - lib/openassets/error.rb
151
167
  - lib/openassets/medhod_filter.rb
@@ -191,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
207
  version: '0'
192
208
  requirements: []
193
209
  rubyforge_project:
194
- rubygems_version: 2.4.6
210
+ rubygems_version: 2.4.5
195
211
  signing_key:
196
212
  specification_version: 4
197
213
  summary: The implementation of the Open Assets Protocol for Ruby.