class IOSTSdk::Crypto::KeyPair

Attributes

algo[R]

Public Class Methods

new(algo:, public_key:, private_key:) click to toggle source

Create an instance of KeyPair

@param algo [String] the algorithm used to generate the key pair @param public_key [Ed25519::VerifyKey|OpenSSL::PKey::EC::Point] an instance of the public key @param private_key [Ed25519::SigningKey|OpenSSL::BN] an instance of the private key @return an instance of KeyPair

# 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

Public Instance Methods

id() click to toggle source
# File lib/iost_sdk/crypto.rb, line 125
def id
  public_key
end
private_key() click to toggle source
# File lib/iost_sdk/crypto.rb, line 112
def private_key
  Base58.binary_to_base58(private_key_raw, :bitcoin)
end
private_key_raw() click to toggle source
# 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_key() click to toggle source
# File lib/iost_sdk/crypto.rb, line 99
def public_key
  Base58.binary_to_base58(public_key_raw, :bitcoin)
end
public_key_raw() click to toggle source
# 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:) click to toggle source
# 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
value() click to toggle source
# 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