pub(crate) fn extract_keypair(
    input: Option<String>,
    module_path: Option<String>,
    directory: Option<String>,
    keygen_type: KeyPairType,
    disable_keygen: bool,
) -> Result<KeyPair, Box<dyn std::error::Error>> {
    let seed = if let Some(input_str) = input {
        match File::open(input_str.clone()) {
            // User provided file path to seed as argument
            Ok(mut f) => {
                let mut s = String::new();
                f.read_to_string(&mut s)?;
                s
            }
            // User provided seed as an argument
            Err(_e) => input_str,
        }
    } else if let Some(module) = module_path {
        // No seed value provided, attempting to source from provided or default directory
        let dir = determine_directory(directory)?;
        // Account key should be re-used, and will attempt to generate based on the terminal USER
        let module_name = match keygen_type {
            KeyPairType::Account => std::env::var("USER").unwrap_or("user".to_string()),
            _ => PathBuf::from(module)
                .file_stem()
                .unwrap()
                .to_str()
                .unwrap()
                .to_string(),
        };
        let path = format!(
            "{}/{}_{}.nk",
            dir,
            module_name,
            keypair_type_to_string(keygen_type.clone())
        );
        match File::open(path.clone()) {
            // Default key found
            Ok(mut f) => {
                let mut s = String::new();
                f.read_to_string(&mut s)?;
                s
            }
            // No default key, generating for user
            Err(_e) if !disable_keygen => {
                let kp = KeyPair::new(keygen_type);
                println!("No keypair found in \"{}\".\nWe will generate one for you and place it there.\nIf you'd like to use alternative keys, you can supply them as a flag.\n", path);
                let seed = kp.seed()?;
                fs::create_dir_all(Path::new(&path).parent().unwrap())?;
                let mut f = File::create(path)?;
                f.write_all(seed.as_bytes())?;
                seed
            }
            _ => {
                return Err(format!(
                    "No keypair found in {}, please ensure key exists or supply one as a flag",
                    path
                )
                .into());
            }
        }
    } else {
        return Err("Keypair path or string not supplied. Ensure provided keypair is valid".into());
    };

    KeyPair::from_seed(&seed).map_err(|e| format!("{}", e).into())
}
/// Helper function to locate and extract keypair from user input
Helper function to locate and extract keypair from user input
Helper function to locate and extract keypair from user input
pub(crate) fn extract_keypair(
    input: Option<String>,
    module_path: Option<String>,
    directory: Option<String>,
    keygen_type: KeyPairType,
    disable_keygen: bool,
) -> Result<KeyPair, Box<dyn std::error::Error>> {
    let seed = if let Some(input_str) = input {
        match File::open(input_str.clone()) {
            // User provided file path to seed as argument
            Ok(mut f) => {
                let mut s = String::new();
                f.read_to_string(&mut s)?;
                s
            }
            // User provided seed as an argument
            Err(_e) => input_str,
        }
    } else if let Some(module) = module_path {
        // No seed value provided, attempting to source from provided or default directory
        let dir = determine_directory(directory)?;
        // Account key should be re-used, and will attempt to generate based on the terminal USER
        let module_name = match keygen_type {
            KeyPairType::Account => std::env::var("USER").unwrap_or("user".to_string()),
            _ => PathBuf::from(module)
                .file_stem()
                .unwrap()
                .to_str()
                .unwrap()
                .to_string(),
        };
        let path = format!(
            "{}/{}_{}.nk",
            dir,
            module_name,
            keypair_type_to_string(keygen_type.clone())
        );
        match File::open(path.clone()) {
            // Default key found
            Ok(mut f) => {
                let mut s = String::new();
                f.read_to_string(&mut s)?;
                s
            }
            // No default key, generating for user
            Err(_e) if !disable_keygen => {
                let kp = KeyPair::new(keygen_type);
                println!("No keypair found in \"{}\".\nWe will generate one for you and place it there.\nIf you'd like to use alternative keys, you can supply them as a flag.\n", path);
                let seed = kp.seed()?;
                fs::create_dir_all(Path::new(&path).parent().unwrap())?;
                let mut f = File::create(path)?;
                f.write_all(seed.as_bytes())?;
                seed
            }
            _ => {
                return Err(format!(
                    "No keypair found in {}, please ensure key exists or supply one as a flag",
                    path
                )
                .into());
            }
        }
    } else {
        return Err("Keypair path or string not supplied. Ensure provided keypair is valid".into());
    };
    KeyPair::from_seed(&seed).map_err(|e| format!("{}", e).into())
}
pub ( crate ) fn extract_keypair ( input : Option < String > , module_path : Option < String > , directory : Option < String > , keygen_type : KeyPairType , disable_keygen : bool , ) -> Result < KeyPair , Box < dyn std :: error :: Error > > { let seed = if let Some ( input_str ) = input { match File :: open ( input_str . clone ( ) ) { // User provided file path to seed as argument Ok ( mut f ) => { let mut s = String :: new ( ) ; f . read_to_string ( & mut s ) ? ; s } // User provided seed as an argument Err ( _e ) => input_str , } } else if let Some ( module ) = module_path { // No seed value provided, attempting to source from provided or default directory let dir = determine_directory ( directory ) ? ; // Account key should be re-used, and will attempt to generate based on the terminal USER let module_name = match keygen_type { KeyPairType :: Account => std :: env :: var ( " " ) . unwrap_or ( " " . to_string ( ) ) , _ => PathBuf :: from ( module ) . file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) , } ; let path = format ! ( " " dir module_name keypair_type_to_string ( keygen_type clone ( ) ) ) ; match File :: open ( path . clone ( ) ) { // Default key found Ok ( mut f ) => { let mut s = String :: new ( ) ; f . read_to_string ( & mut s ) ? ; s } // No default key, generating for user Err ( _e ) if ! disable_keygen => { let kp = KeyPair :: new ( keygen_type ) ; println ! ( " \" \" \n \n \n " path ) ; let seed = kp . seed ( ) ? ; fs :: create_dir_all ( Path :: new ( & path ) . parent ( ) . unwrap ( ) ) ? ; let mut f = File :: create ( path ) ? ; f . write_all ( seed . as_bytes ( ) ) ? ; seed } _ => { return Err ( format ! ( " " path ) . into ( ) ) ; } } } else { return Err ( " " . into ( ) ) ; } ; KeyPair :: from_seed ( & seed ) . map_err ( | e | format ! ( " " e ) . into ( ) ) }
Helper function to locate and extract keypair from user input
Helper function to locate and extract keypair from user input
input
Option<String>
module_path
Option<String>
directory
Option<String>
keygen_type
KeyPairType
disable_keygen
bool
input
Option<String>
module_path
Option<String>
directory
Option<String>
keygen_type
KeyPairType
disable_keygen
bool