Commit Graph

2403 Commits

Author SHA1 Message Date
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
Martin Habovstiak 2e7effc604 Feature `use-serde` renamed to `serde`
Features activating external crates are supposed to have same name as
those crates. However we depend on same feature in other crates so we
need a separate feature. After MSRV bump it is possible to rename the
crates and features so we can now fix this inconsistency.

Sadly, derive can't see that the crate was renamed so all derives must
be told to use the other one.
2022-05-26 10:10:47 +02:00
Tobin C. Harding 553a6813c5 Do not pin transitive ryu dependency
We do not need to pin the `ryu` transitive dependency now that MSRV is
not 1.29.
2022-05-26 14:50:11 +10:00
Tobin C. Harding 88ce8fe923 Match against an optional single trailing colon
Currently we allow multiple trailing colons when matching within the
`check_format_non_negative` macro. We can be more restrictive with no
loss of usability.

Use `$(;)?` instead of `$(;)*` to match against 0 or 1 semi-colons
instead of 0 or more.
2022-05-26 11:26:42 +10:00
Tobin C. Harding b80cfeed85 Bind to error_kind instead of e
To make it explicit that this is not an error bind to the local variable
`error_kind` instead of `e`.
2022-05-26 10:06:18 +10:00
Tobin C. Harding 51c60b8507 Allow no is_empty method for VarInt
Clippy emits:

  warning: struct `VarInt` has a public `len` method, but no `is_empty`
  method

However, `VarInt` has no concept of 'is empty' so add a compiler
directive to allow the lint.
2022-05-26 08:50:48 +10:00
Tobin C. Harding 841f1f5832 Implement Default for TaprootBuilder
Clippy emits:

  warning: you should consider adding a `Default` implementation for
  `TaprootBuilder`

As suggested, implement `Default` or `TaprootBuilder`.
2022-05-26 08:50:48 +10:00
Tobin C. Harding f81d4aa9bd Remove unnecessary call to clone
Clippy emits:

warning: using `clone` on type `secp256k1::XOnlyPublicKey` which
implements the `Copy` trait

As suggested, remove call to `clone`.
2022-05-26 08:50:48 +10:00
Tobin C. Harding 27649ba182 Use copied instead of map to copy
Clippy emits:

  warning: you are using an explicit closure for copying elements

In one instance we have `map` followed by `flatten`, this can be
replaced by the `flat_map` combinator.

As suggested use `copied` combinator.
2022-05-26 08:50:48 +10:00
Tobin C. Harding 62ccc9102c Use iter().flatten().any() instead of if let Some
Clippy emits:

  warning: unnecessary `if let` since only the `Some` variant of the
  iterator element is used

Use combinator chain `iter().flatten().any()` to check for an node with
hidden nodes.
2022-05-26 08:50:43 +10:00
Tobin C. Harding 4b28a1bb97 Remove unneeded return statement
Clippy emits:

  warning: unneeded `return` statement

As suggested, remove the unneeded return statement.
2022-05-25 13:32:42 +10:00
Tobin C. Harding 16cac3cd70 Derive Default for Witness
No need for an explicit `Default` implementation for `Witness`, it can
be derived. Found by Clippy.
2022-05-25 13:31:15 +10:00
Tobin C. Harding c75189841a Remove unnecessary closure
Clippy emits:

  warning: unnecessary closure used to substitute value for
  `Option::None`

As suggested, use `ok_or` removing the unnecessary closure.
2022-05-25 13:30:07 +10:00
Tobin C. Harding dfff85352a Ignore bytes written for sighash_single bug output
Clippy emits:

  error: written amount is not handled

This code is explicitly writing garbage to the writer, no need to handle
the number of bytes written.
2022-05-25 13:25:23 +10:00
Tobin C. Harding 14c72e755b Use contains combinator instead of manual range
Clippy emits:

  warning: manual `RangeInclusive::contains` implementation

As suggested, use `contains` combinator instead of manual range check.
2022-05-25 13:24:15 +10:00
Tobin C. Harding b7d6c3e02c Remove additional reference
Clippy emits:

  warning: this expression creates a reference which is immediately
  dereferenced by the compiler

As suggested, remove the additional reference.
2022-05-25 13:21:25 +10:00
Tobin C. Harding 1940b00132 Implement From instead of Into
Implementing `From` gives us an implementation of `Into` for free so is
therefore superior.

Found by Clippy.
2022-05-25 13:19:17 +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
Tobin C. Harding 641960f037 Use rustfmt::skip
Clippy emits:

  warning: `cfg_attr` is deprecated for rustfmt and got replaced by tool
  attributes

