hyperledger-fabric-sdk 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fabric/block_decoder.rb +299 -0
  3. data/lib/fabric/chaincode_response.rb +47 -0
  4. data/lib/fabric/channel.rb +55 -34
  5. data/lib/fabric/client.rb +107 -25
  6. data/lib/fabric/configuration.rb +12 -4
  7. data/lib/fabric/constants.rb +1 -1
  8. data/lib/fabric/crypto_suite.rb +145 -0
  9. data/lib/fabric/enumerator_queue.rb +26 -0
  10. data/lib/fabric/error.rb +52 -0
  11. data/lib/fabric/event_hub.rb +81 -0
  12. data/lib/fabric/helper.rb +31 -0
  13. data/lib/fabric/identity.rb +25 -19
  14. data/lib/fabric/logger.rb +37 -0
  15. data/lib/fabric/orderer.rb +18 -2
  16. data/lib/fabric/peer.rb +15 -36
  17. data/lib/fabric/proposal.rb +100 -0
  18. data/lib/{protos → fabric/protos}/common/collection_pb.rb +14 -14
  19. data/lib/{protos → fabric/protos}/common/common_pb.rb +37 -36
  20. data/lib/fabric/protos/common/configtx_pb.rb +71 -0
  21. data/lib/{protos → fabric/protos}/common/configuration_pb.rb +13 -13
  22. data/lib/{protos → fabric/protos}/common/ledger_pb.rb +2 -2
  23. data/lib/{protos → fabric/protos}/common/policies_pb.rb +19 -19
  24. data/lib/fabric/protos/discovery/protocol_pb.rb +120 -0
  25. data/lib/{protos → fabric/protos}/discovery/protocol_services_pb.rb +0 -1
  26. data/lib/fabric/protos/gossip/message_pb.rb +233 -0
  27. data/lib/{protos → fabric/protos}/gossip/message_services_pb.rb +0 -1
  28. data/lib/{protos → fabric/protos}/idemix/idemix_pb.rb +37 -37
  29. data/lib/{protos → fabric/protos}/ledger/queryresult/kv_query_result_pb.rb +5 -5
  30. data/lib/fabric/protos/ledger/rwset/kvrwset/kv_rwset_pb.rb +85 -0
  31. data/lib/fabric/protos/ledger/rwset/rwset_pb.rb +46 -0
  32. data/lib/{protos → fabric/protos}/msp/identities_pb.rb +4 -4
  33. data/lib/{protos → fabric/protos}/msp/msp_config_pb.rb +27 -27
  34. data/lib/{protos → fabric/protos}/msp/msp_principal_pb.rb +20 -20
  35. data/lib/fabric/protos/orderer/ab_pb.rb +52 -0
  36. data/lib/{protos → fabric/protos}/orderer/ab_services_pb.rb +0 -1
  37. data/lib/fabric/protos/orderer/cluster_pb.rb +31 -0
  38. data/lib/fabric/protos/orderer/cluster_services_pb.rb +31 -0
  39. data/lib/{protos → fabric/protos}/orderer/configuration_pb.rb +11 -10
  40. data/lib/fabric/protos/orderer/etcdraft/configuration_pb.rb +21 -0
  41. data/lib/{protos → fabric/protos}/orderer/kafka_pb.rb +16 -16
  42. data/lib/{protos → fabric/protos}/peer/admin_pb.rb +12 -12
  43. data/lib/{protos → fabric/protos}/peer/admin_services_pb.rb +0 -1
  44. data/lib/{protos → fabric/protos}/peer/chaincode_event_pb.rb +2 -2
  45. data/lib/fabric/protos/peer/chaincode_pb.rb +60 -0
  46. data/lib/fabric/protos/peer/chaincode_shim_pb.rb +129 -0
  47. data/lib/{protos → fabric/protos}/peer/chaincode_shim_services_pb.rb +0 -1
  48. data/lib/{protos → fabric/protos}/peer/configuration_pb.rb +10 -10
  49. data/lib/fabric/protos/peer/events_pb.rb +45 -0
  50. data/lib/fabric/protos/peer/events_services_pb.rb +34 -0
  51. data/lib/{protos → fabric/protos}/peer/peer_pb.rb +5 -5
  52. data/lib/{protos → fabric/protos}/peer/peer_services_pb.rb +0 -1
  53. data/lib/{protos → fabric/protos}/peer/proposal_pb.rb +13 -13
  54. data/lib/{protos → fabric/protos}/peer/proposal_response_pb.rb +11 -11
  55. data/lib/{protos → fabric/protos}/peer/query_pb.rb +10 -10
  56. data/lib/{protos → fabric/protos}/peer/resources_pb.rb +12 -12
  57. data/lib/{protos → fabric/protos}/peer/signed_cc_dep_spec_pb.rb +3 -3
  58. data/lib/{protos → fabric/protos}/peer/transaction_pb.rb +22 -18
  59. data/lib/fabric/protos/token/prover_pb.rb +66 -0
  60. data/lib/fabric/protos/token/prover_services_pb.rb +33 -0
  61. data/lib/fabric/protos/token/transaction_pb.rb +41 -0
  62. data/lib/{protos → fabric/protos}/transientstore/transientstore_pb.rb +4 -4
  63. data/lib/fabric/queue.rb +26 -0
  64. data/lib/fabric/transaction.rb +51 -0
  65. data/lib/fabric/transaction_info.rb +20 -0
  66. data/lib/fabric/version.rb +1 -1
  67. data/lib/fabric.rb +74 -0
  68. data/lib/fabric_ca/attribute.rb +7 -0
  69. data/lib/fabric_ca/client.rb +11 -19
  70. data/lib/fabric_ca/configuration.rb +4 -2
  71. data/lib/fabric_ca/connection.rb +22 -20
  72. data/lib/fabric_ca/error.rb +3 -2
  73. data/lib/fabric_ca/faraday_middleware/basic_auth.rb +3 -3
  74. data/lib/fabric_ca/faraday_middleware/raise_http_exception.rb +13 -13
  75. data/lib/fabric_ca/faraday_middleware/token_auth.rb +10 -12
  76. data/lib/fabric_ca/request.rb +13 -24
  77. data/lib/fabric_ca/version.rb +1 -1
  78. data/lib/fabric_ca.rb +16 -0
  79. data/lib/hyperledger-fabric-sdk.rb +2 -47
  80. metadata +161 -61
  81. data/lib/crypto_suite/ecdsa_aes.rb +0 -86
  82. data/lib/fabric/peer_endorser.rb +0 -85
  83. data/lib/fabric/transaction_id.rb +0 -11
  84. data/lib/fabric/user.rb +0 -24
  85. data/lib/fabric_ca/tools.rb +0 -35
  86. data/lib/protos/common/configtx_pb.rb +0 -71
  87. data/lib/protos/discovery/protocol_pb.rb +0 -119
  88. data/lib/protos/gossip/message_pb.rb +0 -233
  89. data/lib/protos/ledger/rwset/kvrwset/kv_rwset_pb.rb +0 -85
  90. data/lib/protos/ledger/rwset/rwset_pb.rb +0 -46
  91. data/lib/protos/orderer/ab_pb.rb +0 -52
  92. data/lib/protos/peer/chaincode_pb.rb +0 -60
  93. data/lib/protos/peer/chaincode_shim_pb.rb +0 -94
  94. data/lib/protos/peer/events_pb.rb +0 -98
  95. data/lib/protos/peer/events_services_pb.rb +0 -59
