Class: Nostrbase::Keys

Inherits:
Object
  • Object
show all
Defined in:
lib/nostrbase/keys.rb

Constant Summary collapse

CONTEXT =
Secp256k1::Context.create

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_hex = nil) ⇒ Keys

Returns a new instance of Keys.



7
8
9
10
11
12
13
14
# File 'lib/nostrbase/keys.rb', line 7

def initialize(secret_hex = nil)
  private_key_data = secret_hex ? Secp256k1::Util.hex_to_bin(secret_hex) : SecureRandom.random_bytes(32)
  @secret = Secp256k1::Util.bin_to_hex(Secp256k1::PrivateKey.from_data(private_key_data).data)
  @key_pair = CONTEXT.key_pair_from_private_key(private_key_data)

  # Nostr ignores first byte of pubkey, more details here https://bips.xyz/340
  @public = Secp256k1::Util.bin_to_hex(@key_pair.public_key.compressed)[2..]
end

Instance Attribute Details

#publicObject (readonly)

Returns the value of attribute public.



3
4
5
# File 'lib/nostrbase/keys.rb', line 3

def public
  @public
end

#secretObject (readonly)

Returns the value of attribute secret.



3
4
5
# File 'lib/nostrbase/keys.rb', line 3

def secret
  @secret
end

Instance Method Details

#sign(message) ⇒ Object



16
17
18
# File 'lib/nostrbase/keys.rb', line 16

def sign(message)
  CONTEXT.sign_schnorr(@key_pair, [message].pack("H*")).serialized.unpack1("H*")
end