Class: Nostr::Keygen
- Inherits:
-
Object
- Object
- Nostr::Keygen
- Defined in:
- lib/nostr/keygen.rb
Overview
Generates private keys, public keys and key pairs.
Instance Method Summary collapse
-
#extract_public_key(private_key) ⇒ PublicKey
Extracts a public key from a private key.
-
#generate_key_pair ⇒ KeyPair
Generates a pair of private and public keys.
-
#generate_private_key ⇒ PrivateKey
Generates a private key.
-
#get_key_pair_from_private_key(private_key) ⇒ Nostr::KeyPair
Builds a key pair from an existing private key.
-
#initialize ⇒ Keygen
constructor
Instantiates a new keygen.
Constructor Details
#initialize ⇒ Keygen
Instantiates a new keygen
16 17 18 |
# File 'lib/nostr/keygen.rb', line 16 def initialize @group = ECDSA::Group::Secp256k1 end |
Instance Method Details
#extract_public_key(private_key) ⇒ PublicKey
Extracts a public key from a private key
69 70 71 72 73 |
# File 'lib/nostr/keygen.rb', line 69 def extract_public_key(private_key) validate_private_key(private_key) hex_value = group.generator.multiply_by_scalar(private_key.to_i(16)).x.to_s(16).rjust(64, '0') PublicKey.new(hex_value) end |
#generate_key_pair ⇒ KeyPair
Generates a pair of private and public keys
32 33 34 35 36 37 |
# File 'lib/nostr/keygen.rb', line 32 def generate_key_pair private_key = generate_private_key public_key = extract_public_key(private_key) KeyPair.new(private_key:, public_key:) end |
#generate_private_key ⇒ PrivateKey
Generates a private key
49 50 51 52 |
# File 'lib/nostr/keygen.rb', line 49 def generate_private_key hex_value = (SecureRandom.random_number(group.order - 1) + 1).to_s(16).rjust(64, '0') PrivateKey.new(hex_value) end |
#get_key_pair_from_private_key(private_key) ⇒ Nostr::KeyPair
Builds a key pair from an existing private key
89 90 91 92 93 |
# File 'lib/nostr/keygen.rb', line 89 def get_key_pair_from_private_key(private_key) validate_private_key(private_key) public_key = extract_public_key(private_key) KeyPair.new(private_key:, public_key:) end |