As suggested use `rustfmt::skip`.
2022-05-25 13:13:13 +10:00
Tobin C. Harding 3cd00e5d47 Remove unnecessary whitespace 2022-05-25 13:12:31 +10:00
Tobin C. Harding 241ec72497 Bind to b instead of e
This error variant contains a byte value not an error type, bind to
local variable `b` instead of `e` to make this explicit.
2022-05-25 12:53:33 +10:00
Tobin C. Harding 01f481bf5c Bind to s instead of e
This error variant contains a string not an error type, bind to local
variable `s` instead of `e` to make this explicit.
2022-05-25 12:33:56 +10:00
Tobin C. Harding 5c6d369289 network: Remove unused error variants
Remove unused error variants from `network::Error`.
2022-05-25 12:32:35 +10:00
Tobin C. Harding e67e97bb37 Put From impl below std::error::Error impl
As we do for all the other error types put the `From` impl blocks below
the `std::error::Erro` impl block.

Refactor only, no logic changes.
2022-05-25 12:31:32 +10:00
Tobin C. Harding 6ca98e5275 Remove error TODO
Remove the TODO from comments and raise a GitHub issue to track it.

  https://github.com/rust-bitcoin/rust-bitcoin/issues/1002
2022-05-25 11:59:58 +10:00
Andrew Poelstra 324fa0f7be
Merge rust-bitcoin/rust-bitcoin#968: Refactor address byte swapping
07c75304d2 Refactor address byte swapping (Tobin C. Harding)

Pull request description:

  Refactor address byte swapping

  When encoding a `network::Address` two of the fields are encoded
  big-endian instead of little-endian as is done by `consensus_encode`. In
  order to achieve this we have a helper function `addr_to_be` that swaps
  the bytes. This function is miss-named because it is not converting to a
  specific endian-ness (which implies different behaviour on machines with
  different endian-ness) but is reversing the byte order irrespective of
  the underlying architecture.

  - Remove function `addr_to_be`
  - Inline the endian-ness code when encoding an address
  - Remove TODO and use `to_be_bytes` when encoding port
  - Add a function for reading big-endian bytes `read_be_address`
  - Use `read_be_address` when decoding `Address` and `Addrv2`

  Refactor only, no logic changes. Code path is already covered by
  unit tests.

ACKs for top commit:
  apoelstra:
    ACK 07c75304d2
  Kixunil:
    ACK 07c75304d2

Tree-SHA512: 186bc86512e264a7b306f3bc2e18d1619f3cd84fc54412148cfc2663e8d6e9616ea9e2fe19eafec72d76cc11367a9b39cac2b73210d9e43eb8f453bd253b33de
2022-05-24 17:41:12 +00: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 2b1154cefe
Merge rust-bitcoin/rust-bitcoin#996: Box value encoded in a variant to reduce enum stack space
9906cea14c Box value encoded in a variant to reduce enum stack space (Riccardo Casatta)

Pull request description:

  before

  ```
  print-type-size type: `util::psbt::error::Error`: 120 bytes, alignment: 8 bytes
  print-type-size     discriminant: 1 bytes
  print-type-size     variant `CombineInconsistentKeySources`: 115 bytes
  print-type-size         padding: 3 bytes
  print-type-size         field `.0`: 112 bytes, alignment: 4 bytes
  print-type-size     variant `InvalidKey`: 39 bytes
  print-type-size         padding: 7 bytes
  print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
  ```

  after
  ```
  print-type-size type: `util::psbt::error::Error`: 40 bytes, alignment: 8 bytes
  print-type-size     discriminant: 1 bytes
  print-type-size     variant `InvalidKey`: 39 bytes
  print-type-size         padding: 7 bytes
  print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
  print-type-size     variant `DuplicateKey`: 39 bytes
  print-type-size         padding: 7 bytes
  print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
  ```

  `util::psbt::error::Error` is wrapped also in `consensus::encode::Error` and stack savings are gained there also

ACKs for top commit:
  apoelstra:
    ACK 9906cea14c
  tcharding:
    ACK 9906cea14c
  sanket1729:
    utACK 9906cea14c

Tree-SHA512: e03988fcbc3dd87f83d00dd84ec1c538bc5c63bea97ff4a69a715621f498f57d7fe2a623e351942d9532af40c723e42a9eb6ef48ebf4c62ddf5c0f44e9ea0a07
2022-05-19 19:04:58 -07:00
sanket1729 fcb035fb4f
Merge rust-bitcoin/rust-bitcoin#956: Improve docs in `sighash` and `psbt/mod.rs`
9896f27eae psbt: Improve documentation (Tobin C. Harding)
33a50831ce sighash: Improve documentation (Tobin Harding)

Pull request description:

  Done while working on sighash and PSBT signing. Just the usual docs fixes. Note, does not do the whole `psbt` module just the file mentioned.

