Class: IOSTSdk::Crypto::KeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/iost_sdk/crypto.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algo:, public_key:, private_key:) ⇒ Object

Create an instance of KeyPair

Parameters:

  • algo (String)

    the algorithm used to generate the key pair

  • public_key (Ed25519::VerifyKey|OpenSSL::PKey::EC::Point)

    an instance of the public key

  • private_key (Ed25519::SigningKey|OpenSSL::BN)

    an instance of the private key



88
89
90
91
92
# File 'lib/iost_sdk/crypto.rb', line 88

def initialize(algo:, public_key:, private_key:)
  @algo = algo
  @public_key = public_key
  @private_key = private_key
end

Instance Attribute Details

#algoObject (readonly)

Returns the value of attribute algo.



80
81
82
# File 'lib/iost_sdk/crypto.rb', line 80

def algo
  @algo
end

Instance Method Details

#idObject



125
126
127
# File 'lib/iost_sdk/crypto.rb', line 125

def id
  public_key
end

#private_keyObject



112
113
114
# File 'lib/iost_sdk/crypto.rb', line 112

def private_key
  Base58.binary_to_base58(private_key_raw, :bitcoin)
end

#private_key_rawObject



116
117
118
119
120
121
122
123
# File 'lib/iost_sdk/crypto.rb', line 116

def private_key_raw
  if @algo == IOSTSdk::Crypto.key_algos[:Secp256k1]
    # @private_key is in bytes already
    @private_key
  elsif @algo == IOSTSdk::Crypto.key_algos[:Ed25519]
    @private_key.to_bytes
  end
end

#public_keyObject



99
100
101
# File 'lib/iost_sdk/crypto.rb', line 99

def public_key
  Base58.binary_to_base58(public_key_raw, :bitcoin)
end

#public_key_rawObject



103
104
105
106
107
108
109
110
# File 'lib/iost_sdk/crypto.rb', line 103

def public_key_raw
  if @algo == IOSTSdk::Crypto.key_algos[:Secp256k1]
    # @public_key is in bytes already
    @public_key
  elsif @algo == IOSTSdk::Crypto.key_algos[:Ed25519]
    @public_key.to_bytes
  end
end

#sign(message:) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/iost_sdk/crypto.rb', line 129

def sign(message:)
  if @algo == IOSTSdk::Crypto.key_algos[:Secp256k1]
    p_key = BTC::Key.new(private_key: @private_key)
    der_signature = p_key.ecdsa_signature(message)
    decoded_der = OpenSSL::ASN1.decode(der_signature)
    decoded_der.value
               .map { |v| v.value.to_s(2) }
               .flatten
               .join
  elsif @algo == IOSTSdk::Crypto.key_algos[:Ed25519]
    @private_key.sign(message)
  end
end

#valueObject



94
95
96
97
# File 'lib/iost_sdk/crypto.rb', line 94

def value
  Base58.binary_to_base58(@private_key.keypair, :bitcoin) if
    @algo == IOSTSdk::Crypto.key_algos[:Ed25519]
end