hyperledger-fabric-sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/lib/crypto_suite/ecdsa_aes.rb +86 -0
  3. data/lib/fabric/channel.rb +54 -0
  4. data/lib/fabric/client.rb +45 -0
  5. data/lib/fabric/configuration.rb +25 -0
  6. data/lib/fabric/constants.rb +18 -0
  7. data/lib/fabric/identity.rb +36 -0
  8. data/lib/fabric/orderer.rb +19 -0
  9. data/lib/fabric/peer.rb +58 -0
  10. data/lib/fabric/peer_endorser.rb +85 -0
  11. data/lib/fabric/transaction_id.rb +11 -0
  12. data/lib/fabric/user.rb +24 -0
  13. data/lib/fabric/version.rb +3 -0
  14. data/lib/fabric_ca/client.rb +51 -0
  15. data/lib/fabric_ca/configuration.rb +25 -0
  16. data/lib/fabric_ca/connection.rb +32 -0
  17. data/lib/fabric_ca/error.rb +11 -0
  18. data/lib/fabric_ca/faraday_middleware/basic_auth.rb +33 -0
  19. data/lib/fabric_ca/faraday_middleware/raise_http_exception.rb +47 -0
  20. data/lib/fabric_ca/faraday_middleware/token_auth.rb +38 -0
  21. data/lib/fabric_ca/request.rb +50 -0
  22. data/lib/fabric_ca/response.rb +10 -0
  23. data/lib/fabric_ca/tools.rb +35 -0
  24. data/lib/fabric_ca/version.rb +3 -0
  25. data/lib/hyperledger-fabric-sdk.rb +47 -0
  26. data/lib/protos/common/collection_pb.rb +42 -0
  27. data/lib/protos/common/common_pb.rb +106 -0
  28. data/lib/protos/common/configtx_pb.rb +71 -0
  29. data/lib/protos/common/configuration_pb.rb +33 -0
  30. data/lib/protos/common/ledger_pb.rb +16 -0
  31. data/lib/protos/common/policies_pb.rb +52 -0
  32. data/lib/protos/discovery/protocol_pb.rb +119 -0
  33. data/lib/protos/discovery/protocol_services_pb.rb +30 -0
  34. data/lib/protos/gossip/message_pb.rb +233 -0
  35. data/lib/protos/gossip/message_services_pb.rb +31 -0
  36. data/lib/protos/idemix/idemix_pb.rb +93 -0
  37. data/lib/protos/ledger/queryresult/kv_query_result_pb.rb +24 -0
  38. data/lib/protos/ledger/rwset/kvrwset/kv_rwset_pb.rb +85 -0
  39. data/lib/protos/ledger/rwset/rwset_pb.rb +46 -0
  40. data/lib/protos/msp/identities_pb.rb +23 -0
  41. data/lib/protos/msp/msp_config_pb.rb +72 -0
  42. data/lib/protos/msp/msp_principal_pb.rb +54 -0
  43. data/lib/protos/orderer/ab_pb.rb +52 -0
  44. data/lib/protos/orderer/ab_services_pb.rb +41 -0
  45. data/lib/protos/orderer/configuration_pb.rb +32 -0
  46. data/lib/protos/orderer/kafka_pb.rb +45 -0
  47. data/lib/protos/peer/admin_pb.rb +41 -0
  48. data/lib/protos/peer/admin_services_pb.rb +33 -0
  49. data/lib/protos/peer/chaincode_event_pb.rb +17 -0
  50. data/lib/protos/peer/chaincode_pb.rb +60 -0
  51. data/lib/protos/peer/chaincode_shim_pb.rb +94 -0
  52. data/lib/protos/peer/chaincode_shim_services_pb.rb +30 -0
  53. data/lib/protos/peer/configuration_pb.rb +27 -0
  54. data/lib/protos/peer/events_pb.rb +98 -0
  55. data/lib/protos/peer/events_services_pb.rb +59 -0
  56. data/lib/protos/peer/peer_pb.rb +21 -0
  57. data/lib/protos/peer/peer_services_pb.rb +37 -0
  58. data/lib/protos/peer/proposal_pb.rb +40 -0
  59. data/lib/protos/peer/proposal_response_pb.rb +35 -0
  60. data/lib/protos/peer/query_pb.rb +32 -0
  61. data/lib/protos/peer/resources_pb.rb +34 -0
  62. data/lib/protos/peer/signed_cc_dep_spec_pb.rb +17 -0
  63. data/lib/protos/peer/transaction_pb.rb +72 -0
  64. data/lib/protos/transientstore/transientstore_pb.rb +18 -0
  65. metadata +123 -0
