@@ -6,6 +6,10 @@ use std::fs;
66use std:: io:: { self , Error , ErrorKind } ;
77use std:: path:: Path ; // To validate a key from a file before adding
88
9+ // Solana keypair constants
10+ const KEYPAIR_BYTES : usize = 64 ; // Full keypair: 32 bytes secret + 32 bytes public
11+ const SECRET_KEY_BYTES : usize = 32 ; // Just the secret key portion
12+
913/// Adds a new wallet by reading a private key from a JSON file and storing it securely.
1014/// The wallet will be stored under the given `wallet_name`.
1115pub fn add_wallet_from_file ( wallet_name : & str , key_file_path : & str ) -> io:: Result < ( ) > {
@@ -162,14 +166,14 @@ pub fn get_wallet_keypair(
162166 Some ( key_bytes) => {
163167 // new_from_array expects only the 32-byte secret key, not the full 64-byte keypair
164168 // Convert Vec<u8> to [u8; 32] array
165- if key_bytes. len ( ) != 64 {
169+ if key_bytes. len ( ) != KEYPAIR_BYTES {
166170 return Err ( io:: Error :: new (
167171 io:: ErrorKind :: InvalidData ,
168- format ! ( "Invalid key length: expected 64 bytes, got {}" , key_bytes. len( ) )
172+ format ! ( "Invalid key length: expected {} bytes, got {}" , KEYPAIR_BYTES , key_bytes. len( ) )
169173 ) ) ;
170174 }
171- let mut secret_key = [ 0u8 ; 32 ] ;
172- secret_key. copy_from_slice ( & key_bytes[ 0 ..32 ] ) ;
175+ let mut secret_key = [ 0u8 ; SECRET_KEY_BYTES ] ;
176+ secret_key. copy_from_slice ( & key_bytes[ 0 ..SECRET_KEY_BYTES ] ) ;
173177 let keypair = solana_sdk:: signer:: keypair:: Keypair :: new_from_array ( secret_key) ;
174178 Ok ( Some ( keypair) )
175179 }
0 commit comments