Commit Graph

150 Commits

Author SHA1 Message Date
Tobin C. Harding 242c640603 Remove redundant field names
Clippy emits:

  warning: redundant field names in struct initialization

As suggested, remove redundant field names in struct initialization.
2022-06-07 14:26:57 +10:00
Tobin C. Harding 0f8f4c5609 Collapse if statements
Clippy emits:

  warning: this `if` statement can be collapsed

As suggested, collapse the if statements into a single statement, with
no loss of clarity.
2022-06-07 14:24:44 +10:00
Andrew Poelstra adf3958127
Merge rust-bitcoin/rust-bitcoin#1011: Add non_exhaustive compiler directive to `AddressType`
43b684bbe6 Add non_exhaustive compiler directive to AddressType (Tobin C. Harding)

Pull request description:

  Add non_exhaustive compiler directive to AddressType

  Currently adding variants to enums is a breaking change. In an effort to
  reduce the upgrade burden on users we can use the `non_exhaustive`
  compiler directive so that adding a new variant does not cause
  downstream code to break.

  Add `non_exhaustive` to the `AddressType` since it may be extended in
  the future.

ACKs for top commit:
  sanket1729:
    ACK 43b684bbe6
  Kixunil:
    ACK 43b684bbe6
  apoelstra:
    ACK 43b684bbe6

Tree-SHA512: 2b2a15fb501d23058acca94318776ffcccedf463d43d07afa290fba46a7bd58b3a730f6e1f25605ef399afcfdb5de4c7ad67eaa0adff0ba39b0096cbcec10f57
2022-06-02 14:13:52 +00:00
Tobin C. Harding 43b684bbe6 Add non_exhaustive compiler directive to AddressType
Currently adding variants to enums is a breaking change. In an effort to
reduce the upgrade burden on users we can use the `non_exhaustive`
compiler directive so that adding a new variant does not cause
downstream code to break.

Add `non_exhaustive` to the `AddressType` since it may be extended in
the future.
2022-06-02 12:43:25 +10:00
sanket1729 165cae959a
Merge rust-bitcoin/rust-bitcoin#1003: Improve error `Display` implementations
57dd6739c3 Do not print error when displaying for std builds (Tobin C. Harding)
b80cfeed85 Bind to error_kind instead of e (Tobin C. Harding)
241ec72497 Bind to b instead of e (Tobin C. Harding)
01f481bf5c Bind to s instead of e (Tobin C. Harding)
5c6d369289 network: Remove unused error variants (Tobin C. Harding)
e67e97bb37 Put From impl below std::error::Error impl (Tobin C. Harding)
6ca98e5275 Remove error TODO (Tobin C. Harding)

