Method: Klay::Key#initialize

Defined in:
lib/klay/key.rb

#initialize(priv: nil) ⇒ Key

Constructor of the Klay::Key class. Creates a new random key-pair if no priv key is provided.

Parameters:

  • priv (String) (defaults to: nil)

    binary string of private key data.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/klay/key.rb', line 43

def initialize(priv: nil)

  # Creates a new, randomized libsecp256k1 context.
  ctx = Secp256k1::Context.new context_randomization_bytes: SecureRandom.random_bytes(32)

  # Creates a new random key pair (public, private).
  key = ctx.generate_key_pair

  unless priv.nil?

    # Converts hex private keys to binary strings.
    priv = Util.hex_to_bin priv if Util.is_hex? priv

    # Creates a keypair from existing private key data.
    key = ctx.key_pair_from_private_key priv
  end

  # Sets the attributes.
  @private_key = key.private_key
  @public_key = key.public_key
end