Skip to content

Commit

Permalink
refactor(wallet)!: rename LoadParams methods
Browse files Browse the repository at this point in the history
The network and genesis_hash methods on the LoadParams struct have been
renamed to check_network and check_genesis_hash to better reflect their
use.
  • Loading branch information
thunderbiscuit committed Aug 14, 2024
1 parent cc84872 commit 2391b76
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ let network = Network::Testnet;
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
let wallet_opt = Wallet::load()
.network(network)
.descriptor(KeychainKind::External, Some(descriptor))
.descriptor(KeychainKind::Internal, Some(change_descriptor))
.extract_keys()
.check_network(network)
.load_wallet(&mut db)
.expect("wallet");
let mut wallet = match wallet_opt {
Expand Down
2 changes: 1 addition & 1 deletion crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl Wallet {
/// .keymap(KeychainKind::External, external_keymap)
/// .keymap(KeychainKind::Internal, internal_keymap)
/// // ensure loaded wallet's genesis hash matches this value
/// .genesis_hash(genesis_hash)
/// .check_genesis_hash(genesis_hash)
/// // set a lookahead for our indexer
/// .lookahead(101)
/// .load_wallet(&mut conn)?
Expand Down
8 changes: 4 additions & 4 deletions crates/wallet/src/wallet/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ impl LoadParams {
self
}

/// Check for `network`.
pub fn network(mut self, network: Network) -> Self {
/// Checks that the given network matches the one loaded from persistence.
pub fn check_network(mut self, network: Network) -> Self {
self.check_network = Some(network);
self
}

/// Check for a `genesis_hash`.
pub fn genesis_hash(mut self, genesis_hash: BlockHash) -> Self {
/// Checks that the given `genesis_hash` matches the one loaded from persistence.
pub fn check_genesis_hash(mut self, genesis_hash: BlockHash) -> Self {
self.check_genesis_hash = Some(genesis_hash);
self
}
Expand Down
20 changes: 4 additions & 16 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
{
let mut db = open_db(&file_path).context("failed to recover db")?;
let _ = Wallet::load()
.network(Network::Testnet)
.check_network(Network::Testnet)
.load_wallet(&mut db)?
.expect("wallet must exist");
}
Expand All @@ -146,7 +146,7 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
let wallet = Wallet::load()
.descriptor(KeychainKind::External, Some(external_desc))
.descriptor(KeychainKind::Internal, Some(internal_desc))
.network(Network::Testnet)
.check_network(Network::Testnet)
.load_wallet(&mut db)?
.expect("wallet must exist");

Expand Down Expand Up @@ -218,7 +218,7 @@ fn wallet_load_checks() -> anyhow::Result<()> {

assert_matches!(
Wallet::load()
.network(Network::Regtest)
.check_network(Network::Regtest)
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Network {
Expand All @@ -228,21 +228,9 @@ fn wallet_load_checks() -> anyhow::Result<()> {
))),
"unexpected network check result: Regtest (check) is not Testnet (loaded)",
);
assert_matches!(
Wallet::load()
.network(Network::Bitcoin)
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Network {
loaded: Network::Testnet,
expected: Network::Bitcoin,
}
))),
"unexpected network check result: Bitcoin (check) is not Testnet (loaded)",
);
let mainnet_hash = BlockHash::from_byte_array(ChainHash::BITCOIN.to_bytes());
assert_matches!(
Wallet::load().genesis_hash(mainnet_hash).load_wallet(&mut open_db(&file_path)?),
Wallet::load().check_genesis_hash(mainnet_hash).load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(LoadMismatch::Genesis { .. }))),
"unexpected genesis hash check result: mainnet hash (check) is not testnet hash (loaded)",
);
Expand Down
2 changes: 1 addition & 1 deletion example-crates/wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ fn main() -> Result<(), anyhow::Error> {
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;

let wallet_opt = Wallet::load()
.network(NETWORK)
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
.extract_keys()
.check_network(NETWORK)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Some(wallet) => wallet,
Expand Down
2 changes: 1 addition & 1 deletion example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ async fn main() -> Result<(), anyhow::Error> {
let mut conn = Connection::open(DB_PATH)?;

let wallet_opt = Wallet::load()
.network(NETWORK)
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
.extract_keys()
.check_network(NETWORK)
.load_wallet(&mut conn)?;
let mut wallet = match wallet_opt {
Some(wallet) => wallet,
Expand Down
2 changes: 1 addition & 1 deletion example-crates/wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ fn main() -> Result<(), anyhow::Error> {
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), DB_PATH)?;

let wallet_opt = Wallet::load()
.network(NETWORK)
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
.extract_keys()
.check_network(NETWORK)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Some(wallet) => wallet,
Expand Down
2 changes: 1 addition & 1 deletion example-crates/wallet_rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ fn main() -> anyhow::Result<()> {
let mut db =
Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), args.db_path)?;
let wallet_opt = Wallet::load()
.network(args.network)
.descriptor(KeychainKind::External, Some(args.descriptor.clone()))
.descriptor(KeychainKind::Internal, Some(args.change_descriptor.clone()))
.extract_keys()
.check_network(args.network)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Some(wallet) => wallet,
Expand Down

0 comments on commit 2391b76

Please sign in to comment.