Skip to content

Conversation

luisschwab
Copy link
Member

@luisschwab luisschwab commented Jun 1, 2025

Description

Closes #22.
Closes #94.

This PR replaces bitcoin::Network for bitcoin::NetworkKind where it applies.

I also took the liberty of fixing up and adding comments on the files I touched.

Changelog

  • wallet_name_from_descriptor() takes NetworkKind instead of Network.
  • DescriptorToExtract takes NetworkKind instead of Network.
  • impl IntoWalletDescriptor for <T> takes Network instead NetworkKind.
  • DescriptorTemplate::build() takes NetworkKind instead of Network.
  • ExtendedKey::into_xprv() takes NetworkKind instead of Network.
  • ExtendedKey::into_xpub() takes NetworkKind instead of Network.
  • DerivableKey::into_extended_key() examples updated to use NetworkKind instead of Network.
  • any_network() renamed to any_network_kind().
  • mainnet_network() renamed to mainnet_network_kind().
  • test_networks() renamed to test_network_kind().
  • merge_networks() renamed to merge_network_kinds().
  • ValidNetworks type alias renamed to ValidNetworkKinds.
  • GeneratedKey::new() takes ValidNetworkKinds instead of ValidNetworks.
  • DescriptorKey::from_public() takes ValidNetworkKinds instead of ValidNetworks.
  • DescriptorKey::from_secret() takes ValidNetworkKinds instead of ValidNetworks.
  • KeyError::InvalidNetwork renamed to KeyError::InvalidNetworkKind.
  • DescriptorKey::override_valid_networks() renamed to override_valid_network_kinds().
  • Network validation logic updated to use NetworkKind instead of Network.
  • Test network validation now uses NetworkKind::Test instead of Network::{Regtest, Signet, Testnet, Testnet4}.
  • Examples and comments updated to use NetworkKind instead of Network.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo +nightly fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@coveralls
Copy link

coveralls commented Jun 1, 2025

Pull Request Test Coverage Report for Build 18329864246

Details

  • 372 of 403 (92.31%) changed or added relevant lines in 9 files are covered.
  • 4 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.2%) to 84.995%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/keys/bip39.rs 13 14 92.86%
src/descriptor/dsl.rs 38 40 95.0%
src/wallet/mod.rs 10 13 76.92%
src/descriptor/template.rs 119 123 96.75%
src/descriptor/policy.rs 15 21 71.43%
src/descriptor/mod.rs 48 55 87.27%
src/keys/mod.rs 127 135 94.07%
Files with Coverage Reduction New Missed Lines %
src/descriptor/template.rs 4 97.46%
Totals Coverage Status
Change from base Build 18318022693: 0.2%
Covered Lines: 7001
Relevant Lines: 8237

💛 - Coveralls

@luisschwab luisschwab force-pushed the feat/networkkind branch 2 times, most recently from 22d27a6 to e6e6266 Compare June 1, 2025 17:41
@luisschwab luisschwab self-assigned this Jun 1, 2025
@luisschwab luisschwab added the api A breaking API change label Jun 1, 2025
@luisschwab luisschwab moved this to Needs Review in BDK Wallet Jun 1, 2025
@luisschwab luisschwab modified the milestones: Wallet 3.1.0, Wallet 3.0.0 Jun 1, 2025
@ValuedMammal
Copy link
Collaborator

ValuedMammal commented Sep 20, 2025

Concept ACK. To consider this work complete I think we should also strive for a fix to #22. Edit: This is a proposed solution 277ae25. Also I think GeneratedKey should hold the NetworkKind internally (instead of ValidNetworkKinds).

@luisschwab luisschwab added the bug Something isn't working label Sep 24, 2025
@ValuedMammal
Copy link
Collaborator

You certainly did take some liberties with the code, definitely an improvement overall. In the future it would be nice to make a separate patch for things like documentation and code comments.

@ValuedMammal
Copy link
Collaborator

ACK 3ee3ae2

@ValuedMammal
Copy link
Collaborator

Will need a rebase. Sorry for the inconvenience.

Comment on lines +1198 to +1202
// As expected, this does not compile due to invalid context:
// ```
// let desc_key: DescriptorKey<Segwitv0> = (xprv, path.clone()).into_descriptor_key().unwrap();
// let (desc, _key_map, _valid_network_kinds) = descriptor!(pkh(desc_key)).unwrap();
// ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question/nit: could we have this as doctest instead and use compile_fail ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a single doc block look odd among all tests?

.unwrap();
check(
P2Pkh(prvkey).build(Network::Bitcoin),
P2Pkh(prvkey).build(NetworkKind::Test),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: any reason to change this from mainnet to test ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we agreed to use NetworkKind::Test on all tests.

);
check(
Bip44Public(pubkey, fingerprint, KeychainKind::Internal).build(Network::Bitcoin),
Bip44Public(pubkey, fingerprint, KeychainKind::Internal).build(NetworkKind::Test),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: same here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we agreed to use NetworkKind::Test on all tests.

@thunderbiscuit
Copy link
Member

I have just a quick feedback that might help you in further PRs @luisschwab. Last week I saw your message on Discord looking for review on this, and I decided to take a look. In the end the diffs are really big because it's so much docs changes all mixed up with code changes, and I didn't have enough time before my day was over.

My advice for further work would be to separate docs and code changes in different commits (I don't actually mind that they are in the same PR, and I know that sometimes we're in a given part of the codebase and it makes sense to fix docs while we're there and thinking about it). I actually like these little ongoing fixes/polish of the docs. But when the diff is big, I review one commit at a time, and that makes it much easier if the two ideas are separate.

@luisschwab
Copy link
Member Author

Yeah, Mammal mentioned that as well. This PR is pretty old, but I'll keep that in mind for the future.

..to require the NetworkKind when generating the key. This fixes
an issue where keys generated for mainnet were incorrectly marked
valid for any network.

test(keys): Add `test_xpriv_generate_options`

fixes bitcoindevkit#22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api A breaking API change bug Something isn't working
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

feat: use new NetworkKind throughout the BDK codebase A bug for generating testnet descriptor?
5 participants