feat(sqlite): add bdk_sqlite crate implementing PersistBackend backed by a SQLite database

This commit is contained in:
Steve Myers
2023-09-19 17:29:23 -05:00
parent b8aa76cd05
commit 475c5024ec
13 changed files with 1196 additions and 153 deletions

View File

@@ -8,6 +8,6 @@ edition = "2021"
[dependencies]
bdk_wallet = { path = "../../crates/wallet" }
bdk_esplora = { path = "../../crates/esplora", features = ["async-https"] }
bdk_file_store = { path = "../../crates/file_store" }
bdk_sqlite = { path = "../../crates/sqlite" }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
anyhow = "1"

View File

@@ -1,22 +1,22 @@
use std::{collections::BTreeSet, io::Write, str::FromStr};
use bdk_esplora::{esplora_client, EsploraAsyncExt};
use bdk_file_store::Store;
use bdk_wallet::{
bitcoin::{Address, Amount, Network, Script},
KeychainKind, SignOptions, Wallet,
};
const DB_MAGIC: &str = "bdk_wallet_esplora_async_example";
use bdk_sqlite::{rusqlite::Connection, Store};
const SEND_AMOUNT: Amount = Amount::from_sat(5000);
const STOP_GAP: usize = 50;
const PARALLEL_REQUESTS: usize = 5;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let db_path = std::env::temp_dir().join("bdk-esplora-async-example");
let db =
Store::<bdk_wallet::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let db_path = "bdk-esplora-async-example.sqlite";
let conn = Connection::open(db_path)?;
let db = Store::new(conn)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
@@ -24,7 +24,7 @@ async fn main() -> Result<(), anyhow::Error> {
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
Network::Signet,
)?;
let address = wallet.next_unused_address(KeychainKind::External)?;
@@ -34,8 +34,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!("Wallet balance before syncing: {} sats", balance.total());
print!("Syncing...");
let client =
esplora_client::Builder::new("https://blockstream.info/testnet/api").build_async()?;
let client = esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_async()?;
fn generate_inspect(kind: KeychainKind) -> impl FnMut(u32, &Script) + Send + Sync + 'static {
let mut once = Some(());
@@ -91,7 +90,7 @@ async fn main() -> Result<(), anyhow::Error> {
}
let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
.require_network(Network::Testnet)?;
.require_network(Network::Signet)?;
let mut tx_builder = wallet.build_tx();
tx_builder