Commit Graph

1191 Commits

Author SHA1 Message Date
Andrew Poelstra c0de0f7bde
Merge rust-bitcoin/rust-bitcoin#2120: Improve public re-exports
7d695f6b41 Improve public re-exports (Tobin C. Harding)
33774122e0 Remove public re-exports from private module (Tobin C. Harding)

Pull request description:

  Improve the public exports in two ways:

  1. Inline re-exports into the docs of the module that re-exports them.
  2. Separate public and private use statements

  Recently we discussed a way to separate the public and private import statements to make the code more clear and prevent `rustfmt` joining them all together.

  Separate public exports using a code block and `#[rustfmt::skip]`. Has the nice advantage of reducing the number of `#[doc(inline)]` attributes also.

  1. Modules first, as they are part of the project's structure.
  2. Private imports
  3. Public re-exports (using `rustfmt::skip` to prevent merge)

  Use the format

  ```rust
  mod xyz;
  mod abc;

  use ...;

  pub use {
      ...,
  };
  ```

  This patch introduces changes to the rendered HTML docs.

ACKs for top commit:
  apoelstra:
    ACK 7d695f6b41

Tree-SHA512: dc9121c0fe282e3035d862beadb89e2d5a374a7dab6b1c3147a9b5960f8bc2f5af49892f0f713f55c645c46f53464c32daf390c11d85c75553b3ea7e0efc8246
2023-11-15 13:51:51 +00:00
conduition 6c6c08ca50 add second test case 2023-11-10 17:19:33 +00:00
conduition 0c56131819 fix: FeeRate::checked_mul_by_weight should scale output down by 1000 2023-11-09 19:20:34 +00:00
Matt Corallo add371d263 Remove `core2` dependency entirely 2023-11-09 16:46:44 +00:00
Matt Corallo c95b59327a Explicitly use `std::io::Error` when implementing `std` traits
In the next commit, `std::io::Error` will be replaced with a
different type, and as a result `std::io::Error` must be referred
to explicitly.
2023-11-07 17:35:18 +00:00
Matt Corallo 9e1cd372cb Use `io::Error::get_ref()` over `std::error::Error::source()` 2023-11-07 17:35:18 +00:00
Matt Corallo 3caaadf9bb [IO] Replace the `io::Cursor` re-export with our own `Cursor` 2023-11-07 17:35:17 +00:00
Matt Corallo 141343edb4 [IO] Move to custom `Read` trait mirroring `std::io::Read`
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

Here we take the second big step, replacing
`{std,core2}::io::Read` with our own `bitcoin_io::io::Read`. We
provide a blanket impl for our trait for all `std::io::Read`, if
the `std` feature is enabled, allowing users who use their own
streams or `std` streams to call `rust-bitcoin` methods directly.
2023-11-07 05:51:39 +00:00
Matt Corallo 7395093f94 Stop relying on `Take`'s `by_ref` method 2023-11-07 05:51:37 +00:00
Matt Corallo 2364e1a877 Stop relying on blanket Read impl for all &mut Read 2023-11-07 05:51:36 +00:00
Matt Corallo 6aa7ccf841 [IO] Replace `std::io::Sink` usage with our own trivial impl 2023-11-07 05:51:36 +00:00
Matt Corallo 5f2395ce56 Add missing `?Sized` bounds to `io::Write` parameters
Since we are no longer relying on the blanket `io::Write` impl for
`&mut io::Write`, we should now ensure that we do not require
`Sized` for our `io::Write` bounds, as its unnecessarily
restrictive and can no longer be worked around by simply adding an
`&mut`.
2023-11-07 05:50:51 +00:00
Matt Corallo 2348449d2a Stop relying on `std::io::Write`'s `&mut Write` blanket impl
`std::io::Write` is implemented for all `&mut std::io::Write`. This
makes it easy to have APIs that mix and match owned `Write`s with
mutable references to `Write`s.

However, in the next commit we add our own `Write` trait which we
intend to implement for all `std::io::Write`. Sadly, this is
mutually exclusive with a blanket implementation on our own
`&mut Write`, as that would conflict with an `std::io::Write`
blanket impl.

Thus, in order to use the `Write for all &mut Write` blanket impl
in rust-bitcoin, we'd have to bound all `Write`s by
`std::io::Write`, as we're unable to provide a blanket
`Write for &mut Write` impl.

Here we stop relying on that blanket impl in order to introduce the
new trait in the next commit.
2023-11-07 05:50:47 +00:00
Matt Corallo 5e0209569c Use `io::sink` rather than our custom `EmptyWrite` utility 2023-11-07 05:50:45 +00:00
Matt Corallo 27c7c4e26a Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.

Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.

Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.

Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.

This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-11-07 05:50:42 +00:00
Andrew Poelstra 966b190f23
Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty
12d615d900 Use network when calculating difficulty (Tobin C. Harding)
62af5b54f3 Improve difficulty rustdocs (Tobin C. Harding)

Pull request description:

  The difficulty is a ratio of the max and current targets, since the max is network specific the difficulty calculation is also network specific.

  We already have network specific maximum target constants, use them when calculating the difficulty.

  Patch 1 is a trival docs improvement to `block::Header::difficulty`.

ACKs for top commit:
  Kixunil:
    ACK 12d615d900
  apoelstra:
    ACK 12d615d900

Tree-SHA512: 8b414c975306667309b0918109b3e5e8774496fc4c0f3413709e95ad7499bebf1a017def4c180a2bb5f1750c69bb505d94c738a28525b7ccc8b36e5e42514000
2023-11-06 14:38:00 +00:00
Andrew Poelstra 7b6de6a22b
Merge rust-bitcoin/rust-bitcoin#2170: Remove deprecated since NEXT-RELEASE
01e2233f6c Remove deprecated since NEXT-RELEASE (Tobin C. Harding)

Pull request description:

  Not sure what happened here but our release job didn't catch this? We should have updated this to "since = 0.31.0" before release. Since we only deprecate for one release lets go ahead and remove this.

ACKs for top commit:
  apoelstra:
    ACK 01e2233f6c
  clarkmoody:
    ACK 01e2233f6c
  Kixunil:
    ACK 01e2233f6c

Tree-SHA512: ad362058371e5e8ac7b577c3e32a0c65ce29e5723ff5efbccbcc57684fd61364f3caf7a4c8f0ee8c24bcb7a765bad2539a862860a902e5cec495ed9972379c2d
2023-11-06 14:04:47 +00:00
Andrew Poelstra 9f602a613b
Merge rust-bitcoin/rust-bitcoin#1979: Make `Payload` and `AddressEncoding` private
7f75447c1d Make Payload private and inline functionality (Tobin C. Harding)
b12bf07232 Make the AddressEncoding type private (Tobin C. Harding)

Pull request description:

  The `AddressEncoding` and `Payload` types are implementation details and should never have been public.    Make them private.

  Fix: #1908

ACKs for top commit:
  Kixunil:
    ACK 7f75447c1d
  apoelstra:
    ACK 7f75447c1d

Tree-SHA512: 37083bc759f32e5187126c4f8d39c9c9cb39bd80a92b2479128da39f4db7672fe0be24a58756a387fe944c63efb4ffacc58c1dac071f3314e882ed0f0e9f5a23
2023-11-06 13:56:56 +00:00
Tobin C. Harding 7f75447c1d
Make Payload private and inline functionality
Currently we have functions on `Address` that call through to a public
`Payload` type. The `Payload` type is an implementation detail and
should never have been public. In preparation for modifying the
`AddressInner` and removing `Payload` altogether lets move all the
functionality from `Payload` into `Address` - this is basically just
code moves so it is feasible to review with some confidence.

This is an API breaking change because it makes `Payload` private and
also removes from the pubic `Address` API functions that accept and
return `Payload`. Apart from that the changes can be seen as
refactoring.
2023-11-06 16:09:11 +11:00
Tobin C. Harding b12bf07232
Make the AddressEncoding type private
The `AddressEncoding` type exists solely to assist us in implementing
`Display` on `Address`, it may have been used in the past by alt-coins
back when we had a more tolerant outlook on supporting them. Nowadays
we explicitly do not support alts.
2023-11-06 16:09:07 +11:00
Martin Habovstiak 9282cc4dad Implement standard conversions `Network`->`Params` 2023-11-05 19:58:15 +01:00
Martin Habovstiak 9a8694fae5 Add `params` method to `Network`
Writing `network.params()` is less annoying than `Params::network()`, so
this adds it. Making it return a static could also improve performance.
2023-11-05 19:57:47 +01:00
Tobin C. Harding 2c33744baa
Remove code deprecated since v0.31.0
We only deprecate for a single release.

Remove all code deprecated since `v0.31.0`.
2023-11-04 12:14:54 +11:00
Tobin C. Harding 01e2233f6c
Remove deprecated since NEXT-RELEASE
Not sure what happened here but our release job didn't catch this? We
should have updated this to "since = 0.31.0" before release. Since we
only deprecate for one release lets go ahead and remove this.
2023-11-04 09:52:52 +11:00
Tobin C. Harding 12d615d900
Use network when calculating difficulty
The difficulty is a ratio  of the max and current targets, since the
max is network specific the difficulty calculation is also network
specific.

We already have network specific maximum target constants, use them when
calculating the difficulty.
2023-11-03 12:05:23 +11:00
Tobin C. Harding 62af5b54f3
Improve difficulty rustdocs
Copy a sentence from the `pow::Target::difficulty` function onto the
`block:Header::difficulty` function.
2023-11-03 11:58:02 +11:00
Andrew Poelstra 071208ccdd
Merge rust-bitcoin/rust-bitcoin#2042: Split Prevouts errors out into specific error types
e21ee381bc Split Prevouts errors out into specific error types (Tobin C. Harding)

Pull request description:

  Done as part of the great error clean up.

  Currently we are returning a general `Error` from `Prevouts` functions, this is un-informative, we can do better by returning specific types that indicate the exact error path.

ACKs for top commit:
  Kixunil:
    ACK e21ee381bc
  apoelstra:
    ACK e21ee381bc

Tree-SHA512: 2a4900f9e31584ad2b6faafa17ea98742fff9206ee1bf77ed29624e0c7b05e655b3b6bf3710e2da26b0b2b8bd5eb36fdd81decbb1f55b41f153f0fbcc4a9165e
2023-11-01 14:20:24 +00:00
Andrew Poelstra df28e2f679
Merge rust-bitcoin/rust-bitcoin#2151: Do trivial docs fixes
d6298fe711 Use capital B for Bitcoin in rustdoc (Tobin C. Harding)
bcfabc3556 Fix typo, missing word (Tobin C. Harding)

Pull request description:

  In an effort to make review merge quicker push these two changes up as a separate PR.

  Totally trivial.

ACKs for top commit:
  Kixunil:
    ACK d6298fe711
  apoelstra:
    ACK d6298fe711

Tree-SHA512: 34635ecd87c918f106694a81f50e69dda233000ac616557744466eb58422a2742f35cadbb059f0f81449efd2f61a37ceb71840bf216009914ba2162834e13fc6
2023-10-31 14:30:58 +00:00
Andrew Poelstra 0b1fe094e4
Merge rust-bitcoin/rust-bitcoin#2146: Create uniform build script
fde6479c6a Create uniform build script (yancy)

Pull request description:

  Previously, each unique compiler cfg attribute that appeared in the codebase was hard coded and emitted to stdout at compile time.  This meant keeping the file up to date as different compiler cfg attributes changed.  It's inconsequential to emit a compiler version that's not used, so this change just emits all possibilities to reduce the maintenance burden of the build script.

  Note that there is a bit of diff noise in one of the `build.rs` since I simply did a copy of one to the other to make them uniform.

ACKs for top commit:
  Kixunil:
    ACK fde6479c6a
  tcharding:
    ACK fde6479c6a
  apoelstra:
    ACK fde6479c6a

Tree-SHA512: 401134c168a604a092b72c3980fae6d20adda761273ea47a887cf4c01838536241a45f310cbc2b5941311d533e1d10c848062198af70c0ed619a57973e842840
2023-10-31 14:04:19 +00:00
Tobin C. Harding 7d695f6b41
Improve public re-exports
Improve the public exports in two ways:

1. Inline re-exports into the docs of the module that re-exports them.
2. Separate public and private use statements

Recently we discussed a way to separate the public and private import
statements to make the code more clear and prevent `rustfmt` joining
them all together.

Separate public exports using a code block and `#[rustfmt::skip]`. Has
the nice advantage of reducing the number of `#[doc(inline)]` attributes
also.

1. Modules first, as they are part of the project's structure.
2. Private imports
3. Public re-exports (using `rustfmt::skip` to prevent merge)

Use the format

```rust
mod xyz;
mod abc;

use ...;

pub use {
    ...,
};
```

This patch introduces changes to the rendered HTML docs.
2023-10-31 15:16:47 +11:00
Tobin C. Harding 33774122e0
Remove public re-exports from private module
The `crypto::taproot` module is private, public re-exports are
inaccessible, remove them.
2023-10-31 15:16:47 +11:00
Tobin C. Harding e21ee381bc
Split Prevouts errors out into specific error types
Done as part of the great error clean up.

Currently we are returning a general `Error` from `Prevouts` functions,
this is un-informative, we can do better by returning specific types
that indicate the exact error path.
2023-10-31 14:21:24 +11:00
Tobin C. Harding fa104aefa5
bitcoin: Add signing examples
Add two signing examples to showcase signing a simple one input two
output transaction using both segwit v0 outputs and taproot outputs.

This patch is the result of the recent rust-bitcoin TABConf workshop,
wit bug fix by Sanket, updated to use APIs from tip of master branch.

This code, depending on v0.30.0 is what is being introduced to the
cookbook at the moment.
2023-10-31 10:11:48 +11:00
Tobin C. Harding d6298fe711
Use capital B for Bitcoin in rustdoc
As we do in another part of this file use capital 'B' for Bitcoin.
2023-10-31 08:37:11 +11:00
Tobin C. Harding bcfabc3556
Fix typo, missing word
Add a 'by' to fix the grammar in sentence.
2023-10-31 08:36:09 +11:00
yancy fde6479c6a Create uniform build script
Previously, each unique compiler cfg attribute that appeared in the
codebase was hard coded and emitted to stdout at compile time. This
meant keeping the file up to date as different compiler cfg attributes
changed. It's inconsequential to emit a compiler version that's not
used, so this change just emits all possibilities to reduce the
maintenance burden of the build script.
2023-10-30 14:40:41 +01:00
Tobin C. Harding efedf862b0
bitcoin: Bump version number to v0.31.0
In preparation for release of v0.31.0 bump the version number.

The changelog is already up to date because we have done two RC
releases.
2023-10-28 08:32:30 +11:00
Andrew Poelstra 7246585588
Merge rust-bitcoin/rust-bitcoin#2096: Move psbt macro to the psbt test module
2ecab31f94 Remove stale comment and map_err (yancy)
b166442fb0 Replace hex_psbt macro with test helper function (yancy)
9e4a784b8b Move psbt macro to the psbt test module (yancy)

Pull request description:

  Remove `#[cfg(test)]` and the macro `psbt_with_values` from macros.rs and place it in the tests module for psbt.

ACKs for top commit:
  apoelstra:
    ACK 2ecab31f94
  tcharding:
    ACK 2ecab31f94

Tree-SHA512: 06a55056e864befac8b33968bf4e469c3c7bc20e651ad5bb3b80aa76749169af1266e1d4101d3e9e9bbffe7c860e8b9fcd675a78ca7ae67dc09892c75fba0dd0
2023-10-27 13:17:07 +00:00
lateminer 2a891e0be8
Add a missing link to #2006 in 'bitcoin/CHANGELOG.md' 2023-10-26 21:06:48 +02:00
Andrew Poelstra 1eb6e0c7d8
Merge rust-bitcoin/rust-bitcoin#2134: Add clippy exceptions for needless_question_mark lint
875545517d Add clippy exceptions for needless_question_mark lint (Steven Roose)

Pull request description:

  This lint forces you to write semantically different code that is in most cases inferior, just to save you 5 characters.

  The reason why the code is inferior is because it doesn't do error conversion so it would break when either of the two function signatures changes while in the original code using the `?` operator, nothing would break if the inner error can be converted into the outer error.

ACKs for top commit:
  apoelstra:
    ACK 875545517d
  tcharding:
    ACK 875545517d

Tree-SHA512: 8429e0fb7d759a3d19231e7bcaed61b0988172d931e758a9522d7c994854fd403408bb93b06778a5c09746cd38b6a96d3d2e0a862fb4516f2dbfffffe8735ce0
2023-10-24 01:17:32 +00:00
Steven Roose 875545517d
Add clippy exceptions for needless_question_mark lint 2023-10-23 16:45:52 +01:00
Steven Roose 750ee2ba56
Remove unnecessary clippy attribute on is_sighash_single_bug 2023-10-23 01:41:44 +01:00
Steven Roose f522a0290c
Remove unnecessary clippy attribute on relative::LockTime 2023-10-23 01:37:50 +01:00
Steven Roose b7f11d4493
Remove unnecessary clippy attribute on absolute::LockTime
I ran the clippy locally without it and it doesn't seem to be necessary
anymore.
2023-10-23 01:36:19 +01:00
Andrew Poelstra e436b5b900
Merge rust-bitcoin/rust-bitcoin#2132: Replace helper macro with helper function
b163d9b59a Replace hex_script macro with a helper function (yancy)
7f26439e20 Add track_caller to test helper functions (yancy)
bf08ee4499 Replace helper macro with helper function (yancy)

Pull request description:

  Remove the macro hex_psbt and replace with a function.  Using track_caller to accurately report the test name and line number during a panic is used in place of a macro.

  This is a refactor commit to the test suit.

ACKs for top commit:
  apoelstra:
    ACK b163d9b59a
  tcharding:
    ACK b163d9b59a

Tree-SHA512: 6955c966d1c37020515ae990e5e0ec154f591ce025321dee71264e4e53cbab38afdb681ca193e626efdb7be4b4e84763cd8baf41b2a1258d34c38acd58713254
2023-10-22 13:16:42 +00:00
yancy b163d9b59a Replace hex_script macro with a helper function
Remove the macro hex_script and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 10:20:23 +02:00
yancy 7f26439e20 Add track_caller to test helper functions
The test helper files can panic when calling hex_psbt().  hex_psbt()
will report the calling function, instead of the offending test.  Adding
track_caller to functions that call hex_psbt will report the line number
of the failing test.
2023-10-20 09:41:50 +02:00
yancy bf08ee4499 Replace helper macro with helper function
Remove the macro hex_psbt and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 09:25:00 +02:00
Andrew Poelstra fe07c13bcc
Merge rust-bitcoin/rust-bitcoin#2131: encode: Implement encoding traits on Vec<BlockHeader>
10c1f7b9c6 encode: Implement encoding traits on Vec<BlockHeader> (Steven Roose)

Pull request description:

ACKs for top commit:
  clarkmoody:
    ACK 10c1f7b9c6
  apoelstra:
    ACK 10c1f7b9c6
  vincenzopalazzo:
    ACK 10c1f7b9c6

Tree-SHA512: 32182f4b18a2e4aee20efc4c649827be4e2f61991fae48d7e1360e2c8224bcd98ef89391b50ec2d4e12a720daf7350ca0beb23094153313b668a54cee9cda278
2023-10-19 15:38:54 +00:00
Andrew Poelstra 3a95365f46
Merge rust-bitcoin/rust-bitcoin#2124: Tracking PR for `v0.31.0-rc2` release
38960ab5a5 Bump version to 0.31.0-rc2 (Tobin C. Harding)
79dfe8d270 justfile: Add update-lock-files command (Tobin C. Harding)
178069c13e Add changelog entries for v0.31.0 (Tobin C. Harding)

