Class: SolanaRuby::Keypair
- Inherits:
-
Object
- Object
- SolanaRuby::Keypair
- Defined in:
- lib/solana_ruby/keypair.rb
Class Method Summary collapse
-
.from_private_key(private_key_hex) ⇒ Object
Restores a keypair from a private key in hex format.
-
.generate ⇒ Object
Generates a new Ed25519 keypair.
-
.load_keypair(file_path) ⇒ Object
Load a keypair from a JSON file.
Class Method Details
.from_private_key(private_key_hex) ⇒ Object
Restores a keypair from a private key in hex format
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/solana_ruby/keypair.rb', line 15 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) keys(signing_key, private_key_bytes) end |
.generate ⇒ Object
Generates a new Ed25519 keypair
7 8 9 10 11 12 |
# File 'lib/solana_ruby/keypair.rb', line 7 def self.generate signing_key = RbNaCl::Signatures::Ed25519::SigningKey.generate private_key_bytes = signing_key.to_bytes keys(signing_key, private_key_bytes) end |
.load_keypair(file_path) ⇒ Object
Load a keypair from a JSON file
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/solana_ruby/keypair.rb', line 28 def self.load_keypair(file_path) # Parse the JSON file keypair_data = JSON.parse(File.read(file_path)) # Ensure it contains exactly 64 bytes for Ed25519 (32 private + 32 public) raise "Invalid keypair length: expected 64 bytes, got #{keypair_data.length}" unless keypair_data.length == 64 # Convert the array to a binary string private_key_bytes = keypair_data[0, 32].pack('C*') public_key_bytes = keypair_data[32, 32].pack('C*') # Create the signing key signing_key = RbNaCl::Signatures::Ed25519::SigningKey.new(private_key_bytes) # Verify the public key matches raise "Public key mismatch" unless signing_key.verify_key.to_bytes == public_key_bytes keys(signing_key, private_key_bytes) rescue JSON::ParserError => e raise "Failed to parse JSON file: #{e.}" end |