Class: Solace::PublicKey

Inherits:
Object
  • Object
show all
Includes:
Utils::PDA
Defined in:
lib/solace/public_key.rb

Overview

Class representing a Solana Ed25519 Public Key

This class provides utility methods for encoding, decoding, and validating public keys.

Examples:

# Initialize a public key from a 32-byte array
pubkey = Solace::PublicKey.new(public_key_bytes)

# Get the address representation of the public key
pubkey.to_base58
pubkey.address

Since:

  • 0.0.1

Constant Summary collapse

LENGTH =

The length of a Solana public key in bytes

Since:

  • 0.0.1

32
MAX_BUMP_SEED =

The maximum seed value for a Program Derived Address

Since:

  • 0.0.1

255
PDA_MARKER =

The marker for a Program Derived Address

Since:

  • 0.0.1

'ProgramDerivedAddress'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::PDA

create_program_address, find_program_address, looks_like_base58_address?, seed_to_bytes

Constructor Details

#initialize(bytes) ⇒ PublicKey

Initialize with a 32-byte array or string

Examples:

pubkey = Solace::PubKey.new(bytes)

Parameters:

  • bytes (String, Array<Integer>)

    32-byte array or string

Raises:

  • (ArgumentError)

    If the public key bytes length isn’t 32 long

Since:

  • 0.0.1



42
43
44
45
46
# File 'lib/solace/public_key.rb', line 42

def initialize(bytes)
  raise ArgumentError, "Public key must be #{LENGTH} bytes" unless bytes.length == LENGTH

  @bytes = bytes.freeze
end

Instance Attribute Details

#bytesArray<u8> (readonly)

The bytes of the public key

Returns:

  • (Array<u8>)

    The bytes of the public key

Since:

  • 0.0.1



32
33
34
# File 'lib/solace/public_key.rb', line 32

def bytes
  @bytes
end

Class Method Details

.from_address(address) ⇒ PublicKey

Create a public key instance from a base58 address

Examples:

pubkey = Solace::PublicKey.from_address(address)

Parameters:

  • address (String)

    The base58 address of the public key

Returns:

Raises:

  • (ArgumentError)

    If the address is not a valid base58 string

Since:

  • 0.0.6



111
112
113
# File 'lib/solace/public_key.rb', line 111

def from_address(address)
  new(Solace::Utils::Codecs.base58_to_bytes(address))
end

Instance Method Details

#==(other) ⇒ Boolean

Compare two public keys for equality

Examples:

pubkey1 == pubkey2

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.0.1



85
86
87
# File 'lib/solace/public_key.rb', line 85

def ==(other)
  other.is_a?(Solace::PublicKey) && other.bytes == bytes
end

#to_base58String Also known as: to_s, address

Return the base58 representation of the public key

Examples:

pubkey_str = instance.to_base58

Returns:

  • (String)

Since:

  • 0.0.1



55
56
57
# File 'lib/solace/public_key.rb', line 55

def to_base58
  Solace::Utils::Codecs.bytes_to_base58(@bytes)
end

#to_bytesArray<Integer>

Return the public key as a byte array

Examples:

pubkey_bytes = instance.to_bytes

Returns:

  • (Array<Integer>)

Since:

  • 0.0.1



96
97
98
# File 'lib/solace/public_key.rb', line 96

def to_bytes
  @bytes.dup
end