Skip to content

Commit

Permalink
fixup! wip: update example to use sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Oct 22, 2024
1 parent 8756a9f commit ffa2eef
Showing 1 changed file with 33 additions and 38 deletions.
71 changes: 33 additions & 38 deletions example-crates/example_migrate_wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,48 @@ use bdk::wallet::{AddressIndex, Wallet};
use bdk_electrum::{electrum_client, BdkElectrumClient};
use bdk_wallet::rusqlite;

const DESC: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
const CHANGE_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
const ELECTRUM_URL: &str = "ssl://mempool.space:60602";
const NETWORK: Network = Network::Signet;
const EXTERNAL: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
const INTERNAL: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
const ELECTRUM_URL: &str = "ssl://electrum.blockstream.info:60002";
const NETWORK: Network = Network::Testnet;

const BDK_DB_PATH: &str = "example-bdk.sqlite";
const BDK_WALLET_DB_PATH: &str = "example-bdk-wallet.sqlite";
const BDK_DB_PATH: &str = ".bdk-example.sqlite";
const BDK_WALLET_DB_PATH: &str = ".bdk-wallet-example.sqlite";

// Steps for migrating wallet details from bdk v0.29 to bdk_wallet v1.0. These steps
// are applicable to wallets backed by a SQLite database.
// Steps for migrating wallet parameters from the old `bdk` 0.30 to the new `bdk_wallet` 1.0.
// These steps can be applied to wallets backed by a SQLite database.

// May be run with no arguments or optionally providing a path to an existing sqlite db
// e.g. `cargo run --bin example_migrate_wallet -- <path-to-bdk-database>`
// Usage: `cargo run --bin example_migrate_wallet`

fn main() -> anyhow::Result<()> {
let cargo_dir = std::env::var("CARGO_MANIFEST_DIR")?;

// Accept a db config passed in via command line, or else create a new one for testing
let args: Vec<String> = std::env::args().collect();
let db = if args.len() < 2 {
let path = format!("{}/{}", cargo_dir, BDK_DB_PATH);
SqliteDatabase::new(path)
} else {
let path = args[1].to_string();
SqliteDatabase::new(path)
};
// Open old wallet
let db = SqliteDatabase::new(BDK_DB_PATH);
let old_wallet = Wallet::new(EXTERNAL, Some(INTERNAL), NETWORK, db)?;

// Open wallet
let wallet = Wallet::new(DESC, Some(CHANGE_DESC), NETWORK, db)?;
// note: can't get this to work
// let client = bdk::electrum_client::Client::new(ELECTRUM_URL)?;
// let electrum = bdk::blockchain::ElectrumBlockchain::from(client);
// old_wallet.sync(&electrum, bdk::SyncOptions::default())?;

// Get last revealed addresses for each keychain
let addr = wallet.get_address(AddressIndex::LastUnused)?;
let addr = old_wallet.get_address(AddressIndex::LastUnused)?;
println!("Last revealed external {} {}", addr.index, addr.address);
let external_derivation_index = addr.index;
let last_revealed_external = addr.address.to_string();

let addr = wallet.get_internal_address(AddressIndex::LastUnused)?;
let addr = old_wallet.get_internal_address(AddressIndex::LastUnused)?;
println!("Last revealed internal {} {}", addr.index, addr.address);
let internal_derivation_index = addr.index;
let last_revealed_internal = addr.address.to_string();

let balance = old_wallet.get_balance()?;
println!("{balance:#?}");

// Create new wallet
// For the new bdk wallet we pass in the same descriptors as before. If the given descriptors
// contain secret keys the wallet will be able to sign transactions as well.
let new_db_path = format!("{}/{}", cargo_dir, BDK_WALLET_DB_PATH);
let mut db = rusqlite::Connection::open(new_db_path)?;
let mut wallet = match bdk_wallet::Wallet::create(DESC, CHANGE_DESC)
let mut db = rusqlite::Connection::open(BDK_WALLET_DB_PATH)?;
let mut new_wallet = match bdk_wallet::Wallet::create(EXTERNAL, INTERNAL)
.network(bdk_wallet::bitcoin::Network::Signet)
.create_wallet(&mut db)
{
Expand All @@ -63,27 +58,27 @@ fn main() -> anyhow::Result<()> {
};

// Retore revealed addresses
let _ = wallet.reveal_addresses_to(
let _ = new_wallet.reveal_addresses_to(
bdk_wallet::KeychainKind::External,
external_derivation_index,
);
let _ = wallet.reveal_addresses_to(
let _ = new_wallet.reveal_addresses_to(
bdk_wallet::KeychainKind::Internal,
internal_derivation_index,
);

// Remember to persist the new wallet
wallet.persist(&mut db)?;
new_wallet.persist(&mut db)?;

println!("\n========== New database created. ==========");

let addr = wallet
let addr = new_wallet
.list_unused_addresses(bdk_wallet::KeychainKind::External)
.last()
.unwrap();
assert_eq!(addr.to_string(), last_revealed_external);
println!("Last revealed external {} {}", addr.index, addr.address);
let addr = wallet
let addr = new_wallet
.list_unused_addresses(bdk_wallet::KeychainKind::Internal)
.last()
.unwrap();
Expand All @@ -97,7 +92,7 @@ fn main() -> anyhow::Result<()> {
// for a fully functioning wallet.

let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);
let req = wallet.start_full_scan().inspect({
let request = new_wallet.start_full_scan().inspect({
let mut stdout = std::io::stdout();
let mut once = HashSet::new();
move |k, spk_i, _| {
Expand All @@ -109,11 +104,11 @@ fn main() -> anyhow::Result<()> {
}
});

let update = client.full_scan(req, 20, 1, true)?;
let update = client.full_scan(request, 20, 5, true)?;

wallet.apply_update(update)?;
wallet.persist(&mut db)?;
println!("\n{:#?}", wallet.balance());
new_wallet.apply_update(update)?;
new_wallet.persist(&mut db)?;
println!("\n{:#?}", new_wallet.balance());

Ok(())
}

0 comments on commit ffa2eef

Please sign in to comment.