Pull request description:

  Prepare for the v0.31.0-rc2 release.

  - Add changlog entries
  - Add a `just update-lock-files` command
  - Bump the version ready for next RC release

ACKs for top commit:
  apoelstra:
    ACK 38960ab5a5
  clarkmoody:
    ACK 38960ab5a5

Tree-SHA512: d2d459666117992689ea2bde2ba4a5d9a9cd36a85c5f0df443ceca3bf9d4ec351baffb4be09fbe25bf0767145d11a59b97031e7c8f9182fa9348a6883420b8fe
2023-10-19 13:32:03 +00:00
Steven Roose 10c1f7b9c6
encode: Implement encoding traits on Vec<BlockHeader> 2023-10-18 23:34:56 +01:00
Tobin C. Harding 38960ab5a5
Bump version to 0.31.0-rc2
In preparation for doing the next RC release bump the version.
2023-10-18 13:52:49 +11:00
Tobin C. Harding 178069c13e
Add changelog entries for v0.31.0
Add the remaining changelog entries for the v0.31.0 release. Includes
everything released in v0.31.0-rc1
2023-10-18 13:46:42 +11:00
Andrew Poelstra 37daf4620c
Merge rust-bitcoin/rust-bitcoin#2126: Implement manual fmt::Debug for BlockHeader to include block hash
b108ffa2ec Implement manual fmt::Debug for BlockHeader to include block hash (Steven Roose)

Pull request description:

  I think it makes sense for a block header to have the block hash shown in the debug print.

ACKs for top commit:
  tcharding:
    ACK b108ffa2ec
  vincenzopalazzo:
    ACK b108ffa2ec
  apoelstra:
    ACK b108ffa2ec

Tree-SHA512: 37b77f8b2d8da3903bc5ff329abd45dcb5f189ba2ac38fc309fa477b0eb569b01dd85c41f160e073bb56b4a599b7c4774fbfbd8da4b82ddb41540d60bfeafea3
2023-10-17 23:41:07 +00:00
Steven Roose b108ffa2ec
Implement manual fmt::Debug for BlockHeader to include block hash 2023-10-17 02:00:42 +01:00
yancy 2ecab31f94 Remove stale comment and map_err
The current MSRV is greater than rustc 0.29.
2023-10-16 14:48:14 +02:00
yancy b166442fb0 Replace hex_psbt macro with test helper function
hex_psbt was added as a macro so that a panic would reveal the line
number of the failing test by expanding the macro at the test location.
However, a stack trace can be used to reveal  the test that caused the
failure using RUST_BACKTRACE=1.  Furthermore, the track_caller macro is
added to the helper methods which will reveal the line number of the
calling function (the offending test). More detailed information for
debugging has been added to hex_psbt() so that the offending string
will be included in the panic message.
2023-10-16 14:48:00 +02:00
yancy 9e4a784b8b Move psbt macro to the psbt test module
The macro psbt_with_values is used by the psbt test module.  Since
there is no pre-processing required, there is no reason to use
metaprogramming here, so this commit moves the logic from a
macro to common function in the test module.
2023-10-16 11:31:01 +02:00
Tobin C. Harding 6731e85dc1
bitcoin: Bump version to 0.31.0-rc1
In preparation for the first rc release; bump the version number.
2023-10-13 08:34:46 +11:00
Andrew Poelstra e0ddead255
Merge rust-bitcoin/rust-bitcoin#2107: Use `Target` as type for `pow_limit`
38005f6aa7 Use Target for pow_limit (Tobin C. Harding)

Pull request description:

  The `Params::pow_limit` field is currently a `Work` type, this is incorrect. The proof of work limit is the highest _target_ not the lowest work (even though these have a relationship).

  Note that we use the highest _attainable_ target, this differs from Bitcoin Core and the reasoning is already documented in the code.

  Add new consts and document where they came from as well as how they differ to Core.

  Use the new consts in the various network specific `Params` types.

  Fix: #2106

ACKs for top commit:
  junderw:
    ACK 38005f6aa7
  apoelstra:
    ACK 38005f6aa7

Tree-SHA512: 5e71f69cdd555fd95a52fc1479e27b5e11226772f6432758c1364107a068bd1271486da6db1ece694da0287ce10cfbc18d28d6e3dbb0b9d387ff968eea43ab18
2023-10-12 15:05:51 +00:00
Tobin C. Harding 38005f6aa7
Use Target for pow_limit
The `Params::pow_limit` field is currently a `Work` type, this is
incorrect. The proof of work limit is the highest _target_ not the
lowest work (even though these have a relationship).

Note that we use the highest _attainable_ target, this differs from
Bitcoin Core and the reasoning is already documented in the code.

Add new consts and document where they came from as well as how they
differ to Core.

Use the new consts in the various network specific `Params` types.
2023-10-11 08:50:42 +11:00
Tobin C. Harding 678eee8d73
Use Message::from_digest
We have a new API function available with recent version of `secp256k1`
to create a `Message` directly from a sighash byte array.

Use `Message::from_digest(sighash.to_byte_array())` to construct
messages ready to sign.
2023-10-11 08:16:25 +11:00
Tobin C. Harding 6f30ac9d02
Upgrade the secp dependency
Upgrade the `secp256k1` dependency to the newly released `v0.28.0`.

FTR this includes two simple changes:
- Use `Message::from_digest_slice` instead of `Message::from_slice`.
- Use `secp256k1::Keypair` instead of `secp256k1::KeyPair`.
2023-10-10 10:04:31 +11:00
Tobin C. Harding 818a3c5424
Use Keypair instead of KeyPair in identifiers
In preparation for updating the secp dependency to v0.28.0, which
includes a change of `KeyPair` to `Keypair`, change our identifier usage
to indicate that "keypair" is a single word.

Deprecate the old forms.
2023-10-10 10:02:09 +11:00
Tobin C. Harding 33ee49f8e5
Update bech32 dependency
Update the `bech32` dependency to use the newly release beta version.

The main fix here is silent, a bug fix in `bech32` that was being hit by
our fuzzing suite.
2023-10-10 06:47:58 +11:00
Andrew Poelstra 342a35b4c4
Merge rust-bitcoin/rust-bitcoin#2109: ci: bump rustc to 1.60 for fuzz test
6b5d06f23e ci: fix the byteorder to 1.4.3 for edition 2018 (Vincenzo Palazzo)
98513ef151 clippy: more worning fixes (Vincenzo Palazzo)
05d3dc5d72 Remove redundant guard (Tobin C. Harding)
4537634e7e ci: bump rustc to 1.60 for fuzz test (Vincenzo Palazzo)

Pull request description:

  Ci looks like broken, so this should fix
  it

ACKs for top commit:
  apoelstra:
    ACK 6b5d06f23e

Tree-SHA512: bfa0eaf8cbc02a671237d99221db8c21264ce9df91301818c95c41dcc5ad4935e91254b0b3fa8f36738a9d71b6541fb8784ac8280d67057960a3d20e385a9f17
2023-10-07 16:40:36 +00:00
Vincenzo Palazzo 98513ef151
clippy: more worning fixes
error[E0308]: mismatched types
  --> bitcoin/src/psbt/raw.rs:87:24
   |
87 |               return Err(encode::Error::OversizedVectorAllocation {
   |  ________________________^
88 | |                 requested: key_byte_size as usize,
89 | |                 max: MAX_VEC_SIZE,
90 | |             });
   | |_____________^ expected enum `psbt::error::Error`, found enum `consensus::encode::Error`
   |
help: try wrapping the expression in `psbt::error::Error::ConsensusEncoding`
   |
87 ~             return Err(psbt::error::Error::ConsensusEncoding(encode::Error::OversizedVectorAllocation {
88 |                 requested: key_byte_size as usize,
89 |                 max: MAX_VEC_SIZE,
90 ~             }));
   |

----

  Compiling bitcoin v0.30.0 (/home/vincent/github/work/rust-btc/rust-bitcoin/bitcoin)
    Checking bitcoin-fuzz v0.0.1 (/home/vincent/github/work/rust-btc/rust-bitcoin/fuzz)
error: redundant clone
   --> bitcoin/examples/taproot-psbt.rs:453:77
    |
453 |             witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
    |                                                                             ^^^^^^^^ help: remove this
    |
    = note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
   --> bitcoin/examples/taproot-psbt.rs:453:64
    |
453 |             witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
    |                                                                ^^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

error: could not compile `bitcoin` due to previous error
warning: build failed, waiting for other jobs to finish...
error: redundant clone
    --> bitcoin/src/psbt/mod.rs:1095:13
     |
1095 |             .clone()
     |             ^^^^^^^^ help: remove this
     |
     = note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
    --> bitcoin/src/psbt/mod.rs:1094:17
     |
