Skip to content

Commit

Permalink
feat!: move WalletChangeSet to bdk_wallet and fix import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed Jul 17, 2024
1 parent b394a67 commit 47fb0ac
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 165 deletions.
8 changes: 2 additions & 6 deletions crates/chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ pub use descriptor_ext::{DescriptorExt, DescriptorId};
#[cfg(feature = "miniscript")]
mod spk_iter;
#[cfg(feature = "miniscript")]
pub use spk_iter::*;
#[cfg(feature = "miniscript")]
mod changeset;
#[cfg(feature = "miniscript")]
pub use changeset::*;
#[cfg(feature = "miniscript")]
pub use indexer::keychain_txout;
#[cfg(feature = "miniscript")]
pub use spk_iter::*;
#[cfg(feature = "sqlite")]
pub mod sqlite;
#[cfg(feature = "sqlite")]
Expand Down
2 changes: 1 addition & 1 deletion crates/hwi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! # }
//! ```
//!
//! [`TransactionSigner`]: bdk_wallet::wallet::signer::TransactionSigner
//! [`TransactionSigner`]: bdk_wallet::signer::TransactionSigner
mod signer;
pub use signer::*;
2 changes: 1 addition & 1 deletion crates/hwi/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl TransactionSigner for HWISigner {
// Arc::new(custom_signer),
// );
//
// let addr = wallet.get_address(bdk_wallet::wallet::AddressIndex::LastUnused);
// let addr = wallet.get_address(bdk_wallet::AddressIndex::LastUnused);
// let mut builder = wallet.build_tx();
// builder.drain_to(addr.script_pubkey()).drain_wallet();
// let (mut psbt, _) = builder.finish().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bdk_file_store = { path = "../file_store", version = "0.13.0", optional = true }
bip39 = { version = "2.0", optional = true }

[features]
default = ["std", "file_store"]
default = ["std"]
std = ["bitcoin/std", "bitcoin/rand-std", "miniscript/std", "bdk_chain/std"]
compiler = ["miniscript/compiler"]
all-keys = ["keys-bip39"]
Expand Down
6 changes: 3 additions & 3 deletions crates/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ that the `Wallet` can use to update its view of the chain.

## Persistence

To persist `Wallet` state data use a data store crate that reads and writes [`bdk_chain::WalletChangeSet`].
To persist `Wallet` state data use a data store crate that reads and writes [`ChangeSet`].

**Implementations**

Expand Down Expand Up @@ -122,7 +122,7 @@ println!("Your new receive address is: {}", receive_address.address);

<!-- ```rust -->
<!-- use bdk_wallet::Wallet; -->
<!-- use bdk_wallet::wallet::AddressIndex::New; -->
<!-- use bdk_wallet::AddressIndex::New; -->
<!-- use bdk_wallet::bitcoin::Network; -->

<!-- fn main() -> Result<(), bdk_wallet::Error> { -->
Expand All @@ -147,7 +147,7 @@ println!("Your new receive address is: {}", receive_address.address);
<!-- use bdk_wallet::blockchain::ElectrumBlockchain; -->

<!-- use bdk_wallet::electrum_client::Client; -->
<!-- use bdk_wallet::wallet::AddressIndex::New; -->
<!-- use bdk_wallet::AddressIndex::New; -->

<!-- use bitcoin::base64; -->
<!-- use bdk_wallet::bitcoin::consensus::serialize; -->
Expand Down
4 changes: 2 additions & 2 deletions crates/wallet/examples/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::error::Error;

use bdk_wallet::bitcoin::Network;
use bdk_wallet::descriptor::{policy::BuildSatisfaction, ExtractPolicy, IntoWalletDescriptor};
use bdk_wallet::wallet::signer::SignersContainer;
use bdk_wallet::signer::SignersContainer;

/// This example describes the use of the BDK's [`bdk_wallet::descriptor::policy`] module.
///
Expand All @@ -38,7 +38,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// While the `keymap` can be used to create a `SignerContainer`.
//
// The `SignerContainer` can sign for `PSBT`s.
// a bdk_wallet::wallet internally uses these to handle transaction signing.
// a `bdk_wallet::Wallet` internally uses these to handle transaction signing.
// But they can be used as independent tools also.
let (wallet_desc, keymap) = desc.into_wallet_descriptor(&secp, Network::Testnet)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/wallet/src/descriptor/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! ```
//! # use std::sync::Arc;
//! # use bdk_wallet::descriptor::*;
//! # use bdk_wallet::wallet::signer::*;
//! # use bdk_wallet::signer::*;
//! # use bdk_wallet::bitcoin::secp256k1::Secp256k1;
//! use bdk_wallet::descriptor::policy::BuildSatisfaction;
//! let secp = Secp256k1::new();
Expand Down
43 changes: 18 additions & 25 deletions crates/wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,36 @@ extern crate std;
#[doc(hidden)]
#[macro_use]
pub extern crate alloc;

pub extern crate bdk_chain as chain;
#[cfg(feature = "file_store")]
pub extern crate bdk_file_store as file_store;
#[cfg(feature = "keys-bip39")]
pub extern crate bip39;
pub extern crate bitcoin;
pub extern crate miniscript;
extern crate serde;
extern crate serde_json;

#[cfg(feature = "keys-bip39")]
extern crate bip39;
pub extern crate serde;
pub extern crate serde_json;

pub mod descriptor;
pub mod keys;
pub mod psbt;
pub(crate) mod types;
pub mod wallet;
mod types;
mod wallet;

pub(crate) use bdk_chain::collections;
#[cfg(feature = "sqlite")]
pub use bdk_chain::rusqlite;
#[cfg(feature = "sqlite")]
pub use bdk_chain::sqlite;
pub use descriptor::template;
pub use descriptor::HdKeyPaths;
pub use signer;
pub use signer::SignOptions;
pub use tx_builder::*;
pub use types::*;
pub use wallet::signer;
pub use wallet::signer::SignOptions;
pub use wallet::tx_builder::TxBuilder;
pub use wallet::ChangeSet;
pub use wallet::CreateParams;
pub use wallet::LoadParams;
pub use wallet::PersistedWallet;
pub use wallet::Wallet;
pub use wallet::*;

/// Get the version of [`bdk_wallet`](crate) at runtime.
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION", "unknown")
}

