Skip to content

Commit 0e3e136

Browse files
doc(bdk): Add instructions for manually inserting...
...secret keys in the wallet in Wallet::load
1 parent 76afccc commit 0e3e136

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,46 @@ impl Wallet {
517517
}
518518

519519
/// Load [`Wallet`] from the given persistence backend.
520+
///
521+
/// Note that the descriptor secret keys are not persisted to the db; this means that after
522+
/// calling this method the [`Wallet`] **won't** know the secret keys, and as such, won't be
523+
/// able to sign transactions.
524+
///
525+
/// If you wish to use the wallet to sign transactions, you need to add the secret keys
526+
/// manually to the [`Wallet`]:
527+
///
528+
/// ```rust,no_run
529+
/// # use bdk::Wallet;
530+
/// # use bdk::signer::{SignersContainer, SignerOrdering};
531+
/// # use bdk::descriptor::Descriptor;
532+
/// # use bitcoin::key::Secp256k1;
533+
/// # use bdk::KeychainKind;
534+
/// # use bdk_file_store::Store;
535+
/// #
536+
/// # fn main() -> Result<(), anyhow::Error> {
537+
/// # let temp_dir = tempfile::tempdir().expect("must create tempdir");
538+
/// # let file_path = temp_dir.path().join("store.db");
539+
/// # let db: Store<bdk::wallet::ChangeSet> = Store::create_new(&[], &file_path).expect("must create db");
540+
/// let secp = Secp256k1::new();
541+
///
542+
/// let (external_descriptor, external_keymap) = Descriptor::parse_descriptor(&secp, "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)").unwrap();
543+
/// let (internal_descriptor, internal_keymap) = Descriptor::parse_descriptor(&secp, "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)").unwrap();
544+
///
545+
/// let external_signer_container = SignersContainer::build(external_keymap, &external_descriptor, &secp);
546+
/// let internal_signer_container = SignersContainer::build(internal_keymap, &internal_descriptor, &secp);
547+
///
548+
/// let mut wallet = Wallet::load(db)?;
549+
///
550+
/// external_signer_container.signers().into_iter()
551+
/// .for_each(|s| wallet.add_signer(KeychainKind::External, SignerOrdering::default(), s.clone()));
552+
/// internal_signer_container.signers().into_iter()
553+
/// .for_each(|s| wallet.add_signer(KeychainKind::Internal, SignerOrdering::default(), s.clone()));
554+
/// # Ok(())
555+
/// # }
556+
/// ```
557+
///
558+
/// Alternatively, you can call [`Wallet::new_or_load`], which will add the private keys of the
559+
/// passed-in descriptors to the [`Wallet`].
520560
pub fn load(
521561
mut db: impl PersistBackend<ChangeSet> + Send + Sync + 'static,
522562
) -> Result<Self, LoadError> {

0 commit comments

Comments
 (0)