Class: SolanaRuby::Keypair

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

Class Method Summary collapse

Class Method Details

.from_private_key(private_key_hex) ⇒ Object

Restores a keypair from a private key in hex format

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/solana_ruby/keypair.rb', line 22

def self.from_private_key(private_key_hex)
  raise ArgumentError, "Invalid private key length" unless private_key_hex.size == 64

  # Convert hex private key to binary format for signing key
  private_key_bytes = [private_key_hex].pack('H*')

  # Initialize signing key
  signing_key = RbNaCl::Signatures::Ed25519::SigningKey.new(private_key_bytes)

  # Extract public key in binary format
  public_key_bytes = signing_key.verify_key.to_bytes

  # Return public key in Base58 format and private key in hex format
  {
    public_key: Base58.binary_to_base58(public_key_bytes, :bitcoin),
    private_key: private_key_hex,
    full_private_key: Base58.binary_to_base58((private_key_bytes + public_key_bytes), :bitcoin)
  }
end

.generateObject

Generates a new Ed25519 keypair



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/solana_ruby/keypair.rb', line 7

def self.generate
  signing_key = RbNaCl::Signatures::Ed25519::SigningKey.generate
  public_key_bytes = signing_key.verify_key.to_bytes # Binary format for public key
  private_key_bytes = signing_key.to_bytes
  private_key_hex = private_key_bytes.unpack1('H*') # Hex format for private key

  # Convert public key binary to Base58 for readability and compatibility
  {
    public_key: Base58.binary_to_base58(public_key_bytes, :bitcoin),
    private_key: private_key_hex,
    full_private_key: Base58.binary_to_base58((private_key_bytes + public_key_bytes), :bitcoin)
  }
end