tapedrive-cli 0.1.9

Your data, permanently recorded — uncensorable, uneditable, and here for good.
use solana_sdk::signature::Keypair;
use std::path::PathBuf;
use anyhow::{Result, anyhow};
use std::fs;

pub fn create_keypair(path: &PathBuf) -> Result<Keypair> {
    let keypair = Keypair::new();
    let bytes = keypair.to_bytes().to_vec();
    let json = serde_json::to_string(&bytes)
        .map_err(|e| anyhow!("Failed to serialize keypair to JSON: {}", e))?;
    fs::write(path, json)
        .map_err(|e| anyhow!("Failed to write keypair file {}: {}", path.display(), e))?;
    Ok(keypair)
}

pub fn load_keypair(path: &PathBuf) -> Result<Keypair> {
    let data = fs::read_to_string(path)
        .map_err(|e| anyhow!("Failed to read keypair file {}: {}", path.display(), e))?;
    let bytes: Vec<u8> = serde_json::from_str(&data)
        .map_err(|e| anyhow!("Failed to parse keypair JSON: {}", e))?;
    Keypair::from_bytes(&bytes)
        .map_err(|e| anyhow!("Failed to create keypair from bytes: {}", e))
}

/// Loads the keypair from a specified path or the default Solana keypair location.
pub fn get_keypair_path(keypair_path: Option<PathBuf>) -> PathBuf {
    keypair_path.unwrap_or_else(|| {
        dirs::home_dir()
            .expect("Could not find home directory")
            .join(".config/solana/id.json")
    })
}