Class: Solana::Keypair
- Inherits:
-
Object
- Object
- Solana::Keypair
- 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
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Class Method Summary collapse
-
.from_secret_key(secret_key) ⇒ Keypair
Creates a Keypair from a provided secret key.
-
.generate ⇒ Keypair
Generates a new Keypair.
-
.load_from_json(file_path) ⇒ Keypair
Loads a keypair from a JSON file.
Instance Method Summary collapse
-
#initialize(secret_key = nil) ⇒ Keypair
constructor
Initializes a new Keypair.
-
#public_key_base58 ⇒ String
Returns the public key for this keypair in Base58 format.
-
#save_to_json(file_path) ⇒ Object
Saves the keypair to a JSON file.
-
#secret_key_base58 ⇒ String
Returns the raw secret key for this keypair in Base58 format.
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.
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_key ⇒ Object (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_key ⇒ Object (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.
46 47 48 |
# File 'lib/solana-ruby/keypair.rb', line 46 def self.from_secret_key(secret_key) new(secret_key) end |
.generate ⇒ Keypair
Generates a new Keypair.
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.
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_base58 ⇒ String
Returns the public key for this keypair 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.
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_base58 ⇒ String
Returns the raw secret key for this keypair in Base58 format.
101 102 103 |
# File 'lib/solana-ruby/keypair.rb', line 101 def secret_key_base58 Utils::base58_encode(@secret_key) end |