-
Notifications
You must be signed in to change notification settings - Fork 391
Introduce bdk::Wallet
builder API
#1250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4fd539b
feat(chain)!: `KeychainTxOutIndex` uses a universal lookahead
darosior bc796f4
fix(example): bitcoind_rpc_polling now initializes local_chain properly
evanlinjin 78dff4a
feat(wallet)!: use builder pattern to initialize wallet
evanlinjin 1662340
refactor(chain): make clippy happy
evanlinjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use bdk_chain::PersistBackend; | ||
use bitcoin::constants::genesis_block; | ||
use bitcoin::{BlockHash, Network}; | ||
|
||
use crate::descriptor::{DescriptorError, IntoWalletDescriptor}; | ||
use crate::wallet::{ChangeSet, Wallet}; | ||
|
||
use super::{InitError, InitOrLoadError}; | ||
|
||
/// Helper structure to initialize a fresh [`Wallet`] instance. | ||
pub struct WalletBuilder<E> { | ||
pub(super) descriptor: E, | ||
pub(super) change_descriptor: Option<E>, | ||
pub(super) network: Network, | ||
pub(super) custom_genesis_hash: Option<BlockHash>, | ||
} | ||
|
||
impl<E> WalletBuilder<E> { | ||
/// Start building a fresh [`Wallet`] instance. | ||
pub(super) fn new(descriptor: E) -> Self { | ||
Self { | ||
descriptor, | ||
change_descriptor: None, | ||
network: Network::Bitcoin, | ||
custom_genesis_hash: None, | ||
} | ||
} | ||
|
||
/// Set the internal (change) keychain. | ||
/// | ||
/// The internal keychain will be used to derive change addresses for change outputs. | ||
/// | ||
/// If no internal keychain is set, the wallet will use the external keychain for deriving | ||
/// change addresses. | ||
pub fn with_change_descriptor(mut self, change_descriptor: E) -> Self { | ||
self.change_descriptor = Some(change_descriptor); | ||
self | ||
} | ||
|
||
/// Set the [`Network`] type for the wallet. | ||
/// | ||
/// This changes the format of the derived wallet, as well as the internal genesis block hash. | ||
/// The internal genesis block hash can be overriden by [`with_genesis_hash`]. | ||
/// | ||
/// By default, [`Network::Bitcoin`] is used. | ||
/// | ||
/// [`with_genesis_hash`]: Self::with_genesis_hash | ||
pub fn with_network(mut self, network: Network) -> Self { | ||
self.network = network; | ||
self | ||
} | ||
|
||
/// Overrides the genesis hash implied by [`with_network`]. | ||
/// | ||
/// [`with_network`]: Self::with_network | ||
pub fn with_genesis_hash(mut self, genesis_hash: BlockHash) -> Self { | ||
self.custom_genesis_hash = Some(genesis_hash); | ||
self | ||
} | ||
|
||
pub(super) fn determine_genesis_hash(&self) -> BlockHash { | ||
self.custom_genesis_hash | ||
.unwrap_or_else(|| genesis_block(self.network).block_hash()) | ||
} | ||
} | ||
|
||
impl<E: IntoWalletDescriptor> WalletBuilder<E> { | ||
/// Initializes a fresh wallet and persists it in `db`. | ||
pub fn init<D>(self, db: D) -> Result<Wallet<D>, InitError<D::WriteError>> | ||
where | ||
D: PersistBackend<ChangeSet>, | ||
{ | ||
Wallet::init(self, db) | ||
} | ||
|
||
/// Either loads [`Wallet`] from persistence, or initializes a fresh wallet if it does not | ||
/// exist. | ||
/// | ||
/// This method will fail if the persistence is non-empty with parameters that are different to | ||
/// those specified by [`WalletBuilder`]. | ||
pub fn init_or_load<D>( | ||
self, | ||
db: D, | ||
) -> Result<Wallet<D>, InitOrLoadError<D::WriteError, D::LoadError>> | ||
where | ||
D: PersistBackend<ChangeSet>, | ||
{ | ||
Wallet::init_or_load(self, db) | ||
} | ||
|
||
/// Initializes a fresh wallet with no persistence. | ||
pub fn init_without_persistence(self) -> Result<Wallet<()>, DescriptorError> { | ||
Wallet::init(self, ()).map_err(|err| match err { | ||
InitError::Descriptor(err) => err, | ||
InitError::Write(_) => panic!("there is no db to write to"), | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel about just calling this
Builder
, i.e.bdk::wallet::Builder
?