Class: Fabric::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/fabric/entities/identity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key: nil, public_key: nil, certificate: nil, msp_id: nil, crypto_suite: nil) ⇒ Identity

Returns a new instance of Identity.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fabric/entities/identity.rb', line 24

def initialize(private_key: nil, public_key: nil, certificate: nil, msp_id: nil, crypto_suite: nil)
  @crypto_suite = crypto_suite || Fabric.crypto_suite

  @private_key = private_key || @crypto_suite.generate_private_key
  @public_key = public_key || @crypto_suite.restore_public_key(@private_key)
  @certificate = certificate
  @msp_id = msp_id

  @address = @crypto_suite.address_from_public_key @public_key

  return unless @certificate

  raise Fabric::Error, 'Key mismatch (public_key or certificate) for identity' unless validate_key_integrity
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



17
18
19
# File 'lib/fabric/entities/identity.rb', line 17

def address
  @address
end

#certificateString

raw certificate in pem format

Returns:

  • (String)

    the current value of certificate



16
17
18
# File 'lib/fabric/entities/identity.rb', line 16

def certificate
  @certificate
end

#crypto_suiteObject (readonly)

Returns the value of attribute crypto_suite.



17
18
19
# File 'lib/fabric/entities/identity.rb', line 17

def crypto_suite
  @crypto_suite
end

#msp_idString

MSP (Membership Service Provider) Identifier

Returns:

  • (String)

    the current value of msp_id



16
17
18
# File 'lib/fabric/entities/identity.rb', line 16

def msp_id
  @msp_id
end

#private_keyString (readonly)

raw private key in hex format

Returns:

  • (String)

    the current value of private_key



16
17
18
# File 'lib/fabric/entities/identity.rb', line 16

def private_key
  @private_key
end

#public_keyString (readonly)

raw public key in hex format

Returns:

  • (String)

    the current value of public_key



16
17
18
# File 'lib/fabric/entities/identity.rb', line 16

def public_key
  @public_key
end

Instance Method Details

#as_protoObject



68
69
70
# File 'lib/fabric/entities/identity.rb', line 68

def as_proto
  @as_proto ||= Msp::SerializedIdentity.new(mspid: msp_id, id_bytes: certificate)
end

#digest(message) ⇒ Object



59
60
61
# File 'lib/fabric/entities/identity.rb', line 59

def digest(message)
  @crypto_suite.digest message
end

#generate_csr(attrs = []) ⇒ Object



51
52
53
# File 'lib/fabric/entities/identity.rb', line 51

def generate_csr(attrs = [])
  @crypto_suite.generate_csr private_key, attrs
end

#new_gateway(client) ⇒ Fabric::Gateway

Creates a new gateway passing in the current identity

Parameters:

Returns:



83
84
85
# File 'lib/fabric/entities/identity.rb', line 83

def new_gateway(client)
  Fabric::Gateway.new(self, client)
end

#shared_secret_by(public_key) ⇒ Object

TODO: Do we need this?



64
65
66
# File 'lib/fabric/entities/identity.rb', line 64

def shared_secret_by(public_key)
  @crypto_suite.build_shared_key private_key, public_key
end

#sign(message) ⇒ Object



55
56
57
# File 'lib/fabric/entities/identity.rb', line 55

def sign(message)
  @crypto_suite.sign(private_key, message)
end

#to_protoObject



72
73
74
# File 'lib/fabric/entities/identity.rb', line 72

def to_proto
  @to_proto ||= Msp::SerializedIdentity.new(mspid: msp_id, id_bytes: certificate).to_proto
end

#validate_key_integrityboolean

Validates that the private_key, public_key, and certificate are valid and match

Returns:

  • (boolean)

    true if valid, false otherwise



44
45
46
47
48
49
# File 'lib/fabric/entities/identity.rb', line 44

def validate_key_integrity
  cert_pubkey = @crypto_suite.pkey_from_x509_certificate(certificate)
  priv_pubkey = @crypto_suite.restore_public_key(@private_key)

  @public_key == cert_pubkey && @public_key == priv_pubkey
end