Skip to content

Commit 3d4ad7b

Browse files
Copilot0xrinegade
andcommitted
Address code review feedback - Add constants and fix Cargo.lock
Co-authored-by: 0xrinegade <[email protected]>
1 parent 6aaf03f commit 3d4ad7b

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Rust build artifacts
22
/target/
3-
Cargo.lock
43

54
# IDE files
65
.vscode/

src/key_validator.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::fs;
44
use std::fs::File;
55
use std::io;
66

7+
// Solana keypair constants
8+
const KEYPAIR_BYTES: usize = 64; // Full keypair: 32 bytes secret + 32 bytes public
9+
const SECRET_KEY_BYTES: usize = 32; // Just the secret key portion
10+
711
/// Validates if the content of a given JSON file represents a Solana private key.
812
/// A Solana private key is typically represented as a JSON array of 64 u8 values.
913
pub fn is_solana_wallet_json_file(file_path: &str) -> io::Result<bool> {
@@ -15,8 +19,8 @@ pub fn is_solana_wallet_json_file(file_path: &str) -> io::Result<bool> {
1519
match parsed_json {
1620
Ok(Value::Array(arr)) => {
1721
// Check if the array contains 64 numbers (u8 values for a private key)
18-
if arr.len() == 64 {
19-
let mut key_bytes: Vec<u8> = Vec::with_capacity(64);
22+
if arr.len() == KEYPAIR_BYTES {
23+
let mut key_bytes: Vec<u8> = Vec::with_capacity(KEYPAIR_BYTES);
2024
for val in arr {
2125
if let Value::Number(num) = val {
2226
if let Some(byte_val) = num.as_u64() {
@@ -35,8 +39,8 @@ pub fn is_solana_wallet_json_file(file_path: &str) -> io::Result<bool> {
3539
// If we successfully collected 64 bytes, try to create a Keypair from it.
3640
// This is the definitive check for a valid Solana secret key.
3741
// new_from_array expects only the 32-byte secret key
38-
let mut secret_key = [0u8; 32];
39-
secret_key.copy_from_slice(&key_bytes[0..32]);
42+
let mut secret_key = [0u8; SECRET_KEY_BYTES];
43+
secret_key.copy_from_slice(&key_bytes[0..SECRET_KEY_BYTES]);
4044
let _keypair = Keypair::new_from_array(secret_key);
4145
// Successfully created a keypair, this is a valid Solana secret key
4246
Ok(true)

src/tui.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use crate::secure_storage;
2424
use crate::wallet_manager; // To interact with wallet data
2525
use crate::vanity_wallet::{self, VanityConfig, VanityStatus}; // For vanity wallet creation
2626

27+
// Solana keypair constants
28+
const SECRET_KEY_BYTES: usize = 32; // Just the secret key portion
29+
2730
// Define different views for the TUI
2831
enum View {
2932
WalletList,
@@ -346,7 +349,6 @@ impl App {
346349

347350
// Start vanity wallet generation in a separate thread
348351
let vanity_config = self.vanity_config.clone();
349-
let _cancelled = Arc::clone(&self.vanity_cancelled);
350352
let result = Arc::clone(&self.vanity_result);
351353

352354
let handle = thread::spawn(move || {
@@ -395,8 +397,8 @@ impl App {
395397

396398
// Reconstruct the keypair from bytes
397399
// new_from_array expects only the 32-byte secret key
398-
let mut secret_key = [0u8; 32];
399-
secret_key.copy_from_slice(&keypair_bytes[0..32]);
400+
let mut secret_key = [0u8; SECRET_KEY_BYTES];
401+
secret_key.copy_from_slice(&keypair_bytes[0..SECRET_KEY_BYTES]);
400402
let keypair_copy = solana_sdk::signer::keypair::Keypair::new_from_array(secret_key);
401403
self.save_vanity_wallet(&keypair_copy);
402404
}

src/wallet_manager.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use std::fs;
66
use std::io::{self, Error, ErrorKind};
77
use 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`.
1115
pub 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

Comments
 (0)