Class: Nostr::Key Private

Inherits:
String
  • Object
show all
Defined in:
lib/nostr/key.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Abstract class for all keys

API:

  • private

Direct Known Subclasses

PrivateKey, PublicKey

Constant Summary collapse

FORMAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The regular expression for hexadecimal lowercase characters

Returns:

  • The regular expression for hexadecimal lowercase characters

API:

  • private

/^[a-f0-9]+$/
LENGTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The length of the key in hex format

Returns:

  • The length of the key in hex format

API:

  • private

64

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_value) ⇒ Key

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiates a new key. Can’t be used directly because this is an abstract class. Raises a KeyValidationError

Parameters:

  • Hex-encoded value of the key

Raises:

See Also:

API:

  • private



30
31
32
33
34
# File 'lib/nostr/key.rb', line 30

def initialize(hex_value)
  validate_hex_value(hex_value)

  super(hex_value)
end

Class Method Details

.from_bech32(bech32_value) ⇒ Key

Instantiates a key from a bech32 string

Examples:

bech32_key = 'nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5'
bech32_key.to_key # => #<Nostr::PublicKey:0x000000010601e3c8 @hex_value="...">

Parameters:

  • The bech32 string representation of the key.

Returns:

  • the key.

Raises:

  • if the bech32 string is invalid.

API:

  • public



50
51
52
53
54
55
56
# File 'lib/nostr/key.rb', line 50

def self.from_bech32(bech32_value)
  type, data = Bech32.decode(bech32_value)

  raise InvalidHRPError.new(type, hrp) unless type == hrp

  new(data)
end

.hrpString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Abstract method to be implemented by subclasses to provide the HRP (npub, nsec)

Returns:

  • The HRP

API:

  • private



64
65
66
# File 'lib/nostr/key.rb', line 64

def self.hrp
  raise 'Subclasses must implement this method'
end

Instance Method Details

#to_bech32String

Converts the key to a bech32 string representation

Examples:

Converting a private key to a bech32 string

public_key = Nostr::PrivateKey.new('67dea2ed018072d675f5415ecfaed7d2597555e202d85b3d65ea4e58d2d92ffa')
public_key.to_bech32 # => 'nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5'

Converting a public key to a bech32 string

public_key = Nostr::PublicKey.new('7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e')
public_key.to_bech32 # => 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'

Returns:

  • The bech32 string representation of the key

API:

  • public



82
# File 'lib/nostr/key.rb', line 82

def to_bech32 = Bech32.encode(hrp: self.class.hrp, data: self)