data/lib/fabric.rb ADDED
@@ -0,0 +1,74 @@
1
+ lib_dir = File.join(__dir__, 'fabric/protos')
2
+
3
+ $LOAD_PATH.push(lib_dir) unless $LOAD_PATH.include?(lib_dir)
4
+
5
+ require 'colorize'
6
+
7
+ require 'common/common_pb'
8
+ require 'common/configtx_pb'
9
+ require 'peer/events_services_pb'
10
+ require 'ledger/rwset/rwset_pb'
11
+ require 'ledger/rwset/kvrwset/kv_rwset_pb'
12
+
13
+ require_relative 'fabric/version'
14
+ require_relative 'fabric/configuration'
15
+ require_relative 'fabric/constants'
16
+ require_relative 'fabric/client'
17
+ require_relative 'fabric/peer'
18
+ require_relative 'fabric/orderer'
19
+ require_relative 'fabric/enumerator_queue'
20
+ require_relative 'fabric/crypto_suite'
21
+ require_relative 'fabric/identity'
22
+ require_relative 'fabric/proposal'
23
+ require_relative 'fabric/transaction'
24
+ require_relative 'fabric/error'
25
+ require_relative 'fabric/logger'
26
+ require_relative 'fabric/chaincode_response'
27
+ require_relative 'fabric/channel'
28
+ require_relative 'fabric/transaction_info'
29
+ require_relative 'fabric/helper'
30
+ require_relative 'fabric/block_decoder'
31
+ require_relative 'fabric/event_hub'
32
+
33
+ module Fabric
34
+ extend Configuration
35
+ @orderers = []
36
+ @peers = []
37
+
38
+ def self.new(config)
39
+ @orderers = config[:orderers]
40
+ @peers = config[:peers]
41
+ self
42
+ end
43
+
44
+ def self.client(opts = {})
45
+ client = Fabric::Client.new opts
46
+
47
+ @orderers.each { |url| client.register_orderer url, opts }
48
+ @peers.each { |url| client.register_peer url, opts }
49
+
50
+ client
51
+ end
52
+
53
+ def self.channel(opts = {})
54
+ channel = Fabric::Channel.new opts
55
+
56
+ @orderers.each { |url| channel.register_orderer url, opts }
57
+ @peers.each { |url| channel.register_peer url, opts }
58
+
59
+ channel
60
+ end
61
+
62
+ def self.event_hub(opts = {})
63
+ options = Fabric.options.merge opts
64
+
65
+ options[:crypto_suite] ||= Fabric.crypto_suite
66
+ options[:url] = options[:event_hub_url]
67
+
68
+ Fabric::EventHub.new options
69
+ end
70
+
71
+ def self.crypto_suite(opts = {})
72
+ @crypto_suite ||= Fabric::CryptoSuite.new opts
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ module FabricCA
2
+ class Attribute
3
+ REGISTRAR_ROLES = 'hf.Registrar.Roles'.freeze
4
+ REGISTRAR_DELEGATE_ROLES = 'hf.Registrar.DelegateRoles'.freeze
5
+ REGISTRAR_ATTRIBUTES = 'hf.Registrar.Attributes'.freeze
6
+ end
7
+ end
@@ -1,12 +1,9 @@
1
- require File.expand_path('../connection', __FILE__)
2
- require File.expand_path('../request', __FILE__)
3
-
4
1
  module FabricCA
