smbcloud-wallet-core 1.0.9

smbCloud Wallet Core for NotWallet Crypto.
Documentation
use {
    bip39::Mnemonic,
    log::debug,
    solana_derivation_path::DerivationPath,
    solana_sdk::signer::keypair::{keypair_from_seed_and_derivation_path, Keypair},
    std::str::FromStr,
};

/// Derives a Solana keypair from a mnemonic, account, and change index.
/// Returns Result<Keypair, String>
pub fn derive_keypair(mnemonic_phrase: &str, account: u32, change: u32) -> Result<Keypair, String> {
    let mnemonic =
        Mnemonic::from_str(mnemonic_phrase).map_err(|e| format!("Invalid mnemonic: {:?}", e))?;
    let seed = Mnemonic::to_seed(&mnemonic, "");
    let derivation_path = DerivationPath::new_bip44(Some(account), Some(change));
    debug!("Deriving keypair: {:?}", derivation_path);
    keypair_from_seed_and_derivation_path(&seed, Some(derivation_path))
        .map_err(|e| format!("Keypair derivation failed: {:?}", e))
}

/// Overload: default change = 0
pub fn derive_keypair_default(mnemonic_phrase: &str, account: u32) -> Result<Keypair, String> {
    derive_keypair(mnemonic_phrase, account, 0)
}