pub fn keypair_from_seed_phrase(
    keypair_name: &str,
    skip_validation: bool,
    confirm_pubkey: bool,
    derivation_path: Option<DerivationPath>,
    legacy: bool,
) -> Result<Keypair, Box<dyn error::Error>> {
    let seed_phrase = prompt_password_stderr(&format!("[{}] seed phrase: ", keypair_name))?;
    let seed_phrase = seed_phrase.trim();
    let passphrase_prompt = format!(
        "[{}] If this seed phrase has an associated passphrase, enter it now. Otherwise, press ENTER to continue: ",
        keypair_name,
    );

    let keypair = if skip_validation {
        let passphrase = prompt_passphrase(&passphrase_prompt)?;
        if legacy {
            keypair_from_seed_phrase_and_passphrase(seed_phrase, &passphrase)?
        } else {
            let seed = generate_seed_from_seed_phrase_and_passphrase(seed_phrase, &passphrase);
            keypair_from_seed_and_derivation_path(&seed, derivation_path)?
        }
    } else {
        let sanitized = sanitize_seed_phrase(seed_phrase);
        let parse_language_fn = || {
            for language in &[
                Language::English,
                Language::ChineseSimplified,
                Language::ChineseTraditional,
                Language::Japanese,
                Language::Spanish,
                Language::Korean,
                Language::French,
                Language::Italian,
            ] {
                if let Ok(mnemonic) = Mnemonic::from_phrase(&sanitized, *language) {
                    return Ok(mnemonic);
                }
            }
            Err("Can't get mnemonic from seed phrases")
        };
        let mnemonic = parse_language_fn()?;
        let passphrase = prompt_passphrase(&passphrase_prompt)?;
        let seed = Seed::new(&mnemonic, &passphrase);
        if legacy {
            keypair_from_seed(seed.as_bytes())?
        } else {
            keypair_from_seed_and_derivation_path(seed.as_bytes(), derivation_path)?
        }
    };

    if confirm_pubkey {
        let pubkey = keypair.pubkey();
        print!("Recovered pubkey `{:?}`. Continue? (y/n): ", pubkey);
        let _ignored = stdout().flush();
        let mut input = String::new();
        stdin().read_line(&mut input).expect("Unexpected input");
        if input.to_lowercase().trim() != "y" {
            println!("Exiting");
            exit(1);
        }
    }

    Ok(keypair)
}
/// Reads user input from stdin to retrieve a seed phrase and passphrase for keypair derivation.
///
/// Optionally skips validation of seed phrase. Optionally confirms recovered
/// public key.
Reads user input from stdin to retrieve a seed phrase and passphrase for keypair derivation.
Optionally skips validation of seed phrase. Optionally confirms recovered
public key.
Reads user input from stdin to retrieve a seed phrase and passphrase for keypair derivation . Optionally skips validation of seed phrase . Optionally confirms recovered public key .
pub fn keypair_from_seed_phrase(
    keypair_name: &str,
    skip_validation: bool,
    confirm_pubkey: bool,
    derivation_path: Option<DerivationPath>,
    legacy: bool,
) -> Result<Keypair, Box<dyn error::Error>> {
    let seed_phrase = prompt_password_stderr(&format!("[{}] seed phrase: ", keypair_name))?;
    let seed_phrase = seed_phrase.trim();
    let passphrase_prompt = format!(
        "[{}] If this seed phrase has an associated passphrase, enter it now. Otherwise, press ENTER to continue: ",
        keypair_name,
    );
    let keypair = if skip_validation {
        let passphrase = prompt_passphrase(&passphrase_prompt)?;
        if legacy {
            keypair_from_seed_phrase_and_passphrase(seed_phrase, &passphrase)?
        } else {
            let seed = generate_seed_from_seed_phrase_and_passphrase(seed_phrase, &passphrase);
            keypair_from_seed_and_derivation_path(&seed, derivation_path)?
        }
    } else {
        let sanitized = sanitize_seed_phrase(seed_phrase);
        let parse_language_fn = || {
            for language in &[
                Language::English,
                Language::ChineseSimplified,
                Language::ChineseTraditional,
                Language::Japanese,
                Language::Spanish,
                Language::Korean,
                Language::French,
                Language::Italian,
            ] {
                if let Ok(mnemonic) = Mnemonic::from_phrase(&sanitized, *language) {
                    return Ok(mnemonic);
                }
            }
            Err("Can't get mnemonic from seed phrases")
        };
        let mnemonic = parse_language_fn()?;
        let passphrase = prompt_passphrase(&passphrase_prompt)?;
        let seed = Seed::new(&mnemonic, &passphrase);
        if legacy {
            keypair_from_seed(seed.as_bytes())?
        } else {
            keypair_from_seed_and_derivation_path(seed.as_bytes(), derivation_path)?
        }
    };
    if confirm_pubkey {
        let pubkey = keypair.pubkey();
        print!("Recovered pubkey `{:?}`. Continue? (y/n): ", pubkey);
        let _ignored = stdout().flush();
        let mut input = String::new();
        stdin().read_line(&mut input).expect("Unexpected input");
        if input.to_lowercase().trim() != "y" {
            println!("Exiting");
            exit(1);
        }
    }
    Ok(keypair)
}
pub fn keypair_from_seed_phrase ( keypair_name : & str , skip_validation : bool , confirm_pubkey : bool , derivation_path : Option < DerivationPath > , legacy : bool , ) -> Result < Keypair , Box < dyn error :: Error > > { let seed_phrase = prompt_password_stderr ( & format ! ( " " keypair_name ) ) ? ; let seed_phrase = seed_phrase . trim ( ) ; let passphrase_prompt = format ! ( " " keypair_name ) ; let keypair = if skip_validation { let passphrase = prompt_passphrase ( & passphrase_prompt ) ? ; if legacy { keypair_from_seed_phrase_and_passphrase ( seed_phrase , & passphrase ) ? } else { let seed = generate_seed_from_seed_phrase_and_passphrase ( seed_phrase , & passphrase ) ; keypair_from_seed_and_derivation_path ( & seed , derivation_path ) ? } } else { let sanitized = sanitize_seed_phrase ( seed_phrase ) ; let parse_language_fn = | | { for language in & [ Language :: English , Language :: ChineseSimplified , Language :: ChineseTraditional , Language :: Japanese , Language :: Spanish , Language :: Korean , Language :: French , Language :: Italian , ] { if let Ok ( mnemonic ) = Mnemonic :: from_phrase ( & sanitized , * language ) { return Ok ( mnemonic ) ; } } Err ( " " ) } ; let mnemonic = parse_language_fn ( ) ? ; let passphrase = prompt_passphrase ( & passphrase_prompt ) ? ; let seed = Seed :: new ( & mnemonic , & passphrase ) ; if legacy { keypair_from_seed ( seed . as_bytes ( ) ) ? } else { keypair_from_seed_and_derivation_path ( seed . as_bytes ( ) , derivation_path ) ? } } ; if confirm_pubkey { let pubkey = keypair . pubkey ( ) ; print ! ( " " pubkey ) ; let _ignored = stdout ( ) . flush ( ) ; let mut input = String :: new ( ) ; stdin ( ) . read_line ( & mut input ) . expect ( " " ) ; if input . to_lowercase ( ) . trim ( ) != " " { println ! ( " " ) ; exit ( 1 ) ; } } Ok ( keypair ) }
Reads user input from stdin to retrieve a seed phrase and passphrase for keypair derivation.
Reads user input from stdin to retrieve a seed phrase and passphrase for keypair derivation .
keypair_name
&str
skip_validation
bool
confirm_pubkey
bool
derivation_path
Option<DerivationPath>
legacy
bool