pub use bdk_chain as chain;
pub(crate) use bdk_chain::collections;
#[cfg(feature = "sqlite")]
pub use bdk_chain::rusqlite;
#[cfg(feature = "sqlite")]
pub use bdk_chain::sqlite;

pub use chain::WalletChangeSet;
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
use crate::{ConfirmationBlockTime, Merge};
use bdk_chain::{
indexed_tx_graph, keychain_txout, local_chain, tx_graph, ConfirmationBlockTime, Merge,
};
use miniscript::{Descriptor, DescriptorPublicKey};

type IndexedTxGraphChangeSet =
crate::indexed_tx_graph::ChangeSet<ConfirmationBlockTime, crate::keychain_txout::ChangeSet>;

/// A changeset containing [`crate`] structures typically persisted together.
#[derive(Default, Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(crate::serde::Deserialize, crate::serde::Serialize),
serde(crate = "crate::serde")
)]
pub struct WalletChangeSet {
indexed_tx_graph::ChangeSet<ConfirmationBlockTime, keychain_txout::ChangeSet>;

/// A changeset for [`Wallet`](crate::Wallet).
#[derive(Default, Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ChangeSet {
/// Descriptor for recipient addresses.
pub descriptor: Option<miniscript::Descriptor<miniscript::DescriptorPublicKey>>,
pub descriptor: Option<Descriptor<DescriptorPublicKey>>,
/// Descriptor for change addresses.
pub change_descriptor: Option<miniscript::Descriptor<miniscript::DescriptorPublicKey>>,
pub change_descriptor: Option<Descriptor<DescriptorPublicKey>>,
/// Stores the network type of the transaction data.
pub network: Option<bitcoin::Network>,
/// Changes to the [`LocalChain`](crate::local_chain::LocalChain).
pub local_chain: crate::local_chain::ChangeSet,
/// Changes to [`TxGraph`](crate::tx_graph::TxGraph).
pub tx_graph: crate::tx_graph::ChangeSet<crate::ConfirmationBlockTime>,
/// Changes to [`KeychainTxOutIndex`](crate::keychain_txout::KeychainTxOutIndex).
pub indexer: crate::keychain_txout::ChangeSet,
/// Changes to the [`LocalChain`](local_chain::LocalChain).
pub local_chain: local_chain::ChangeSet,
/// Changes to [`TxGraph`](tx_graph::TxGraph).
pub tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>,
/// Changes to [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex).
pub indexer: keychain_txout::ChangeSet,
}

