Class: Solana::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/solana-ruby/keypair.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key = nil) ⇒ Keypair

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.

Parameters:

  • secret_key (String, nil) (defaults to: nil)

    The secret key to use for the keypair, in binary format (optional).



21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#public_keyObject (readonly)

Returns the value of attribute public_key.



10
11
12
# File 'lib/solana-ruby/keypair.rb', line 10

def public_key
  @public_key
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



10
11
12
# File 'lib/solana-ruby/keypair.rb', line 10

def secret_key
  @secret_key
end

Class Method Details

.from_secret_key(secret_key) ⇒ Keypair

Creates a Keypair from a provided secret key.

Parameters:

  • secret_key (String)

    The secret key in binary format.

Returns:

  • (Keypair)

    A new Keypair instance initialized with the provided secret key.



46
47
48
# File 'lib/solana-ruby/keypair.rb', line 46

def self.from_secret_key(secret_key)
  new(secret_key)
end

.generateKeypair

Generates a new Keypair.

Returns:

  • (Keypair)

    A new Keypair instance.



37
38
39
# File 'lib/solana-ruby/keypair.rb', line 37

def self.generate
  new
end

.load_from_json(file_path) ⇒ Keypair

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.

Parameters:

  • file_path (String)

    The path to the file from which the keypair will be loaded.

Returns:

  • (Keypair)

    The loaded keypair.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

Instance Method Details

#public_key_base58String

Returns the public key for this keypair in Base58 format.

Returns:

  • (String)

    The public key in Base58 format.



93
94
95
# File 'lib/solana-ruby/keypair.rb', line 93

def public_key_base58
  Utils::base58_encode(@public_key)
end

#save_to_json(file_path) ⇒ Object

Saves the keypair to a JSON file.

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

Parameters:

  • file_path (String)

    The path to the file where the keypair will be saved.



56
57
58
59
60
61
62
# 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_base58String

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

Returns:

  • (String)

    The secret key in Base58 format.



101
102
103
# File 'lib/solana-ruby/keypair.rb', line 101

def secret_key_base58
  Utils::base58_encode(@secret_key)
end