ACKs for top commit:
  apoelstra:
    ACK 9896f27eae

Tree-SHA512: 5fbfa258cdb216189922a49a42b7ab9fb78faeee72d82f8cb99a1b3d930d170074013e317b0e7af259a404ac4db93841b4d2b525e933c5e145da71e7522800fd
2022-05-19 18:59:37 -07:00
sanket1729 fa8091866d
Merge rust-bitcoin/rust-bitcoin#980: Remove sha256t_hash_newtype macro
58f94bee9b Remove sha256t_hash_newtype macro (Tobin C. Harding)

Pull request description:

  Since commit `commit 275adc6c335a4326699cfbd444949e1725864ea1` on `bitcoin_hashes` we have the identical implementation of the macro `sha256t1_hash_newtype` in this crate and in `bitcoin_hashes`.

  Remove the `sha256t_hash_newtype` macro from this crate in favour of the one in `bitcoin_hashes`.

ACKs for top commit:
  apoelstra:
    ACK 58f94bee9b
  sanket1729:
    ACK 58f94bee9b

Tree-SHA512: ec08fd25c1cca71a07ea61cb5838ce8962daae7cbb84d8beccc3d0d285439909721edd643292a8f3f6989e1c2c41fda9addfd5cdb063ef53ebc6ef646da79cf3
2022-05-19 18:53:32 -07:00
sanket1729 d73a94a5d9
Merge rust-bitcoin/rust-bitcoin#960: Trival docs fixes
90b4f1cde8 Clear TapTreeIter clippy warning (Tobin C. Harding)
e6084a1af8 Improve documentation around EcdsaSig (Tobin Harding)

Pull request description:

  Do a couple of trivial docs fixes, done during other work.

  - Patch 1 improves docs on the `EcdsaSig` struct
  - Patch 2 clears a clippy warning during docs build - no sure if the solution is the best available though

ACKs for top commit:
  apoelstra:
    re-ACK 90b4f1cde8
  sanket1729:
    ACK 90b4f1cde8

Tree-SHA512: 0647dc2e6550938ccca658a9dddffba7175d5c4eb8cec0e165d3a7fa8f2b1dfb902e795aca77d96a6c31092baf64244fa1d7151a304134d3b1895619a2823338
2022-05-19 18:50:48 -07: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
Riccardo Casatta 9906cea14c
Box value encoded in a variant to reduce enum stack space
before

```
print-type-size type: `util::psbt::error::Error`: 120 bytes, alignment: 8 bytes
print-type-size     discriminant: 1 bytes
print-type-size     variant `CombineInconsistentKeySources`: 115 bytes
print-type-size         padding: 3 bytes
print-type-size         field `.0`: 112 bytes, alignment: 4 bytes
print-type-size     variant `InvalidKey`: 39 bytes
print-type-size         padding: 7 bytes
print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
```

after
```
print-type-size type: `util::psbt::error::Error`: 40 bytes, alignment: 8 bytes
print-type-size     discriminant: 1 bytes
print-type-size     variant `InvalidKey`: 39 bytes
print-type-size         padding: 7 bytes
print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
print-type-size     variant `DuplicateKey`: 39 bytes
print-type-size         padding: 7 bytes
print-type-size         field `.0`: 32 bytes, alignment: 8 bytes
```
2022-05-19 17:05:42 +02: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 0a9191b429 Add parenthesis around left hand side of companion
Parenthesis are not needed around this expression but my editor is going
mad and cannot format the code without them. Since it does not hurt
readability add parenthesis around the expression.
2022-05-19 16:33:49 +10:00
Tobin C. Harding 7cf8af2f86 Put Error impl block below Display
In an effort to be uniform throughout the codebase; put the
`std::error::Error` impl block below the `Display` impl block.
2022-05-19 16:33:49 +10:00
Tobin C. Harding 2384712364 Re-order Display match arms
Put the match arms in the same order as the enum that defines them.
2022-05-19 16:33:49 +10:00
Tobin C. Harding 07c75304d2 Refactor address byte swapping
When encoding a `network::Address` two of the fields are encoded
big-endian instead of little-endian as is done by `consensus_encode`. In
order to achieve this we have a helper function `addr_to_be` that swaps
the bytes. This function is miss-named because it is not converting to a
specific endian-ness (which implies different behaviour on machines with
different endian-ness) but is reversing the byte order irrespective of
the underlying architecture.

- Remove function `addr_to_be`
- Inline the endian-ness code when encoding an address
- Remove TODO and use `to_be_bytes` when encoding port
- Add a function for reading big-endian bytes `read_be_address`
- Use `read_be_address` when decoding `Address` and `Addrv2`