impl Merge for WalletChangeSet {
/// Merge another [`WalletChangeSet`] into itself.
///
/// The `keychains_added` field respects the invariants of... TODO: FINISH THIS!
impl Merge for ChangeSet {
/// Merge another [`ChangeSet`] into itself.
fn merge(&mut self, other: Self) {
if other.descriptor.is_some() {
debug_assert!(
Expand All @@ -52,9 +48,9 @@ impl Merge for WalletChangeSet {
self.network = other.network;
}

crate::Merge::merge(&mut self.local_chain, other.local_chain);
crate::Merge::merge(&mut self.tx_graph, other.tx_graph);
crate::Merge::merge(&mut self.indexer, other.indexer);
Merge::merge(&mut self.local_chain, other.local_chain);
Merge::merge(&mut self.tx_graph, other.tx_graph);
Merge::merge(&mut self.indexer, other.indexer);
}

fn is_empty(&self) -> bool {
Expand All @@ -68,14 +64,14 @@ impl Merge for WalletChangeSet {
}

#[cfg(feature = "sqlite")]
impl WalletChangeSet {
impl ChangeSet {
/// Schema name for wallet.
pub const WALLET_SCHEMA_NAME: &'static str = "bdk_wallet";
/// Name of table to store wallet descriptors and network.
pub const WALLET_TABLE_NAME: &'static str = "bdk_wallet";

/// Initialize sqlite tables for wallet schema & table.
fn init_wallet_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
fn init_wallet_sqlite_tables(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<()> {
let schema_v0: &[&str] = &[&format!(
"CREATE TABLE {} ( \
id INTEGER PRIMARY KEY NOT NULL CHECK (id = 0), \
Expand All @@ -88,12 +84,12 @@ impl WalletChangeSet {
crate::sqlite::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])
}

/// Recover a [`WalletChangeSet`] from sqlite database.
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
/// Recover a [`ChangeSet`] from sqlite database.
pub fn from_sqlite(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<Self> {
Self::init_wallet_sqlite_tables(db_tx)?;
use crate::sqlite::Sql;
use miniscript::{Descriptor, DescriptorPublicKey};
use rusqlite::OptionalExtension;
use chain::rusqlite::OptionalExtension;

let mut changeset = Self::default();

Expand All @@ -116,18 +112,18 @@ impl WalletChangeSet {
changeset.network = Some(network);
}

changeset.local_chain = crate::local_chain::ChangeSet::from_sqlite(db_tx)?;
changeset.tx_graph = crate::tx_graph::ChangeSet::<_>::from_sqlite(db_tx)?;
changeset.indexer = crate::indexer::keychain_txout::ChangeSet::from_sqlite(db_tx)?;
changeset.local_chain = local_chain::ChangeSet::from_sqlite(db_tx)?;
changeset.tx_graph = tx_graph::ChangeSet::<_>::from_sqlite(db_tx)?;
changeset.indexer = keychain_txout::ChangeSet::from_sqlite(db_tx)?;

Ok(changeset)
}

/// Persist [`WalletChangeSet`] to sqlite database.
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
/// Persist [`ChangeSet`] to sqlite database.
pub fn persist_to_sqlite(&self, db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<()> {
Self::init_wallet_sqlite_tables(db_tx)?;
use crate::sqlite::Sql;
use rusqlite::named_params;
use chain::sqlite::Sql;
use chain::rusqlite::named_params;

let mut descriptor_statement = db_tx.prepare_cached(&format!(
"INSERT INTO {}(id, descriptor) VALUES(:id, :descriptor) ON CONFLICT(id) DO UPDATE SET descriptor=:descriptor",
Expand Down Expand Up @@ -169,16 +165,16 @@ impl WalletChangeSet {
}
}

impl From<crate::local_chain::ChangeSet> for WalletChangeSet {
fn from(chain: crate::local_chain::ChangeSet) -> Self {
impl From<local_chain::ChangeSet> for ChangeSet {
fn from(chain: local_chain::ChangeSet) -> Self {
Self {
local_chain: chain,
..Default::default()
}
}
}

impl From<IndexedTxGraphChangeSet> for WalletChangeSet {
impl From<IndexedTxGraphChangeSet> for ChangeSet {
fn from(indexed_tx_graph: IndexedTxGraphChangeSet) -> Self {
Self {
tx_graph: indexed_tx_graph.tx_graph,
Expand All @@ -188,17 +184,17 @@ impl From<IndexedTxGraphChangeSet> for WalletChangeSet {
}
}

impl From<crate::tx_graph::ChangeSet<ConfirmationBlockTime>> for WalletChangeSet {
fn from(tx_graph: crate::tx_graph::ChangeSet<ConfirmationBlockTime>) -> Self {
impl From<tx_graph::ChangeSet<ConfirmationBlockTime>> for ChangeSet {
fn from(tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>) -> Self {
Self {
tx_graph,
..Default::default()
}
}
}

impl From<crate::keychain_txout::ChangeSet> for WalletChangeSet {
fn from(indexer: crate::keychain_txout::ChangeSet) -> Self {
impl From<keychain_txout::ChangeSet> for ChangeSet {
fn from(indexer: keychain_txout::ChangeSet) -> Self {
Self {
indexer,
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions crates/wallet/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
//! ```
//! # use std::str::FromStr;
//! # use bitcoin::*;
//! # use bdk_wallet::wallet::{self, ChangeSet, coin_selection::*, coin_selection};
//! # use bdk_wallet::wallet::error::CreateTxError;
//! # use bdk_wallet::{self, ChangeSet, coin_selection::*, coin_selection};
//! # use bdk_wallet::error::CreateTxError;
//! # use bdk_wallet::*;
//! # use bdk_wallet::wallet::coin_selection::decide_change;
//! # use bdk_wallet::coin_selection::decide_change;
//! # use anyhow::Error;
//! #[derive(Debug)]
//! struct AlwaysSpendEverything;
Expand Down
4 changes: 2 additions & 2 deletions crates/wallet/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! ```
//! # use std::str::FromStr;
//! # use bitcoin::*;
//! # use bdk_wallet::wallet::export::*;
//! # use bdk_wallet::export::*;
//! # use bdk_wallet::*;
//! let import = r#"{
//! "descriptor": "wpkh([c258d2e4\/84h\/1h\/0h]tpubDD3ynpHgJQW8VvWRzQ5WFDCrs4jqVFGHB3vLC3r49XHJSqP8bHKdK4AriuUKLccK68zfzowx7YhmDN8SiSkgCDENUFx9qVw65YyqM78vyVe\/0\/*)",
Expand All @@ -41,7 +41,7 @@
//! ### Export a `Wallet`
//! ```
//! # use bitcoin::*;
//! # use bdk_wallet::wallet::export::*;
//! # use bdk_wallet::export::*;
//! # use bdk_wallet::*;
//! let wallet = CreateParams::new(
//! "wpkh([c258d2e4/84h/1h/0h]tpubDD3ynpHgJQW8VvWRzQ5WFDCrs4jqVFGHB3vLC3r49XHJSqP8bHKdK4AriuUKLccK68zfzowx7YhmDN8SiSkgCDENUFx9qVw65YyqM78vyVe/0/*)",
Expand Down
4 changes: 2 additions & 2 deletions crates/wallet/src/wallet/hardwaresigner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
//! ```no_run
//! # use bdk_wallet::bitcoin::Network;
//! # use bdk_wallet::signer::SignerOrdering;
//! # use bdk_wallet::wallet::hardwaresigner::HWISigner;
//! # use bdk_wallet::wallet::AddressIndex::New;
//! # use bdk_wallet::hardwaresigner::HWISigner;
//! # use bdk_wallet::AddressIndex::New;
//! # use bdk_wallet::{CreateParams, KeychainKind, SignOptions};
//! # use hwi::HWIClient;
//! # use std::sync::Arc;
Expand Down
Loading

0 comments on commit 47fb0ac

Please sign in to comment.