@@ -0,0 +1,11 @@
1
+ module Fabric
2
+ class TransactionID
3
+ attr_reader :nonce, :identity_context, :id
4
+
5
+ def initialize(identity_context)
6
+ @identity_context = identity_context
7
+ @nonce = identity_context.crypto_suite.generate_nonce
8
+ @id = identity_context.crypto_suite.hexdigest(nonce + identity_context.identity.serialize)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Fabric
2
+ class User
3
+ attr_reader :username, :roles, :affiliation,
4
+ :identity, :crypto_suite
5
+
6
+ def initialize(args)
7
+ @crypto_suite = args[:crypto_suite]
8
+ @username = args[:username]
9
+ @roles = args[:roles] || []
10
+ @affiliation = args[:affiliation]
11
+ @identity = nil
12
+ end
13
+
14
+ def enroll(opts)
15
+ opts[:crypto_suite] = crypto_suite
16
+
17
+ @identity = Identity.new opts
18
+ end
19
+
20
+ def enrolled?
21
+ identity
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Fabric
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../connection', __FILE__)
2
+ require File.expand_path('../request', __FILE__)
3
+
4
+ module FabricCA
5
+ class Client
6
+ include Connection
7
+ include Request
8
+
9
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
10
+
11
+ def initialize(options = {})
12
+ options = FabricCA.options.merge(options)
13
+
14
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
15
+ send("#{key}=", options[key])
16
+ end
17
+ end
18
+
19
+ def config
20
+ conf = {}
21
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
22
+ conf[key] = send key
23
+ end
24
+
25
+ conf
26
+ end
27
+
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
41
+ end
42
+
43
+ def register(params = {})
44
+ post 'register', params
45
+ end
46
+
47
+ def tcert(params = {})
48
+ post 'tcert', params
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ module FabricCA
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [:endpoint, :identity_context, :username, :password, :logger]
4
+
5
+ attr_accessor *VALID_OPTIONS_KEYS
6
+
7
+ def self.extended(base)
8
+ base.reset
9
+ end
10
+
11
+ def configure
12
+ yield self
13
+ end
14
+
15
+ def options
16
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
17
+ option.merge!(key => send(key))
18
+ end
19
+ end
20
+
21
+ def reset
22
+ VALID_OPTIONS_KEYS.each { |key| send("#{key}=", nil) }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'faraday_middleware'
2
+
3
+ Dir[File.expand_path('../faraday_middleware/*.rb', __FILE__)].each { |f| require_relative f }
4
+
5
+ module FabricCA
6
+ module Connection
7
+ private
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
20
+ end
21
+
22
+ def options
23
+ {
24
+ url: endpoint
25
+ }
26
+ end
27
+
28
+ def adapter
29
+ Faraday.default_adapter
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module FabricCA
2
+ class Error < StandardError
3
+ attr_reader :response
4
+
5
+ def initialize(message, response)
6
+ super message
7
+
8
+ @response = response
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'faraday'
2
+ require 'base64'
3
+
4
+ module FabricCA
5
+ module FaradayMiddleware
6
+ class BasicAuth < Faraday::Middleware
7
+ attr_reader :app, :username, :password
8
+
9
+ def call(env)
10
+ if username && password
11
+ env[:request_headers] =
12
+ env[:request_headers].merge(
13
+ 'Authorization' => "Basic #{generate_auth_header}"
14
+ )
15
+ end
16
+
17
+ app.call env
18
+ end
19
+
20
+ def initialize(app, username, password)
21
+ @app = app
22
+ @username = username
23
+ @password = password
24
+ end
25
+
26
+ private
27
+
28
+ def generate_auth_header
29
+ Base64.encode64 "#{username}:#{password}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ require 'faraday'
2
+
3
+ module FabricCA
4
+ module FaradayMiddleware
5
+ class RaiseHttpException < Faraday::Middleware
6
+ ## Variables
7
+ LOGGER_TAG = 'FABRIC CA'
8
+
9
+ ## Attributes
10
+ attr_reader :app, :logger
11
+
12
+ def call(env)
13
+ app.call(env).on_complete do |response|
14
+ case response[:status].to_i
15
+ when 400..504
16
+ message = build_error_message response
17
+ logging message
18
+
19
+ raise FabricCA::Error.new message, response
20
+ end
21
+ end
22
+ end
23
+
24
+ def initialize(app, logger)
25
+ @app = app
26
+ @logger = logger
27
+ end
28
+
29
+ private
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
39
+
40
+ def logging(message)
41
+ return unless logger
42
+
43
+ logger.tagged(LOGGER_TAG) { |logger| logger.error message }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ require 'faraday'
2
+ require 'base64'
3
+
4
+ module FabricCA
5
+ module FaradayMiddleware
6
+ class TokenAuth < Faraday::Middleware
7
+ attr_reader :app, :identity_context
8
+
9
+ def call(env)
10
+ if identity_context&.enrolled?
11
+ env[:request_headers] =
12
+ env[:request_headers].merge(
13
+ 'Authorization' => generate_auth_header(env)
14
+ )
15
+ end
16
+
17
+ app.call env
18
+ end
19
+
20
+ def initialize(app, identity_context)
21
+ @app = app
22
+ @identity_context = identity_context
23
+ end
24
+
25
+ private
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)
33
+
34
+ cert + '.' + sign
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ module FabricCA
2
+ module Request
3
+ def get(path, params = {}, options = {})
4
+ request :get, path, params, options
5
+ end
6
+
7
+ def post(path, params = {}, options = {})
8
+ request :post, path, params, options
9
+ end
10
+
11
+ def put(path, params = {}, options = {})
12
+ request :put, path, params, options
13
+ end
14
+
15
+ def delete(path, params = {}, options = {})
16
+ request :delete, path, params, options
17
+ end
18
+
19
+ private
20
+
21
+ def request(method, path, params, options)
22
+ response = send_request method, path, params, options
23
+
24
+ Response.create response.body
25
+ end
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
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ class Encoder
47
+ def encode(param)
48
+ param.to_json
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ module FabricCA
2
+ module Response
3
+ def self.create(response_hash)
4
+ data = response_hash
5
+ data.extend(self)
6
+
7
+ data
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ module FabricCA
2
+ module Tools
3
+ HEADER_IDENTIFIER = [1, 2, 3, 4, 5, 6, 9].freeze
4
+
5
+ def self.extract_attributes(pem)
6
+ extensions = extract_extensions pem
7
+
8
+ attributes = {}
9
+ extensions[HEADER_IDENTIFIER.join('.')].to_s.split('#').each do |key|
10
+ id, num = key.split('->')
11
+
12
+ attributes[id] = extensions[build_attribute_id(HEADER_IDENTIFIER, num.to_i).join('.')]
13
+ end
14
+
15
+ attributes
16
+ end
17
+
18
+ def self.extract_extensions(pem)
19
+ certificate = OpenSSL::X509::Certificate.new pem
20
+ extensions = {}
21
+ certificate.extensions.each { |ext| extensions[ext.oid] = ext.value }
22
+
23
+ extensions
24
+ end
25
+
26
+ private
27
+
28
+ def self.build_attribute_id(header_identifier, step)
29
+ numbers = header_identifier.dup
30
+ numbers[-1] += step
31
+
32
+ numbers
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module FabricCA
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,47 @@
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