1094 |           assert!(psbt
     |  _________________^
1095 | |             .clone()
     | |____________^
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-10-07 11:40:44 +02:00
Tobin C. Harding 05d3dc5d72 Remove redundant guard
Clippy warns: redundant guard

As suggested, remove the redundant guard.
2023-10-07 11:19:47 +02:00
Andrew Poelstra eab9f89779
Merge rust-bitcoin/rust-bitcoin#1986: Remove private hex test macro
8eff4d0385 Remove private hex test macro (Tobin C. Harding)

Pull request description:

  We have this macro in `hex-conservative` now, remove the version here.

  This patch does not change the public API and only touches test code.

ACKs for top commit:
  apoelstra:
    ACK 8eff4d0385
  clarkmoody:
    ACK 8eff4d0385

Tree-SHA512: 93a08fff778930071cd1a28c19202e4a94ca8881b2e873538de2e942b71c2cd6184ed6364c572538a8a699295a71761c6f836accaf251a15683138b71f148fab
2023-10-06 22:14:12 +00:00
Andrew Poelstra 3743f2743b
Merge rust-bitcoin/rust-bitcoin#2101: Audit error types code base wide
10374af75c Make error types uniform (Tobin C. Harding)
43d3306822 Use explicit error::Error impl instead of the default (Tobin C. Harding)
2512dbafc2 Remove impl_std_error macro (Tobin C. Harding)
6933ca4fc2 Add suffix to HiddenNodes error type (Tobin C. Harding)
2b40ea24fb Add suffix to IncompleteBuilder error type (Tobin C. Harding)
f41416a0ea Add suffix to UnknownMagic error type (Tobin C. Harding)
5658dac024 Add suffix to UnknownChainHash error type (Tobin C. Harding)
2fb71dd943 Move p2p error types to bottom of file (Tobin C. Harding)
39314ad52f Move error code to match conventional layout (Tobin C. Harding)

Pull request description:

  PR aims to achieve two things:
  - Make error code brain dead easy to read
  - Get error code closer to being ready for v1.0

  The first 8 patches are pretty basic, and are broken up into really small changes. The last patch is much bigger, it has a long git log to explain it but reviewing should not take too much brain power.

  This PR does not introduce anything new, it just applies what we have been doing recently with errors. Before v1.0.0 others will likely want to re go over all the error types. As such I believe this PR can be merged under the one ack carve-out.

  ### TODOs (future PRs)

  We have a few errors that still need splitting up:

  - Split up `merkle_tree::block::MerkleBlockError`
  - Split up `psbt::error::Error`
  - Split up `IncompleteBuilderError`

  Also, all error From's should probably have `#[inline]`, I noticed late in the process and did not have the heart to visit every error again.

ACKs for top commit:
  apoelstra:
    ACK 10374af75c
  clarkmoody:
    ACK 10374af75c

Tree-SHA512: 4f4f3533f42dc11af8e7978f3272752bb56d12a68199752ed4af0c02a46a87892b55c695b7007bc3d0bdf389493068d068e2be1780e8c3008815efec3a02eedf
2023-10-06 14:19:39 +00:00
Andrew Poelstra b549faa1ca
Merge rust-bitcoin/rust-bitcoin#2102: rand-std should enable std
a1538a42c1 rand-std should enable std (sanket1729)

Pull request description:

ACKs for top commit:
  realeinherjar:
    ACK a1538a42c1
  apoelstra:
    ACK a1538a42c1
  tcharding:
    ACK a1538a42c1

Tree-SHA512: 5a1f9b6f40bfc24bc1f9a21dce4b8d91e5f4066170096443694068788def23653759bca30a36b60016ca81ba6196b8a6b5badea0d3ce654ecfbd8981c65d53f5
2023-10-05 12:23:39 +00:00
Tobin C. Harding be8aee6968
Remove stale link 2023-10-05 15:19:33 +11:00
Tobin C. Harding 7d54cd3485
Fix typo in docs 2023-10-05 15:13:09 +11:00
sanket1729 a1538a42c1
rand-std should enable std
Users should not need to specify both.
2023-10-03 23:10:23 -07:00
Tobin C. Harding 10374af75c
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:

- Uses the following derives (unless not possible, usually because of `io::Error`)

  `#[derive(Debug, Clone, PartialEq, Eq)]`

- Has `non_exhaustive` unless we really know we can commit to not adding
  anything.

Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:

- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after

Re: non_exhaustive

To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 15:15:52 +11:00
Tobin C. Harding 43d3306822
Use explicit error::Error impl instead of the default
In a further effort to make the code brain-dead easy to read; use an
explicit implementation of `std::error::Error` that returns `None`
instead of relying on the default trait implementation.
2023-10-04 15:15:43 +11:00
Tobin C. Harding 2512dbafc2
Remove impl_std_error macro
We would like the codebase to be optimized for readability not ease of
development, as such code that is write-once-read-many should not use
macros.

Currently we use the `impl_std_error` macro to implement
`std::error::Error` for struct error types. This makes the code harder
to read at a glance because one has to think what the macro does.

Remove the `impl_std_error` macro and write the code explicitly.
2023-10-04 15:15:34 +11:00
Tobin C. Harding 6933ca4fc2
Add suffix to HiddenNodes error type
By convention we always include the suffix "Error" on our error types.

Rename the error type `HiddenNodes` to `HiddenNodesError`.
2023-10-04 15:15:21 +11:00
Tobin C. Harding 2b40ea24fb
Add suffix to IncompleteBuilder error type
By convention we always include the suffix "Error" on our error types.

Rename the error type `IncompleteBuilder` to `IncompleteBuilderError`.
2023-10-04 15:14:45 +11:00
Tobin C. Harding f41416a0ea
Add suffix to UnknownMagic error type
By convention we always include the suffix "Error" on our error types.

Rename the error type `UnknownMagic` to `UnknownMagicError`.
2023-10-04 12:29:05 +11:00
Tobin C. Harding 5658dac024
Add suffix to UnknownChainHash error type
By convention we always include the suffix "Error" on our error types.

Rename the error type `UnknownChainHash` to `UnknownChainHashError`.
2023-10-04 12:28:05 +11:00
Tobin C. Harding 2fb71dd943
Move p2p error types to bottom of file
Move the p2p error types to the bottom of the file next to the various
impls for these types.

Code move only, no other changes.
2023-10-04 12:28:05 +11:00
Tobin C. Harding 39314ad52f
Move error code to match conventional layout
We typically layout error code as: definition, [impl block], `Display` impl,
`error::Error` impl, from imlps.

Code move only, no other changes.
2023-10-04 12:11:07 +11:00
Andrew Poelstra fcd4ad51ef
Merge rust-bitcoin/rust-bitcoin#1389: Add `PartialMerkleTree::calc_tree_height` method
752adff9d1 Add method calc_height (Tobin C. Harding)
46f5588646 Add unit test for calc_tree_width (Tobin C. Harding)

Pull request description:

  Add a private `PartialMerkleTree::calc_tree_width` function and a unit test to test it.

ACKs for top commit:
  apoelstra:
    ACK 752adff9d1
  clarkmoody:
    ACK 752adff9d1

Tree-SHA512: 9c4ad9f6ff47d8faad1c7c1e977427f1528af2712ceffd05357d0c9117b5fdb7b2783afc00a75cb19b853bfbd7b3895baa3a3563bdc496593cc9b06ce80dbbf8
2023-10-03 13:55:45 +00:00
Andrew Poelstra bd9c4125cf
Merge rust-bitcoin/rust-bitcoin#2082: example: Modify `taproot-psbt.rs` to make the use of prevouts clearer.
3b60ad5567 example: Modify `taproot-psbt.rs` to make the use of prevouts clearer. (S. Santos)

Pull request description:

  The `taroot-psbt.rs` example uses only one input, and therefore the current code may not make it clear that the number of prevout items must correspond to the number of transaction inputs, since the prevout slice is built within a loop.

  This PR aims to make this clear to any user who wants to reuse the logic from the example code.

ACKs for top commit:
  tcharding:
    ACK 3b60ad5567
  apoelstra:
    ACK 3b60ad5567

Tree-SHA512: afad63782b0e8a459de6cf69712d31fdab860c0d4cf9f3a51c3d85544a067bd50f4febc10ec4046e3a37d9ca518bbf2460c2599f1569549701c07f8a267dfd05
2023-10-01 15:12:25 +00:00
S. Santos 3b60ad5567 example: Modify `taproot-psbt.rs` to make the use of prevouts clearer. 2023-09-29 20:19:32 -03:00
Tobin C. Harding 8eff4d0385
Remove private hex test macro
We have this macro in `hex-conservative` now, remove the version here.

This patch does not change the public API and only touches test code.
2023-09-30 06:22:52 +10:00
Andrew Poelstra a0540bdb21
Merge rust-bitcoin/rust-bitcoin#2064: Feature: Psbt fee checks
dac627cc09 Feature: Psbt fee checks (junderw)

Pull request description:

  Closes #2061

  These new methods on Psbt will add checks for high fees by default. The threshold for "high fees" is currently set to 25000 sat/vbyte, which is about 20x higher than the highest next block fees seen on the "Mempool" website.

  The primary goal of this change is to prevent users of the library from accidentally sending absurd amounts of fees.

  (ie. Recently in September 2023 there was a transaction that sent an absurd amount of fees and made news in the Bitcoin world. Luckily the mining pool gave it back, but some might not be so lucky.)

  There are variants of the method that allow for users to set their own "absurd" threshold using a `FeeRate` value. And there is a method that performs no checks, and the method name is alarming enough to draw attention in a review, so at least developers will be aware of the concept.

ACKs for top commit:
  apoelstra:
    ACK dac627cc09
  tcharding:
    ACK dac627cc09

Tree-SHA512: ae0beafdb50339ba3efc44a48ba19c0aeeb0a2671eb43867c1e02b807677ce99fb6b4c47b74a9ed2999f827b3edc00a8871fa4730dd12a4cb265be99437c13db
2023-09-29 17:13:30 +00:00
junderw dac627cc09
Feature: Psbt fee checks 2023-09-28 00:11:33 -07:00
Andrew Poelstra 0de8ec5b19
Merge rust-bitcoin/rust-bitcoin#2076: Re-write the weight/size API
c34e3cc7cc Re-write size/weight API (Tobin C. Harding)
73f7fbf520 Add code comments to transaction serialization (Tobin C. Harding)
29f20c1d0b Add segwit serialization constants (Tobin C. Harding)

Pull request description:

  Audit and re-write the weight/size API for `Block` and `Transaction`. First two patches are trivial, patch 3 contains justification and explanation for this work, copied here:

  ```
      Recently we introduced a bug in the weight/size code, while
      investigating I found that our `Transaction`/`Block` weight/size APIs
      were in a total mess because:

      - The docs were stale
      - The concept of weight (weight units) and size (bytes) were mixed up

      I audited all the API functions, read some bips (141, 144) and re-wrote
      the API with the following goals:

      - Use terminology from the bips
      - Use abstractions that mirror the bips where possible
  ```

  Please note, this PR introduces panics if a sciptPubkey overflows the calculation `weight = spk.size() * 4`.

  Fix #2049

ACKs for top commit:
  apoelstra:
    ACK c34e3cc7cc
  sanket1729:
    ACK c34e3cc7cc.

Tree-SHA512: 4944f652e6e362a282a5731140a9438a82d243a4c646b4627d9046a9f9cf13c476881750d432cfbc6b5fe5de1f0c4c9c44ed4569dac4bc11b55a5db28793803c
2023-09-26 20:08:56 +00:00
Andrew Poelstra 1c29dd97ce
Merge rust-bitcoin/rust-bitcoin#2084: Add `Witness::p2wpkh` constructor
5901d35095 Add push_p2wpkh function on Witness (Tobin C. Harding)
8cd409d561 Deprecate push_bitcoin_signature (Tobin C. Harding)

Pull request description:

  In order to create the witness to spend p2wpkh output one must create a `Witness` that includes the signature and the pubkey, we should have a function for this.

  ## Notes
  The PR originally added a `push_p2wphk` method, this is now instead a constrcutor `Witness:p2wpkh` (after review discussion below).

  - Patch 1 changes `push_bitcoin_signature` to take an `ecdsa::Sigtnture` instead of an `ecdsa::SerializedSignature`
  - Patch 2 takes a `secp256k1::PublicKey` removing the need for an error path (discussed below).

ACKs for top commit:
  sanket1729:
    ACK 5901d35095
  apoelstra:
    ACK 5901d35095

Tree-SHA512: 646014d97daafbf0909106d8990debaf481ac6f3578f0ddf232d739c3e2d55ae1d0275abe5a4a1db1c5c192c8c5f0b5546fc65aac37b91a3729db881c5ad3dec
2023-09-26 13:08:56 +00:00
Tobin C. Harding 5901d35095
Add push_p2wpkh function on Witness
In order to create the witness to spend p2wpkh output one must push the
signature and the pubkey, we should have a function for this.
2023-09-26 10:14:23 +10:00
Andrew Poelstra c31b933c10
Merge rust-bitcoin/rust-bitcoin#1952: Add changelog entries
24e5e19ed0 Add changelog entries (Tobin C. Harding)
79f2157311 bitcoin: Grab changelog entry from 0.30.1 release (Tobin C. Harding)

Pull request description:

  Gearing up for the next release add changelog entries for everything merged upto July 24.

ACKs for top commit:
  apoelstra:
    ACK 24e5e19ed0
  clarkmoody:
    ACK 24e5e19ed0

Tree-SHA512: 12761623f3d61fea77b5dfe4c505dcdb5041ec4473ae05524b818eb2d4e3cbc3b039b8f1400022916c9f2d1ee97fcccd7e7b6da0bb5f868a1b77bdcfade3d7ed
2023-09-25 19:53:42 +00:00
Tobin C. Harding c34e3cc7cc
Re-write size/weight API
Recently we introduced a bug in the weight/size code, while
investigating I found that our `Transaction`/`Block` weight/size APIs
were in a total mess because:

- The docs were stale
- The concept of weight (weight units) and size (bytes) were mixed up

I audited all the API functions, read some bips (141, 144) and re-wrote
the API with the following goals:

- Use terminology from the bips
- Use abstractions that mirror the bips where possible
2023-09-25 08:25:49 +10:00
Tobin C. Harding 24e5e19ed0
Add changelog entries
Add changelog entries to `bitcoin` after going through all merged PRs
upto July 24 (Sydney time).
2023-09-25 07:44:26 +10:00
Tobin C. Harding 79f2157311
bitcoin: Grab changelog entry from 0.30.1 release
In preparation for adding a 0.31.0 changelog entry grab the changelog
entry from the 0.30.1 tagged release branch so it does not get lost.
2023-09-25 07:43:53 +10:00
Tobin C. Harding 8cd409d561
Deprecate push_bitcoin_signature
The `Witness::push_bitcoin_signature` method is old and a bit stale.
Bitcoin has taproot signatures now so the name is stale, also we have
the `crate::ecdsa::Signature` type that holds the secp sig and the hash
type so we can use that instead of having two separate parameters.

Add a new, up to date, `Witness::push_ecdsa_signature` function and
deprecate the `push_bitcoin_signature` one.
2023-09-25 07:21:20 +10:00
Tobin C. Harding f31fb08651
Remove default impl from transaction version
There is no logical default for the transaction version number, there is
only pre-bip68 (v1) and post-bip68 (v2). Uses should specify the version
they want not rely on us making the choice.

(I originally added this impl to support testing, this was in hindsight
the wrong thing to do, props to Sanket for noticing.)
2023-09-25 05:51:24 +10:00
Clark Moody 72a7280d7d
Merge pull request #2006 from tcharding/08-18-tx-version
Add transaction::Version data type
2023-09-23 13:13:52 -05:00
Tobin C. Harding 73f7fbf520
Add code comments to transaction serialization
In an attempt to help super new devs add code comments about transaction
serialization formats pre and post segwit.
2023-09-23 14:38:25 +10:00
Tobin C. Harding 29f20c1d0b
Add segwit serialization constants
One of our stated aims is to make it possible to learn bitcoin by using
our library. To help with this aim add to private consts for the segwit
transaction marker and flag serialization fields.
2023-09-23 14:38:25 +10:00
Andrew Poelstra 141d805ddc
Merge rust-bitcoin/rust-bitcoin#2073: Feature: Count sigops for Transaction
158ba26a8a Feature: Count sigops for Transaction (junderw)

Pull request description:

  I copied over the sigop counting logic from Bitcoin Core, but I made a few adjustments.

  1. I removed 2 consensus flags that checked for P2SH and SegWit activation. This code assumes both are activated. If we were to include that, what would be a good way to go about it? (ie. If I run this method on a transaction from the 1000th block and it just so happened to have a P2SH-like input, Bitcoin Core wouldn't accidentally count those sigops because the consensus flag will stop them from running the P2SH logic. Same goes for SegWit)
  3. Since there's no guarantee that we have an index from which we can get the prevout scripts, I made it into a generic closure that looks up the prevout script for us. If the caller doesn't provide it, We can only count sigops directly in the scriptSig and scriptPubkey (no P2SH or SegWit).

  ## TODO
  - [x] Write tests for transaction sigop counting

  ~~Edit: The test changes are just to get the 1.48 tests passing. I'll remove them and replace them with whatever solution that is agreed upon in another PR etc.~~

  Edit 2: This is the code I used as a guide:

  8105bce5b3/src/consensus/tx_verify.cpp (L147-L166)

  Edit 3: I found a subtle bug in the implementation of `count_sigops` (https://github.com/rust-bitcoin/rust-bitcoin/pull/2073#issuecomment-1722403687)

ACKs for top commit:
  apoelstra:
    ACK 158ba26a8a
  tcharding:
    ACK 158ba26a8a

Tree-SHA512: 2b8a0c50b9390bfb914da1ba687e8599b957c75c511f764a2f3ed3414580150ce3aa2ac7aed97a4f7587d3fbeece269444c65c7449b88f1bdb02e573e6f6febd
2023-09-22 17:12:29 +00:00
Andrew Poelstra 675fd54c95
Merge rust-bitcoin/rust-bitcoin#1951: Use newly released bech32 API
e4c7e01a6f Use the new bech32 iterator API (Tobin C. Harding)

Pull request description:

  Depend on the newly released version of `bech32`, BOOM!

ACKs for top commit:
  apoelstra:
    ACK e4c7e01a6f
  clarkmoody:
    ACK e4c7e01a6f

Tree-SHA512: 91675a830cf67f8dcabd42e7dc1b70d80b669330be5244bb8102e0ec5d1a206d5ead07f73b328a158b761c328bc78d573185af8d31f14183ccc17318d752c02b
2023-09-21 22:26:51 +00:00
junderw 158ba26a8a
Feature: Count sigops for Transaction
Co-authored-by: Tobin C. Harding <me@tobin.cc>
2023-09-21 14:50:23 -07:00
Andrew Poelstra c32c7d1611
Merge rust-bitcoin/rust-bitcoin#2026: Use valid Psbt for serde regression test
95b7a95fc2 test: correct psbt used for regression test (Subhradeep Chakraborty)

Pull request description:

  The `unknown` key used in the `Psbt` (in `serde_regression_psbt` test) is not really unknown. Also the `preimages` in the `Input` don't seem to be valid. Though they don't affect the tests because of the `serde::serialize`, the `Psbt` itself is invalid and hence this PR.

ACKs for top commit:
  tcharding:
    ACK 95b7a95fc2
  apoelstra:
    ACK 95b7a95fc2

Tree-SHA512: 275320bb273163364640351e55a3f16c34c1eef06e9dde79b72bf40187d313f037edb5e4544f4dce32fb8a7cc552504aa2e4d22a646c89a3dec022f917539219
2023-09-21 18:53:54 +00:00
Andrew Poelstra f71948a778
Merge rust-bitcoin/rust-bitcoin#2021: Move and rename `XpubIdentifier`
b2a7d7023c Rename XpubIdentifier to XKeyIdentifier (Tobin C. Harding)
ffd2466ad1 Move XpubIdentifier to the bip32 module (Tobin C. Harding)

Pull request description:

  - Patch 1: Move the hash to the `bip32` module where it is used, as we have done with other hashes recently (and re-export it at crate root).
  - Patch 2: Rename the hash to `XKeyIdentifier` as discussed in #2014

  Fix: #2014

ACKs for top commit:
  apoelstra:
    ACK b2a7d7023c

Tree-SHA512: 5efa9fc857c71e506263bf6adee3b4294f22838d5b119177c9108c69191d545338c11a4796bc95e956a67f3418010725f3d12c06d2c4c3bb5cf038d59976ae0f
2023-09-21 17:55:31 +00:00
Andrew Poelstra eda7e7df0d
Merge rust-bitcoin/rust-bitcoin#2068: Fixes #2011: Customize Debug implementation of `absolute::LockTime`
71a5fe2b54 Customize Debug implementation of absolute::LockTime (Subhradeep Chakraborty)

Pull request description:

  Fixes https://github.com/rust-bitcoin/rust-bitcoin/issues/2011.

  This PR aims to make the "pretty print" of `absolute::LockTime` prettier by printing `X blocks` and `X seconds` for `Blocks` and `Seconds` respectively instead of the default Enum printing.

ACKs for top commit:
  apoelstra:
    ACK 71a5fe2b54
  tcharding:
    ACK 71a5fe2b54

Tree-SHA512: 79baad5ee0ba1aa892cb38588dae6a24977b8e42356208d6cc9adb007d1f8371e61dc82e35bb4dac9961216d68c56907f0db376c5c6fbb5a406728b8f7c3f5ad
2023-09-21 17:03:59 +00:00
Andrew Poelstra 36805b5283
Merge rust-bitcoin/rust-bitcoin#1994: Remove redundant segwit version from function names
bc398204bf Remove redundant segwit version from function names (Tobin C. Harding)

Pull request description:

  A P2TR output does not need to be clarified with version 1, it is implicit. As with p2wpkh/p2wsh and version 0.

  Remove redundant version identifiers from function names, deprecating the originals.

ACKs for top commit:
  apoelstra:
    ACK bc398204bf

Tree-SHA512: 49806c564badca25ce02161445b2b41497b565f2002aa1edfc0cf0c57b38683480deec0d9b682e18dc7e59c22128e0b641abcccc2cbedd0b5603cbcbf2fd26df
2023-09-21 16:10:50 +00:00
Andrew Poelstra f80ec98f35
Merge rust-bitcoin/rust-bitcoin#2002: Remove unnecessary reference
f17bb0d18f Remove unnecessary reference (Tobin C. Harding)

Pull request description:

  `T` is a generic that implements`AsRef<PushBytes>`, it should not be a reference. This is inline with other usages of `AsRef<PushBytes>` for example in `Builder::push_slice`.

  Found while working on #2003

ACKs for top commit:
  apoelstra:
    ACK f17bb0d18f

Tree-SHA512: 6f6ae0ba5d5010db53d9c2af107df84bc058277b2b7cc35800f4e6ed93d351838b7f101284b7d80345bee639615d27d76a2e5c4c784782c5b3e5090444defe29
2023-09-21 14:23:50 +00:00
Subhradeep Chakraborty 95b7a95fc2 test: correct psbt used for regression test 2023-09-21 11:45:18 +05:30
Tobin C. Harding e4c7e01a6f
Use the new bech32 iterator API
Use the new bech32 iterator API that Andrew and I wrote.
2023-09-21 15:10:57 +10:00
Tobin C. Harding c950ef4bbd
Add transaction::Version data type
BIP-68 activated a fair while ago (circa 2019) and since then only
transaction versions 1 and 2 have been considered standard.

Currently in our `Transaction` struct we use an `i32`, this means users
can construct a non-standard transaction if they do not first look up
what the value should be. We can help folk out here by abstracting over
the version number.

Since the version number only governs standardness elect to make the
inner `i32` public (ie., not an invariant). The aim of the type is to
make life easy not restrict what versions are used.

Add transaction::Version data type that simply provides two consts `ONE`
and `TWO`.

Add a `Default` impl on `Version` that returns `Version::TWO`.

In tests that used version 0, instead use `Version::default` because the
test obviously does not care.
2023-09-21 15:02:02 +10:00
Andrew Poelstra a2a4efbe6a
Merge rust-bitcoin/rust-bitcoin#1975: Prepare for using new bech32 release
52f2332383 Remove docs from witness version conversion functions (Tobin C. Harding)
47d6d785cb Remove bip 173/350 test vectors (Tobin C. Harding)
e0eaeaad99 Split ParseError out of Error (Tobin C. Harding)
0f536e86dc Add new UnknownAddressTypeError for parsing address type (Tobin C. Harding)
e2014cba1b Import error variants within dislay impl (Tobin C. Harding)
9d7791fcd6 Remove unnecessary self:: from error import (Tobin C. Harding)
b2e485ed51 Split the address error code out into a separate module (Tobin C. Harding)
f34ca0c52b Move address.rs to address/mod.rs (Tobin C. Harding)

Pull request description:

  In preparation for depending on the recently released version of `rust-bech32` do a bunch of preparatory fixes.

  1. Improve `address` module error handling as we are doing else where at the moment
  2. Remove bip 173 and 350 test vector tests, these are fully covered in bech32
  3. Trim down the docs on `WitnessVersion`

  This PR is the first 8 patches of https://github.com/rust-bitcoin/rust-bitcoin/pull/1951

ACKs for top commit:
  sanket1729:
    ACK 52f2332383
  apoelstra:
    ACK 52f2332383

Tree-SHA512: 67a4ea4020b4e5c9c8396e4195e06dbd1d11335788f9e52f60abbc0b399e37e5dacc9bb7fa4e88221670322fa3c3407ade059d5c709f96e2df97240f4524e08c
2023-09-20 19:54:36 +00:00
Andrew Poelstra 3b9bc9a063
Merge rust-bitcoin/rust-bitcoin#2038: Re-export Opcode
ccdcffe69c Re-export Opcode (Tobin C. Harding)

Pull request description:

  We recently rename `opcodes::All` to `Opcode` but did not re-export it from the crate root. Since it now has a nice clear name we can do so.

  Found while hacking up the `rust-bitcoincore-rpc` dependency upgrade for upcoming release `bitcoin v0.31.0`.

ACKs for top commit:
  apoelstra:
    ACK ccdcffe69c

Tree-SHA512: 8acac90d01f14245f93edc10f3483d8aa9865aca59b6ef42ab744b875558fca8ad74894ad95a83637f0cec7a2a353d74d4d6f7ee5f1d1276cc0fc3dcb6983362
2023-09-20 17:33:53 +00:00
Andrew Poelstra f4c83b4d8e
Merge rust-bitcoin/rust-bitcoin#2078: Script helper to classify bare multisig
acbf23aaa5 Add `is_multisig` helper to Script type (Clark Moody)

Pull request description:

  A new `is_multisig` helper method to classify bare multisig output scripts.

  The form of a valid multisig script is:
  - Pushnum `M`
  - &lt;N&gt; pubkeys
  - Pushnum `N`
  - `OP_CHECKMULTISIG`

  `N` must equal the number of pushed pubkeys, and `M` must be less than or equal to `N`.

  I've tested this against the RPC output of Core at the block level, checking that the total number of multisig outputs matches.

  ```
  Block 350338, 89 multisig
  Block 350340, 29 multisig
  Block 350341, 4 multisig
  Block 350343, 579 multisig
  Block 350344, 48 multisig
  Block 350346, 11 multisig
  Block 350347, 404 multisig
  Block 350350, 127 multisig
  Block 350351, 1 multisig
  Block 350353, 40 multisig
  Block 350356, 13 multisig
  Block 350357, 2 multisig
  Block 350358, 1 multisig
  ```

ACKs for top commit:
  tcharding:
    ACK acbf23aaa5
  apoelstra:
    ACK acbf23aaa5

Tree-SHA512: b8feeaa8725ac63a658897dac3b303fc8b3d56674d796b14569548124928329993bea45482928d9ce85231f1b5837922af8c0a77b2601a92f88b5e2a9394e97f
2023-09-20 16:38:33 +00:00
Andrew Poelstra 6e6847a263
Merge rust-bitcoin/rust-bitcoin#2075: Bugfix: Script::count_sigops should not return a Result
026a55809e Fix: Script::count_sigops parsing should not return a Result (junderw)

Pull request description:

  When implementing some tests for the Transaction PR, I noticed that there were coinbase transactions that would pass Bitcoin Core parsing and fail my code.

  It turns out that the Script parsing for sigops calls `break`  to exit the loop and returns the current n value whenever there is an EarlyEndOfScript error.

  See this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/2073#issuecomment-1722403687 for some links to the relevant source.

ACKs for top commit:
  apoelstra:
    ACK 026a55809e
  tcharding:
    ACK 026a55809e

Tree-SHA512: 57c1b88add5e1c9ef9245fcec0e471db55c2f9b1b0b0f8ebd471f1bede0ca5eeb8492d8c75dea1fd43f1343037df44969c9b9fde26a7de9ac68a26dca899e47f
2023-09-20 15:53:30 +00:00
junderw cd15c746cb
Feature: Instruction can read the script number 2023-09-19 21:08:17 -07:00
Tobin C. Harding 52f2332383
Remove docs from witness version conversion functions
These docs do not add that much value, we do not typically bother
documenting `From` and `TryFrom` implementations because they are super
well known and its obvious from the function signature what is going on.
2023-09-20 13:13:56 +10:00
Tobin C. Harding 47d6d785cb
Remove bip 173/350 test vectors
The BIP-173 and BIP-350 test vectors are implemented in `rust-bech32`,
no need to duplicate those tests here.
2023-09-20 13:13:56 +10:00
Tobin C. Harding e0eaeaad99
Split ParseError out of Error
The `address::Error` is module level general, we can make the code
easier to maintain and easier to stabalize by splitting the parse error
out of the general error.

Create a `ParseError` that is returned by `FromStr for Address`. Remove
the now unused variants from the general `address::Error`.
2023-09-20 13:13:56 +10:00
Tobin C. Harding 0f536e86dc
Add new UnknownAddressTypeError for parsing address type
There is no need to use the general `address::Error` when parsing an
address type, there is only one error path.
2023-09-20 13:13:56 +10:00
Tobin C. Harding e2014cba1b
Import error variants within dislay impl
In an effort to reduce the number of lines of code import the error
variants locally within the `Display` impl on `Error`.

Refactor only, no logic changes.
2023-09-20 13:13:55 +10:00
Tobin C. Harding 9d7791fcd6
Remove unnecessary self:: from error import
`Error` is in this file, no need for `self::Error`.
2023-09-20 13:13:55 +10:00
Tobin C. Harding b2e485ed51
Split the address error code out into a separate module
Split the error code out of `address/mod.rs` and into
`address/error.rs`. Code move only, no changes other than to
imports/exports etc. to make it build.
2023-09-20 13:13:55 +10:00
Tobin C. Harding f34ca0c52b
Move address.rs to address/mod.rs
In preparation for splitting out the error code move `address.rs` to
`address/mod.rs`.

File move only, no other changes.
2023-09-20 13:13:55 +10:00
Clark Moody acbf23aaa5
Add `is_multisig` helper to Script type 2023-09-19 21:37:14 -05:00
junderw 026a55809e
Fix: Script::count_sigops parsing should not return a Result 2023-09-19 00:33:54 -07:00
Subhradeep Chakraborty 71a5fe2b54 Customize Debug implementation of absolute::LockTime 2023-09-15 22:59:26 +05:30
Riccardo Casatta 3dc75b5071
Merge rust-bitcoin/rust-bitcoin#2041: Fix deprecation notice
a0a3d4728a Fix deprecation notice (Tobin C. Harding)

Pull request description:

  Recently we deprecated the `segwit_signature_hash` function but during development the deprecation notice got stale.

  Fix deprecation notice to use the actual function names.

ACKs for top commit:
  RCasatta:
    ACK a0a3d4728a
  apoelstra:
    ACK a0a3d4728a

Tree-SHA512: d84941b605c5bc6ceab75cd60eb820c1d2c16fcd1431dc3927dc22d79886d3de26fd796fab92d97e7f8d567eab0b5a1987303107720524e7b648b1168541a2ed
2023-09-12 09:01:02 +02:00
junderw c98106ef2e
Fix: IndexOutOfBoundsError Display impl has values in wrong order 2023-09-11 11:25:21 -07:00
Andrew Poelstra 5a9d70757d
Merge rust-bitcoin/rust-bitcoin#2025: Split witness version errors up
7309c7749a Split witness version errors up (Tobin C. Harding)
40db2f5ed6 witness_version: Remove rustdocs from TryFrom imlps (Tobin C. Harding)
3397ff9910 witness_version: Use Self in error From impl (Tobin C. Harding)

Pull request description:

  Done as part of the push to have small specific errors instead of large general ones.

  Split the `witness_version::Error` up into small specific errors.

  The first two patches are preparatory clean up.

ACKs for top commit:
  stevenroose:
    ACK 7309c7749a
  apoelstra:
    ACK 7309c7749a

Tree-SHA512: 9e8b4bc5db3435c88aa6de9d92f668146b20b292c3609e2d4415ff0c32a0e3923bbe765333e0d7c255c326d65680015fc9cdf3e4a994727f4d0273dc396314df
2023-09-10 15:03:33 +00:00
Andrew Poelstra 9677247b25
Merge rust-bitcoin/rust-bitcoin#2022: Make Encodable/Decodable usage uniform
4f43965ade Make Encodable/Decodable usage uniform (Tobin C. Harding)

Pull request description:

  One encodes to a writer and decodes from a reader, most of the time in the consensus `Encodable`/`Decodable` traits we use generic `R`/`W` and variable `r`/`w` but there are other places that use other characters.

  While touching these lines note also that there are a bunch of unneeded `mut`s, I'm not sure why since usually between the compiler and the linter `mut` is handled correctly.

  Make implementations of `Encodable` and `Decodable` uniform by:
  - Use R/W and r/w for trait and variable name
  - Remove unneeded mut

  (This is split out of #1891 to assist review.)

ACKs for top commit:
  apoelstra:
    ACK 4f43965ade
  stevenroose:
    ACK 4f43965ade

Tree-SHA512: 256d080b32e60a7cabf6db4945a18d7ff5b296cb848712238e33f9b1ff3cabbe7d76723fed61a04e4b2a6c9423f12c32ec10e3ebb633761122add50e6a6cc7c9
2023-09-10 14:08:03 +00:00
Andrew Poelstra f8d7bcfce2
Merge rust-bitcoin/rust-bitcoin#2040: Use weight for block size function
a68c42e113 Remove test from Transaction test names (yancy)
f796d6fef9 Use Weight type for scaled_size (yancy)
e746341f33 Add tests for scaled_size (yancy)
97b7a2dee9 Use Weight type for block base_size (yancy)
9536a9947c Add base_size test (yancy)

Pull request description:

  Use Weight type for `base_size` in Transaction.  Also a small re-factor to remove `test_` and `_tests` from the testname for transaction tests.

ACKs for top commit:
  apoelstra:
    ACK a68c42e113
  tcharding:
    ACK a68c42e113

Tree-SHA512: f4ab54143cbd9b1439912390f1e0857069a32b715477a4bc08692c5e32860a7090c95a92f78b118b17c1295c45a3bbdd209ba1d68c3a934341269235040e6911
2023-09-06 14:26:09 +00:00
Andrew Poelstra 70223027b9
Merge rust-bitcoin/rust-bitcoin#2035: Improve witness display
de95bf52cb Use checked_sub (Tobin C. Harding)

Pull request description:

  Recently we "if" guarded subtraction manually using `> 0`, we can better convey the meaning by using `checked_sub` and pattern match on the option.

  Refactor only, no logic changes.

ACKs for top commit:
  RCasatta:
    utACK de95bf52cb
  apoelstra:
    ACK de95bf52cb

Tree-SHA512: 2514cc2d8af89158e5e5e5a866f3fadb4927ba07dfb4e077fd16a98acf638588bee5ce03e2dc73fbda0b5064c30d8773d3be583c03c2a5336b8738c212a9776f
2023-09-05 13:23:38 +00:00
yancy a68c42e113 Remove test from Transaction test names 2023-09-04 15:26:44 +02:00
yancy f796d6fef9 Use Weight type for scaled_size 2023-09-04 15:26:44 +02:00
yancy e746341f33 Add tests for scaled_size 2023-09-04 15:26:44 +02:00
yancy 97b7a2dee9 Use Weight type for block base_size 2023-09-04 15:26:44 +02:00
yancy 9536a9947c Add base_size test 2023-09-04 15:26:44 +02:00
Tobin C. Harding b2a7d7023c
Rename XpubIdentifier to XKeyIdentifier
The BIP-32 extended key identifier is used to identify xpubs and xprivs,
we can rename it to show this.
2023-09-04 02:15:21 +10:00
Tobin C. Harding ffd2466ad1
Move XpubIdentifier to the bip32 module
As we have recently been doing, move the declaration of the hash type to
where it is used.

Move the `XpubIdentifier` hash declaration to the `bip32` module.

This is an API breaking change.
2023-09-04 02:13:55 +10:00
junderw 18e2854a42
Update base64 usage to 0.21.3 2023-08-30 22:47:56 -07:00
Tobin C. Harding bc398204bf
Remove redundant segwit version from function names
A P2TR output does not need to be clarified with version 1, it is
implicit. As with p2wpkh/p2wsh and version 0.

Remove redundant version identifiers from function names, deprecating
the originals.
2023-08-31 13:23:46 +10:00
Tobin C. Harding f17bb0d18f
Remove unnecessary reference
`T` is a generic that implements`AsRef<PushBytes>`, it should not be a
reference. This is inline with other usages of `AsRef<PushBytes>` for
example in `Builder::push_slice`.
2023-08-31 13:21:06 +10:00
Tobin C. Harding 4f43965ade
Make Encodable/Decodable usage uniform
One encodes to a writer and decodes from a reader, most of the time in
the consensus `Encodable`/`Decodable` traits we use generic `R`/`W` and
variable `r`/`w` but there are other places that use other characters.

While touching these lines note also that there are a bunch of unneeded
`mut`s, I'm not sure why since usually between the compiler and the
linter `mut` is handled correctly.

Make implementations of `Encodable` and `Decodable` uniform by:
- Use R/W and r/w for trait and variable name
- Remove unneeded mut
2023-08-31 13:14:02 +10:00
Tobin C. Harding 7309c7749a
Split witness version errors up
Done as part of the push to have small specific errors instead of large
general ones.

Split the `witness_version::Error` up into small specific errors.
2023-08-31 13:12:35 +10:00
Tobin C. Harding 40db2f5ed6
witness_version: Remove rustdocs from TryFrom imlps
These docs do not add much value. Done in preparation for splitting up
the `witness_version::Error` type into specific errors.
2023-08-31 13:12:35 +10:00
Tobin C. Harding 3397ff9910
witness_version: Use Self in error From impl
We can use self in error `From` impls with no loss of clarity and it is
more maintainable.
2023-08-31 13:12:35 +10:00
Tobin C. Harding de95bf52cb
Use checked_sub
Recently we "if" guarded subtraction manually using `> 0`, we can better
convey the meaning by using `checked_sub` and pattern match on the
option.

Refactor only, no logic changes.
2023-08-31 13:11:11 +10:00
Tobin C. Harding ccdcffe69c
Re-export Opcode
We recently rename `opcodes::All` to `Opcode` but did not re-export it
from the crate root. Since it now has a nice clear name we can do so.
2023-08-31 13:10:19 +10:00
Tobin C. Harding a0a3d4728a
Fix deprecation notice
Recently we deprecated the `segwit_signature_hash` function but during
development the deprecation notice got stale.

Fix deprecation notice to use the actual function names.
2023-08-31 13:06:03 +10:00
Riccardo Casatta feafac3c65
Merge rust-bitcoin/rust-bitcoin#1999: Fix witness display bug
84614d9997 Unit test debug print of witness with empty instruction (Tobin C. Harding)
e96be5ee6e Fix Witness debug display bug (Tobin C. Harding)

Pull request description:

  When we introduce a custom `Debug` implementation for the `Witness` we introduced a bug that causes code to panic if the witness contains an empty instruction.

  The bug can be verified by putting patch 2 first or by running `cargo run --example sighash` on master.

ACKs for top commit:
  apoelstra:
    ACK 84614d9997
  RCasatta:
    ACK 84614d9997

Tree-SHA512: d51891206ab15f74dda07eb29ff3f6c69dc3f983a5a5abb55685688548481a19f7c1d33aa1183a89c553ff2bc86cf41057c2bae33d75e8a7f3b801056775bf9e
2023-08-30 11:48:27 +02:00
Andrew Poelstra 7fd9b89e82
Merge rust-bitcoin/rust-bitcoin#2010: Use weight type for stripped_size
55e94b5dea Remove test from test names for Weight type (yancy)
142dde64c3 Use Weight type for stripped_size (yancy)
cb76f3ec43 Add scale_by_witness_factor to Weight type (yancy)
38c9e9947e Add witness scale factor to the Weight type (yancy)
77552987ab Add from_wu_usize to Weight type (yancy)
1a88c887f5 Rename strippedsize to stripped_size (yancy)
3369257c75 Fix grammar (yancy)

Pull request description:

  Return Weight type for the strippedize function.

ACKs for top commit:
  apoelstra:
    ACK 55e94b5dea
  tcharding:
    ACK 55e94b5dea

Tree-SHA512: ad3e4bc29380f22e20a6302c1b24c201c772be759c655c62ba4717840a01fcaa36f0f8442c9a3ba71c6400d6af47a9a815e6d90877b5f14c6883fb950b9669fd
2023-08-26 16:12:43 +00:00
Andrew Poelstra eef5cc3b92
Merge rust-bitcoin/rust-bitcoin#1978: Wrap the `bitcoinconsensus` error
29a4f9b114 Wrap the bitcoinconsensus error (Tobin C. Harding)

Pull request description:

  Currently the `bitcoinconsensus` error is part of the public API. This hinders maintainability because changes to the verison of `bitcoinconsensus` force a re-release in `rust-bitcoin`. This is an unnecessary maintenance burden, we can wrap the error instead.

ACKs for top commit:
  apoelstra:
    ACK 29a4f9b114
  sanket1729:
    utACK 29a4f9b114

Tree-SHA512: 36bc1b0ad5f5675d79eea2409844a839d862997c256e301c53c5f1af547edc9a0b83e586bd70e1b8853722cd7ef279e7515e09fbe942660f8049090d1be39d3a
2023-08-25 15:06:42 +00:00
Andrew Poelstra 4dc71b0de7
Merge rust-bitcoin/rust-bitcoin#2020: Add version bytes consts
f18f684ad2 Add version bytes consts (Tobin C. Harding)

Pull request description:

  BIP-32 defines 4 4-byte consts used as version bytes; currently we are hardcoding the version bytes in multiple places.

  Add BIP-32 version bytes consts and use them throughout the module.

ACKs for top commit:
  apoelstra:
    ACK f18f684ad2
  RCasatta:
    utACK f18f684ad2

Tree-SHA512: 50bf2d26f0f8e3528642ffcc621c03b82f536994deb808a6c84225676b4b8849db8e0d16e46f3819e0810296a422b31cf90d0595739910afdb92fb768ef7696e
2023-08-25 14:57:59 +00:00
Andrew Poelstra a458461a6b
Merge rust-bitcoin/rust-bitcoin#2004: psbt: Add IndexOutOfBounds error
66d5800ac0 psbt: Add IndexOutOfBounds error (Tobin C. Harding)

Pull request description:

  We currently have a bunch of functions that are infallible if the `index` argument is within-bounds however we return a `SignError`, this obfuscates the code.

  Add an `IndexOutOfBoundsError`. While we are at it make it an enum so users can differentiate between which vector the out of bounds access was attempted against.

ACKs for top commit:
  sanket1729:
    utACK 66d5800ac0. This is a clean improvement over existing code.
  apoelstra:
    ACK 66d5800ac0

Tree-SHA512: fa8a24990d1dcdab0c9b019fb2387b5a518b02d0a65715f0ab62519894b19c0c74750d3dcdc928626fa68b146038b907d79de3ba9712c9287db8fa64693ebc11
2023-08-25 13:58:06 +00:00
Andrew Poelstra 082bd03120
Merge rust-bitcoin/rust-bitcoin#2019: Rename xpub and xpriv types
be05f9d852 Rename xpub and xpriv types (Tobin C. Harding)

Pull request description:

  The BIP-32 extended public key and extended private key exist in the Bitcoin vernacular as xpub and xpriv. We can use these terms with no loss of clarity.

  Rename our current BIP-32 types

  - `ExtendedPubKey` to `Xpub`
  - `ExtendedPrivKey` to `Xpriv`

  This patch is a mechanical search-and-replace, followed by running the formatter, no other manual changes.

ACKs for top commit:
  apoelstra:
    ACK be05f9d852
  sanket1729:
    ACK be05f9d852

Tree-SHA512: 49925688783c3f37a9b92a9767a0df095323a3fa51f3d672a0b5dd1d8bca86f7facbcc33921274bc147b369de09042c4850b08c31e63f71110903435daa6c00c
2023-08-25 13:41:50 +00:00
Andrew Poelstra bd05e3f309
Merge rust-bitcoin/rust-bitcoin#2029: Fix clippy warnings
724be17394 Remove useless usage of vec! macro (Tobin C. Harding)
e84ca292d9 Clear incorrect implementation of clone warning (Tobin C. Harding)

Pull request description:

  A new version `clippy 0.1.72 (5680fa1 2023-08-23)` just came out and we get a two new warnings. Fix them up.

ACKs for top commit:
  apoelstra:
    ACK 724be17394

Tree-SHA512: f82f026773f8738d8d89710b36b979850e0e33c4d1afb4f78d2d4e957b37dd850ef9e83c9e25197dac981c7b49c448e49078777261749567303f1aac282b3d33
2023-08-25 13:27:12 +00:00
Andrew Poelstra 9dbefc6f85
Merge rust-bitcoin/rust-bitcoin#1988: Export all hash types
7bbdd9b2af Export all hash types (Tobin C. Harding)

Pull request description:

  During the 0.30.0 release we removed the re-exports of hash types. This upset some folk and since the aim of our re-exports is not exactly clean as well as the fact that the public API surface is not yet fixed just re-export all the hash types at the crate root again.

  This is #1792 but does not use a wildcard and also grabs the other hashes that we recently moved.

ACKs for top commit:
  apoelstra:
    ACK 7bbdd9b2af
  sanket1729:
    ACK 7bbdd9b2af

Tree-SHA512: addfb617fae2fce12eb9d198ffeb1b0c99792dfd757222c21c62bd4b408432de5dc93c42da7d886b43c29b2c34bd318573e813f567a29080fc264ee7beba0f70
2023-08-25 13:02:19 +00:00
yancy 55e94b5dea Remove test from test names for Weight type 2023-08-25 10:28:34 +02:00
yancy 142dde64c3 Use Weight type for stripped_size 2023-08-25 10:28:34 +02:00
yancy cb76f3ec43 Add scale_by_witness_factor to Weight type 2023-08-25 10:28:34 +02:00
yancy 38c9e9947e Add witness scale factor to the Weight type 2023-08-25 10:28:05 +02:00
yancy 77552987ab Add from_wu_usize to Weight type 2023-08-25 10:03:59 +02:00
yancy 1a88c887f5 Rename strippedsize to stripped_size 2023-08-25 10:03:39 +02:00
Tobin C. Harding 724be17394
Remove useless usage of vec! macro
Clippy emits a bunch of warnings of form:

  warning: useless use of `vec!`

As suggested, remove the vec and just use an array.
2023-08-25 12:30:04 +10:00
Andrew Poelstra cdbd4be4b1
Merge rust-bitcoin/rust-bitcoin#1917: hashes: Release `v0.13.0`
53f68383b7 hashes: Bump version to 0.13.0 (Tobin C. Harding)

Pull request description:

  In preparation for `hashes` release, bump the version. Depend on new version in `rust-bitcoin`.

  Note the `bitcoin-private` addition to the lock files is because we temporarily have two `bitcoin_hashes` dependencies and the secp one (v.0.12.0) depends on `bitcoin-private`.

ACKs for top commit:
  sanket1729:
    utACK 53f68383b7 . I am not sure about the exact nature of release dance between various crates in rust-bitcoin. This code and changelog entries looks good.
  apoelstra:
    ACK 53f68383b7

Tree-SHA512: a1933bcda1fa9a06c96a4c7079ff49f531e4f366373e98700446cecdf1eed5a104d4d4aa737202ec426cb1f1edf88eff5eee80df9f0cc3a9779c051a55f847b5
2023-08-24 17:22:10 +00:00
Tobin C. Harding 53f68383b7
hashes: Bump version to 0.13.0
Add a changelog entry and bump the version to 0.13.0

Does not include changes to `bitcoin` to depend on the new version.
2023-08-24 12:25:42 +10:00
Tobin C. Harding 66d5800ac0
psbt: Add IndexOutOfBounds error
We currently have a bunch of functions that are infallible if the
`index` argument is within-bounds however we return a `SignError`, this
obfuscates the code.

Add an `IndexOutOfBoundsErorr`. While we are at it make it an enum so
users can differentiate between which vector the out of bounds access
was attempted against.
2023-08-24 12:12:04 +10:00
Tobin C. Harding 0419fa278b
Add VarInt from implementations by way of macro
Throughout the codebase we cast values to `u64` when constructing a
`VarInt`. We can make the code marginally cleaner by adding `From<T>`
impls for all unsigned integer types less than or equal to 64 bits.
Also allows us to (possibly unnecessarily) comment the cast in a single
place.
2023-08-24 10:37:53 +10:00
Tobin C. Harding d9533523ac
Remove usage of ThirtyTwoByteHash
The `ThirtyTwoByteHash` trait is defined in `secp256k1` and used in
`hashes` as well as `bitcoin`. This means that we must use the same
version of `hashes` in both `bitcoin` and `secp256k1`. This makes doing
release difficult.

Remove usage of `ThirtyTwoByteHash` and use `Message::from_slice`.
Include TODO above each usage because as soon as we release the new
version of secp we can use the new `Message::from_digest`.

This is step backwards as far as type safety goes and it makes the code
more ugly as well because it uses `expect` but thems the breaks.
2023-08-23 12:21:26 +10:00
Tobin C. Harding f18f684ad2
Add version bytes consts
BIP-32 defines 4 4-byte consts used as version bytes; currently we are
hardcoding the version bytes in multiple places.

Add BIP-32 version bytes consts and use them throughout the module.
2023-08-22 13:49:46 +10:00
Tobin C. Harding be05f9d852
Rename xpub and xpriv types
The BIP-32 extended public key and extended private key exist in the
Bitcoin vernacular as xpub and xpriv. We can use these terms with no
loss of clarity.

Rename our current BIP-32 types

- `ExtendedPubKey` to `Xpub`
- `ExtendedPrivKey` to `Xpriv`

This patch is a mechanical search-and-replace, followed by running the
formatter, no other manual changes.
2023-08-22 13:47:35 +10:00
Andrew Poelstra 5bf2117dc4
Merge rust-bitcoin/rust-bitcoin#1995: Improve segwit signature hash API
4300cf2210 Add p2wpkh and p2wsh signature hash functions (Tobin C. Harding)

Pull request description:

  The word "segwit" refers to segwit v0 and taproot but these functions are version specific. Add `v0` into the function names.

  This is similar to #1994, both based on recent post of mine to bitcoin dev mailing list.

ACKs for top commit:
  stevenroose:
    ACK 4300cf2210
  apoelstra:
    ACK 4300cf2210

Tree-SHA512: 723fc302954514da0fa57a3890b9f62e9d8d1b25289b8db00611d8bc34c5000b9e54943f57b8e94befcaf72633ac078b2ff66a1da0c5bb483cfaa584e3cb6014
2023-08-21 14:53:04 +00:00
yancy 3369257c75 Fix grammar 2023-08-21 15:51:58 +02:00
Tobin C. Harding 752adff9d1
Add method calc_height
Add a private method to the `PartialMerkleTree` to calculate the height.
Enables removal of duplicate code.
2023-08-21 15:34:04 +10:00
Tobin C. Harding 46f5588646
Add unit test for calc_tree_width
Add a unit test for a `PartialMerkleTree` with node counts of 1-7
2023-08-21 15:34:04 +10:00
Andrew Poelstra 672656515e
Merge rust-bitcoin/rust-bitcoin#2001: Add txin base weight
7ec33d29eb refactor: developer doc first (yancy)
5496feb5c1 Add base weight const to TxIn (yancy)

Pull request description:

  Add a base weight const to TxIn.  I also used this const in strippedsize() and scaledsize().  As a different PR, I think strippedsize and scaledsize could return Weight instead of usize.  Also added a small commit to re-arrange commit messages.

ACKs for top commit:
  apoelstra:
    ACK 7ec33d29eb
  tcharding:
    ACK 7ec33d29eb

Tree-SHA512: b20f95605ed664b88df0a5a178d48f15f27d90eb404c9707aef010c4504d7ffd4a3565c217710b9289f87ed2a0724fd8f7cc78a79a58547fe3ee87339c0d74c1
2023-08-18 14:28:38 +00:00
yancy 7ec33d29eb refactor: developer doc first 2023-08-17 10:37:12 +02:00
yancy 5496feb5c1 Add base weight const to TxIn 2023-08-17 10:37:02 +02:00
Tobin C. Harding 84614d9997
Unit test debug print of witness with empty instruction
We recently fixed a bug that causes a panic if a `Witness` contains an
empty instruction. Add a unit test to verify it.
2023-08-17 09:36:11 +10:00
Tobin C. Harding e96be5ee6e
Fix Witness debug display bug
Currently if the witness has zero elements or any of the individual
witnesses is empty we panic. Panic is caused by subtracting 1 from a
zero length.

Check the length is non-zero before subtracting 1, print `[]` if empty.
2023-08-17 09:34:46 +10:00
Tobin C. Harding 7bbdd9b2af
Export all hash types
During the 0.30.0 release we removed the re-exports of hash types. This
upset some folk and since the aim of our re-exports is not exactly clean
as well as the fact that the public API surface is not yet fixed just
re-export all the hash types at the crate root again.

This is 1792 but does not use a wildcard and also grabs the other
hashes that we recently moved.
2023-08-15 14:53:27 +10:00
Tobin C. Harding 4300cf2210
Add p2wpkh and p2wsh signature hash functions
The word "segwit" refers to segwit v0 and taproot but currently we have
`segwit_signature_hash` that is version specific (segwit v0).

- Rename `segwit_encode_signing_data_to` to
  `segwit_v0_encode_signing_data_to`
- Add `p2wpkh_signature_hash` and `p2wsh_signature_hash` functions

We keep the single encode function because the error handling is better
that way.

While we are at it test the bip-143 test vectors against all the
sighash types of wrapped p2wsh.
2023-08-15 11:54:08 +10:00
Andrew Poelstra c276cea66d
Merge rust-bitcoin/rust-bitcoin#1744: Expose XOnlyPublicKey in crate root
1db0b21b84 Expose XonlyPublicKey and TapSighashType in crate root (sanket1729)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 1db0b21b84
  apoelstra:
    ACK 1db0b21b84

Tree-SHA512: c997024e046139d5aa89e16793e582dce6cfe8a06867aa11769c15f39f15159b6207227cd103b72dbb9e9dde7ad4451bfcc32bc04d74143bd2c8344f1ec488d1
2023-08-14 14:56:38 +00:00
sanket1729 1db0b21b84
Expose XonlyPublicKey and TapSighashType in crate root 2023-08-12 16:28:35 -07:00
Andrew Poelstra 19bb55f77e
Merge rust-bitcoin/rust-bitcoin#1991: Eliminate a heap allocation from PartialMerkleTree encoding & decoding
fa10668a35 Eliminate a heap allocation from PartialMerkleTree encoding & decoding (Steven Roose)

Pull request description:

  Just came across this and felt like doing this.

ACKs for top commit:
  apoelstra:
    ACK fa10668a35
  tcharding:
    ACK fa10668a35

Tree-SHA512: 7167c63077851c4c461b33292948d9b09fe21eb45d52ed278ecf884ce15bc8b21c14040fa1eb5f50bfe51c5cde10abc133d0c59be502de139408c0d107ffa7eb
2023-08-11 22:23:02 +00:00
Steven Roose fa10668a35
Eliminate a heap allocation from PartialMerkleTree encoding & decoding
Just came across this and felt like doing this.
2023-08-11 20:51:55 +01:00
Andrew Poelstra f1b3e69f20
Merge rust-bitcoin/rust-bitcoin#1955: Rename `opcodes::All` to `Opcode`
63d0fa0164 Rename All to Opcode (Tobin C. Harding)
881d1b1a9d Move opcode aliases to the all module (Tobin C. Harding)
f3e8dbc392 Remove floating code comment (Tobin C. Harding)
dfd9384d14 opcodes: Fix whitespace (Tobin C. Harding)

Pull request description:

  I may be missing something about the `All` type but it seems it can be re-named to `Opcode` with no loss of clarity.

  The first few patches do some other clean ups, the last patch is the meat and potatoes.

ACKs for top commit:
  apoelstra:
    ACK 63d0fa0164

Tree-SHA512: c1b8be9e0c090d015c2bbfcb7ed6abe998ccada5a1eb0f6966c2a7dc462f8bbc45e6b99bf1387c736d2fca21a993a2c7fd5844c18b97a2e37ec43da517566ab1
2023-08-11 19:07:22 +00:00
yancy 443a4c5869 Remove pins from test script 2023-08-09 12:48:26 +02:00
Tobin C. Harding 29a4f9b114
Wrap the bitcoinconsensus error
Currently the `bitcoinconsensus` error is part of the public API. This
hinders maintainability because changes to the verison of
`bitcoinconsensus` force a re-release in `rust-bitcoin`. This is
an unnecessary maintenance burden, we can wrap the error instead.
2023-08-08 10:21:05 +10:00
Tobin C. Harding 63d0fa0164
Rename All to Opcode
The `opcodes::All` type can seemingly be re-named to `Opcode` with no
loss of clarity - unless I'm missing something.
2023-08-07 17:08:11 +10:00
Tobin C. Harding 881d1b1a9d
Move opcode aliases to the all module
The opcode aliases are just that, aliases to opcodes so they can live in
the `all` module along with the other opcodes.
2023-08-07 17:07:20 +10:00
Tobin C. Harding f3e8dbc392
Remove floating code comment
This code comment is not commenting anything, remove it.
2023-08-07 17:07:20 +10:00
Tobin C. Harding dfd9384d14
opcodes: Fix whitespace
Fix indentation of macro code and remove unnecessary line of whitespace.
2023-08-07 17:07:20 +10:00
Andrew Poelstra caf53d755d
Merge rust-bitcoin/rust-bitcoin#1968: Add max standard tx weight constant to transaction
2e3006a729 Add max standard tx weight constant to transaction (yancy)

Pull request description:

  Add a constant for the max transaction weight.  Similar to [max block weight](1b009b809b/bitcoin/src/blockdata/weight.rs (L35)).  This value is pulled from core  [here](44b05bf3fe/src/policy/policy.h (L27))

ACKs for top commit:
  apoelstra:
    ACK 2e3006a729
  sanket1729:
    ACK 2e3006a729

Tree-SHA512: 1583695f43387538f948be85ded7ff9a4bf9778169acb958debcbe1572a6dc8bfcd26ddfb8dbe0c030c98ab1f8a66d239a5bc663bf65ec3376a46d5f71e90894
2023-08-02 22:11:48 +00:00
Andrew Poelstra 49fa879b19
Merge rust-bitcoin/rust-bitcoin#1947: Add absolute fee rate convenience functions to `FeeRate`
9eff2f2f5e fee_rate: Add public absolute weight convenience functions (Tobin C. Harding)
f00e93bdcd Fix typos in rustdoc (Tobin C. Harding)
f3412325ea weight: Make docs uniform and terse (Tobin C. Harding)

Pull request description:

  - Patch 1 is docs cleanup
  - Patch 2 adds two functions to `FeeRate`

  From the commit log of patch 2:

      Calculating the absolute fee from a fee rate can currently be achieved
      by creating a `Weight` object and using
      `FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
      can add two public convenience functions that make discovery of the
      functionality easier.

      Add two functions for calculating the absolute fee by multiplying by
      weight units (`Weight`) and virtual bytes (by first converting to weight
      units).

  This seems like an obvious thing so I'm inclined to think that Kixunil left it out for a reason. (Mentioning you here Kix so even if this merges you'll see it in notifications later on.)

ACKs for top commit:
  sanket1729:
    utACK 9eff2f2f5e
  apoelstra:
    ACK 9eff2f2f5e

Tree-SHA512: 5b3997b721b7d7225bcf0e8a4a3efb6f207393541dbbcef533135af06a4d9c95210d450bb2322fd65a727e4560b29f0b20fb96c154d019fd4e745506213abc1c
2023-08-02 16:43:28 +00:00
Riccardo Casatta ff5fa41975
Merge rust-bitcoin/rust-bitcoin#1933: Fix links in docs
154552e334 docs: Do not link to std::option::Option (Tobin C. Harding)
24843468c3 Remove rustdocs links to serde (Tobin C. Harding)

Pull request description:

  Two minor patches to fix up docs links. These were originally done as part of #1880 but are unrelated so pushing them up separately.

ACKs for top commit:
  apoelstra:
    ACK 154552e334
  RCasatta:
    utACK 154552e334

Tree-SHA512: e45e1538c66b59d63a66898896927bb6c1336fb4c8515bb9e2204c8035870ef8e4a6fd32dfc83db2938afda67feb27c48989e382410f9e7ea7a967132941c720
2023-08-02 16:37:13 +02:00
Andrew Poelstra f99459522d
Merge rust-bitcoin/rust-bitcoin#1914: Move type definitions of pubkey/script hash types
27b3c1e0e6 Improve the ScriptHash and WScriptHash types (Tobin C. Harding)
2197f1377f Improve PubkeyHash and WPubkeyHash (Tobin C. Harding)

Pull request description:

  Total re-write since review. Now this PR moves the hash type definitions out of `hash_types`. Please see https://github.com/rust-bitcoin/rust-bitcoin/issues/1909#issuecomment-1603634440 for more.

  No longer adds unit tests.

  Fix: #1909

ACKs for top commit:
  apoelstra:
    ACK 27b3c1e0e6

Tree-SHA512: 216b9bed05d1a4a4fc493262664ceb5d60f9c30685b63d6f6675d21a7bf811053320a002165487b29599c52f345057d9c92babb0fc1ccd4628671ec468c804f9
2023-08-02 13:57:39 +00:00
Andrew Poelstra c042b66931
Merge rust-bitcoin/rust-bitcoin#1957: Remove deprecated code
50ada8298f Move EncodeSigningDataResult to sighash module (Tobin C. Harding)
1b7dc51ccb Remove deprecated code (Tobin C. Harding)

Pull request description:

  We only keep deprecated code around for one release so we can now remove code deprecated in v0.30.0

  Done in preparation as we gear up for v0.31.0 release.

ACKs for top commit:
  apoelstra:
    ACK 50ada8298f
  sanket1729:
    ACK 50ada8298f

Tree-SHA512: 40769258605563e2e12a6118306655fc9a012ae1f86509fca757ca411f0cef74480b7bb7b0db147f30a7d362b8494a077d5ec04f719351661ceb5a0697a5369d
2023-08-02 13:40:50 +00:00
Andrew Poelstra 48be5372d6
Merge rust-bitcoin/rust-bitcoin#1970: Return Weight in the TxIn/TxOut weight() methods
40c37adaf3 Return Weight in the TxIn/TxOut weight() methods (Daniela Brozzoni)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 40c37adaf3
  apoelstra:
    ACK 40c37adaf3

Tree-SHA512: 24e8962626deb016856d9b47f1c196eb2ca9923cd90b21304de7c2e0cc3d41596ad7d5373fd05f1664771f5b72b4070ca60a4845012eb5711a9fad7f672dfc42
2023-08-02 13:34:52 +00:00
Andrew Poelstra 205544b6aa
Merge rust-bitcoin/rust-bitcoin#1862: crypto: Overhaul the errors
3c0bb63423 Do trivial rustdoc improvements (Tobin C. Harding)
3225aa9556 Use defensive documentation (Tobin C. Harding)
80d5d6665a crypto: key: Move error code to the bottom of the file (Tobin C. Harding)
fe3b1e1140 Move From for Error impl (Tobin C. Harding)
5f8e0ad67e Fix docs on error type (Tobin C. Harding)
f23155aa16 Do not capitalize error messages (Tobin C. Harding)
ae07786c27 Add InvalidSighashTypeError (Tobin C. Harding)
baba0fde57 Put NonStandardSighashTypeError inside ecdsa::Error variant (Tobin C. Harding)
6c9d9d9c36 Improve error display imlps (Tobin C. Harding)
22c7aa8808 Rename non standard sighash error type (Tobin C. Harding)

Pull request description:

  EDIT: The commit hashes below are stale but the text is valid still.

  In an effort to "perfect" our error handling, overhaul the error handling in the `crypto` module.

  The aim is to do a small chunk so we can bikeshed on it then I can apply the learnings to the rest of the codebase.

  Its all pretty trivial except:

  - commit `4c180277 Put NonStandardSighashTypeError inside ecdsa::Error variant`
  - comimt `5a196535 Add InvalidSighashTypeError`
  - commit `05772ade Use defensive documentation`

  Particularly the last one might be incorrect/controversial.

  Also, please take the time to check the overall state of error code in the `crypto` module on this branch in case there is anything else we want to do.

  Thanks

ACKs for top commit:
  apoelstra:
    ACK 3c0bb63423

Tree-SHA512: 7e5f8590aec5826098d4d8d33351a41b10c42b6379ff86e5b889e73271b71921fc3ca9525baa5da53e07fa2e961e710393694e04658a8243799950b4604caf43
2023-08-02 13:18:19 +00:00
yancy 2e3006a729 Add max standard tx weight constant to transaction 2023-08-02 10:51:55 +02:00
Tobin C. Harding 27b3c1e0e6
Improve the ScriptHash and WScriptHash types
Improve the script hash types by doing:

- Define the types in the `crypto::script` module
- Put the From impls directly below the type definitions

Keep the current crate level re-export so this does not impact the
public API _if_ people are using the re-export but is still a breaking
change.
2023-08-02 11:28:14 +10:00
Tobin C. Harding 2197f1377f
Improve PubkeyHash and WPubkeyHash
Improve the pubkey hash types by doing:

- Define the types in the `crypto::key` module
- Add From<&PublicKey> impl for `PubkeyHash`

Keep the current crate level re-export so this does not impact the
public API _if_ people are using the re-export but is still a breaking
change.
2023-08-02 11:27:55 +10:00
Tobin C. Harding 9eff2f2f5e
fee_rate: Add public absolute weight convenience functions
Calculating the absolute fee from a fee rate can currently be achieved
by creating a `Weight` object and using
`FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
can add two public convenience functions that make discovery of the
functionality easier.

Add two functions for calculating the absolute fee by multiplying by
weight units (`Weight`) and virtual bytes (by first converting to weight
units).
2023-08-02 09:10:49 +10:00
Tobin C. Harding f00e93bdcd
Fix typos in rustdoc 2023-08-02 08:29:00 +10:00
Daniela Brozzoni 40c37adaf3
Return Weight in the TxIn/TxOut weight() methods 2023-08-01 16:51:41 +02:00
Tobin C. Harding d4e8f49fc3
Move p2p::constants::Network to crate root
The `Network` type is not a p2p construct, it is more general, used
throughout the codebase to define _which_ Bitcoin network we are
operating on.
2023-08-01 16:46:59 +10:00
Tobin C. Harding 0f78943ef0
Move p2p::constants::Magic to p2p module
In preparation for removing the `p2p::constants` module; move the
`p2p::constants::Magic` type to the `p2p` module.
2023-08-01 16:42:05 +10:00
Tobin C. Harding d9d5a4ed4f
Move p2p::constants::ServiceFlags to p2p module
The `ServiceFlags` type is used by the p2p layer. It can live in the
`mod.rs` file of the `p2p` module. Done in preparation for removing the
`p2p::constants` module.

This is a straight code move, the `ServiceFlags` replaces the
current re-export.
2023-08-01 16:36:12 +10:00
Tobin C. Harding 99d8ae1173
Improve rustdocs on PROTOCOL_VERSION 2023-08-01 16:36:12 +10:00
Tobin C. Harding 4330722d62
Move p2p::constants::PROTOCOL_VERSION to p2p module
The `PROTOCOL_VERSION` const is a p2p layer constant. It can live in the
`mod.rs` file of the `p2p` module.

This is a straight code move, the `PROTOCOL_VERSION` replaces the
current re-export.
2023-08-01 16:36:12 +10:00
Tobin C. Harding 1bac1fd518
Rename the network module to p2p
The `network` module deals with data types and logic related to
internetworking bitcoind nodes, this is commonly referred to as the p2p
layer.

Rename the `network` module to `p2p` and fix all the paths.
2023-08-01 16:36:12 +10:00
Tobin C. Harding dac97072a5
Add BorrowMut to prelude
We already have `Borrow`, add `BorrowMut`.
2023-08-01 16:36:12 +10:00
Tobin C. Harding 3c0bb63423
Do trivial rustdoc improvements 2023-08-01 16:30:21 +10:00
Tobin C. Harding 3225aa9556
Use defensive documentation
Only commit in the docs and error messages to what we _really_ know.

In an attempt to reduce the likelyhood of the code going stale only
commit to what is guaranteed - that we have an error from a module.

This does arguably reduce the amount of context around the error.
2023-08-01 16:30:17 +10:00
Tobin C. Harding 80d5d6665a
crypto: key: Move error code to the bottom of the file
Error code is boring, put it at the bottom of the file.

Refactor only, no logic changes.
2023-08-01 16:28:28 +10:00
Tobin C. Harding fe3b1e1140
Move From for Error impl
Move the From impl to be below the other code for the error.

Refactor only, no logic changes.
2023-08-01 16:25:31 +10:00
Tobin C. Harding 5f8e0ad67e
Fix docs on error type
Make the rustdoc arguable clearer, this is a sig error.
2023-08-01 16:25:31 +10:00
Tobin C. Harding f23155aa16
Do not capitalize error messages
As per convention; do not capitalize error messages.
2023-08-01 16:25:24 +10:00
Tobin C. Harding ae07786c27
Add InvalidSighashTypeError
As we do for `NonStandardSighashErrorType` add an error struct for
invalid sighash type, used by the `taproot` module instead of returning
a generic error enum with loads of unused variants.
2023-08-01 16:18:45 +10:00
Tobin C. Harding baba0fde57
Put NonStandardSighashTypeError inside ecdsa::Error variant
As per convention; put the error type inside a variant and delegate to
it instead of carrying an integer around.
2023-08-01 16:18:45 +10:00
Tobin C. Harding 6c9d9d9c36
Improve error display imlps
Improve the `Error` `Display` impls by doing:

- Be more terse by importing the error enum's variants.
- Do not use capital letters for error messages.
2023-08-01 16:18:44 +10:00
Tobin C. Harding 22c7aa8808
Rename non standard sighash error type
Error types conventionally include `Error` as a suffix.

Rename `NonStandardSighashType` to `NonStandardSighashTypeError`.

While we are at it make the inner type private to the crate, there is no
need to leak the inner values type.
2023-08-01 15:55:45 +10:00
Andrew Poelstra cdf3e30b9d
Merge rust-bitcoin/rust-bitcoin#1964: script: Move some inspector methods from ScriptBuf to Script
07041d584d Apply rustfmt (The rustfmt Tyranny)
dada6d65b7 script: Move some inspector methods from ScriptBuf to Script (Steven Roose)

Pull request description:

  Noticed that these methods belong in Script.

ACKs for top commit:
  tcharding:
    ACK 07041d584d
  sanket1729:
    ACK 07041d584d.
  apoelstra:
    ACK 07041d584d

Tree-SHA512: cdcbdf22f0457123205621ec2834164c4598be1e5b221cf859d60e88110b19f8c1e484e86f60653af237e9c2acbcdbe5d2b4c98ccf239924386639c4ba6222f7
2023-07-31 14:54:25 +00:00
The rustfmt Tyranny 07041d584d Apply rustfmt 2023-07-29 20:52:16 +01:00
Steven Roose dada6d65b7
script: Move some inspector methods from ScriptBuf to Script 2023-07-27 23:50:20 +01:00
Tobin C. Harding a70b1b9c6c
Use standard set of derives on all error types
As part of an ongoing effort to make our error types stable and useful
add a stand set of derives to all error types in the library.

    `#[derive(Debug, Clone, PartialEq, Eq)]`

Add `Copy` if possible and the error type does not include
`#[non_exhaustive]`.

If an error type includes `io::Error` it only gets `#[derive(Debug)]`.
2023-07-28 06:15:49 +10:00
Tobin C. Harding 2867acfffa
Add non_exhaustive to SignError
Errors that may have variants added/removed should have `non_exhaustive`
on them.
2023-07-28 06:13:33 +10:00
Andrew Poelstra 04976eddcf
Merge rust-bitcoin/rust-bitcoin#1833: Use new `hex-conservative` crate
bb8bd16302 internals: Remove hex module (Tobin C. Harding)
2268b44911 Depend on hex-conservative (Tobin C. Harding)
db50509cd3 Add usage docs to the "core2" feature (Tobin C. Harding)

Pull request description:

  Use the newly released `hex-conservative` crate, by doing the following:

  - Depend on `hex-conservative` in `bitcoin` and `hashes`
  - Re-export `hex-conservative` as `hex` from both crate roots.
  - Remove all the old hex code from `hashes`
  - Remove all the old hex code from `internals`
  - Remove the now unused `internals::prelude`
  - Fix all the import statements (makes up the bulk of the lines changes in this patch)

ACKs for top commit:
  apoelstra:
    ACK bb8bd16302
  sanket1729:
    utACK bb8bd16302

Tree-SHA512: ec83b3941cae6f32272471779f28461bb04959a3f6a126a68bbf2c748d83ff9518ff8932d9e937a6f389c10028bf3eb58c6b6d71ea066924dd7a34faaec7a087
2023-07-27 16:27:01 +00:00
Andrew Poelstra 2b50859753
Merge rust-bitcoin/rust-bitcoin#1958: Fill in deprecated since NEXT-RELEASE placeholder
1edc5f4098 Fill in deprecated since NEXT-RELEASE placeholder (Tobin C. Harding)

Pull request description:

  As we gear up for the v0.31.0 release we can fill in the NEXT-RELEASE placeholders.

ACKs for top commit:
  sanket1729:
    utACK 1edc5f4098
  apoelstra:
    ACK 1edc5f4098

Tree-SHA512: 61a78e9df0d563a1ebefef3d6c3447b767c5d878f05b45e9a20aecdcef897cc9d8c40d499882a7b8d74d5e99b25a256946c39edf2f05abab370cc0223346f66b
2023-07-27 13:14:03 +00:00
Riccardo Casatta 0e6341d4c9
Merge rust-bitcoin/rust-bitcoin#1954: Avoid vector allocation in `RawNetworkMessage` encoding
5c8933001c Avoid serialize inner data in RawNetworkMessage (Riccardo Casatta)
bc66ed82b2 Impl Encodable for NetworkMessage (Riccardo Casatta)
8560baaca2 Make fields of RawNetworkMessage non public (Riccardo Casatta)

Pull request description:

  This PR removes the need to serialize the inner NetworkMessage in the RawNetworkMessage encoding, thus saving memory and reducing allocations.

  To achieve this payload_len and checksum are kept in the RawNetworkMessage and checksum kept in CheckedData, to preserve invariants fields of the struct are made non-public.

ACKs for top commit:
  apoelstra:
    ACK 5c8933001c
  tcharding:
    ACK 5c8933001c

Tree-SHA512: aca3c7ac13d2d71184288f7815449e72c4c04fc617a65effba592592ef4ec50f18b6f83dbff58e9c4237cb1fe8e7af52cd43db9036658bdaf7888c07011e46cc
2023-07-27 08:55:21 +02:00
Tobin C. Harding 50ada8298f
Move EncodeSigningDataResult to sighash module
This type was defined in the `transaction` module because it was
originally used in a function that had been deprecated in favour of
moving the logic to the `sighash` module.

We just removed the deprecated code so we can now move this type to the
`sighash` module where it is used.
2023-07-27 09:21:34 +10:00
Tobin C. Harding 1b7dc51ccb
Remove deprecated code
We only keep deprecated code around for one release so we can now remove
code deprecated in v0.30.0

Done in preparation as we gear up for v0.31.0 release.
2023-07-27 09:20:19 +10:00
Tobin C. Harding 1edc5f4098
Fill in deprecated since NEXT-RELEASE placeholder
As we gear up for the v0.31.0 release we can fill in the NEXT-RELEASE
placeholders.
2023-07-27 07:26:21 +10:00
Riccardo Casatta 5c8933001c
Avoid serialize inner data in RawNetworkMessage
RawNetworkMessage keep the payload_len and its checksum in the struct, thus
is not needed to serialize the inner network message

pub in fields of both RawNetworkMessage and CheckedData are removed so that
invariant are preserved.
2023-07-26 08:34:49 +02:00
Steven Roose 56343bd7b5
Merge pull request #1912 from tcharding/06-20-consensus-validation
Separate out bitcoinconsensus validation code
2023-07-25 11:16:53 -05:00
Riccardo Casatta bc66ed82b2 Impl Encodable for NetworkMessage
Using it in RawNetworkMessage encoding
2023-07-25 14:47:31 +02:00
Riccardo Casatta 8560baaca2 Make fields of RawNetworkMessage non public
provide accessor method and new for downstream libs.
This is done in order to more easily change the struct without impacting
downstream and also in order to add another field while preserving struct
invariant in future commit.
2023-07-25 14:43:13 +02:00
Tobin C. Harding d3460daee7
Rename HashParse error variant to InvalidHash
Recently we changed the inner type of this variant and the name became
stale because it is caused by a from slice constructor not from parsing.
2023-07-24 15:08:07 +10:00
Andrew Poelstra f50bb05e99
Merge rust-bitcoin/rust-bitcoin#1949: key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey
ebcce38b4f run cargo fmt (Andrew Poelstra)
b3ac00b8b3 key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey (Steven Roose)

Pull request description:

ACKs for top commit:
  apoelstra:
    ACK ebcce38b4f
  tcharding:
    ACK ebcce38b4f

Tree-SHA512: 9b862f0bb75e74780e935e5292c6a67ae479f699f0ee2f90515f972698dc06af5ea7d1725908fb435cafd6c0bab7263570d780607e507d6127efaea97f2464c8
2023-07-21 15:06:56 +00:00
Tobin C. Harding 2268b44911
Depend on hex-conservative
We have just released the `hex-conservative` crate, we can now use it.

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
Tobin C. Harding db50509cd3
Add usage docs to the "core2" feature
We do not expect users of `rust-bitcoin` to ever activate the "core2"
dependency directly, add a comment explaining that.
2023-07-21 10:45:23 +10:00
Andrew Poelstra ebcce38b4f
run cargo fmt 2023-07-20 17:13:15 +00:00
Steven Roose b3ac00b8b3
key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey 2023-07-20 17:09:37 +01:00
Tobin C. Harding f3412325ea
weight: Make docs uniform and terse
Make the docs use the same form as everywhere else, which also mimics
stdlib docs on integer types.
2023-07-20 09:32:23 +10:00
Andrew Poelstra 894c926f56
Merge rust-bitcoin/rust-bitcoin#1935: Infrastructure fixes
7b402e930c schemars: Add pinning docs (Tobin C. Harding)
0848ab7e25 Fix clippy warnings for embedded build (Tobin C. Harding)
5b1443a91c hashes/embedded: Add script dir and README (Tobin C. Harding)
94732aecbf Add patch section to test crates (Tobin C. Harding)
512d982275 Remove path field from internals dependency (Tobin C. Harding)

Pull request description:

  Do a bunch of infrastructure fixes that either are needed for adding additional crate deps (hex) or updating deps (internals, hashes), or just make the tests more maintainable.

ACKs for top commit:
  apoelstra:
    ACK 7b402e930c
  sanket1729:
    ACK 7b402e930c

Tree-SHA512: 9349bb20225363914acc774cca672a23e6562fb02aea644777c558074d5eeb65289d68a93b5be59a93e9b32167e2494f6599caedc8a0d9cfbee2f94d406edbfc
2023-07-19 16:11:40 +00:00
Riccardo Casatta 63a09649f7
Merge rust-bitcoin/rust-bitcoin#1946: bitcoin/bip32: add DerivationPath::to_u32_vec
6640074d34 bitcoin/bip32: add DerivationPath::to_u32_vec (Marko Bencun)

Pull request description:

  This is useful to pass the keypath to other libraries which expect it to be represented with a list of u32 ints.

  Fixes #1944

ACKs for top commit:
  apoelstra:
    ACK 6640074d34
  RCasatta:
    ACK 6640074d34

Tree-SHA512: c2327716370558dd9d7e0419f898707ba5e56555284ea7ca746c973080061aae53674b41d8fe7c68a00d7c4bec1e4bb53e8991141749a87dfa40febe9f456369
2023-07-18 20:20:03 +02:00
Riccardo Casatta 28f6ad80cb
Merge rust-bitcoin/rust-bitcoin#1942: witness: clean up Debug implementation
e30c492faf witness: clean up Debug implementation (Andrew Poelstra)

Pull request description:

  The previous code seems to have been rebased/iterated on too many times, and had room for significant simplification. By inlining the indentation logic we can eliminate 40 LOC and also clean up the output by removing trailing spaces.

  Fixes #1937

  It is not good form to add unit tests for debug output but you can test this locally with the patch
  ```
  diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs
  index d0b7408c..a2c38af0 100644
  --- a/bitcoin/src/blockdata/witness.rs
  +++ b/bitcoin/src/blockdata/witness.rs
  @@ -619,6 +619,9 @@ mod test {
               "304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b54101")
               ];
           assert_eq!(witness.to_vec(), expected_witness);
  +
  +            println!("{:?}", witness);
  +            panic!();
       }

       #[test]
  ```
  And by sticking `{:#?}` in there to see the alternate output.

ACKs for top commit:
  tcharding:
    tACK e30c492faf
  RCasatta:
    ACK e30c492faf

Tree-SHA512: 0ec07885f5c75f3f34965852cf5b42b63290295d1f56e9fef7d5b3610b8ac8d318cbf8f184da5b8a9ed5b352bb2c0402797b41714cb9d5488e93c2e290340c2a
2023-07-18 20:18:04 +02:00
Andrew Poelstra 9a34f0cf5e
Merge rust-bitcoin/rust-bitcoin#1925: Rename `Script::empty` to `Script::new`
9787ba6c96 Rename Script::empty to Script::new (Tobin C. Harding)

Pull request description:

  The `empty` constructor is mis-named for the following reasons:

  - Non-uniform with `ScriptBuf::new`
  - Non-standard with respect to stdlib which uses `Path::new` and `PathBuf::new` (on which we based the `Scritp`/`ScriptBuf`)

  Rename the function to `new`, put it at the top of the impl block while we are at it.

ACKs for top commit:
  apoelstra:
    ACK 9787ba6c96
  RCasatta:
    ACK 9787ba6c96

Tree-SHA512: 2dee0f61fa9097a48369a0df802ebf238b00ad3e9ed520fbf31affa1cb2a1820cbb910b525be63513e4586acb2aa0b593cecddcad0b6cd894cdac0ba7fcf0871
2023-07-18 16:49:00 +00:00
Marko Bencun 6640074d34
bitcoin/bip32: add DerivationPath::to_u32_vec
This is useful to pass the keypath to other libraries which expect it
to be represented with a list of u32 ints.
2023-07-18 14:33:29 +02:00
Tobin C. Harding dae2b508bc
Separate out bitcoinconsensus validation code
Pull all the code that depends on `bitcoinconsensus` out into a separate
module `consensus::validation`.

Leave transaction testing of bitcoinconsensus code in the transaction
module.
2023-07-18 10:29:51 +10:00
Tobin C. Harding 0cd1af1f56
Remove unused import statement
This import is not used, our CI obviously does not warn for all feature
combinations.
2023-07-18 10:29:50 +10:00
Tobin C. Harding ca38dbd16d
transaction:: Return custom error from verify function
There is not need to return the general `script::Error` from the
transaction verify functions. We can better describe the error path by
returning a custom error.
2023-07-18 10:29:50 +10:00
Tobin C. Harding ca2512f471
script: Return bitcoinconsensus::Error from verify functions
There is no need no nest the `bitcoinconsensus::Error` type within the
`script::Error`, it is the only error type returned by the verify
functions so just return it directly.
2023-07-18 10:29:50 +10:00
Tobin C. Harding 8ed74c4f0d
Add comment to bitcoinconsensus dependency
Add a comment to point users towards the `bitcoinconsensus-std` feature
for std builds.
2023-07-18 10:29:50 +10:00
Tobin C. Harding 0848ab7e25
Fix clippy warnings for embedded build
When building the `embedded` test crate `clippy` emits two warnings
of form:

  warning: unused variable: `foo`

Use `_` instead of a named variable.
2023-07-18 10:27:48 +10:00
Tobin C. Harding 94732aecbf
Add patch section to test crates
In order to keep the embedded and schemacs test crates building when we
update their local transient dependencies we need to use a `patch`
section.

- For `bitcoin/embedded` add `patch` section for `internals`, `hashes`
already has an entry.
- For `hashes/embedded` add `patch` section for `internals`.
- For `hashes/extendend_tests/schemars` add `patch` section for
  `internals`.

FTR for direct local dependencies we use a `path` field when specifying
the dependency.
2023-07-18 10:27:48 +10:00
Tobin C. Harding 512d982275
Remove path field from internals dependency
We use two different methods for specifying local dependencies, `patch`
and also `path`. There does not seem to be a reason why we use both,
lets be uniform. Elect to use `patch` for all local crates.
2023-07-18 10:27:46 +10:00
Andrew Poelstra 922996b032
Merge rust-bitcoin/rust-bitcoin#1938: Rename PartiallySignedTransaction to Psbt
92749d29e4 Rename PartiallySignedTransaction to Psbt (Tobin C. Harding)

Pull request description:

  Last release we added a type alias for `Psbt`, now lets just rename the type and be done with it.

  Includes re-export at the crate root because `bitcoin::Psbt` is clear and obvious.

ACKs for top commit:
  sanket1729:
    ACK 92749d29e4.
  apoelstra:
    ACK 92749d29e4

Tree-SHA512: 2ded728409829709a46acd2a83ce9a91839bce222264b2fca122b346ec4f45a52c3f970eb05001794e2f355ce9391df1a184b57baf24589e8a5c3f77f72f6ec7
2023-07-17 16:35:43 +00:00
Andrew Poelstra c7eb9e61ec
Merge rust-bitcoin/rust-bitcoin#1916: internals: Bump version to 0.2.0
8813a63ec9 internals: Bump version to 0.2.0 (Tobin C. Harding)

Pull request description:

  In preparation for release bump the version and add a changelog entry. Includes updating the dependency in `bitcoin` and `hashes`.

ACKs for top commit:
  apoelstra:
    ACK 8813a63ec9
  sanket1729:
    utACK 8813a63ec9

Tree-SHA512: a9bd9d4d69cba21329f3f63a9948afe566bb97c8c65f5d46c329a696a814e9eb31372d378de1ecf0f43f0cb42f11d53dc51bc467223b34629e61315d48b39a29
2023-07-17 16:27:22 +00:00
Tobin C. Harding 92749d29e4
Rename PartiallySignedTransaction to Psbt
Last release we added a type alias for `Psbt`, now lets just rename the
type and be done with it.

Includes re-export at the crate root because `bitcoin::Psbt` is clear
and obvious.
2023-07-15 08:32:29 +10:00
Andrew Poelstra e30c492faf
witness: clean up Debug implementation
The previous code seems to have been rebased/iterated on too many times,
and had room for significant simplification. By inlining the indentation
logic we can eliminate 40 LOC and also clean up the output by removing
trailing spaces.
2023-07-14 20:37:11 +00:00
Andrew Poelstra 8f4b57e3c5
Merge rust-bitcoin/rust-bitcoin#1940: Remove docsrs attributes
71c0043127 Remove docsrs attributes (Tobin C. Harding)

Pull request description:

  Somehow when we started using `doc_auto_cfg` we forgot to remove a bunch of docsrs attributes.

ACKs for top commit:
  apoelstra:
    ACK 71c0043127
  sanket1729:
    utACK 71c0043127

Tree-SHA512: 16ff8eec0f6cd392d496f8f07cc0773bbda28f7c71022ae6b5e2c47a98d40c94a9169c60c0d8fa5a819f07910593d65a47b69bdc748d64cda0aac3323e9599a6
2023-07-14 20:11:25 +00:00
Tobin C. Harding 81a42536f9
Use hex_lit::hex in benches
Currently the test `hex` macro is only available when the `test`
compiler configuration option is set but we are using it in benches
code, this works for use because `cargo bench` sets `test` for the
current crate, however it breaks downstream crates.

Fix: #1830
2023-07-14 15:30:22 +10:00
Tobin C. Harding 71c0043127
Remove docsrs attributes
Somehow when we started using `doc_auto_cfg` we forgot to remove a bunch
of docsrs attributes.
2023-07-14 14:59:11 +10:00
Tobin C. Harding 8813a63ec9
internals: Bump version to 0.2.0
In preparation for release bump the version and add a changelog entry.

Includes updating the dependency in `bitcoin` and `hashes` as well as
the minimal/recent lock files.
2023-07-14 14:31:21 +10:00
Andrew Poelstra a7fe0f5695
Merge rust-bitcoin/rust-bitcoin#1913: Manually implement Debug on Witness
d45dbef3e7 Manually implement Debug on Witness (Tobin C. Harding)

Pull request description:

  The current derived debug implementation on `Witness` prints the content field as an array of integers. We can do better than this by manually implementing `Debug`.

  With this applied `Witness` is printed as follows: (first line is `{:?}` and the next is `{:#?}`):

  Using `{:?}`:
  ```
  Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
  ```

  Using `{:#?}`:
  ```
  Witness: {
      indices: 3,
      indices_start: 8,
      witnesses: [
          [0x00],
          [0x02, 0x03],
          [0x04, 0x05],
       ],
  }
  ```

ACKs for top commit:
  sanket1729:
    tested ACK d45dbef3e7. This would be helpful for debugging downstream.
  apoelstra:
    ACK d45dbef3e7

Tree-SHA512: eacf4fa8e3f38c4e9ddc45de78afb8eab5b5b196b77a6612f61860e0e4e7ba96de2e7f434b92816e0b00532e73c05378cafc046ec9c34108e9d9216fb36c524a
2023-07-13 23:08:45 +00:00
Tobin C. Harding 552f19abe3
Add more rustdocs to WitnessProgram
Add rustdocs to `WitnessProgram` commenting on why we carry the witness
version number around with the witness program. This is mainly a dev
comment but it helps document the invariants so make it a rustdoc
comment.
2023-07-13 09:53:33 +10:00
Tobin C. Harding 89303c1464
Move witness types to the script module
From BIP 141:

> A scriptPubKey (or redeemScript as defined in BIP16/P2SH) that
> consists of a 1-byte push opcode (for 0 to 16) followed by a data push
> between 2 and 40 bytes gets a new special meaning. The value of the
> first push is called the "version byte". The following byte vector
> pushed is called the "witness program".

`WitnessVersion` and `WitnessProgram` are scriptPubkey concerns and
scriptPubkey is basically synonymous with address so in one way it makes
sense that these types are in `address` however we are in the process of
overhauling the `Address` (and `AddressInner`) types so lets move the
witness stuff to `script` and put it in individual sub-modules.

This move helps simplify the address error type also.

Note please, there are a bunch of formatting changes in here in the
error type that I cannot explain and could not remove.
2023-07-13 09:51:40 +10:00
Tobin C. Harding c3a99c62ad
CI: Pin serde_json for MSRV build
Recent release of `serde_json` depends on `serde` 1.0.66 but we pin to
1.0.56

Pin `serde_json` for MSRV build to v1.0.99
2023-07-12 15:50:18 +10:00
Tobin C. Harding 154552e334
docs: Do not link to std::option::Option
This link breaks when building docs using no-std. Just use code ticks,
everyone knows what `Option` is, no need for a link.
2023-07-08 11:29:11 +10:00
Tobin C. Harding d45dbef3e7
Manually implement Debug on Witness
The current derived debug implementation on `Witness` prints the content
field as an array of integers. We can do better than this by manually
implementing `Debug`.

With this applied `Witness` is printed as follows: (first line is `{:?}`
and the next is `{:#?}`):

Using `{:?}`:
```
Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
```

Using `{:#?}`:
```
Witness: {
    indices: 3,
    indices_start: 8,
    witnesses: [
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}
```
2023-07-08 11:28:04 +10:00
Tobin C. Harding 9787ba6c96
Rename Script::empty to Script::new
The `empty` constructor is mis-named for the following reasons:

- Non-uniform with `ScriptBuf::new`
- Non-standard with respect to stdlib which uses `Path::new` and
  `PathBuf::new` (on which we based the `Scritp`/`ScriptBuf`)

Rename the function to `new`, put it at the top of the impl block while
we are at it.
2023-07-08 11:26:30 +10:00
Andrew Poelstra 83cf389a02
Merge rust-bitcoin/rust-bitcoin#1926: Refactor transaction weight
f5591d8dee Use weight instead of checked_weight (yancy)
80a4d692c4 Change weight to call predict_weight (yancy)

Pull request description:

  Followup from https://github.com/rust-bitcoin/rust-bitcoin/pull/1835.  Call `predict_weight` from `weight` instead of `scaled_size()` https://github.com/rust-bitcoin/rust-bitcoin/pull/1835#issuecomment-1543687210.  I think we could also deprecate `scaled_size()` and `strippedsize()` in a future refactor.

ACKs for top commit:
  apoelstra:
    ACK f5591d8dee
  tcharding:
    ACK f5591d8dee

Tree-SHA512: 73d719a98bd0e7e1b9b667d4a613db86a97cb16c70201ad039094bd8025e16984e74ea5110a02eedd10604663461682b7fd527023a0b0c7c94989e6f79603997
2023-06-29 01:46:46 +00:00
yancy f5591d8dee Use weight instead of checked_weight 2023-06-26 12:44:27 +02:00
yancy 80a4d692c4 Change weight to call predict_weight 2023-06-26 12:35:59 +02:00
Andrew Poelstra 51cc18ef8c
Merge rust-bitcoin/rust-bitcoin#1776: Added examples for signature verification
fc167097aa Added examples for sighash computations (Alec Matusis)

Pull request description:

  So far computed sighashes and verified signatures for:
   - P2WPKH
   - P2MS 2of3
   - P2SH 2of2 multisig
   - P2SH 2of3 multisig
   - P2WSH 2of2 multisig.

  TODOs:
    - Add P2TR script-path multisig and key-path examples
    - Are there mutisig transactions where flags are different for diff signatures within an input?
    - Maybe switch to  segwit_signature_hash()?
    - Consider also verifying script hash if we go for full P2(W)SH transactions verifications?

ACKs for top commit:
  tcharding:
    ACK fc167097aa
  apoelstra:
    ACK fc167097aa

Tree-SHA512: 67750b614592391d8252fc270be8676f8aef61eb842c49816386396e7afaa472921c21df40d13291ee80e653f3a0ec367f7b941920f1777f086815bf222e8e62
2023-06-23 16:23:06 +00:00
Andrew Poelstra f7673d9ddb
Merge rust-bitcoin/rust-bitcoin#1911: Add a verify function to PublicKey
e04ac1e743 Add a verify function to PublicKey (Tobin C. Harding)

Pull request description:

  Expose signature verification functionality for ECDSA signatures on the `PublicKey` type.

  We should have an identical function on `XOnlyPublicKey` but this will have to be done in `secp2561`: https://github.com/rust-bitcoin/rust-secp256k1/pull/618

  Idea from Kixunil: https://github.com/rust-bitcoin/rust-bitcoin/pull/1744#issuecomment-1534200841

ACKs for top commit:
  apoelstra:
    ACK e04ac1e743
  Kixunil:
    ACK e04ac1e743

Tree-SHA512: f26c223a1e5cc89e5c5fc12b22e621b9e8c395b8f91d7a58c6c938d45bc531e6682b178990b5a049718dbea66fff6d19d6fbcf926f142c781ad5213708ee7afa
2023-06-23 15:33:25 +00:00
Alec Matusis fc167097aa Added examples for sighash computations
So far computed sighashes for:
 - P2WPKH
 - P2MS
 - P2SH multisig
 - P2WSH multisig.

TODOs:
  - Add P2TR script-path multisig and key-path examples
2023-06-20 02:07:21 -07:00
Tobin C. Harding 6881080f8e
Fix incorrect comment in ci script
MSRV build breaks because of edition _2021_ not 2018.
2023-06-20 13:34:38 +10:00
Tobin C. Harding e04ac1e743
Add a verify function to PublicKey
Expose signature verification functionality for ECDSA signatures on the
`PublicKey` type.

We should have an identical function on `XOnlyPublicKey` but this will
have to be done in `secp2561`.
2023-06-20 05:08:07 +10:00
sanket1729 de7fe5e4ec
Merge rust-bitcoin/rust-bitcoin#1739: Mutate mul_u64 with mutagen
7cdc90565f Mutate mul_u64 with mutagen (Tobin C. Harding)

Pull request description:

  Add the `mutate` attribute to mutate `mul_u64`. Add non-doc comments listing the two false positives. These are identical but we list them twice so when devs grep for `mutagen false pos` the same number of lines for each function is displayed as is displayed by the `mutagen` run. This coding false positives thing is also introduced in PR #1655.

ACKs for top commit:
  apoelstra:
    ACK 7cdc90565f
  sanket1729:
    utACK 7cdc90565f

Tree-SHA512: d066beb2f9ba198f5af36258ba15cfbd2c7c9ce7596f6340ed1fe2f62a2b0b5296eeb2cb4be30146d019671f1858521c29db917936895b5b3fd36bdb0bd07e57
2023-06-14 17:53:39 -07:00
Andrew Poelstra b1078febc2
Merge rust-bitcoin/rust-bitcoin#1870: Add from_int_btc method to Amount
9f7449b572 Use from_int_btc function for const context (yancy)
f93e67977a Add from_int_btc function to Amount (yancy)

Pull request description:

  Followup PR from https://github.com/rust-bitcoin/rust-bitcoin/pull/1811

  Added a `const` associated function `from_int_btc()` for Amount.  `panic()` in const context is only available after 1.57+ so a work around is provided.

ACKs for top commit:
  tcharding:
    ACK 9f7449b572
  apoelstra:
    ACK 9f7449b572

Tree-SHA512: 7755234f2e573577d754f0224083cb7acc059e58833790fe344b0d9bad0acd89b0f74054d9efcba2133576222c7e9ab8dc3d81ddc10fbdcd4f83638d697118c4
2023-06-14 01:17:59 +00:00
yancy 9f7449b572 Use from_int_btc function for const context 2023-06-13 11:32:58 +02:00
yancy f93e67977a Add from_int_btc function to Amount 2023-06-13 11:32:51 +02:00
Andrew Poelstra 79bd294476
Merge rust-bitcoin/rust-bitcoin#1901: key: Implement From<PublicKey> for XOnlyPublicKey
445aa84f8c cargo fmt (Andrew Poelstra)
d990084481 key: Implement From<PublicKey> for XOnlyPublicKey (Steven Roose)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 445aa84f8c
  apoelstra:
    ACK 445aa84f8c

Tree-SHA512: c27b23dcf66139720011dc8bee379a8e4be642f8f60e8982c643013e163ad1610d355c6f5de04efc57d0dc11ba4ff0e893d3f760d5662d8f0c7eaba87f0a89a0
2023-06-12 23:49:45 +00:00
Andrew Poelstra 445aa84f8c
cargo fmt 2023-06-12 13:01:12 +00:00
roy9495 654f58da17
This library is not solely dependent on Rust 2023-06-08 20:03:17 +00:00
Andrew Poelstra 0750168c4f
Merge rust-bitcoin/rust-bitcoin#1297: `alloc`-free parse errors
2b6bcf085c Implement support for `alloc`-free parse errors (Martin Habovstiak)
783e1e81dc Move `impl_std_error` to `bitcoin-internals` (Martin Habovstiak)

Pull request description:

  This implements various helpers for parse errors that will not require `alloc`. This PR is useless while all of the crates require `alloc` and is thus a draft so that you can look at the design.

ACKs for top commit:
  tcharding:
    ACK 2b6bcf085c
  apoelstra:
    ACK 2b6bcf085c

Tree-SHA512: 776838a754b2c17263cf167c8cf8a3e69e51d8de45eb08d072ef930cbd1149360da2cb5fc430ce58f31c2b07dbf06c9f8384c567358873a3440e85632fcc2af8
2023-06-07 18:45:54 +00:00
Andrew Poelstra 12dc0b013b
Merge rust-bitcoin/rust-bitcoin#1863: Removed only available in 1.46.0 line
6a18997e3c Removed only available in 1.46.0 line (TATHAGATA ROY)

Pull request description:

  Fix: #1850

  Removed "*Important: only available in Rust 1.46+*" on the file  transaction.rs from lines 1288 and 1407 respectively.

ACKs for top commit:
  Kixunil:
    ACK 6a18997e3c
  apoelstra:
    ACK 6a18997e3c
  tcharding:
    ACK 6a18997e3c
  sanket1729:
    ACK 6a18997e3c

Tree-SHA512: 1395384ffe301b628687cc6d154e191b6a4415acd33eb4209065c5bf94115c3210ea1d28f7d7186e41665b39b5bebae849c3fa5394786ce24bdcd57b765cdbd3
2023-06-07 15:22:48 +00:00
Steven Roose d990084481
key: Implement From<PublicKey> for XOnlyPublicKey 2023-06-06 20:24:28 +01:00
Steven Roose 4c18ba779c
network: Implement Default on ServiceFlags 2023-06-06 00:06:28 +01:00
Andrew Poelstra 0266e762bd
Merge rust-bitcoin/rust-bitcoin#1898: Fix comments for #1890
d961b9c4ee Fix minor comments on count_sigops PR (junderw)

Pull request description:

  Fixing some comments that were left on #1890

ACKs for top commit:
  yancyribbens:
    ACK d961b9c4ee
  apoelstra:
    ACK d961b9c4ee
  tcharding:
    ACK d961b9c4ee

Tree-SHA512: caa04428eb7c09915964e4a7bae2d1fca2426317f3620d16e73e992269a99d7adb3d360affb954a173835661a9960cf760d29ae9861816b1a898c01428b0f2d6
2023-06-05 18:16:42 +00:00
junderw d961b9c4ee
Fix minor comments on count_sigops PR 2023-06-04 15:17:36 -07:00
Andrew Poelstra 7eeb3e6a4a
Merge rust-bitcoin/rust-bitcoin#1895: Improve `crytpo::taproot` error type
202d1cd581 Rename taproot::Error to SigFromSliceError (Tobin C. Harding)
29678cb82b Correctly document InvalidSighashType variant (Tobin C. Harding)
13d5c0536b Remove explicit error conversion (Tobin C. Harding)
d86517ae4f taproot: Use error variants locally (Tobin C. Harding)

Pull request description:

  First three patches are preparatory cleanup, last patch renames `crypto::taproot::Error` to `SigFromSliceError`. See commit log for justification of the `Sig` prefix.

  Done as part of the great error cleanup.

ACKs for top commit:
  apoelstra:
    ACK 202d1cd581
  Kixunil:
    ACK 202d1cd581

Tree-SHA512: 87aef07d2a3518c68c070e348d2331a9fbf1dc5cd36fd4d966607ddb0531eca9dc6be08f1923f941d33973f173b915490de9ef0cad724cce7bf108cdc8a82af0
2023-06-04 21:22:21 +00:00
Andrew Poelstra 1a1fe0e313
Merge rust-bitcoin/rust-bitcoin#1890: [script] Add method get_sigop_count
638445f8a9 Feature: Add opcodes::All::decode_pushnum and Script::count_sigops (junderw)

Pull request description:

  Planning to also add methods for the various parts of Transaction etc. to eventually allow for easier sigops calculation.

  Bare multisig is making a comeback, which is causing a large amount of transactions' effective vSizes (for fee calculation) to be dependent on the sigop count.

  This is a first step at making those transactions easier to estimate fees for / template blocks for etc.

ACKs for top commit:
  Kixunil:
    ACK 638445f8a9
  tcharding:
    ACK 638445f8a9

Tree-SHA512: 5e87d0f5ab58ed22ed50e43eac392b9b84ebccab5086553a6234d825766842057ab89bd0753f3c9de50d9ab17536182b8f64a57e8d5632a55494180f2cc26bbd
2023-06-04 19:59:44 +00:00
Martin Habovstiak 2b6bcf085c Implement support for `alloc`-free parse errors
This implements basic facilities to conditionally carry string inputs in
parse errors. This includes:

* `InputString` type that may carry the input and format it
* `parse_error_type!` macro creating a special type for parse errors
* `impl_parse` implementing parsing for various types as well as its
  `serde`-supporting alternative
2023-06-04 21:15:37 +02:00
junderw 638445f8a9
Feature: Add opcodes::All::decode_pushnum and Script::count_sigops 2023-06-02 08:46:46 -07:00
Tobin C. Harding ccb6e3eeba
Rename TaprootSpendInfo::as_script_map to script_map
The `as_script_map` is a getter not a conversion function (to/into/as),
as such it should not include the prefix `as_`.

Deprecate `as_script_map` in favour of `script_map`.
2023-06-02 16:59:08 +10:00
Tobin C. Harding 202d1cd581
Rename taproot::Error to SigFromSliceError
This error type is only used in the `from_slice` function. Use prefix
`Sig` because `taproot::FromSliceError` does not fully express how the
error came about.

Use specific identifier for the error, this aids usage but also prevents
us later adding "random" other variants into this error and using it in
other functions.
2023-06-02 15:48:45 +10:00
Tobin C. Harding 29678cb82b
Correctly document InvalidSighashType variant
The rustdoc on the `taproot::Error::InvalidSighashType` is wrong, fix
it.
2023-06-02 15:05:41 +10:00
Tobin C. Harding 13d5c0536b
Remove explicit error conversion
We provide a `From<secp255k1::Error>` impl so we do not need to
explicitly convert the error return, just use `?`.
2023-06-02 15:05:38 +10:00
Tobin C. Harding d86517ae4f
taproot: Use error variants locally
Add 'use Error::*' locally to make the code more terse.
2023-06-02 14:48:22 +10:00
Martin Habovstiak 783e1e81dc Move `impl_std_error` to `bitcoin-internals`
The macro is useful for all other crates thus it is moved to the
internals crate in this commit.
2023-05-30 18:04:54 +02:00
Tobin C. Harding 042dcaa4b7
Remove doc(hidden) from error conversion functions
Give people access to the error type conversion docs, its no harm and it
may be useful when the compiler does not give enough information.
2023-05-30 15:54:40 +10:00
Steven Roose 7bf0a106dd
Merge pull request #1872 from tcharding/05-24-rm-prevouts-export
psbt: Remove reexport of Prevouts
2023-05-30 02:48:34 +01:00
Andrew Poelstra 80efdb065a
Merge rust-bitcoin/rust-bitcoin#1873: Improve `hashes::Error`
06afd52a12 Improve hashes::Error (Tobin C. Harding)

Pull request description:

  We are trying to make error types stable on the way to v1.0

  The current `hashes::Error` is a "general" enum error type with a single variant, better to use a struct and make the error usecase specific.

  Improve the `hashes::Error` by doing:

  - Make it a struct
  - Rename to `FromSliceError`
  - Move it to the crate root (remove `error` module)

  Includes usage in `bitcoin`.

ACKs for top commit:
  apoelstra:
    ACK 06afd52a12
  Kixunil:
    ACK 06afd52a12

Tree-SHA512: 20a517daaf3e9e09744e2a65cde6e238c8f2d1224899a6c04142a3a4e635d54112b0a2e846d25256071bb27cb70f7482380f98e9a535a5498aa4c7dc0d52cc54
2023-05-29 22:58:17 +00:00
Andrew Poelstra ca17257884
Merge rust-bitcoin/rust-bitcoin#1882: made ckd_priv function private and impl AsRef for ChildNumber
4be019180b correcting formatting (startup-dreamer)

Pull request description:

  closes #1866

ACKs for top commit:
  Kixunil:
    ACK 4be019180b
  junderw:
    ACK 4be019180b
  apoelstra:
    ACK 4be019180b

Tree-SHA512: 9539c987a9636f3c50dde2d4448ca0c30c239f5a8e852e7bf560cc27a2681777fa1a91ace21bdf1fd67b1f60679dfb8eecef45b81506618285536599ad741f7e
2023-05-29 12:19:09 +00:00
Andrew Poelstra 5729b034d8
Merge rust-bitcoin/rust-bitcoin#1874: Add `ValidationError`
0f74eb6876 Remove the unused crate::Error (Tobin C. Harding)
74154c2294 Add block::ValidationError (Tobin C. Harding)
3a9b5526b3 Move BlockHash From impls (Tobin C. Harding)

Pull request description:

  Remove the `crate::Error` and replace its usage with `block::ValidationError`.

ACKs for top commit:
  apoelstra:
    ACK 0f74eb6876
  Kixunil:
    ACK 0f74eb6876

Tree-SHA512: 80b2c98d3d8f7c3f060c8ea2d94e1ebe118c07d0dcf91f6d13aed00df2cb0b15bf5e295ec0976d88d81e029cf7d3e8e4a1fe70120db57e49bbd8dd229291836b
2023-05-28 23:08:32 +00:00
Andrew Poelstra 7315eac5ee
Merge rust-bitcoin/rust-bitcoin#1871: Fix conditional configuration options usage
0046bb8ad8 Fix usage of cfg(rust_1_53) (Tobin C. Harding)
c3450f3913 Remove stale usage of doc(cfg) (Tobin C. Harding)

Pull request description:

  These build cfg options are not features, fix broken usage. And remove stale docsrs attribute while we are at it. Bad rust-bitcoin devs.

  Found while reviewing #1870

ACKs for top commit:
  apoelstra:
    ACK 0046bb8ad8
  Kixunil:
    ACK 0046bb8ad8

Tree-SHA512: 7053affef6654ff203c93590bf081e165f019feb040aa8c55259ffe4e15eaf0e7522f6e5a4f6f62e8f578420b0313f4b7b17c46b741b7fcfd05750e5c5976589
2023-05-28 22:27:27 +00:00
Andrew Poelstra ce44711219
Merge rust-bitcoin/rust-bitcoin#1879: embedded: Document how to clean up linker flags
3af9258025 embedded: Document how to clean up linker flags (Tobin C. Harding)

Pull request description:

  Our embedded crate includes instructions to source a shell script that sets the `RUSTFLAGS` env var. Having the env var set like this in ones environment breaks linkage when trying to do "regular" builds.

  Document how to clean up.

ACKs for top commit:
  apoelstra:
    ACK 3af9258025
  Kixunil:
    ACK 3af9258025

Tree-SHA512: 72758fba4dede873da299f01d75fd64b549fe21f954c2720ba3b7fc3c29fd4ed28fd0a749bbe987a7de1551aa32192253dd0033a18c877e877c9960343a5b07c
2023-05-28 21:47:03 +00:00
startup-dreamer 4be019180b correcting formatting 2023-05-29 00:26:01 +05:30
Andrew Poelstra ca06d64455
rename bitcoin-private to bitcoin-internals
Since we now have control over the name on crates.io.
2023-05-26 19:49:52 +00:00
Tobin C. Harding 3af9258025
embedded: Document how to clean up linker flags
Our embedded crate includes instructions to source a shell script that
sets the `RUSTFLAGS` env var. Having the env var set like this in ones
environment breaks linkage when trying to do "regular" builds.

Document how to clean up.
2023-05-26 14:39:14 +10:00
Tobin C. Harding 0f74eb6876
Remove the unused crate::Error
The `crate::Error` type is now unused, remove it.
2023-05-25 13:35:49 +10:00
Tobin C. Harding 74154c2294
Add block::ValidationError
Add a `ValidationError` to the `block` module and remove the two
variants out of `crate::Error`.

This error is only used by the `validate_pow` function, a specific error
better serves our purposes.
2023-05-25 13:35:49 +10:00
Tobin C. Harding 3a9b5526b3
Move BlockHash From impls
Move the `From` impls on `BlockHash` to below the `BlockHash` impl
block.

Code move only, no other changes.
2023-05-25 13:32:25 +10:00
Tobin C. Harding 06afd52a12
Improve hashes::Error
We are trying to make error types stable on the way to v1.0

The current `hashes::Error` is a "general" enum error type with a single
variant, better to use a struct and make the error usecase specific.

Improve the `hashes::Error` by doing:

- Make it a struct
- Rename to `FromSliceError`
- Move it to the crate root (remove `error` module)

Includes usage in `bitcoin`.
2023-05-25 13:25:13 +10:00
Tobin C. Harding ab4e6d6f27
psbt: Remove reexport of Prevouts
No idea why this re-export is here, the `Prevouts` type is not even used
in the `psbt` module.

Remove the re-export of `crate::sighash::Prevouts` from `pstb`.
2023-05-24 22:10:40 +10:00
Tobin C. Harding 0046bb8ad8
Fix usage of cfg(rust_1_53)
Our custom conditional configuration options enables for various
versions of the compiler are not features.

Fix the incorrect usage.
2023-05-24 22:03:57 +10:00
Tobin C. Harding c3450f3913
Remove stale usage of doc(cfg)
We no longer need to use `doc(cfg())` because we use `doc_cfg_auto`,
remove it.
2023-05-24 22:02:24 +10:00
Andrew Poelstra b5aa482532
Merge rust-bitcoin/rust-bitcoin#1857: tests: Use script hash functions
013dffa65d tests: Use script hash methods (Tobin C. Harding)

Pull request description:

  The `ScriptBuf` type can be serialized using it's `to_bytes` function. Do not use the `psbt::Serialize` trait to do so in test code.

  No logic changes, since the impl of `psbt::Serialize` for `ScriptBuf` just calls `to_bytes`.

ACKs for top commit:
  Kixunil:
    ACK 013dffa65d
  apoelstra:
    ACK 013dffa65d

Tree-SHA512: 08959e065f1528f2ca69c12d5e34bceea3ddd17eefcee45094f071b3fa7357dbf289f6fe54d18fea02eecd1d0a7cd04598bbf014a5f10676387dbe27bb490395
2023-05-22 20:31:42 +00:00
Andrew Poelstra 8d111f20ef
Merge rust-bitcoin/rust-bitcoin#1864: Add a checked version of weight mul fee_rate
b03c24db8c Add a checked version of weight mul fee_rate (yancy)

Pull request description:

  Add a checked version of fee_rate * weight.  While I like the trait version of just being able to multiply `feerate * weight`, it's not really very useful imo since a large input feerate could cause an overflow.  Instead of changing the trait in https://github.com/rust-bitcoin/rust-bitcoin/pull/1849 (not idiomatic enough I guess) I added a `checked_weight_mul` method to `FeeRate`.

ACKs for top commit:
  apoelstra:
    ACK b03c24db8c
  Kixunil:
    ACK b03c24db8c

Tree-SHA512: 231ade94291becadcea9ea2a40a5daf96b77f01a29cca2494d7bbe4f7de5b412fa8fc816ea249268569f5378410185d9349fd687533bf3a422a752997e107a2b
2023-05-22 19:36:17 +00:00
Tobin C. Harding 013dffa65d
tests: Use script hash methods
We have methods to convert a script to a `WScritpHash` and `ScriptHash`,
no need to do this manually, let alone use the `psbt::Serialize` trait
to do so.
2023-05-20 07:31:54 +10:00
yancy b03c24db8c Add a checked version of weight mul fee_rate 2023-05-19 10:17:57 +02:00
TATHAGATA ROY 6a18997e3c Removed only available in 1.46.0 line 2023-05-18 12:14:38 +00:00
Tobin C. Harding c2f33f60b8
Add changelo entry for removal of FromHex
During the last round of releases (bitcoin 0.30, hashes 0.12) we removed
the `FromHex` implementation from all types except vecs and arrays. We
added `FromStr` impls for types that roundtrip with `Display`.

We never added a changelog mention to either `bitcoin` or `hashes`, lets
retroactively add an entry.

Fix: #1747
2023-05-18 14:18:42 +10:00
Martin Habovstiak 6cec19f6d3 Update documentation of `InputWeightPrediction`
The doc was written before alternative constructors existed, update it.
2023-05-11 20:38:32 +02:00
Martin Habovstiak 6fec1789b9 Fix associated constants of `InputWeightPrediction`
These constants had an error that they had `script_size` set to 0 which
was incorrect because it's not length of the script but serialized size.
Rather than just bumping the value this uses the `from_slice` method
which is less error-prone.

This also deletes a useless test of the constants.

Closes #1834
2023-05-11 20:38:32 +02:00
Martin Habovstiak 0555695757 Fix formatting
This slipped in and is breaking builds.
2023-05-11 13:03:22 +02:00
Riccardo Casatta 2df9b2c8b2
Merge rust-bitcoin/rust-bitcoin#1844: make bip21 schema lowercase
8835d5d2f1 make bip21 schema lowercase (Riccardo Casatta)

Pull request description:

  The spec RFC3986 specifies the scheme is case insensitive and we were uppercasing to optimize QR code representation.
  Unfortunately, common platform such as Android seems to fail to recognize uppercase schema, so for compatibility reason we use lowercase.

  close #1843

ACKs for top commit:
  Kixunil:
    ACK 8835d5d2f1
  apoelstra:
    ACK 8835d5d2f1

Tree-SHA512: 02d228b52fe4df20edb71ba8e2ab8a2bae4b912252e30a3150ee3af74e65a6e91b165c9579273b57e894366c9792a8312ea973723cd8c5d98037aaba80d7cf07
2023-05-11 10:41:35 +02:00
Andrew Poelstra 684e14caee
Merge rust-bitcoin/rust-bitcoin#1768: Allow parsing sub-sat denominations with decimal points
6c6a89b1d1 Add sub-sat fractions parsing regression test (Martin Habovstiak)
f1a3dc6719 Allow parsing sub-sat denoms with decimal points (Martin Habovstiak)
b3d9a267ea Add a few more amount parsing tests (Martin Habovstiak)

Pull request description:

  Numbers with only zeros after decimal points are valid if they are also
  multiples of `10^precision` (e.g. 1000 for msats). These were
  artificially disallowed as "too precise" which was at least misleading.

  This change allows parsing such numbers.

  And yes, I know this is not perfectly efficient (unless the compiler figures out some magic opts) but so isn't the rest of the code. TBH this parsing code drives me crazy and I'd love to rewrite it to be more efficient and readable.

ACKs for top commit:
  apoelstra:
    ACK 6c6a89b1d1

Tree-SHA512: 03cf4b416f2eac25e0aac57ef964ed06fa36c7fe8244bdcf97852cc58e1613b1ec6132379b834da58ad3240fdd61508a384202f63aa9ffa335c18cd7b2b724d3
2023-05-10 20:30:54 +00:00
Riccardo Casatta 8835d5d2f1
make bip21 schema lowercase
The spec RFC3986 specifies the scheme is case insensitive and we were uppercasing
to optimize QR code representation.
Unfortunately, common platform such as Android seems to fail to recognize
uppercase schema, so for compatibility reason we use lowercase.
2023-05-10 20:36:15 +02:00
Andrew Poelstra d93e781148
Merge rust-bitcoin/rust-bitcoin#1838: Move and rename TxOut default trait to a const called NULL
75b3f19b96 Move and rename TxOut default trait to a const called NULL (yancy)

Pull request description:

  Create an associated constant `const TxOut::NULL` for consensus signing code and remove the default trait.  Note I tried to deprecate the `default()` fn instead of just removing it but it doesn't seem to be possible.  Also because `TxOut::NULL` is `const`, `ScriptBuf::new()` needed to be changed to `const fn`.

ACKs for top commit:
  apoelstra:
    ACK 75b3f19b96
  Kixunil:
    ACK 75b3f19b96

Tree-SHA512: ff61a2b1641a1ba32f183c27205af2d868dbc2eb47cf758c3d8315329d2c23e0b8a82ea0ab59d1de9add0d238f927165e2e4df014aab1ef066d74d4feda0700b
2023-05-09 18:31:34 +00:00
yancy 639c548aed docs: Add doc comments for external crates 2023-05-08 10:57:49 +02:00
yancy 75b3f19b96 Move and rename TxOut default trait to a const called NULL 2023-05-08 09:11:06 +02:00
Andrew Poelstra dea628276c
Merge rust-bitcoin/rust-bitcoin#1837: feat: generate PrivateKey
995c797e0d feat: generate PrivateKey (kshitjj)

Pull request description:

  added a function to generate a private key

  Resolves: #1823

ACKs for top commit:
  apoelstra:
    ACK 995c797e0d
  tcharding:
    ACK 995c797e0d

Tree-SHA512: 29ba54be8cb777e71a4683835686cbf2978b23736f629d7bbff468074235fece261ca170c23f358d1bd878987566d09e4488c3f1a106c59a5c8bdf52b98abffe
2023-05-08 03:13:51 +00:00
kshitjj 995c797e0d feat: generate PrivateKey 2023-05-07 21:56:39 +05:30
Martin Habovstiak 6c6a89b1d1 Add sub-sat fractions parsing regression test
This test triggers the bug fixed in previous commit.
2023-05-07 08:47:53 +02:00
Martin Habovstiak f1a3dc6719 Allow parsing sub-sat denoms with decimal points
Numbers with only zeros after decimal points are valid if they are also
multiples of `10^precision` (e.g. 1000 for msats). These were
artificially disallowed as "too precise" which was at least misleading.

This change allows parsing such numbers.
2023-05-07 08:45:25 +02:00
Martin Habovstiak b3d9a267ea Add a few more amount parsing tests
These tests try to stress various edge cases that should return
`ParseAmountError::TooPrecise`.
2023-05-07 08:39:24 +02:00
Andrew Poelstra 4abbdc20a0
Merge rust-bitcoin/rust-bitcoin#1820: Expose valid (min, max) difficulty transition thresholds
8e6f953aa7 Expose valid (min, max) difficulty transition thresholds (Wilmer Paulino)

Pull request description:

  Once `U256` was made private, we lost the ability to check whether a valid difficulty transition was made in the chain, since `Target` doesn't expose any operations. We only choose to expose `Shl<u32>` and `Shr<u32>` such that we can compute the min and max target thresholds allowed for a difficulty transition.

  This is something we realized was missing after bumping to `rust-bitcoin v0.30.0` in `rust-lightning`, specifically for our `lightning-block-sync` crate. It may also be worth having a helper in `rust-bitcoin` that checks a header properly builds upon the previous, but that can be left for future work.

ACKs for top commit:
  Kixunil:
    ACK 8e6f953aa7
  sanket1729:
    ACK 8e6f953aa7 . Sorry, was confused by some details.
  apoelstra:
    ACK 8e6f953aa7

Tree-SHA512: 740dd64089426463dc6a19726d5a562276bd0966e0e31af8e1e67b28d18945644ac0e50be3cf0cc7fa604acc3d2c5b912a77a7caa69d8cff85f70fd57e5328c5
2023-05-06 18:53:15 +00:00
Andrew Poelstra 64540b9b93
Merge rust-bitcoin/rust-bitcoin#1793: Comment predict_weight
dff757d7db Comment predict_weight (yancy)

Pull request description:

  I've been reading over the `predict_weight` function since it is one of the biggest challenges for coin-selection.  IE choosing inputs and constructing an optimal selection strategy requires predicting the weight to get the best selection.  It's great this work has been done but there are some things I don't understand well enough to comment.

  1) why are we looking at the size of VarInt struct here
  > let script_size = script_len + VarInt(script_len as u64).len()

  2) [predict_weight_internal](36500b4451/bitcoin/src/blockdata/transaction.rs (L1245)) has a bunch of magic numbers.  I'd like to be able to comment this as well but I don't fully understand that function.

  Also, `Transaction.rs` is a big file and it seems like all of the prediction stuff could be moved to a separate module or maybe a separate crate?

ACKs for top commit:
  tcharding:
    ACK dff757d7db
  Kixunil:
    ACK dff757d7db

Tree-SHA512: 8ffa16d500075d691528ce1819b9352a148af431889bebbd7cddcf470bd4e3048ec53a5e778bc3659e33d8c25b68422a93dac1d46b9489ff56f41d88d7f05433
2023-05-05 02:55:23 +00:00
Andrew Poelstra 25f569adeb
Merge rust-bitcoin/rust-bitcoin#1811: Use Amount type for TxOut value field
d57ec019d5 Use Amount type for TxOut value field (yancy)

Pull request description:

  Propose using `Amount` type for the `TxOut` `value` field.  I only implemented `Decodable ` and `Encodable` enough to compile but this needs to completed obviously if using `Amount` seems like a good idea.

ACKs for top commit:
  tcharding:
    ACK d57ec019d5
  apoelstra:
    ACK d57ec019d5

Tree-SHA512: df3fd55294d5f9392ca90bb920be8fbb9d7d285d97669412e07d5a099f70f81fd73e7e259679de9c8ce5c6c855e64f62213700f0fb7db415e0c706c509485377
2023-05-05 00:10:55 +00:00
Andrew Poelstra 1abbed2129
Merge rust-bitcoin/rust-bitcoin#1785: Implement serde::Serialize for Address
ed6421c939 address: Add generic serde::Serialize for Address (Steven Roose)
814b9917da address: Add Sync, Send, Sized and UnPin marker traits on NetworkValidation (Steven Roose)

Pull request description:

  With the new rewrite of Address, `serde::Serialize` is only implemented on `Address<bitcoin::address::NetworkChecked>` and `Address<bitcoin::address::NetworkUnchecked>`. But the compiler has no way of knowing that that are all the possible versions of `Address`, so the generic `Address<impl bitcoin::address::NetworkValidation>` doesn't implement `serde::Serialize`.

ACKs for top commit:
  Kixunil:
    ACK ed6421c939
  tcharding:
    ACK ed6421c939

Tree-SHA512: 65e43dff244c94fe08ccb2d985781a2687a1e2db186960a35d4ae89f3b31c5af66892630a3ebaac9cecdc83638487425afa17374869d278648b348869e0ba091
2023-05-04 21:52:38 +00:00
Wilmer Paulino 8e6f953aa7
Expose valid (min, max) difficulty transition thresholds
Once `U256` was made private, we lost the ability to check whether a
valid difficulty transition was made in the chain, since `Target`
no longer exposes any arithmetic operations.
2023-05-04 12:41:28 -07:00
yancy d57ec019d5 Use Amount type for TxOut value field 2023-05-04 17:09:08 +02:00
Andrew Poelstra ac664106be
Merge rust-bitcoin/rust-bitcoin#1829: Remove `min_value` and `max_value`
6cab7beba3 Deprecate min/max_value methods (Tobin C. Harding)
5fbbd483ea Use MIN/MAX consts instead of min/max_value (Tobin C. Harding)
3885f4d430 Add MIN/MAX consts to amounts (Tobin C. Harding)

Pull request description:

  The new MSRV (1.48.0) uses associated consts MAX/MIN instead of functions, we had functions to be compliant with the old MSRV.

  ~Remove all methods `min_value` and `max_value` including calls to these methods on stdlib types.~

  PR is now split into three patches:
  - patch 1: Add missing associated consts MIN/MAX as needed
  - patch 2: Use consts instead of method calls
  - patch 3: Deprecate methods `min_value` and `max_value`

ACKs for top commit:
  sanket1729:
    ACK 6cab7beba3
  apoelstra:
    ACK 6cab7beba3
  Kixunil:
    ACK 6cab7beba3

Tree-SHA512: 60949d1bb971e0dfbab7f573b4447f889b5fa1a5f1c9ac9325a2970fe17a19ccc93418dba57f07bed7e13864b130de48b6b3741d1d80266c6144237dd4565ff7
2023-05-04 12:59:56 +00:00