Setting up an SDK wallet
The SDK exposes methods for interacting with a filesystem wallet. You can insert, read, or remove both transparent and shielded keys/addresses for later use when constructing and signing transactions.
Add a Keypair to the Wallet
If you know an address and secret (private) key, you can add them directly to the wallet:
use namada_sdk::address::Address;
use namada_sdk::key::common::SecretKey;
let alice_sk = SecretKey::from_str("0071b4b45443ead34bef175cac5eaf266f653d122b75e077c87b5146f35f45b6ee").expect("Invalid secret key");
let alice_address = Address::from_str("tnam1qpm2l32hvlw44fwne8fs0y4qsw2jfy4tvyvyywt8").expect("Invalid address");
sdk.wallet_mut().await.insert_keypair(
"alice".to_string(), // the alias
true, // overwrite this alias if it exists in wallet
alice_sk,
None, // no password
Some(alice_address),
None // default derivation path
);
println!("alice: {:?}", sdk.wallet().await.find_address("alice".to_string()));
Derive a Keypair from a Mnemonic
You can also derive a keypair from a mnemonic:
use namada_sdk::bip39::Mnemonic;
use namada_sdk::key::SchemeType;
use namada_sdk::wallet::DerivationPath;
use namada_sdk::zeroize::Zeroizing;
let phrase = "invest canal odor resource valley property chimney royal puzzle inch earth route diagram letter ceiling clinic post zebra hidden promote list valid define wedding";
let mnemonic = Mnemonic::from_phrase(phrase, namada_sdk::bip39::Language::English).expect("Invalid mnemonic");
let derivation_path = DerivationPath::default_for_transparent_scheme(SchemeType::Ed25519);
let (_key_alias, _sk) = sdk
.wallet_mut()
.await
.derive_store_key_from_mnemonic_code(
SchemeType::Ed25519, // key scheme
Some("bob".to_string()), // alias
true, // overwrite alias if it exists
derivation_path,
Some((mnemonic.clone(), Zeroizing::new("".to_owned()))),
false, // do not prompt for passphrase
None, // no password
)
.expect("unable to derive key from mnemonic code");
println!("bob: {:?}", sdk.wallet().await.find_address("bob".to_string()));
Saving the Wallet
The sdk wallet exists only in memory until you explicitly save it to disk:
sdk.wallet().await.save().expect("Could not save wallet!");
This will write the wallet.toml
file to the directory you provided when creating the context.