Skip to content
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

[wallet] Improve address API #1402

Merged
merged 4 commits into from Apr 20, 2024

Conversation

ValuedMammal
Copy link
Contributor

@ValuedMammal ValuedMammal commented Apr 11, 2024

Improvements to the wallet address API, see commit messages for details.

Notes to the reviewers

The logic of getting addresses is roughly the same as before when using AddressIndex, following this mapping:

  • New -> reveal_next_address
  • LastUnused -> next_unused_address (assuming this is what LastUnused really means)
  • Peek -> peek_address

Wondering whether it makes sense to expose is_used for Wallet as well.

fixes #898

Changelog notice

Added:

  • Added Wallet methods:
    • peek_address
    • reveal_next_address
    • next_unused_address
    • reveal_addresses_to
    • list_unused_addresses
    • mark_used
    • unmark_used

Removed:

  • Removed Wallet methods:

    • get_address
    • get_internal_address
    • try_get_address
    • try_get_internal_address
  • Removed type AddressIndex

Checklists

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

Feature

  • I've added tests for the new feature
  • I've added docs for the new feature
  • This pull request breaks the existing API
  • I'm linking the issue being fixed by this PR

@ValuedMammal ValuedMammal force-pushed the feat/wallet-address branch 3 times, most recently from 6fdf314 to 662e3ca Compare April 15, 2024 20:16
@ValuedMammal ValuedMammal marked this pull request as ready for review April 15, 2024 20:21
@ValuedMammal ValuedMammal force-pushed the feat/wallet-address branch 3 times, most recently from c75c9b6 to 191314a Compare April 16, 2024 17:24
@evanlinjin
Copy link
Member

If peek_address and get_address does the same thing, can we get rid of get_address?

@ValuedMammal
Copy link
Contributor Author

If peek_address and get_address does the same thing, can we get rid of get_address?

Yes. I also feel that replacing get_address with peek_address(KeychainKind::External, 0) is slightly less convenient than before. get_address appears quite often in wallet tests and (although tests shouldn't be a driver of API choices) I think should be replaced by something similarly convenient.

How about something like this:

    /// Get the first derived address of the external keychain without revealing it.
    ///
    /// Shorthand for calling [`peek_address`](Self::peek_address) at index 0.
    pub fn first_address(&self) -> AddressInfo {
        self.peek_address(KeychainKind::External, 0)
    }

@evanlinjin
Copy link
Member

@ValuedMammal Do you mean inconvenient as in we need to refactor many of the tests? As you mentioned, I don't think avoiding large test refactors should be a driving factor in API design.

For the first_address method you proposed, is the main motivation for non-wildcard descriptors? I think it makes sense if it was marketed as such.

@ValuedMammal
Copy link
Contributor Author

ValuedMammal commented Apr 18, 2024

Do you mean inconvenient as in we need to refactor many of the tests? As you mentioned, I don't think avoiding large test refactors should be a driving factor in API design.

For the first_address method you proposed, is the main motivation for non-wildcard descriptors? I think it makes sense if it was marketed as such.

"Inconvenient" in the sense that it requires an additional function arg compared to the current get_address(New).

I think any descriptor is capable of producing a first address, wildcard or not. Alternatively, we drop get_address and use peek_address everywhere. Simpler the better. Actually, next_unused_address works just fine here as well!

@ValuedMammal
Copy link
Contributor Author

  • Rebased on 52f3955
  • Finally disposed of get_address

Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

LGTM, just a small addition to docs and I will be happy to ACK/merge!

crates/bdk/src/wallet/mod.rs Show resolved Hide resolved
@ValuedMammal
Copy link
Contributor Author

The last push includes documentation improvements and the fix for map_keychain.

📌 We're using expect when converting an spk to a bitcoin Address under the assumption that the script will be valid. Is this something we can always guarantee? Food for thought I guess.

@LagginTimes
Copy link
Contributor

Everything looks pretty good to me. 👍

Introduce a new API for getting addresses from the Wallet that
reflects a similiar interface as the underlying indexer
`KeychainTxOutIndex` in preparation for removing `AddressIndex` enum.

Before this change, the only way to get an address was via the methods
`try_get{_internal}_address` which required a `&mut` reference to the
wallet, matching on the desired AddressIndex variant. This is too
restrictive since for example peeking or listing unused addresses
shouldn't change the state of the wallet. Hence we provide separate
methods for each use case which makes for a more efficient API.
As this is now made redundant by the newly added
wallet address methods.
Adds test coverage for Wallet methods `reveal_addresses_to`,
`mark_used`, and `unmark_used`
Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

ACK d39b319

@evanlinjin evanlinjin merged commit e0bcca3 into bitcoindevkit:master Apr 20, 2024
12 checks passed
@ValuedMammal ValuedMammal deleted the feat/wallet-address branch April 21, 2024 12:21
@notmandatory notmandatory added the api A breaking API change label Apr 22, 2024
@notmandatory notmandatory added this to the 1.0.0-alpha milestone Apr 22, 2024
@danielabrozzoni danielabrozzoni mentioned this pull request May 2, 2024
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api A breaking API change
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Improve wallet address API
4 participants