Refactor only, no logic changes. Code path is already covered by
unit tests.
2022-05-19 16:03:03 +10:00
Tobin Harding 8e29f2b493 Add ChainHash type
The Lightning network defines a type called 'chain hash' that is used to
uniquely represent the various Bitcoin networks as a 32 byte hash value.
Chain hash is now being used by the DLC folks, as such it is useful to
have it implemented in rust-bitcoin.

One method of calculating a chain hash is by hashing the genesis block
for the respective network.

Add a `ChainHash` type that can be used to get the unique identifier of
each of the 4 Bitcoin networks we support. Add a method that returns
the chain hash for a network using the double sha256 of the genesis
block. Do so using hard coded consts and add unit
tests (regression/sanity) that show these hard code byte arrays match
the hash of the data we return for the genesis block for the respective
network.

The chain hash for the main Bitcoin network can be verified from LN
docs (BOLT 0), add a link to this document.
2022-05-19 15:07:39 +10:00
Tobin Harding cd8f511fcb blockdata: constants: Use wildcard import in unit tests
Import with wildcard is applicable in unit tests, use it.
2022-05-19 14:20:06 +10:00
Tobin Harding 71bf19621a Use fully qualified path in macro
As we do for the rest of the macros use the fully qualified path to
`fmt` so users of the macro do not have to import it.
2022-05-19 14:14:32 +10:00
Tobin C. Harding 58f94bee9b Remove sha256t_hash_newtype macro
Since commit `commit 275adc6c335a4326699cfbd444949e1725864ea1` on
`bitcoin_hashes` we have the identical implementation of the macro
`sha256t1_hash_newtype` in this crate and in `bitcoin_hashes`.

Remove the `sha256t_hash_newtype` macro from this crate in favour of the
one in `bitcoin_hashes`.
2022-05-19 14:10:57 +10:00
Tobin C. Harding 90b4f1cde8 Clear TapTreeIter clippy warning
Clippy emits warning:

 public documentation for `script_leaves` links to private item `TapTreeIter`

I'm not exactly sure why this is but adding the generic type place
holder clears the warning.
2022-05-19 14:06:08 +10:00
Tobin Harding e6084a1af8 Improve documentation around EcdsaSig
Improve the rustdocs for serialization methods of the `EcdsaSig` type.
2022-05-19 14:05:34 +10:00
Tobin C. Harding 9896f27eae psbt: Improve documentation
Improve documentation in `psbt/mod.rs` by doing:

- Use full sentences (full stops and capitalisation)
- Use 100 line column width
- Use back ticks and links as appropriate
- Use `Errors` section
- Use third person tense to describe functions
2022-05-19 12:47:33 +10:00
Tobin Harding 33a50831ce sighash: Improve documentation
Improve the rustdoc documentation in the `sighash` module by doing:

- Improve grammar
- Use full sentences (full stops and capitalisation)
- Use 100 line column width
- Use back ticks and links as appropriate
- Improve correctness of `SigHashCache::new` function
2022-05-19 12:29:27 +10:00
Andrew Poelstra 50d9394582
Merge rust-bitcoin/rust-bitcoin#983: Edition 2018
9f0c687d89 Enable edition 2018 (Tobin C. Harding)
dca0d67771 Fix in preparation for next edition (Tobin C. Harding)

Pull request description:

  This PR supersedes #635 at the permission of @Kixunil in the thread of that PR.

  Do a minimal set of changes to enable edition 2018.  Patch 1 is  the biggest change set, it is done with `cargo`, no other manual changes are included in patch 1.  It can verified by running `cargo fix --edition` on master and checking the diffs are the same.

  Patch 2 enables 2018 and includes all the manual changes required to get the code to build (with _all_ the feature combinations :)

ACKs for top commit:
  dunxen:
    re-ACK  9f0c687
  apoelstra:
    re-ACK 9f0c687d89
  RCasatta:
    ACK 9f0c687d89,

Tree-SHA512: 7c23554adb4c1dd932af1e80f04397bad0418b4fdae2d0fe243c3f19ba1169686a386bffd38a3c02871c7544b5a7bc8af525b50617a2695726408c091700f081
2022-05-18 19:21:01 +00:00
Matt Corallo 491da3e701
Merge pull request #992 from tcharding/rm-dr
Remove dr-orlovsky from maintainer list
2022-05-12 21:33:50 +00:00
Tobin C. Harding 38c41e4612 Replace base64-compat dependency
Now that we have MSRV 1.41.1 we can use the more modern `base64` instead
of the compat crate. Requires no changes other than changing the
dependency.
2022-05-11 10:33:03 +10:00
Tobin C. Harding 9f0c687d89 Enable edition 2018
Add 'edition = "2018"' to the manifest and do a bunch of manual path
fixups (use statements and fully qualified paths).
2022-05-11 10:16:48 +10:00