Pull request description:

  As part of the ongoing error improvement work and as a direct result of [this comment](https://github.com/rust-bitcoin/rust-bitcoin/pull/987#issuecomment-1135563287) improve the `Display` implementations of all our error types so as to not repeat the source error when printing.

  The first 5 patches are trivial clean ups around the errors. Patch 6 is the real work.

  EDIT: ~CC @Kixunil, have I got the right idea here bro?~ Patch 6 now includes a macro as suggested.

ACKs for top commit:
  Kixunil:
    ACK 57dd6739c3
  apoelstra:
    ACK 57dd6739c3
  sanket1729:
    ACK 57dd6739c3. Did not check if we covered all cases. We need to remember to use `write_err!` instead of `write!` in future.

Tree-SHA512: 1ed26b0cc5f9a0f71684c431cbb9f94404c116c9136be696434c56a2f56fd93cb5406b0955edbd0dc6f8612e77345c93fa70a70650118968cc58e680333a41de
2022-06-01 15:17:24 -07:00
sanket1729 911a9ec579
Merge rust-bitcoin/rust-bitcoin#1018: Add more documentation to some core types
1875c912c3 Extend docstring for more types (Dawid Ciężarkiewicz)
325ea8fb7d Add "Relevant BIPs` to `Address` (Dawid Ciężarkiewicz)
7c2ca3d20b Add `BlockHeader` Bitcoin Core reference link (Dawid Ciężarkiewicz)
f4922f6fe7 Update `BlockHeader::version` documentation (Dawid Ciężarkiewicz)

Pull request description:

  This is meant to make it more educational, and handy even for experienced developers.

  A first step to make https://docs.rs/bitcoin (or `cargo doc --open`) a go-to place for
  convenient Bitcoin documentation.

ACKs for top commit:
  tcharding:
    tACK 1875c912c3
  apoelstra:
    ACK 1875c912c3
  sanket1729:
    utACK 1875c912c3. Thanks for doing this.

Tree-SHA512: 8457e120f9979bfd95e55e8b18faf6131610aa2241f8e5fc4630fe61dc7e16ddfc35fb6eff46339804016db7b176465943cc0c02d84dcf478ed55da9f5e06fc5
2022-06-01 15:06:35 -07:00
Andrew Poelstra 57eaf13c86
Merge rust-bitcoin/rust-bitcoin#1021: Enforce segwit v0 script validity when creating address.
6c10d77ecb Address::from_script() - Check witness v0 program lengths. (Noah)

Pull request description:

  Adds a check in `Address::from_script()` that checks if segwit v0 scripts have a valid length.

  Fix: #995

ACKs for top commit:
  tcharding:
    ACK 6c10d77ecb
  sanket1729:
    ACK 6c10d77ecb. Left a comment can be addressed in separate PR.
  apoelstra:
    ACK 6c10d77ecb

Tree-SHA512: 32aebb13477958b1455c688f668aaa3d3af4db0a7936b9549bcd1d03bd0e16635b8471549d96f1e8d408d6501e8fb515df2eb86b17a08c3152774a5be78ae8b1
2022-06-01 16:24:18 +00:00
Andrew Poelstra 50489c8d5e
Merge rust-bitcoin/rust-bitcoin#1026: Add non_exhaustive to all error enums
99f565f932 Add non_exhaustive to all error enums (Tobin C. Harding)

Pull request description:

  Adding an error variant to a public enum is an API breaking change, this means making, what could be, small refactorings or improvements harder. If we use `non_exhaustive` for error types then we mitigate this cost.

  There is a tradeoff however, downstream users who explicitly match on our public error types must include a wildcard pattern.

ACKs for top commit:
  apoelstra:
    ACK 99f565f932
  Kixunil:
    ACK 99f565f932

Tree-SHA512: ff329f87d52b3fbe24654f32e4062ddae73173cba5a13d511591158e68ee278e9bdc0a70a3e0b42d6606b369255923f9c46d8b3d1b2ff75f8461a82567df80cd
2022-06-01 16:22:44 +00:00
Andrew Poelstra 95548afccf
Merge rust-bitcoin/rust-bitcoin#798: Audit conversion methods
5fbb211085 Use fn name to_ instead of as_ (Tobin Harding)
8ffa32315d Use fn name to_ instead of into_ (Tobin Harding)
6874ce91e2 Remove as_inner (Tobin C. Harding)

Pull request description:

  Rust has naming conventions surrounding conversion functions

  We have a handful of methods that are not following convention. This PR is done as three patches, separated by incorrect function name (`into_` or `as_`) and by whether or not the original method needs deprecating. Can be squashed if folks prefer.

  From the docs: https://rust-lang.github.io/api-guidelines/naming.html

  <h2><a class="header" href="https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv" id="ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv">Ad-hoc conversions follow <code>as_</code>, <code>to_</code>, <code>into_</code> conventions (C-CONV)</a></h2>
  <p>Conversions should be provided as methods, with names prefixed as follows:</p>

  Prefix | Cost | Ownership
  -- | -- | --
  as_ | Free | borrowed -> borrowed
  to_ | Expensive | borrowed -> borrowed
  | | | borrowed -> owned (non-Copy types)
  | | | owned -> owned (Copy types)
  into_ | Variable | owned -> owned (non-Copy types)

  EDIT: I did actually audit all uses of `to_` when I first did this, I did this by grepping for `fn to_` and checking the output against the table.

ACKs for top commit:
  apoelstra:
    ACK 5fbb211085
  Kixunil:
    ACK 5fbb211085

Tree-SHA512: f750b2d1a10bc1d4bb030d8528a582701cc3d615aa8a8ab391324dae639544bb3629a19b372784e1e274a8ddcc613c621c7aae21a3ea54fde356a6aa5e611ac0
2022-06-01 16:19:36 +00:00
Noah 6c10d77ecb Address::from_script() - Check witness v0 program lengths. 2022-05-31 18:13:52 +10:00
Tobin C. Harding 99f565f932 Add non_exhaustive to all error enums
Adding an error variant to a public enum is an API breaking change, this
means making what could be small refactorings or improvements harder. If
we use `non_exhaustive` for error types then we mitigate this cost.
There is a tradeoff however, downstream users who explicitly match on
our public error types must include a wildcard pattern.
2022-05-31 14:29:50 +10:00
Tobin Harding 8ffa32315d Use fn name to_ instead of into_
Rust convention is to use `to_` for conversion methods that convert from
an owned type to an owned `Copy` type. `into_` is for owned to owned
non-`Copy` types.

Re-name and deprecate conversion methods that use `into_` for `Copy`
types to use `to_`.
2022-05-31 10:07:24 +10:00
Tobin C. Harding 6874ce91e2 Remove as_inner
`self` and the referenced type returned by `as_inner` are both `Copy`
types. There is no need to provide an reference getter method to a
`Copy` type since implementing `Copy` implies that copying is cheap.
2022-05-31 10:07:24 +10:00
Dawid Ciężarkiewicz 325ea8fb7d Add "Relevant BIPs` to `Address` 2022-05-27 20:34:36 -07:00
Tobin C. Harding 57dd6739c3 Do not print error when displaying for std builds
We implement `source` for all our error types. This means that we should
not display the source error explicitly because users can call `source`
to get the source error.

However, `std::Error::source()` is only available for "std" builds, so
that we do not loose the error source information in "no-std" builds add
a macro that conditionally adds the source onto the error message.
2022-05-27 08:38:52 +10:00
Tobin C. Harding fcd0f4deac Use struct field init shorthand
Clippy emits:

  warning: redundant field names in struct initialization

As suggested use struct field init shorthand.
2022-05-25 13:15:20 +10:00
Andrew Poelstra 0e82376bf8
Merge rust-bitcoin/rust-bitcoin#987: Implement `std::error::Error` for the new MSRV
97a5bb1439 Implement std::error::source codebase wide (Tobin C. Harding)
0a9191b429 Add parenthesis around left hand side of companion (Tobin C. Harding)
7cf8af2f86 Put Error impl block below Display (Tobin C. Harding)
2384712364 Re-order Display match arms (Tobin C. Harding)

Pull request description:

  Now that we have MSRV of 1.41.1 we should use `source` instead of `cause`. Audit the whole codebase and implement `source` for _every_ error type we have.

  The first three patches are preparatory cleanup, patch 3 is particularly shameful (adds parenthesis to make my editor work).

  CC @Kixunil because he is championing the error stuff.

ACKs for top commit:
  apoelstra:
    ACK 97a5bb1439

Tree-SHA512: 46313a28929445f32e01e30ca3b0246b30bc9d5e43db5754d4b441e9c30d3e427efaf247100eb6b452f98beec5a4fcde1daba7943a772114aa34f78ab52cbc60
2022-05-21 14:08:52 +00:00
sanket1729 48466bdf93
Merge rust-bitcoin/rust-bitcoin#978: Make Address::get_payload_bytes public
7ca30b6aa8 Move Address::payload_as_bytes to Payload::as_bytes (Fredrik Meringdal)
525ea00e0f Make Address::get_payload_bytes public (Fredrik Meringdal)

Pull request description:

  Hi, thanks for the amazing work on this crate.

  I am trying to upgrade from v0.27 to v0.28, but unable to do so because the `Address::get_payload_bytes` was made private. My use-case is that I have a script hash address and an `Address` and need to compare the two, and in order to do so I need access to the payload bytes of `Address`.
  I hope you will consider making this function public again 🙏

ACKs for top commit:
  apoelstra:
    ACK 7ca30b6
  tcharding:
    ACK 7ca30b6aa8
  sanket1729:
    ACK 7ca30b6aa8. Sorry for the delay and congratz on your first time contribution

Tree-SHA512: 02af4565853d93506751ed7cb004f52cb5d8c7936067e06b3e237b448ccdf5716470448eeccbe211958e095b66bb37c7027800c0470c6988dc18d8bd5b48f459
2022-05-19 18:39:14 -07:00
Tobin C. Harding 97a5bb1439 Implement std::error::source codebase wide
Audit ever error type we have and implement `source` for each.
2022-05-19 16:35:11 +10:00
Tobin C. Harding dca0d67771 Fix in preparation for next edition
Use cargo to upgrade from edition 2015 to edition 2018.

 cargo fix --edition

No manual changes made. The result of the command above is just to fix
all the use statements (add `crate::`) and fix the fully qualified path
formats i.e., `::Foo` -> `crate::Foo`.
2022-05-11 10:16:17 +10:00
Fredrik Meringdal 7ca30b6aa8 Move Address::payload_as_bytes to Payload::as_bytes 2022-05-02 12:32:08 +02:00
Fredrik Meringdal 525ea00e0f Make Address::get_payload_bytes public 2022-04-29 11:54:53 +02:00
Andrew Poelstra 0d5565e131
Merge rust-bitcoin/rust-bitcoin#839: feat: Add Address.is_related_to_*_key()
51fef76129 feat: Add Address.is_related_to_pubkey() (Andrew Ahlers)

Pull request description:

  ## Motivation

  This is addressing the second half of this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/684#issuecomment-1012136845

  > but would accept a PR (or two PRs) that returns Result<bool, UnsupportedAddress> and a method to check if a PublicKey is associated with an address.

  (The first half was addressed [here](https://github.com/rust-bitcoin/rust-bitcoin/pull/819))

  These changes will help build out and improve message signature verification. We don't necessarily need to add it to this crate but it allows for easy verification with something such as:
  1. recovering a pubkey
  2. checking if that pubkey relates to the given address

  ## Possible Improvements

  - There is likely a better name than `is_related_to_secp256k1_key()`
  - This could drop the `secp256k1` part of the name and take in a Pubkey enum that also supports Schnorr pubkeys and then this could be used for taproot addresses as well. This felt like a much larger change that will likely get turned down. Verifying taproot is simple enough and if absolutely desired, similar functions can be added for schnorr keys (tweaked and untweaked)

ACKs for top commit:
  Kixunil:
    ACK 51fef76129 for merging after TR
  apoelstra:
    ACK 51fef76129

Tree-SHA512: c9ab8c0f101fb4c647713e7f500656617025d8741676e8eb8a3132009dde9937d50cf9ac3d8055feb14452324a292397e46639cbaca71cac77af4b06dc42d09d
2022-03-28 17:29:48 +00:00
Tobin Harding 71cf00a314 Use less vertical lines
In this library we specifically do not use rustfmt and tend to favour
terse statements that do not use extra lines unnecessarily. In order to
help new devs understand the style modify code that seems to use an
unnecessary number of lines.

None of these changes should reduce the readability of the code.
2022-03-14 13:52:13 +11:00
Tobin Harding bf4f5638e0 Refactor whitespace
Do various whitespace refactorings, of note:

- Use space around equals e.g., 'since = "blah"'
- Put return/break/continue on separate line

Whitespace only, no logic changes.
2022-03-14 13:51:50 +11:00
Andrew Ahlers 51fef76129 feat: Add Address.is_related_to_pubkey() 2022-03-06 20:30:22 +01:00
Tobin Harding 151173821b Use fn name to_ instead of into_
Rust convention is to use `to_` for conversion methods that convert from
an owned type to an owned `Copy` type. `into_` is for owned to owned
non-`Copy` types.

Re-name conversion methods that use `into_` for `Copy` types to use
`to_`, no need to deprecate these ones because they are unreleased.
2022-01-19 14:59:18 +11:00
Dr Maxim Orlovsky b9170162d5 Re-org keys and ecdsa mods - pt.1
This commit tries to achieve separation of signature- and key-related types, previously mixed in a single ECDSA module.

Rationale: bitcoin key types are not specific for signature algorithm.

This is achieved through
- Remove key mod with its content moved to ecdsa mod
- Re-export keys under key module in util mod - to make git generate diff for the rename of ecdsa mod in the next commit correctly.
2022-01-14 01:35:48 +01:00
sanket1729 2178c7367c Update to secp256k1 0.21.2 2022-01-07 04:45:40 +05:30
Tobin Harding 533120899e Put rustdocs above attributes
Rust idiomatic style is to put the rustdoc _above_ any attributes on
types, functions, etc.

Audit the codebase and move comments/attributes to the correct place.
Add a trailing full stop at times to neaten things up a little extra.
2022-01-06 13:04:47 +11:00
Martin Habovstiak 779d4110c6 Fixed a bunch of clippy lints, added clippy.toml
This is the initial step towards using and maybe enforcing clippy.
It does not fix all lints as some are not applicable. They may be
explicitly ignored later.
2021-12-21 22:50:13 +01:00
Marko Bencun 506e03fa4d
util/address: use hash functions of PublicKey/Script
Simpler code, less duplication.
2021-12-12 13:11:15 +01:00
Marko Bencun f826316c25
util/address: avoid .expect/panic 2021-12-12 13:11:15 +01:00
Marko Bencun ad83f6ae00
util/address: make address encoding more modular
This allow library clients to plug their own encoding parameters in a
backwards compatible manner.
2021-12-12 13:10:48 +01:00
Tobin Harding a6d3514f2b Return parity when doing tap_tweak
Currently we calculate the parity during `tap_tweak` but do not return
it, this means others must re-do work done inside `tap_tweak` in order
to calculate the parity. We can just return the parity along with the
tweaked key.
2021-12-10 11:45:58 +11:00
Noah 5b21a9cb1f Use TapTweakHash method for computing tweak 2021-11-25 09:45:27 +11:00
Dr Maxim Orlovsky d614b6c759
Merge rust-bitcoin/rust-bitcoin#704: util/address: Improve docs
822c99222d Improve constructor rustdocs for Address (Tobin Harding)
804a38cb67 Improve documentation of `WitnessVersion` (Tobin Harding)
eb8278fd2e util/address: Improve docs (Tobin Harding)

Pull request description:

  Improve documentation of the `address` module by doing:

  - Add full stops to all sentences
  - Use code ticks even inside links e.g., [`WitnessVersion`]
  - Use 100 character line length
  - Do grammar fixes
  - Use comment sections (e.g. `# Returns`)
  - Use 3rd person for function comments e.g. 'Converts foo to bar' instead of 'Convert foo to bar'
  - Use ticks for scriptPubkey

  This patch does a single file because a bunch of these changes pick an
  arbitrary stlye, if we can bikeshed on this PR then future PRs should be
  able to progress more quickly. I'll take lack of comment on any of the
  above as approval and I'll attempt to be uniform when doing the rest of
  the codebase. I plan on just chipping away at this, I can only do so
  much docs work in a day without getting bored of it :)

  Notes:

  - I didn't touch 'segwit' vs 'SegWit', seems both are widely used.
  - Using ticks inside links may be an overkill but seems more correct?
  - I'm not totally sure where the line is in the Rust ecosystem between
    readability in an editor and rendering as HTML, open to input on this.

ACKs for top commit:
  Kixunil:
    ACK 822c99222d
  dr-orlovsky:
    ACK 822c99222d

Tree-SHA512: bfbaeec74803dd0704ed3e39b9a4966db34dbb3d7ea850ed6230abf220b877687ac1479f4940b7bf39d7e8172cd62c36b232bfaa8186a92cc58b3d7e642674f6
2021-11-23 18:00:55 +01:00
sanket1729 e4774e74eb fixups to taptweaking code 2021-11-21 11:53:55 -08:00
Tobin Harding 822c99222d Improve constructor rustdocs for Address
Improve the rustdocs for the various `Address` constructors by putting
the brief description on a separate line with further description in its
own paragraph. This is the layout best practice for function documentation
using rustdocs.

Also, favour 'creates' over 'constructs' because it is more common in
the docs of this struct.
2021-11-19 09:42:19 +11:00
Tobin Harding 804a38cb67 Improve documentation of `WitnessVersion`
Attempt to improve the rustdocs for `WitnessVersion` in line with
review comments from a previous patch.
2021-11-19 09:35:14 +11:00
Tobin Harding eb8278fd2e util/address: Improve docs
Improve documentation of the `address` module by doing:

- Add full stops to all sentences
- Use code ticks even inside links e.g., [`WitnessVersion`]
- Use 100 character line length
- Do grammar fixes
- Use comment sections (e.g. `# Returns`)
- Use 3rd person for function comments e.g. 'Converts foo to bar' instead of 'Convert foo to bar'
- Use ticks for scriptPubkey

This patch does a single file because a bunch of these changes pick an
arbitrary stlye, if we can bikeshed on this PR then future PRs should be
able to progress more quickly. I'll take lack of comment on any of the
above as approval and I'll attempt to be uniform when doing the rest of
the codebase. I plan on just chipping away at this, I can only do so
much docs work in a day without getting bored of it :)

Notes:

- I didn't touch 'segwit' vs 'SegWit', seems both are widely used.
- Using ticks inside links may be an overkill but seems more correct?
- I'm not totally sure where the line is in the Rust ecosystem between
  readability in an editor and rendering as HTML, open to input on this.
2021-11-18 09:43:08 +11:00
sanket1729 e66a94fa40
Merge rust-bitcoin/rust-bitcoin#683: tests: improve coverage for P2tr and AddressType
0d463ec19e tests: improve coverage for P2tr and AddressType (Leonardo Comandini)

Pull request description:

  The new AddressType test shows addresses that are valid but have
  no type. If in the future some of those get a type or become
  invalid (either voluntarily or due to a regression), this will
  highlight it.

ACKs for top commit:
  dr-orlovsky:
    utACK 0d463ec19e
  sanket1729:
    ACK 0d463ec19e

Tree-SHA512: 9e062a1807173638cb62a61a2e8ea5be8324449a8944c356073e8bd9f53941dea369c65a35dfa0019bd8323eaa5dd26a9907c1823522fef9a524e919728973a6
2021-11-16 08:49:13 -08:00
Martin Habovštiak ab97d2db1a
Merge pull request #689 from tcharding/module-rustdocs
Clean up module level rustdocs
2021-11-16 13:21:20 +01:00
Leonardo Comandini 0d463ec19e
tests: improve coverage for P2tr and AddressType
The new AddressType test shows addresses that are valid but have
no type. If in the future some of those get a type or become
invalid (either voluntarily or due to a regression), this will
highlight it.
2021-11-15 21:36:13 +01:00
Marko Bencun 8b1dbf5c9f
util/address: remove unused generic type 2021-11-14 13:10:40 +01:00
Noah Lanson 803b5fed8a P2TR address from untweaked public key
Ambiguous TweakedPublicKey and UntweakedPublicKey type aliases and methods to convert

Use structs for Untweaked and Tweaked key type

swap dangerous api to work on tweaked keys

remove unecessary allocations and rename methods

Use type alias for UntweakedPublicKey

TweakedPublicKey::new(...) method added

minor naming and doc changes
2021-11-11 14:36:56 +11:00
Tobin Harding 3f5caa501f Clean up module level rustdocs
Docs can always do with a bit of love.

Clean up the module level (`//!`) rustdocs for all public modules.

I claim uniform is better than any specific method/style. I tried to fit
in with what ever was either most sane of most prevalent, therefore
attaining uniformity without unnecessary code churn (one exception being
the changes to headings described below).

Notes:

* Headings - use heading as a regular sentence for all modules e.g.,

```
//! Bitcoin network messages.
```

as opposed to
```
//! # Bitcoin Network Messages
```

It was not clear which style to use so I picked a 'random' mature
project and copied their style.

* Added 'This module' in _most_ places as the start of the module
description, however I was not religious about this one.

* Fixed line length if necessary since most of our code seems to follow
short (80 char) line lengths for comments anyways.

* Added periods and fixed obvious (and sometimes not so obvious)
grammatically errors.

* Added a trailing `//!` to every block since this was almost universal
already. I don't really like this one but I'm guessing it is Andrew's
preferred style since its on the copyright notices as well.
2021-11-06 10:59:53 +11:00
Dr Maxim Orlovsky c1991d748f
Improving error information for address parser 2021-09-21 12:45:17 +02:00
Clark Moody eeeb722155
Bump bech32 to 0.8.0 and use BIP-0350 Bech32m checksum
Replace BIP-0173 test vectors with those in BIP-0350.
2021-09-21 12:45:17 +02:00
Dr Maxim Orlovsky 5573a546ca
Taproot P2TR address 2021-09-21 12:45:17 +02:00