Module: IOSTSdk::Crypto

Defined in:
lib/iost_sdk/crypto.rb

Defined Under Namespace

Classes: KeyPair

Constant Summary collapse

KEY_ALGOS =
{
  Secp256k1: 'secp256k1',
  Ed25519: 'ED25519'
}.freeze

Class Method Summary collapse

Class Method Details

.from_keypair(encoded_keypair:) ⇒ Object

Create an instance of KeyPair from an Ed25519 keypair string

Parameters:

  • encoded_kaypair (String)

    a Base58 encoded Ed25519 keypair string

Returns:

  • an instance of KeyPair



67
68
69
70
71
72
73
74
75
76
77
# 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_algosObject



15
16
17
# File 'lib/iost_sdk/crypto.rb', line 15

def self.key_algos
  KEY_ALGOS
end

.keypair_from_private_key(algo:, encoded_private_key:) ⇒ Object

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

Parameters:

  • algo (String)

    the algorithm used to generate the key pair

  • encoded_private_key (String)

    the Base58 encoded string of an existing private key

Returns:

  • an instance of KeyPair

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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:) ⇒ Object

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

Parameters:

  • algo (String)

    the algorithm should be used to generate the new key pair

Returns:

  • an instance of KeyPair

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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