5
2
  class Client
6
3
  include Connection
7
4
  include Request
8
5
 
9
- attr_accessor *Configuration::VALID_OPTIONS_KEYS
6
+ Configuration::VALID_OPTIONS_KEYS.each { |attr| attr_accessor attr }
10
7
 
11
8
  def initialize(options = {})
12
9
  options = FabricCA.options.merge(options)
@@ -25,27 +22,22 @@ module FabricCA
25
22
  conf
26
23
  end
27
24
 
28
- def enroll(identity_context, msp_id)
29
- crypto_suite = identity_context.crypto_suite
30
- private_key = crypto_suite.generate_private_key
31
- certificate_request = crypto_suite.generate_csr private_key,
32
- [['CN', identity_context.username]]
33
-
34
- response = post 'enroll', certificate_request: certificate_request.to_s
35
-
36
- identity_context.enroll private_key: private_key,
37
- certificate: Base64.decode64(response.result['Cert']),
38
- msp_id: msp_id
39
-
40
- identity_context
25
+ def enroll(certificate_request, attrs = nil)
26
+ post 'enroll', certificate_request: certificate_request.to_s,
27
+ caName: ca_name,
28
+ attr_reqs: attrs
41
29
  end
42
30
 
43
31
  def register(params = {})
44
32
  post 'register', params
45
33
  end
46
34
 
47
- def tcert(params = {})
48
- post 'tcert', params
35
+ def list_identities(opts = {})
36
+ get 'identities', opts
37
+ end
38
+
39
+ def list_affiliations(opts = {})
40
+ get 'affiliations', opts
49
41
  end
50
42
  end
51
43
  end
@@ -1,8 +1,10 @@
1
1
  module FabricCA
2
2
  module Configuration
3
- VALID_OPTIONS_KEYS = [:endpoint, :identity_context, :username, :password, :logger]
3
+ VALID_OPTIONS_KEYS = %i[endpoint ca_name identity
4
+ username password logger
5
+ connection_opts].freeze
4
6
 
5
- attr_accessor *VALID_OPTIONS_KEYS
7
+ VALID_OPTIONS_KEYS.each { |attr| attr_accessor attr }
6
8
 
7
9
  def self.extended(base)
8
10
  base.reset
@@ -1,32 +1,34 @@
1
1
  require 'faraday_middleware'
2
2
 
3
- Dir[File.expand_path('../faraday_middleware/*.rb', __FILE__)].each { |f| require_relative f }
3
+ Dir[File.expand_path('faraday_middleware/*.rb', __dir__)].each { |f| require_relative f }
4
4
 
5
5
  module FabricCA
6
6
  module Connection
7
7
  private
8
8
 
