HD wallet implementation for Solana built with Rust.
This crate utilizes the EdDSA algorithm rust implementation found in the ed25519_dalek crate.
Import crate:
rust
use hdwallet_rs;
Note: This crate uses the bip0039 crate for mnemonic and seed phrase generation; you can choose to generate your mnemonic from English, Japanese, Korean, Italian, French, Czech, Chinese, and Portuguese.
Generate extended key:
``` use hdwalletrs::{ extendedkey::SolanaExPrivateKey, mnemonic }
let seedphrase = mnemonic::newmnemonic(24, "English"); let seed = mnemonic::newseed(seedphrase.unwrap(), "".tostring()); let masterkey = SolanaExPrivateKey::newmasterkey(&seed).expect("master key"); ```
Derive private key with it's derivation and public key:
``` use hdwalletrs::{ extendedkey::SolanaExPrivateKey, mnemonic, solana::{ key_chain::DefaultKeyChain, PrivKey, PubKey, }, traits::{Deserialize, Serialize}, SolanaExPrivateKey, };
let newmnemonic = mnemonic::newmnemonic(24, "English"); let seed = mnemonic::newseed(newmnemonic.unwrap(), "".tostring()); let masterkey = SolanaExPrivateKey::newmasterkey(&seed).expect("master key");
// A default key chain on which others are based on. let keychain = DefaultKeyChain::new(masterkey);
// Chain path. let chain_path = "m/0H/1H";
// Get extended key and derivation from the chain path. let (extendedkey, derivation) = keychain.deriveprivatekey(chain_path.into()).expect("fetch key");
// Private Key. let privatekey = PrivKey { derivation, extendedkey, };
// Public Key. let publickey = PubKey::fromprivatekey(&privatekey);
// Convert to Base58 address. let serializedkey: String = publickey.serialize();
let deserializedkey = PubKey::deserialize((serializedkey, &key_chain, "m/0H/1H".into())).expect("deserialize");
asserteq!(publickey, deserialized_key); ```
Note: In Solana, all key derivations are always in the hardened form, meaning each derivation path is suffixed with (H or ').