Class: Solace::Keypair
- Inherits:
-
Object
- Object
- Solace::Keypair
- 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.
Constant Summary collapse
- SECRET_LENGTH =
The length of a Solana secret key in bytes.
64- SEED_LENGTH =
The length of a Solana seed in bytes.
32
Instance Attribute Summary collapse
-
#keypair_bytes ⇒ Array<u8>
readonly
The full keypair bytes array.
Class Method Summary collapse
-
.from_secret_key(secret_key) ⇒ Keypair
Create a keypair from a 64-byte secret key.
-
.from_seed(seed) ⇒ Keypair
Create a keypair from a 32-byte seed.
-
.generate ⇒ Keypair
Generate a new random keypair.
Instance Method Summary collapse
-
#initialize(keypair_bytes) ⇒ Keypair
constructor
Initialize a new keypair.
-
#private_key_bytes ⇒ Array<u8>
Returns the private key.
-
#public_key ⇒ PublicKey
Returns the public key.
-
#public_key_bytes ⇒ Array<u8>
Returns the public key bytes.
-
#sign(message) ⇒ String
Signs a message (string or binary).
-
#signing_key ⇒ RbNaCl::Signatures::Ed25519::SigningKey
Returns the signing key.
-
#to_base58 ⇒ String
(also: #to_s, #address)
Returns the public key address as a Base58 string.
Constructor Details
#initialize(keypair_bytes) ⇒ Keypair
Initialize a new keypair
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_bytes ⇒ Array<u8> (readonly)
The full keypair bytes array
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
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
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 |
.generate ⇒ Keypair
Generate a new random keypair
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_bytes ⇒ Array<u8>
Returns the private key
The private key is the first 32 bytes of the keypair
128 129 130 |
# File 'lib/solace/keypair.rb', line 128 def private_key_bytes keypair_bytes[0..31] end |
#public_key ⇒ PublicKey
Returns the public key
94 95 96 |
# File 'lib/solace/keypair.rb', line 94 def public_key @public_key ||= Solace::PublicKey.new(public_key_bytes) end |
#public_key_bytes ⇒ Array<u8>
Returns the public key bytes
The public key bytes are the last 32 bytes of the keypair
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)
168 169 170 |
# File 'lib/solace/keypair.rb', line 168 def sign() signing_key.sign() end |
#signing_key ⇒ RbNaCl::Signatures::Ed25519::SigningKey
Returns the signing key
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_base58 ⇒ String Also known as: to_s, address
Returns the public key address as a Base58 string
138 139 140 |
# File 'lib/solace/keypair.rb', line 138 def to_base58 public_key.to_base58 end |