class Secp256k1::Key

Public Class Methods

new(key = nil) click to toggle source
# File lib/secp256k1/key.rb, line 5
def initialize(key = nil)
  if key
    if valid_key?(key)
      @key = key.force_encoding('BINARY')
    else
      raise ArgumentError, "Invalid secp256k1 private key"
    end
  else
    @key = random_key
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/secp256k1/key.rb, line 25
def ==(other)
  @key == other.private_key
end
private_key() click to toggle source
# File lib/secp256k1/key.rb, line 17
def private_key
  @key
end
public_key() click to toggle source
# File lib/secp256k1/key.rb, line 21
def public_key
  PKey.new(to_pubkey)
end

Private Instance Methods

random_key() click to toggle source
# File lib/secp256k1/key.rb, line 31
def random_key
  loop do
    key = SecureRandom.random_bytes(32)
    return key if valid_key?(key)
  end
end
to_pubkey() click to toggle source

Caller must call free

# File lib/secp256k1/key.rb, line 48
def to_pubkey
  out = FFI::MemoryPointer.new(64)
  priv = Secp256k1.key_to_pointer(@key)
  Secp256k1.secp256k1_ec_pubkey_create(Secp256k1.context, out, priv)

  val = out.read_bytes(64)
  out.free
  priv.free

  val
end
valid_key?(key) click to toggle source
# File lib/secp256k1/key.rb, line 38
def valid_key?(key)
  kptr = Secp256k1.key_to_pointer(key)
  valid = Secp256k1.secp256k1_ec_seckey_verify(Secp256k1.context, kptr) == 1

  kptr.free

  valid
end