class Solana::Keypair

The Keypair class represents a keypair for signing transactions on the Solana blockchain.

Attributes

public_key[R]
secret_key[R]

Public Class Methods

from_secret_key(secret_key) click to toggle source

Creates a Keypair from a provided secret key.

@param [String] secret_key The secret key in binary format. @return [Keypair] A new Keypair instance initialized with the provided secret key.

# File lib/solana-ruby/keypair.rb, line 46
def self.from_secret_key(secret_key)
  new(secret_key)
end
generate() click to toggle source

Generates a new Keypair.

@return [Keypair] A new Keypair instance.

# File lib/solana-ruby/keypair.rb, line 37
def self.generate
  new
end
load_from_json(file_path) click to toggle source

Loads a keypair from a JSON file.

The file must contain the public and secret keys in Base58 format. The method verifies the size of the secret key and checks that the derived public key matches the saved public key.

Raises an error if the secret_key is not 64 bytes long or if the derived public key does not match the saved public key.

@param [String] file_path The path to the file from which the keypair will be loaded. @return [Keypair] The loaded keypair.

# File lib/solana-ruby/keypair.rb, line 74
def self.load_from_json(file_path)
  data = JSON.parse(File.read(file_path), symbolize_names: true)
  secret_key = Utils::base58_decode(data[:secret_key])
  public_key = Utils::base58_decode(data[:public_key])

  raise 'Bad secret key size' unless secret_key.bytesize == 64

  signing_key = RbNaCl::Signatures::Ed25519::SigningKey.new(secret_key[0, 32])
  loaded_public_key = signing_key.verify_key.to_bytes

  raise 'Provided secretKey is invalid' unless loaded_public_key == public_key

  new(secret_key)
end
new(secret_key = nil) click to toggle source

Initializes a new Keypair.

If a secret_key is provided, it must be 64 bytes long. The first 32 bytes are used as the signing key, and the public key is derived from it. If no secret_key is provided, a new keypair is generated.

Raises an error if the secret_key is not 64 bytes long.

@param [String, nil] secret_key The secret key to use for the keypair, in binary format (optional).

# File lib/solana-ruby/keypair.rb, line 21
def initialize(secret_key = nil)
  if secret_key
    raise 'Bad secret key size' unless secret_key.bytesize == 64
    @signing_key = RbNaCl::Signatures::Ed25519::SigningKey.new(secret_key[0, 32])
    @public_key = @signing_key.verify_key.to_bytes
  else
    @signing_key = RbNaCl::Signatures::Ed25519::SigningKey.generate
    @public_key = @signing_key.verify_key.to_bytes
  end
  @secret_key = @signing_key.to_bytes + @public_key
end

Public Instance Methods

public_key_base58() click to toggle source

Returns the public key for this keypair in Base58 format.

@return [String] The public key in Base58 format.

# File lib/solana-ruby/keypair.rb, line 93
def public_key_base58
  Utils::base58_encode(@public_key)
end
save_to_json(file_path) click to toggle source

Saves the keypair to a JSON file.

The public and secret keys are encoded in Base58 format before being saved.

@param [String] file_path The path to the file where the keypair will be saved.

# File lib/solana-ruby/keypair.rb, line 56
def save_to_json(file_path)
  data = {
    public_key: Utils::base58_encode(@public_key),
    secret_key: Utils::base58_encode(@secret_key)
  }
  File.write(file_path, data.to_json)
end
secret_key_base58() click to toggle source

Returns the raw secret key for this keypair in Base58 format.

@return [String] The secret key in Base58 format.

# File lib/solana-ruby/keypair.rb, line 101
def secret_key_base58
  Utils::base58_encode(@secret_key)
end