module IOSTSdk::Crypto

Constants

KEY_ALGOS

Public Class Methods

from_keypair(encoded_keypair:) click to toggle source

Create an instance of KeyPair from an Ed25519 keypair string

@param encoded_kaypair [String] a Base58 encoded Ed25519 keypair string @return an instance of KeyPair

# File lib/iost_sdk/crypto.rb, line 67
def self.from_keypair(encoded_keypair:)
  private_key = Ed25519::SigningKey.from_keypair(
    Base58.base58_to_binary(encoded_keypair, :bitcoin)
  )

  KeyPair.new(
    algo: IOSTSdk::Crypto::KEY_ALGOS[:Ed25519],
    public_key: private_key.verify_key,
    private_key: private_key
  )
end
key_algos() click to toggle source
# File lib/iost_sdk/crypto.rb, line 15
def self.key_algos
  KEY_ALGOS
end
keypair_from_private_key(algo:, encoded_private_key:) click to toggle source

Create an instance of KeyPair from a private_key_hex in a given algo

@param algo [String] the algorithm used to generate the key pair @param encoded_private_key [String] the Base58 encoded string of an existing private key @return an instance of KeyPair

# File lib/iost_sdk/crypto.rb, line 43
def self.keypair_from_private_key(algo:, encoded_private_key:)
  raise ArgumentError.new("Invalid algo: #{algo}") unless Set.new(KEY_ALGOS.values).include?(algo)

  public_key, private_key = if algo == KEY_ALGOS[:Secp256k1]
                              p_key = BTC::Key.new(
                                private_key: Base58.base58_to_binary(encoded_private_key, :bitcoin)
                              )
                              [p_key.public_key, p_key.private_key]
                            elsif algo == KEY_ALGOS[:Ed25519]
                              private_key = Ed25519::SigningKey.new(
                                Base58.base58_to_binary(encoded_private_key, :bitcoin)
                              )
                              public_key = private_key.verify_key

                              [public_key, private_key]
                            end

  KeyPair.new(algo: algo, public_key: public_key, private_key: private_key)
end
new_keypair(algo:) click to toggle source

Create an instance of KeyPair by generating a brand new pair of public-private keys

@param algo [String] the algorithm should be used to generate the new key pair @return an instance of KeyPair

# File lib/iost_sdk/crypto.rb, line 23
def self.new_keypair(algo:)
  raise ArgumentError.new("Invalid keypair algo: #{algo}") unless Set.new(KEY_ALGOS.values).include?(algo)

  public_key, private_key = if algo == KEY_ALGOS[:Secp256k1]
                              p_key = BTC::Key.random
                              [p_key.public_key, p_key.private_key]
                            elsif algo == KEY_ALGOS[:Ed25519]
                              private_key = Ed25519::SigningKey.generate
                              public_key = private_key.verify_key

                              [public_key, private_key]
                            end
  KeyPair.new(algo: algo, public_key: public_key, private_key: private_key)
end