9
- def connection(opts = {})
10
- Faraday::Connection.new(opts.merge(options)) do |connection|
11
- connection.use ::FaradayMiddleware::EncodeJson
12
- connection.use FabricCA::FaradayMiddleware::BasicAuth, username, password
13
- connection.use FabricCA::FaradayMiddleware::TokenAuth, identity_context
14
- connection.use ::Faraday::Request::UrlEncoded
15
- connection.use ::FaradayMiddleware::Mashify
16
- connection.use ::Faraday::Response::ParseJson
17
- connection.use FabricCA::FaradayMiddleware::RaiseHttpException, logger
18
- connection.adapter(adapter)
19
- end
9
+ def connection(opts = {})
10
+ Faraday::Connection.new(opts.merge(options)) do |connection|
11
+ connection.use ::FaradayMiddleware::EncodeJson
12
+ connection.use FabricCA::FaradayMiddleware::BasicAuth, username, password
13
+ connection.use FabricCA::FaradayMiddleware::TokenAuth, identity
14
+ connection.use ::Faraday::Request::UrlEncoded
15
+ connection.use ::FaradayMiddleware::Mashify
16
+ connection.use ::Faraday::Response::ParseJson
17
+ connection.use FabricCA::FaradayMiddleware::RaiseHttpException, logger
18
+ connection.adapter(adapter)
20
19
  end
20
+ end
21
21
 
22
- def options
23
- {
24
- url: endpoint
25
- }
26
- end
22
+ def options
23
+ opts = connection_opts || {}
27
24
 
28
- def adapter
29
- Faraday.default_adapter
30
- end
25
+ opts[:url] = endpoint
26
+
27
+ opts
28
+ end
29
+
30
+ def adapter
31
+ Faraday.default_adapter
32
+ end
31
33
  end
32
34
  end
@@ -1,11 +1,12 @@
1
1
  module FabricCA
2
2
  class Error < StandardError
3
- attr_reader :response
3
+ attr_reader :response, :full_message
4
4
 
5
5
  def initialize(message, response)
6
6
  super message
7
7
 
8
- @response = response
8
+ @response = Hashie::Mash.new JSON.parse(response.body)
9
+ @full_message = @response.errors.map(&:message).join(', ')
9
10
  end
10
11
  end
11
12
  end
@@ -25,9 +25,9 @@ module FabricCA
25
25
 
26
26
  private
27
27
 
28
- def generate_auth_header
29
- Base64.encode64 "#{username}:#{password}"
30
- end
28
+ def generate_auth_header
29
+ Base64.strict_encode64 "#{username}:#{password}"
30
+ end
31
31
  end
32
32
  end
33
33
  end
@@ -4,7 +4,7 @@ module FabricCA
4
4
  module FaradayMiddleware
5
5
  class RaiseHttpException < Faraday::Middleware
6
6
  ## Variables
7
- LOGGER_TAG = 'FABRIC CA'
7
+ LOGGER_TAG = 'FABRIC CA'.freeze
8
8
 
9
9
  ## Attributes
10
10
  attr_reader :app, :logger
@@ -28,20 +28,20 @@ module FabricCA
28
28
 
29
29
  private
30
30
 
31
- def build_error_message(response)
32
- [
33
- response[:method].to_s.upcase,
34
- response[:url],
35
- response[:status],
36
- response[:body]
37
- ].join(', ')
38
- end
31
+ def build_error_message(response)
32
+ [
33
+ response[:method].to_s.upcase,
34
+ response[:url],
35
+ response[:status],
36
+ response[:body]
37
+ ].join(', ')
38
+ end
39
39
 
40
- def logging(message)
41
- return unless logger
40
+ def logging(message)
41
+ return unless logger
42
42
 
43
- logger.tagged(LOGGER_TAG) { |logger| logger.error message }
44
- end
43
+ logger.tagged(LOGGER_TAG) { |logger| logger.error message }
44
+ end
45
45
  end
46
46
  end
47
47
  end
@@ -4,10 +4,10 @@ require 'base64'
4
4
  module FabricCA
5
5
  module FaradayMiddleware
6
6
  class TokenAuth < Faraday::Middleware
7
- attr_reader :app, :identity_context
7
+ attr_reader :app, :identity
8
8
 
9
9
  def call(env)
10
- if identity_context&.enrolled?
10
+ if identity
11
11
  env[:request_headers] =
