Splitting key load / wallet creation so we can use keys in esplora client

This commit is contained in:
Dave Hrycyszyn
2024-06-24 17:20:52 +01:00
parent 5c03a77e56
commit 117915bded
4 changed files with 51 additions and 24 deletions

View File

@@ -1,13 +1,12 @@
use bdk::{
bitcoin::Network,
database::MemoryDatabase,
bitcoin::{bip32::ExtendedPrivKey, Network},
keys::{
bip39::{Language, Mnemonic, WordCount},
DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey,
},
miniscript,
template::Bip84,
KeychainKind, Wallet,
KeychainKind,
};
use std::{
@@ -32,7 +31,9 @@ pub(crate) fn write(mnemonic_path: &PathBuf) -> Result<(), std::io::Error> {
}
/// Creates a Signet Bitcoin descriptor wallet from a mnemonic
pub fn create_wallet(mnemonic_words: String) -> anyhow::Result<Wallet<MemoryDatabase>> {
pub fn get(
mnemonic_words: String,
) -> anyhow::Result<(Bip84<ExtendedPrivKey>, Option<Bip84<ExtendedPrivKey>>)> {
let mnemonic = Mnemonic::parse(mnemonic_words).unwrap();
// Generate the extended key
@@ -41,20 +42,17 @@ pub fn create_wallet(mnemonic_words: String) -> anyhow::Result<Wallet<MemoryData
// Get private key from the extended key
let xprv = xkey.into_xprv(Network::Signet).unwrap();
// Create a BDK wallet using BIP 84 descriptor ("m/84h/1h/0h/0" and "m/84h/1h/0h/1")
let wallet = Wallet::new(
Bip84(xprv, KeychainKind::External),
Some(Bip84(xprv, KeychainKind::Internal)),
Network::Testnet,
MemoryDatabase::default(),
)?;
let external_descriptor = Bip84(xprv, KeychainKind::External);
let internal_descriptor = Some(Bip84(xprv, KeychainKind::Internal));
Ok(wallet)
Ok((external_descriptor, internal_descriptor))
}
pub(crate) fn load_from_file(side_dir: &PathBuf) -> anyhow::Result<Wallet<MemoryDatabase>> {
pub(crate) fn load_from_file(
side_dir: &PathBuf,
) -> anyhow::Result<(Bip84<ExtendedPrivKey>, Option<Bip84<ExtendedPrivKey>>)> {
let mnemonic_path = crate::utils::side_paths(side_dir.clone()).1; // TODO: this tuple stinks
let mnemonic_words = fs::read_to_string(mnemonic_path).expect("couldn't read bitcoin key file");
println!("Creating wallet from mnemonic: {mnemonic_words}");
create_wallet(mnemonic_words)
get(mnemonic_words)
}