Class: Solace::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/solace/keypair.rb

Overview

Class representing a Solana Ed25519 Keypair

This class provides utility methods for encoding, decoding, signing, and validating keypairs.

Examples:

# Generate a new keypair
keypair = Solace::Keypair.generate

# Get the address of the pubkey
keypair.address

# Sign a message using the keypair
keypair.sign("<any-message>")

Since:

  • 0.0.1

Constant Summary collapse

SECRET_LENGTH =

The length of a Solana secret key in bytes.

Since:

  • 0.0.1

64
SEED_LENGTH =

The length of a Solana seed in bytes.

Since:

  • 0.0.1

32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keypair_bytes) ⇒ Keypair

Initialize a new keypair

Examples:

keypair = Solace::Keypair.new(bytes)

Parameters:

  • keypair_bytes (Array<Integer>)

    The keypair bytes

Raises:

  • (ArgumentError)

    If the length of the keypair_bytes isn’t 64 bytes

Since:

  • 0.0.1



82
83
84
85
86
# File 'lib/solace/keypair.rb', line 82

def initialize(keypair_bytes)
  raise ArgumentError, 'Keypair must be 64 bytes' unless keypair_bytes.length == SECRET_LENGTH

  @keypair_bytes = keypair_bytes
end

Instance Attribute Details

#keypair_bytesArray<u8> (readonly)

The full keypair bytes array

Returns:

  • (Array<u8>)

    The 64 bytes of the keypair

Since:

  • 0.0.1



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

def keypair_bytes
  @keypair_bytes
end

Class Method Details

.from_secret_key(secret_key) ⇒ Keypair

Create a keypair from a 64-byte secret key

Examples:

keypair = Solace::Keypair.from_secret_key(secret_key)

Parameters:

  • secret_key (String)

    64-byte array

Returns:

Raises:

  • (ArgumentError)

    If the length of the secret_key isn’t 64 bytes

Since:

  • 0.0.1



67
68
69
70
71
# File 'lib/solace/keypair.rb', line 67

def from_secret_key(secret_key)
  raise ArgumentError, 'Secret key must be 64 bytes' unless secret_key.length == SECRET_LENGTH

  new(RbNaCl::Signatures::Ed25519::SigningKey.new(secret_key[0..31]).keypair_bytes.bytes)
end

.from_seed(seed) ⇒ Keypair

Create a keypair from a 32-byte seed

Examples:

keypair = Solace::Keypair.from_seed(seed)

Parameters:

  • seed (String)

    32-byte array

Returns:

Raises:

  • (ArgumentError)

    If the length of the seed isn’t 32 bytes

Since:

  • 0.0.1



53
54
55
56
57
# File 'lib/solace/keypair.rb', line 53

def from_seed(seed)
  raise ArgumentError, 'Seed must be 32 bytes' unless seed.length == SEED_LENGTH

  new(RbNaCl::Signatures::Ed25519::SigningKey.new(seed).keypair_bytes.bytes)
end

.generateKeypair

Generate a new random keypair

Examples:

keypair = Solace::Keypair.generate

Returns:

Since:

  • 0.0.1



41
42
43
# File 'lib/solace/keypair.rb', line 41

def generate
  from_seed(RbNaCl::Random.random_bytes(SEED_LENGTH))
end

Instance Method Details

#private_key_bytesArray<u8>

Returns the private key

The private key is the first 32 bytes of the keypair

Examples:

private_key_bytes = instance.private_key_bytes

Returns:

  • (Array<u8>)

    32 characters

Since:

  • 0.0.1



128
129
130
# File 'lib/solace/keypair.rb', line 128

def private_key_bytes
  keypair_bytes[0..31]
end

#public_keyPublicKey

Returns the public key

Examples:

pubkey = keypair.public_key

Returns:

Since:

  • 0.0.1



94
95
96
# File 'lib/solace/keypair.rb', line 94

def public_key
  @public_key ||= Solace::PublicKey.new(public_key_bytes)
end

#public_key_bytesArray<u8>

Returns the public key bytes

The public key bytes are the last 32 bytes of the keypair

Examples:

public_key_bytes = instance.public_key_bytes

Returns:

  • (Array<u8>)

    32 bytes

Since:

  • 0.0.1



116
117
118
# File 'lib/solace/keypair.rb', line 116

def public_key_bytes
  keypair_bytes[32..63]
end

#sign(message) ⇒ String

Signs a message (string or binary)

Examples:

message = "An important message to be signed,"
signature = instance.sign(message)

Parameters:

  • message (String, Binary)

Returns:

  • (String)

    signature (binary string)

Since:

  • 0.0.1



168
169
170
# File 'lib/solace/keypair.rb', line 168

def sign(message)
  signing_key.sign(message)
end

#signing_keyRbNaCl::Signatures::Ed25519::SigningKey

Returns the signing key

Examples:

signing_key = instance.signing_key

Returns:

  • (RbNaCl::Signatures::Ed25519::SigningKey)

Since:

  • 0.0.1



104
105
106
# File 'lib/solace/keypair.rb', line 104

def signing_key
  @signing_key ||= RbNaCl::Signatures::Ed25519::SigningKey.new(private_key_bytes.pack('C*'))
end

#to_base58String Also known as: to_s, address

Returns the public key address as a Base58 string

Examples:

pubkey_str = instance.to_base58

Returns:

  • (String)

    Base58 encoded public key

Since:

  • 0.0.1



138
139
140
# File 'lib/solace/keypair.rb', line 138

def to_base58
  public_key.to_base58
end