Class: Nostr::KeyPair

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

Overview

A pair of public and private keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, public_key:) ⇒ KeyPair

Instantiates a key pair

Examples:

keypair = Nostr::KeyPair.new(
  private_key: Nostr::PrivateKey.new('893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900'),
  public_key: Nostr::PublicKey.new('2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'),
)

Parameters:

  • private_key (PrivateKey)

    32-bytes hex-encoded private key.

  • public_key (PublicKey)

    32-bytes hex-encoded public key.

Raises:

  • ArgumentError when the private key is not a PrivateKey

  • ArgumentError when the public key is not a PublicKey



44
45
46
47
48
49
# File 'lib/nostr/key_pair.rb', line 44

def initialize(private_key:, public_key:)
  validate_keys(private_key, public_key)

  @private_key = private_key
  @public_key = public_key
end

Instance Attribute Details

#private_keyPrivateKey (readonly)

32-bytes hex-encoded private key

Examples:

keypair.private_key # => '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900'

Returns:



15
16
17
# File 'lib/nostr/key_pair.rb', line 15

def private_key
  @private_key
end

#public_keyPublicKey (readonly)

32-bytes hex-encoded public key

Examples:

keypair.public_key # => '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'

Returns:



26
27
28
# File 'lib/nostr/key_pair.rb', line 26

def public_key
  @public_key
end

Instance Method Details

#to_aryArray<PrivateKey, PublicKey>

Allows array destructuring of the KeyPair, enabling the extraction of PrivateKey and PublicKey separately

Examples:

Implicit usage of ‘to_ary` for destructuring

keypair = Nostr::KeyPair.new(
  private_key: Nostr::PrivateKey.new('7d1e4219a5e7d8342654c675085bfbdee143f0eb0f0921f5541ef1705a2b407d'),
  public_key: Nostr::PublicKey.new('15678d8fbc126fa326fac536acd5a6dcb5ef64b3d939abe31d6830cba6cd26d6'),
)
# The `to_ary` method can be implicitly used for array destructuring:
private_key, public_key = keypair
# Now `private_key` and `public_key` hold the respective values.

Explicit usage of ‘to_ary`

array_representation = keypair.to_ary
# array_representation is now an array: [PrivateKey, PublicKey]
# where PrivateKey and PublicKey are the respective objects.

Returns:



71
72
73
# File 'lib/nostr/key_pair.rb', line 71

def to_ary
  [private_key, public_key]
end