12
12
  env[:request_headers].merge(
13
13
  'Authorization' => generate_auth_header(env)
@@ -17,22 +17,20 @@ module FabricCA
17
17
  app.call env
18
18
  end
19
19
 
20
- def initialize(app, identity_context)
20
+ def initialize(app, identity)
21
21
  @app = app
22
- @identity_context = identity_context
22
+ @identity = identity
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def generate_auth_header(env)
28
- body = Base64.strict_encode64(env.body.to_s)
29
- identity = identity_context.identity
30
- cert = Base64.strict_encode64 identity.certificate
31
- body_and_cert = body + '.' + cert
32
- sign = Base64.strict_encode64 identity.sign(body_and_cert)
27
+ def generate_auth_header(env)
28
+ body = Base64.strict_encode64(env.body.to_s)
29
+ body_and_cert = body + '.' + identity.certificate
33
30
 
34
- cert + '.' + sign
35
- end
31
+ identity.certificate + '.' +
32
+ Base64.strict_encode64(identity.sign(body_and_cert))
33
+ end
36
34
  end
37
35
  end
38
36
  end
@@ -18,33 +18,22 @@ module FabricCA
18
18
 
19
19
  private
20
20
 
21
- def request(method, path, params, options)
22
- response = send_request method, path, params, options
21
+ def request(method, path, params, options)
22
+ response = send_request method, path, params, options
23
23
 
24
- Response.create response.body
25
- end
24
+ Response.create response.body
25
+ end
26
26
 
27
- def send_request(method, path, params, options)
28
- connection(options).send(method) do |request|
29
- case method
30
- when :get, :delete
31
- request.url(URI.encode(path), params)
32
- when :post, :put
33
- p path
34
- p params.to_json
35
- p method
36
- p options
37
- request.path = URI.encode path
38
- request.body = params unless params.empty?
39
- request.options.params_encoder = Encoder.new
40
- end
27
+ def send_request(method, path, params, options)
28
+ connection(options).send(method) do |request|
29
+ case method
30
+ when :get, :delete
31
+ request.url(URI.encode(path), params)
32
+ when :post, :put
33
+ request.path = URI.encode path
34
+ request.body = params unless params.empty?
41
35
  end
42
36
  end
43
- end
44
- end
45
-
46
- class Encoder
47
- def encode(param)
48
- param.to_json
37
+ end
49
38
  end
50
39
  end
@@ -1,3 +1,3 @@
1
1
  module FabricCA
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.1'.freeze
3
3
  end
data/lib/fabric_ca.rb ADDED
@@ -0,0 +1,16 @@
1
+ require_relative 'fabric_ca/version'
2
+ require_relative 'fabric_ca/configuration'
3
+ require_relative 'fabric_ca/error'
4
+ require_relative 'fabric_ca/connection'
5
+ require_relative 'fabric_ca/request'
6
+ require_relative 'fabric_ca/response'
7
+ require_relative 'fabric_ca/client'
8
+ require_relative 'fabric_ca/attribute'
9
+
10
+ module FabricCA
11
+ extend Configuration
12
+
13
+ def self.new(options = {})
14
+ FabricCA::Client.new(options)
15
+ end
16
+ end
@@ -1,47 +1,2 @@
1
- lib_dir = File.join(File.expand_path(File.dirname(__FILE__)), 'protos')
2
- $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
3
-
4
- require_relative 'fabric/version'
5
- require_relative 'fabric/configuration'
6
- require_relative 'fabric/constants'
7
- require_relative 'fabric/client'
8
- require_relative 'fabric/peer_endorser'
9
- require_relative 'fabric/transaction_id'
10
- require_relative 'fabric/channel'
11
- require_relative 'fabric/peer'
12
- require_relative 'fabric/orderer'
13
- require_relative 'fabric/user'
14
- require_relative 'fabric/identity'
15
-
16
- require_relative 'fabric_ca/error'
17
- require_relative 'fabric_ca/configuration'
18
- require_relative 'fabric_ca/client'
19
- require_relative 'fabric_ca/response'
20
- require_relative 'fabric_ca/version'
21
- require_relative 'fabric_ca/tools'
22
-
23
- require_relative 'crypto_suite/ecdsa_aes'
24
-
25
- require 'common/common_pb'
26
- require 'common/configtx_pb'
27
-
28
- module Fabric
29
- extend Configuration
30
-
31
- def self.new(config)
32
- client = Fabric::Client.new config[:options]
33
-
34
- config[:orderer_urls].each { |url| client.new_orderer url }
35
- config[:peer_urls].each { |url| client.new_peer url }
36
-
37
- client
38
- end
39
- end
40
-
41
- module FabricCA
42
- extend Configuration
43
-
44
- def self.new(options = {})
45
- FabricCA::Client.new(options)
46
- end
47
- end
1
+ require_relative 'fabric'
2
+ require_relative 'fabric_ca'