Commit Graph

2326 Commits

Author SHA1 Message Date
merge-script 80dcbd618a
Merge rust-bitcoin/rust-bitcoin#4234: chore: add missing backquotes
61e7226299 chore: add missing backquotes (kevincatty)

Pull request description:

  add missing backquotes

ACKs for top commit:
  apoelstra:
    ACK 61e7226299fbfb13f34ba27e45042019a92b050d; successfully ran local tests
  tcharding:
    ACK 61e7226299

Tree-SHA512: 232cdcd9627cb1ae06e9c9c022460ee4fc405845bdf8863aaadf38d22dc022891f3f9a6a49b6aeeeceef62e48493357cbf293458f687f5d5d41a697c152da007
2025-03-13 03:15:02 +00:00
merge-script bc8de7846e
Merge rust-bitcoin/rust-bitcoin#4233: test: add coverage for ServiceFlags::P2P_V2
c0e20dbf2e test: add coverage for ServiceFlags::P2P_V2 (Bruno Garcia)

Pull request description:

  Include `P2P_V2` on `service_flags_test`

ACKs for top commit:
  tcharding:
    ACK c0e20dbf2e
  apoelstra:
    ACK c0e20dbf2e1406a3f3df5538d1397b233078cd7c; successfully ran local tests

Tree-SHA512: 6ebb2f493bcc8fc6643cec67945a81692f03ceb976d75e10ce6052e775901846b9795f31817439b4484ef2d1eaae122d7da270cac2b59d7bb41cb2015593d1f5
2025-03-13 01:51:10 +00:00
merge-script d8a90f6865
Merge rust-bitcoin/rust-bitcoin#4232: fix comment ecdsa-psbt.rs
968406cb95 Update ecdsa-psbt.rs (planetBoy)

Pull request description:

  desciptor - descriptor -> `fix typo`

ACKs for top commit:
  tcharding:
    ACK 968406cb95
  apoelstra:
    ACK 968406cb95d7490869de9f23022f5c77a3665de8; successfully ran local tests

Tree-SHA512: 40e1d1133b16aaf14e7a0d8a2a40db8e233a1736c1c4caa2f340aa1f243283ef2b3dd2bc238a44122301b5e171b15f3638a21f6cc2089bc324dba54c49daaba2
2025-03-12 23:31:50 +00:00
Tobin C. Harding ab4ea7c13d
Enforce the MAX_MONEY invariant in amount types
Enforcing the MAX_MONEY invariant is quite involved because it means
multiple things:

- Constructing amounts is now fallible
- Converting from unsigned to signed is now infallible
- Taking the absolute value is now infallible
- Integer overflow is illuminated in various places

Details:

- Update from_sat to check the invariant
- Fix all docs including examples
- Use the unchecked constructor in test code
- Comment any other use of the unchecked constructor
- Deprecate unchecked_abs
- Fail serde (using the horrible string error variant)
- Try not to use the unchecked constructor in rustdocs, no need to encourage unsuspecting users to use it.
- Use ? in rustdoc examples (required by Rust API guidlines)
- Remove TryFrom<Amount> for SignedAmount because the conversion is now infallible. Add a From impl.
- Fix the arbitrary impls
- Maintain correct formatting
- Remove private check_max function as its no longer needed
2025-03-13 09:07:14 +11:00
merge-script 1f74571401
Merge rust-bitcoin/rust-bitcoin#4230: Fix BIP32 validation for private keys and master key constraints (#4195)
8f74b823ab Add validation for private key format and master key constraints (Erick Cestari)

Pull request description:

  This PR addresses issue #4195 by adding proper validation when decoding extended private keys:

  ### Changes

  - Add validation to ensure byte 45 is zero as required by BIP-32 specification for private keys
  - For master keys (depth=0), add validation to ensure parent fingerprint is zero
  - For master keys (depth=0), add validation to ensure child number is zero
  - Add corresponding error types to handle these validation failures
  - Add unit tests to verify each validation rule

  ### Validation Rationale
  These checks improve security by rejecting malformed extended keys that could potentially lead to unexpected behavior. As noted in the issue discussion, these validations are explicitly required by the BIP-32 specification.

  ### Testing
  Added three new unit tests to verify each validation rule:

  - test_reject_xpriv_with_non_zero_byte_at_index_45
  - test_reject_xpriv_with_zero_depth_and_non_zero_index
  - test_reject_xpriv_with_zero_depth_and_non_zero_parent_fingerprint

  Fixes #4195

ACKs for top commit:
  jrakibi:
    ACK 8f74b823ab
  tcharding:
    ACK 8f74b823ab
  apoelstra:
    ACK 8f74b823ab8ef44bde7d003f8ba43fbe44dbef3e; successfully ran local tests

Tree-SHA512: 6a013e4917f83cfd7e39a2a18f7491853d791ab1d981a99eeea6204e1dab723fed7a168ff2a89e8850d512c3c381bfa1afef7fa32e5a0d246d949a46b01a3023
2025-03-12 21:59:24 +00:00
merge-script b18aa6f9d4
Merge rust-bitcoin/rust-bitcoin#4229: feature: introduce MAX_BLOCK_SERIALIZED_SIZE to check limit
e4513bf925 feat: add MAX_BLOCK_SERIALIZED_SIZE existing in core (ChrisCho-H)

Pull request description:

  fad0d9ea2d1e807806fa141238e279fddea6ae99: add `MAX_BLOCK_SERIALIZED_SIZE` as constant, which also exists in [bitcoin-core](59ff17e5af/src/consensus/consensus.h (L13)).

  I originally thought it would be better to use this value for checking limit of push_bytes [here](0870cd1660/bitcoin/src/blockdata/script/push_bytes.rs (L31)), as it's the actual limit(`OP_PUSHDATA4` semantic says it could allow up to 4GB though). However, I'm not sure whether there might be need to push_bytes larger than `MAX_BLOCK_SERIALIZED_SIZE`, so just let developer use this constant to check the actual limit rather than enforcing it.

ACKs for top commit:
  tcharding:
    ACK e4513bf925
  apoelstra:
    ACK e4513bf9250799bc18a10728af184d6c86a561a4; successfully ran local tests

Tree-SHA512: 44c5a4882666ad286c1e1c40b9738929e2a8ad4bb44aaf48865fc395291185ae5aae351d26ac9334671e47a11e844bd037bd251a921b6b028a116d1b442b9183
2025-03-12 14:21:32 +00:00
merge-script 3cef539aac
Merge rust-bitcoin/rust-bitcoin#4164: Prepare to enforce MAX_MONEY invariant
5d851f1c3e Remove deprecated amount methods (Tobin C. Harding)
76a2d70b28 Make mul weight by fee return NumOpResult (Tobin C. Harding)
f9eb307953 Remove panic in dust value functions (Tobin C. Harding)
13595fbe7d Fix amount whole bitcoin constructors (Tobin C. Harding)
ac71680202 Pick one - MAX or MAX_MONEY (Tobin C. Harding)
6d70c77cf9 Enforce newtype sanity rules for amount types (Tobin C. Harding)
e6f7b26d80 Use _unchecked in amount const types (Tobin C. Harding)
ef0af8d62e Use sat/ssat constructors throughout tests (Andrew Poelstra)
8ecdc7c275 Use den_ prefix for local Denomination variable (Tobin C. Harding)
938461cc65 psbt: Use Amount::ZERO in unit test (Tobin C. Harding)

Pull request description:

  We want to start enforcing MAX_MONEY as an invariant in the amount types. There are a few more steps we can do first to make that change easier to review.

ACKs for top commit:
  jamillambert:
    ACK 5d851f1c3e
  apoelstra:
    ACK 5d851f1c3e98d7d426e5897b2d734b77a299ccfb; successfully ran local tests

Tree-SHA512: 9e28b273d41fc143656e3a84736b6abe477fae5721b02bce7436551bd489cc235dc7e9fc68ffafa98f75a61065470ac514570a42bea94e90fedbb31f3cd61031
2025-03-12 12:14:14 +00:00
kevincatty 61e7226299 chore: add missing backquotes
Signed-off-by: kevincatty <zhanshanmao@outlook.com>
2025-03-12 16:51:55 +08:00
Bruno Garcia c0e20dbf2e test: add coverage for ServiceFlags::P2P_V2 2025-03-11 18:24:16 -03:00
planetBoy 968406cb95
Update ecdsa-psbt.rs 2025-03-11 15:43:09 +01:00
merge-script c23402c330
Merge rust-bitcoin/rust-bitcoin#4228: docs: Update README to replace use of mutagen with cargo-mutants
199f57849a Remove references to cfg(mutate) from lint allow - no longer allowed (AM)
a65d1d8b95 docs: Update README to replace use of mutagen with cargo-mutants (AM)

Pull request description:

  Hey there!

  I am just getting up to speed with the project and in following the README discovered that there are still references to the previous mutation testing tool `mutagen`. I updated the README to refer to the new tool, `cargo-mutation`.

  I'm suggesting the user use the same command, `cargo mutants --in-place --no-shuffle`, as is run in the weekly CI workflow.

  I noticed that there are still references to the old `mutate` attribute in the following files. I removed these as well as per [feedback](https://github.com/rust-bitcoin/rust-bitcoin/pull/4228#issuecomment-2709407253).

  `primitives/Cargo.toml`:
  ```
  [lints.rust]
  unexpected_cfgs = { level = "deny", check-cfg = ['cfg(mutate)'] }
  ```

  and

  `bitcoin/Cargo.toml`:
  ```
  unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)', 'cfg(fuzzing)', 'cfg(kani)', 'cfg(mutate)'] }
  ```

  Update to PR: removed incorrect understanding of logic in PR description as per [feedback](https://github.com/rust-bitcoin/rust-bitcoin/pull/4228#issuecomment-2709408598) and removed `cfg(mutate)` from above 2 files.

ACKs for top commit:
  tcharding:
    ACK 199f57849a
  apoelstra:
    ACK 199f57849acd9845902a8090ad6490a61ee03d24; successfully ran local tests

Tree-SHA512: e154c504aa5283f1da05d0120ea8dda97d1159389e692b0d57d7d864032ecb2b48c496054ede5500477367bc732dc34b0870f2709b8bd6e7b3f5c18a10f7a29e
2025-03-11 14:00:59 +00:00
merge-script 856ff4c34c
Merge rust-bitcoin/rust-bitcoin#4226: fix typos
08ced04d12 fix typos (wgyt)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 08ced04d12
  apoelstra:
    ACK 08ced04d124440d6b18d5815fd378f4f301f6d7d; successfully ran local tests

Tree-SHA512: 003fc80d2f2dd4e3b088e299776f811ab404314902314ee0136c08363328b2ddf72e7bace8b0440b58c4421922e99c9685b65cd5ef450433cc8b63023545fea9
2025-03-11 03:33:33 +00:00
ChrisCho-H e4513bf925 feat: add MAX_BLOCK_SERIALIZED_SIZE existing in core 2025-03-11 10:04:13 +09:00
Tobin C. Harding f9eb307953
Remove panic in dust value functions
Calculating the minimum non-dust fee currently panics if either the
script is really big or the dust fee rate is really big.

Harden the API by returning an `Option` instead of panicing.
2025-03-11 05:37:40 +11:00
Tobin C. Harding ac71680202
Pick one - MAX or MAX_MONEY
Just use MAX everywhere in this codebase.

After discussion in PR consensus was to just use MAX throughout the
codebase.

ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/4164#discussion_r1979441438
2025-03-11 05:37:39 +11:00
Tobin C. Harding 938461cc65
psbt: Use Amount::ZERO in unit test
We have a const for this, use it.

Internal change only.
2025-03-11 05:32:06 +11:00
Erick Cestari 8f74b823ab Add validation for private key format and master key constraints
This commit adds additional validation checks when decoding extended private keys:

1. Verifies that byte 45 is zero as required by BIP-32 specification
2. For master keys (depth=0), ensures parent fingerprint is zero
3. For master keys (depth=0), ensures child number is zero

These checks improve security by rejecting malformed keys that could
potentially lead to unexpected behavior. Added corresponding error types
and unit tests to verify each validation rule.
2025-03-10 10:22:41 -03:00
AM 199f57849a Remove references to cfg(mutate) from lint allow - no longer allowed
Also fix incorrect spelling of honggfuzz
2025-03-10 12:45:57 +08:00
wgyt 08ced04d12 fix typos 2025-03-10 09:04:59 +08:00
yancy a273814d23 Replace underflow with overflow in doc comments
The use of underflow is misleading.  Adding one to MAX and
subtracting one from MIN are both considered an overflow.
2025-03-08 10:41:20 -06:00
merge-script eee3505d2b
Merge rust-bitcoin/rust-bitcoin#4214: Fix some comments
2aac5a1f81 Fix some comments (NinaLua)

Pull request description:

  I fixed some typos in the comments, please review it.

ACKs for top commit:
  Kixunil:
    ACK 2aac5a1f81
  apoelstra:
    ACK 2aac5a1f81a9bb217c4dfb7e45b96188ea60e35b; successfully ran local tests

Tree-SHA512: 50a55451b166189e8ca3d2725ed7bb8ff95a8f1ebef0296c0003414871f1b211e6ffcc3b7225302dd3d6760bfc3f65cf8ed730327ceab60cd55b868ccb0cea9a
2025-03-08 14:20:59 +00:00
merge-script 8a7c295412
Merge rust-bitcoin/rust-bitcoin#4212: Add `fee_rate::serde` re-export
d1c758f5a4 Add fee_rate::serde re-export (Tobin C. Harding)

Pull request description:

  When we added the `fee_rate::serde` module we forgot to re-export it. This is needed so downstream can do specify serde attributes on struct fields.

  ```rust
      #[serde(with = "bitcoin::fee_rate::serde::as_sat_per_kwu")]
      rate: FeeRate,
  ```

ACKs for top commit:
  Kixunil:
    ACK d1c758f5a4
  apoelstra:
    ACK d1c758f5a472a4a67cf9c7afa9ef9c0d793a2e16; successfully ran local tests

Tree-SHA512: 6e6f7879d8a0dab59d79f0e41dd5f9f791b72dfb5a1583d0c87ec04216c0a9c0e5c4fb328b93f5298af47b56d898f48717b1641f51295314423e6a569b4677fe
2025-03-08 05:01:52 +00:00
merge-script f7b1810b15
Merge rust-bitcoin/rust-bitcoin#4182: Replace uses of `chunks_exact` with `as_chunks`
a013700527 Replace uses of `chunks_exact` with `as_chunks` (Martin Habovstiak)

Pull request description:

  This is now ready for review.

  In the past we've been using `chunks_exact` because const generics were unstable but then, when they were stabilized we didn't use `as_chunks` (or `array_chunks`) since they were unstable. But the instability was only because Rust devs don't know how to handle `0` being passed in. The function is perfectly implementable on stable. (With a tiny, easy-to-understand `unsafe` block.) `core` doesn't want to make a decision for all other crates yet but we can make it for our own crates because we know that we simply never pass zero. (And even if we did, we could just change the decision.)

  It also turns out there's a hack to simulate `const {}` block in our MSRV, so we can make compilation fail early.

  This commit adds an extension trait to internals to provide the methods, so we no longer have to use `chunks_exact`. It also cleans up the code quite nicely.

  Previous unresolved question, leaving for reference:

  > One issue with this change is that the names collide which could lead to hard error in future Rust versions. How do we solve it?
  > * ignore and just backport the fix once that actually happens
  > * rename the methods to something reasonable (e.g. `as_array_chunks`) - this risks that they'll rename the methods to the same thing by accident and it'll break anyway
  > * rename the methods to something silly (`bitcoin_as_chunks`) - yeah, the risk above is not there but then we have silly-looking code.

  We've decide to just rename the methods to something that won't possibly collide.

ACKs for top commit:
  tcharding:
    ACK a013700527
  apoelstra:
    ACK a01370052715b6733f07011f28944105493bda63; successfully ran local tests; nice!

Tree-SHA512: cc3359518f97e510da5ee9a33495e26c338bfc3e4162aaffcc72ed9c7daad0daf5e9ca3d23bce50877b0d3881792e98e28d21174a4426bb01281f12285ce08d1
2025-03-08 03:58:10 +00:00
merge-script a959df3edf
Merge rust-bitcoin/rust-bitcoin#4197: Take `spent` closure by value in `count_witness_sigops`
ae0ba6c135 Take spent closure by value in count_witness_sigops and count_p2sh_sigops (jrakibi)

Pull request description:

  This fixes #4141

  Changed `count_witness_sigops` to take the `spent` closure by value instead of `&mut`
  This removes the need for `&mut` when calling the function while still allowing mutable closure to be passed when needed

ACKs for top commit:
  Kixunil:
    ACK ae0ba6c135
  tcharding:
    ACK ae0ba6c135
  apoelstra:
    ACK ae0ba6c1356505697fc5e841741ac488538e3407; successfully ran local tests

Tree-SHA512: 76c5c98994b00412d0d371c07e3e83538f21754129a67889c66e1299e0453defaecb82bd4305297f772d65b042045d3579eaac14f8ea59419bf26b8b0d2ac84f
2025-03-08 01:25:53 +00:00
Tobin C. Harding c707b959b7
Rename timestamp module to time
We just re-named `Timestamp` to `BlockTime`. We have a `units::block`
module but it currently holds abstractions (`BlockHeight` and
`BlockInterval`) that are not onchain abstractions and therefore
somewhat different from the `BlockTime`. Instead of making `block` a
block 'utils' module instead re-name the `timestamp` module to `time`.
2025-03-08 08:30:10 +11:00
Tobin C. Harding e2dee4900f
Re-name Timestamp to BlockTime
We just added a `Timestamp` type without knowing that there was a push
by OpenTimestamps to also create a timestamp and that our new type may
lead to confusion. Our timestamp is explicitly for the `time` field in a
block so we can call it `BlockTime`. This name change makes the module
name stale but we will change that in a following patch to ease review.
2025-03-08 08:28:46 +11:00
merge-script 5581c49e0f
Merge rust-bitcoin/rust-bitcoin#4085: Remove the `GeneralHash` trait
95ad91cdb6 hashes: Remove the GeneralHash trait (Tobin C. Harding)
6426e59c63 Remove unused trait import (Tobin C. Harding)
791501eabc io: Use function in place of GeneralHashExt (Tobin C. Harding)
2b6ef31469 hashes: Add hash_byte_chunks function to modules (Tobin C. Harding)
d3846895d7 hashes: Add hash function to modules (Tobin C. Harding)
e1bac7da55 Bound HmacEngine on HashEngine (Tobin C. Harding)
ab63b7a0ff Add Hash type and finalize method to HashEngine (Tobin C. Harding)
84623ffaf9 Add hash_again regression test (Tobin C. Harding)

Pull request description:

  This is the done as part of #4051.

  Requires some surgery on the `Hmac` and `Hkdf` types as well as a few other patches to maintain the logic that is currently provided by the trait. Final patch is a pure red diff - enjoy.

ACKs for top commit:
  Kixunil:
    ACK 95ad91cdb6
  apoelstra:
    ACK 95ad91cdb64c8870d3eb992090bab7a70d1369b9; successfully ran local tests

Tree-SHA512: bfd215de51c115144c6f5b3430b17dad5d770a8c876fe3775af2828ec620a1f4e4155e63bb18dac244a82c3840413a615d55a0fef91b6949d3de319aa5bb8c2f
2025-03-07 16:35:09 +00:00
NinaLua 2aac5a1f81 Fix some comments
Signed-off-by: NinaLua <iturf@sina.cn>
2025-03-07 17:03:58 +08:00
Tobin C. Harding d1c758f5a4
Add fee_rate::serde re-export
When we added the `fee_rate::serde` module we forgot to re-export it.
This is needed so downstream can do specify serde attributes on struct
fields.

```rust
    #[serde(with = "bitcoin::fee_rate::serde::as_sat_per_kwu")]
    rate: FeeRate,
```
2025-03-07 14:02:40 +11:00
merge-script 631ea1a2a9
Merge rust-bitcoin/rust-bitcoin#4126: Add additional re-exports
5da506b506 Add additional re-exports (Tobin C. Harding)

Pull request description:

  As we do for other types add two new alias' at the crate root of `primitives` and mirror it in `bitcoin`:

  - `BlockVersion`
  - `TransactionVersion`

ACKs for top commit:
  apoelstra:
    ACK 5da506b506aa41b88aa7e8ecdffdcc0478ec72b6; successfully ran local tests
  Kixunil:
    ACK 5da506b506

Tree-SHA512: 5f91e3aae87b1128256b528d20d9aab562bf054131ed8028e0db789e2b487863ad4c89aaca080d0fbcf760dd160815a1843046c5fab0ed1483230fdedc66402e
2025-03-06 23:13:42 +00:00
merge-script fa8eaf0257
Merge rust-bitcoin/rust-bitcoin#4092: Add `Timestamp` newtype
b3f122b399 Add Timestamp newtype (Tobin C. Harding)

Pull request description:

  Bitcoin block headers have a timestamp. Currently we are using a `u32`. While this functions correctly it gives the compiler no chance to enforce type safety.

  Add a `Timestamp` newtype that is a thin wrapper around a `u32`. Document it and test the API surface in `api.rs`.

ACKs for top commit:
  apoelstra:
    ACK b3f122b3996c1a73479be2f95b7f2ae642c9c56f; successfully ran local tests
  Kixunil:
    ACK b3f122b399

Tree-SHA512: 6f4a4a588bc836243ae28f3d36be6c0ae264cb2b7a0061277910b107d05e5ca0e679497d2890208f5d8ec148f37bf263bcd0b0410f9e5e6370d8e763ff30b78a
2025-03-06 19:34:11 +00:00
merge-script febce17eff
Merge rust-bitcoin/rust-bitcoin#4186: Clean up Witness API
Pull request description:

  Enhance Witness struct element access methods:

  - Rename `nth()` to `get()` for clearer slice-like element retrieval
  - Introduce `get_back()` method for flexible reverse indexing
  - Remove redundant `second_to_last()` and `third_to_last()` methods
  - Add `#[track_caller]` to index implementation for better error tracking
  - Update all references to use new method names
  - Improve documentation with usage examples

  The changes provide a more intuitive and consistent approach to accessing witness elements.

  Close #4098

ACKs for top commit:
  Kixunil:
    ACK 3ca3218c23
  tcharding:
    ACK 3ca3218c23
  apoelstra:
    ACK 3ca3218c236c63a9b006047524e2b47e310f07d9; successfully ran local tests

Tree-SHA512: 163e7457f3fe5141373e27a6df5fe1da6f2f05f02e877ef96243510d030d832c0fa86ade781e015a3c392f004651170b60438a83d330f1059457e5ade6478af7
2025-03-06 18:43:27 +00:00
Martin Habovstiak a013700527 Replace uses of `chunks_exact` with `as_chunks`
In the past we've been using `chunks_exact` because const generics were
unstable but then, when they were stabilized we didn't use `as_chunks`
(or `array_chunks`) since they were unstable. But the instability was
only because Rust devs don't know how to handle `0` being passed in. The
function is perfectly implementable on stable. (With a tiny,
easy-to-understand `unsafe` block.) `core` doesn't want to make a
decision for all other crates yet but we can make it for our own crates
because we know that we simply never pass zero. (And even if we did, we
could just change the decision.)

It also turns out there's a hack to simulate `const {}` block in our
MSRV, so we can make compilation fail early.

This commit adds an extension trait to internals to provide the methods,
so we no longer have to use `chunks_exact`. It also cleans up the code
quite nicely.
2025-03-06 19:02:08 +01:00
merge-script a27a6b9592
Merge rust-bitcoin/rust-bitcoin#4201: Make `hex` in `internals` optional
d1d483491f Make `hex` in `internals` optional (Martin Habovstiak)

Pull request description:

  The `hex` crate is not always desirable - e.g. when the consumer wants to work with raw data only. We already had this optional in `hashes` but if `hashes` is going to depend on `internals` it would break this property.

  This change makes `hash` optional, since it's easy: there's just one struct that depends on it.

ACKs for top commit:
  tcharding:
    ACK d1d483491f
  apoelstra:
    ACK d1d483491f5f0181e60778e557fadd99b44b5d30; successfully ran local tests; nice

Tree-SHA512: c20091e6febb49b9114273c280580ddcdafc91893f3c73288b374f51990f09f035a044806dd26777148f2e4341ad082ab05f1b49f8ceb3bcd24eb210ffa5fd5f
2025-03-06 16:50:12 +00:00
jrakibi ae0ba6c135 Take spent closure by value in count_witness_sigops and count_p2sh_sigops
This fixes issue #4141
Change count_witness_sigops and count_p2sh_sigops to take the spent
closure by value instead of &mut

- Changed both functions to accept S as a value (FnMut) instead of &mut S
- Removes need to annotate &mut when calling the function
2025-03-06 16:38:32 +07:00
Tobin C. Harding d3846895d7
hashes: Add hash function to modules
Add a standalone `hash` function that is a drop in replacement for
`GeneralHash::hash`. Do not add it to `hmac` - this is in parity with
the current code because `Hmac` does not implement `GeneralHash::hash`.

Use the new function in `bitcoin` removing all occurrences of
`GeneralHash` from `bitcoin`.

In `hashes` replace usage of `GeneralHash::hash` with the new `hash`
function.
2025-03-06 11:47:29 +11:00
Tobin C. Harding e1bac7da55
Bound HmacEngine on HashEngine
We would like to do away with the `GeneralHash` trait. Currently we
bound `Hmac` and `HmacEngine` on it but this is unnecessary now that we
have added `HashEngine::finalize` and `HashEngine::Hash`.

Bound the `HmacEngine` on `HashEngine` (which has an associated `Hash`
type returned by `finilalize`).

Bound `Hmac` type on `T::Hash` where `T` is `HashEngine`.

Includes some minor shortening of local variable names around hmac
engine usage.

Note this means that `Hmac` no longer implements `GeneralHash`.
2025-03-06 11:47:26 +11:00
merge-script 5ea4eac442
Merge rust-bitcoin/rust-bitcoin#4189: Private key from slice fix
ecc5791930 Add test checking `XOnlyPublicKey::deserialize` (Martin Habovstiak)
bbe87eccf2 Fix bug in PSBT `Deserialize` for `XOnlyPublicKey` (Martin Habovstiak)
8efacd4dda Deprecate `PrivateKey::from_slice` method (Martin Habovstiak)
0d5cd7af43 Add `from_byte_array` to `PrivateKey`. (Martin Habovstiak)
1778fea66e Add a test checking `PrivateKey::from_slice` (Martin Habovstiak)
b87ddc0043 Don't panic in `PrivateKey::from_slice` (Martin Habovstiak)

Pull request description:

  This fixes the bug introduced in #4154 and deprecates `from_slice` method in favor of `from_byte_array` (see commits).

  Closes #4191

ACKs for top commit:
  jamillambert:
    ACK ecc5791930
  apoelstra:
    ACK ecc5791930b88581fcbc8f2417b0221486bd1031; successfully ran local tests

Tree-SHA512: 05117c68db7b46605dba6104ee7696220416f4efaef1fff01843a910037e4c96bfebc45fcdd16f875e5e800bb33af17193c4aa9b0b1593807df5153e7e935c22
2025-03-05 23:07:23 +00:00
Martin Habovstiak ecc5791930 Add test checking `XOnlyPublicKey::deserialize`
This test checks the previous fix - if ordered before the previous
commit it will fail.
2025-03-05 13:09:44 +01:00
Martin Habovstiak bbe87eccf2 Fix bug in PSBT `Deserialize` for `XOnlyPublicKey`
During upgrade of `secp256k1` a number of function calls needed to be
rewritten to not use `from_slice` but `from_byte_array`. Unfortunately,
the conversion wasn't correct and caused panics on invalid inputs
rather than errors.

This fixes it simply by calling `try_into` on the entire slice and
converting the error.
2025-03-05 13:05:04 +01:00
merge-script d1d538ef49
Merge rust-bitcoin/rust-bitcoin#4167: bitcoin: Remove hash type re-exports
852bcf6017 bitcoin: Remove hash type re-exports (Tobin C. Harding)

Pull request description:

  The `{W}PubkeyHash` and `{W}ScriptHash` types are not likely to be used directly by consumers of the library because we have other function that return them and are more ergonomic to use. There is therefor no good reason to re-export them from the crate root.

ACKs for top commit:
  apoelstra:
    ACK 852bcf60178c9491ebcbf13243cf79857592ccec; successfully ran local tests; will one-ACK merge, still does not affect any public API
  Kixunil:
    ACK 852bcf6017

Tree-SHA512: fbc4a7e3e116e962dc4b65b8681343e6ecd485a2289c5798979ce8e6ead4c0b6bda04ae33af1bdd711e852f95896107f9fa3c63d52fd36a6f802de4a7b677073
2025-03-05 01:56:04 +00:00
merge-script ed772d4fc9
Merge rust-bitcoin/rust-bitcoin#4168: Move `opcodes` back to bitcoin
a74393324b Move opcodes back to bitcoin (Tobin C. Harding)

Pull request description:

  Duplicate `opcodes` in `bitcoin` and hide it in `primitives` so we do not have to commit to the API.

  We use opcodes in `impl fmt::Display for Script`.

  Close: #4144

ACKs for top commit:
  apoelstra:
    ACK a74393324bd47f89fd47281d567ab15ab6bcb2ba; successfully ran local tests; sure
  Kixunil:
    ACK a74393324b

Tree-SHA512: 738685b9cd2288a581daa6219e3b21bd48bb4845ea627bf6b8085e0e48f5649ac5ec616a3421d10cd37543f76b66d31f94fd55bf94effc2fb8f91d1ecf5c8611
2025-03-05 00:57:46 +00:00
merge-script 644adb4c18
Merge rust-bitcoin/rust-bitcoin#4177: Manually update nightly version
a4b9c196b1 Manually update nightly version (Tobin C. Harding)

Pull request description:

  While trying to use the `macro_use_imports` lint I found that there is a bug in last weeks nightly. It has been fixed already so lets update.

  Update to todays nightly compiler. Doing so causes some new linter warnings, for now we just allow them.

ACKs for top commit:
  apoelstra:
    ACK a4b9c196b13a2029ef0de198114a29c71a192d03; successfully ran local tests

Tree-SHA512: a982ba05713c214af3b9375ecf37b66aea07ec0330392c30d2dbe3c9b40e1219b587ee43222f2355292b9b374a6a1d8a19d18b559bb47d33e544ed78509a51b8
2025-03-04 21:47:20 +00:00
Martin Habovstiak 8efacd4dda Deprecate `PrivateKey::from_slice` method
Since arrays better convey the intention than slices when parsing
fixed-sized bytes we're migrating to them. This deprecates the
`from_slice` method similarly to how we do it elsewhere.
2025-03-04 21:01:34 +01:00
Martin Habovstiak 0d5cd7af43 Add `from_byte_array` to `PrivateKey`.
Private keys have statically-known length of 32 bytes and we are
migrating types with known lenths to use `from_byte_array` methods. This
adds the method to `PrivateKey` as well and uses it to implement
`from_slice`.
2025-03-04 20:39:47 +01:00
Martin Habovstiak 1778fea66e Add a test checking `PrivateKey::from_slice`
This test checks the previous fix - if ordered before the previous
commit it will fail.
2025-03-04 20:34:59 +01:00
Martin Habovstiak b87ddc0043 Don't panic in `PrivateKey::from_slice`
During upgrade of `secp256k1` a number of function calls needed to be
rewritten to not use `from_slice` but `from_byte_array`. Unfortunately,
the conversions wasn't correct and caused panics on invalid inputs
rather than errors.

This fixes it simply by calling `try_into` on the entire slice and
converting the error.
2025-03-04 20:25:45 +01:00
Erick Cestari 3ca3218c23 witness: Improve element access methods with get() and get_back()
Enhance Witness struct element access methods:

- Rename `nth()` to `get()` for clearer slice-like element retrieval
- Introduce `get_back()` method for flexible reverse indexing
- Remove redundant `second_to_last()` and `third_to_last()` methods
- Add `#[track_caller]` to index implementation for better error tracking
- Update all references to use new method names
- Improve documentation with usage examples

The changes provide a more intuitive and consistent approach to
accessing witness elements.
2025-03-04 12:16:48 -03:00
Martin Habovstiak d1d483491f Make `hex` in `internals` optional
The `hex` crate is not always desirable - e.g. when the consumer wants
to work with raw data only. We already had this optional in `hashes` but
if `hashes` is going to depend on `internals` it would break this
property.

This change makes `hash` optional, since it's easy: there's just one
struct that depends on it.
2025-03-04 12:33:04 +01:00
Tobin C. Harding 852bcf6017
bitcoin: Remove hash type re-exports
The `{W}PubkeyHash` and `{W}ScriptHash` types are not likely to be used
directly by consumers of the library because we have other function that
return them and are more ergonomic to use. There is therefor no good
reason to re-export them from the crate root.
2025-03-04 08:15:04 +11:00
merge-script 5bc08b1d9a
Merge rust-bitcoin/rust-bitcoin#4154: update secp256k1 to 0.30.0
f80cf2cb2a update secp256k1 to 0.30.0 (19年梦醒)

Pull request description:

ACKs for top commit:
  apoelstra:
    ACK f80cf2cb2aa318978da3a6c5df49d82c49344763; successfully ran local tests
  tcharding:
    ACK f80cf2cb2a

Tree-SHA512: 83b8bb72372025c4a4b81c2b7973a7808a4a1d9d6450adef8b60a890e128b2559b55832159c25fa91daac1856049b070cd910d87313fed2851ced9e72ae5ddf5
2025-03-03 19:04:09 +00:00
Tobin C. Harding a4b9c196b1
Manually update nightly version
While trying to use the `macro_use_imports` lint I found that there is a
bug in last weeks nightly. It has been fixed already so lets update.

Update to todays nightly compiler. Doing so causes some new linter
warnings, for now we just allow them.
2025-03-03 15:05:55 +11:00
Tobin C. Harding a74393324b
Move opcodes back to bitcoin
Duplicate `opcodes` in `bitcoin` and hide it in `primitives` so we do
not have to commit to the API.

We use opcodes in `impl fmt::Display for Script`.

Close: #4144
2025-03-03 11:51:28 +11:00
Tobin C. Harding fb29aadc47
Remove bip158 types from crate root
BIP-158 (Compact Block Filters for Light Clients) is not so common as to
require re-exorting its types at the crate root - remove them.
2025-03-03 11:24:37 +11:00
Tobin C. Harding 5da506b506
Add additional re-exports
As we do for other types add two new alias' at the crate root of
`primitives` and mirror it in `bitcoin`:

- `BlockVersion`
- `TransactionVersion`
2025-03-03 08:25:58 +11:00
merge-script 58baee62ca
Merge rust-bitcoin/rust-bitcoin#4155: Automated nightly rustfmt (2025-03-02)
166e37161a 2025-03-02 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 166e37161a2412c716e82f7046da304d4788a3c9; successfully ran local tests

Tree-SHA512: 630978ca16f514379e0851254f2b7137f2612e98d8614e9d2c1170bda805ece5936371a2b1fb4b846f3ec906c5c72c9dc1bf9a4239cc6c3336e8f456108a826f
2025-03-02 17:27:48 +00:00
merge-script 22cecce99d
Merge rust-bitcoin/rust-bitcoin#4149: Fix typos in code comments and documentation
bb8f833ca0 Update instruction.rs (kilavvy)
0ce622e668 Update message.rs (kilavvy)
f61941bbe6 Update serialized_signature.rs (kilavvy)
1d2de62e01 Update mod.rs (kilavvy)

Pull request description:

  This PR fixes several typos in comments across multiple files:

  - Fixed typo `interpretted` -> `interpreted` in `blockdata/script/instruction.rs`
  - Fixed typo `neccessity` -> `necessity` in `p2p/message.rs`
  - Fixed typo `underlflow` -> `underflow` in `taproot/serialized_signature.rs`
  - Fixed typo `ambigous` -> `ambiguous"` in `units/src/amount/mod.rs`

  These changes only affect comments and documentation, no functional code changes.

ACKs for top commit:
  apoelstra:
    ACK bb8f833ca01688eaae75e0fa322f698d34243185; successfully ran local tests; though all these commits could be squashed IMO

Tree-SHA512: d73dc2a86b20de87c0c5efb3e5042e3901c846236670e3a6501f4c93fd54328fef08bfeca276b93642e7b51d04cb8b9c8e1af558f3aabc3c924d60a61e58b031
2025-03-02 15:42:57 +00:00
19年梦醒 f80cf2cb2a
update secp256k1 to 0.30.0 2025-03-02 23:31:48 +08:00
Fmt Bot 166e37161a 2025-03-02 automated rustfmt nightly 2025-03-02 01:22:29 +00:00
merge-script ef9a24eadf
Merge rust-bitcoin/rust-bitcoin#4091: primitives: Hide error internals
43ae9d7516 primitives: Hide script error internals (Tobin C. Harding)
2d8227f091 Hide relative locktime error internals (Tobin C. Harding)

Pull request description:

  Make the struct fields private and add getters.

ACKs for top commit:
  apoelstra:
    ACK 43ae9d751622c7bef548a469466d74cf01284129; successfully ran local tests; nice! Way easier to understand these types with the new incompatible / expected names

Tree-SHA512: cfe67d60ea61a2a4c27b09071a6b11739ca281bf0b4a655121f90215ce38c3a637acf53a6e01aa2ef26fa80004cd919bf3b3334dbd9566ee2f594cab7750b563
2025-03-01 04:44:01 +00:00
merge-script ea56619df5
Merge rust-bitcoin/rust-bitcoin#4114: Policy: Relax MIN_STANDARD_TX_NONWITNESS_SIZE to 65
2fe3687899 Policy: Relax MIN_STANDARD_TX_NONWITNESS_SIZE to 65 (jrakibi)

Pull request description:

  Align with Bitcoin Core's policy by reducing the minimum non-witness transaction size from 82 to 65 bytes.
  This will allow for more minimal transaction cases (eg: 1 input with 1 OP_RETURN output) while maintaining protection against CVE-2017-12842.

  The 65-byte minimum consists of:
  - 60 bytes: minimal tx (10B skeleton + 41B input + 9B output)
  ```
  # Tx Skeleton: 4 [Version] + 1 [InCount] + 1 [OutCount] + 4 [LockTime] = 10 bytes
  # Blank Input: 32 [PrevTxHash] + 4 [Index] + 1 [scriptSigLen] + 4 [SeqNo] = 41 bytes
  # Output:      8 [Amount] + 1 [scriptPubKeyLen] = 9 bytes
  ```

  - 5 bytes: minimum `scriptPubKey`

  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/4108

ACKs for top commit:
  tcharding:
    ACK 2fe3687899
  Kixunil:
    ACK 2fe3687899

Tree-SHA512: d98fae7ebde5060c7ad8a3555df094e8ed93486c371b1bd43302bbd1b6b2d86c442803dcbcc5b30f3692224a888abc4e49b7b5414d9852317730628a139fbd40
2025-03-01 03:35:47 +00:00
merge-script 65181257bc
Merge rust-bitcoin/rust-bitcoin#4111: Add support for pay to anchor outputs
f7ea6e50b5 Add support for pay to anchor outputs (Erik De Smedt)

Pull request description:

  Add support for the newly created Pay2Anchor output-type which was introduced in bitcoin 28.0

  See https://github.com/bitcoin/bitcoin/pull/30352

ACKs for top commit:
  Kixunil:
    ACK f7ea6e50b5
  apoelstra:
    ACK f7ea6e50b578238b0a7ff421d18d7c7f71d43278; successfully ran local tests

Tree-SHA512: cd3da860e81bd25e6fef72a9118b43d647af2339e9d226c124fa221f63d9c3149189480d40368d38900a999bf59a23fd5302025751ea1bebfea059b4fab21c0b
2025-02-28 20:40:25 +00:00
merge-script 2951345426
Merge rust-bitcoin/rust-bitcoin#4129: Move taproot back to bitcoin crate
cf12ba262a Move taproot back to bitcoin crate (Tobin C. Harding)

Pull request description:

  I don't know what I was thinking when I move the taproot hash types to `primitives`. As correctly pointed out by Kix we agreed to only have blockdata in `primitives`.

  Move the taproot hash types back to `bitcoin::taproot` and remove the extension traits.

ACKs for top commit:
  Kixunil:
    ACK cf12ba262a
  apoelstra:
    ACK cf12ba262a646a6341098ee3f4c178a52fc90211; successfully ran local tests

Tree-SHA512: 0c5eabf395c05a93603a46b277c6ea2cc547f3894eef182fceb80f309123d67fe457936a388bac0249ec24cae7521eaef3bf8bd8facca5282e4ce2ea6fafd5f7
2025-02-28 14:10:13 +00:00
kilavvy bb8f833ca0
Update instruction.rs 2025-02-28 12:35:52 +01:00
kilavvy 0ce622e668
Update message.rs 2025-02-28 12:35:17 +01:00
kilavvy f61941bbe6
Update serialized_signature.rs 2025-02-28 12:34:45 +01:00
merge-script e406eceebe
Merge rust-bitcoin/rust-bitcoin#4118: fix: typos in documentation files
83bd83385e Update sighash.rs (leopardracer)
fc4ea87429 Update transaction.rs (leopardracer)
0e70e85a1a Update key.rs (leopardracer)
f7c28ab44d Update input_string.rs (leopardracer)

Pull request description:

  This pull request contains changes to improve clarity, correctness and structure.
  - Corrected typos in multiple files (e.g., `transacton` → `transaction`, `function panics` formatting).
  - Improved readability of comments in `transaction.rs`, `key.rs`, and `input_string.rs.`
  - No functional code changes, only documentation updates.

  This message provides a clear summary of what was done and why the changes were made. Let me know if you need any modifications!

ACKs for top commit:
  Kixunil:
    ACK 83bd83385e
  tcharding:
    ACK 83bd83385e
  apoelstra:
    ACK 83bd83385e0bbad993f96f5abbe51f71d199aad1; successfully ran local tests

Tree-SHA512: ec0cae0357dd8c60e9286db71c3d18dfa6ba7acbaa27cce78c0586ab73c9631ea58ab453acd85b18d4edd72dcc578a4e08d69b9529e856837d5651efa288a4db
2025-02-27 19:02:22 +00:00
merge-script 889a2668d4
Merge rust-bitcoin/rust-bitcoin#4100: Witness taproot fixes
a8168c3f81 Add `taproot_leaf_script` methood to `Witness` (Martin Habovstiak)
59f21a291f Add a test case checking `taproot_control_block` (Martin Habovstiak)
e810ecff7c Fix key/script spend detection in `Witness` (Martin Habovstiak)

Pull request description:

  Fixes #4097

  High priority because it blocks a bunch of work and should be probably swiftly backported.

  My plan is to backport this entire PR and then in the breaking version remove the broken `tapscript` method entirely. Keeping it around would be way too confusing if we're going to have tagged script.

ACKs for top commit:
  tcharding:
    ACK a8168c3f81
  apoelstra:
    ACK a8168c3f81a76165022af3f3aeec82317d8a6bf1; successfully ran local tests

Tree-SHA512: 9e3c065f045664c7e4fdf8ba4d9e7dc9281a59eda1187f39b297861714007d58e9e5071c37e1f3b16d171372313ae06aca2d98425a06a6be557718d9f73c0e14
2025-02-27 17:44:12 +00:00
Erik De Smedt f7ea6e50b5 Add support for pay to anchor outputs
Add support for the newly created Pay2Anchor output-type.

See https://github.com/bitcoin/bitcoin/pull/30352
2025-02-27 08:57:00 +01:00
merge-script 5b6eebda8a
Merge rust-bitcoin/rust-bitcoin#4117: Grab missing changelog
2d6ee57d9c Grab missing changelog (Tobin C. Harding)

Pull request description:

  We release `v0.32.4` and `v0.32.5` already but forgot to merge the changelog entries back into master.

  Grab the missing changelog entries from the `0.32.x` release branch.

ACKs for top commit:
  Kixunil:
    ACK 2d6ee57d9c
  apoelstra:
    ACK 2d6ee57d9c67a49a39eaf89781129a63c7d05e1a; successfully ran local tests

Tree-SHA512: bfa4d8ef2c58f1cc74c6a027ed33bab70a660c3bfcf2d2a72839741a3beb5ef6a355e1ca99e3aaa24a77a48f8c678a84d1c0bf7e5e30c1b33496b866e8667351
2025-02-26 22:55:37 +00:00
jrakibi 2fe3687899 Policy: Relax MIN_STANDARD_TX_NONWITNESS_SIZE to 65
Align with Bitcoin Core's policy by reducing the minimum non-witness
transaction size from 82 to 65 bytes. This change allows for more
minimal transaction cases (e.g., 1 input with 1 OP_RETURN output),
while still maintaining protection against CVE-2017-12842.

Matches bitcoin/bitcoin#26398
2025-02-27 00:20:08 +07:00
Tobin C. Harding cf12ba262a
Move taproot back to bitcoin crate
I don't know what I was thinking when I move the taproot hash types to
`primitives`. As correctly pointed out by Kix we agreed to only have
blockdata in `primitives`.

Move the taproot hash types back to `bitcoin::taproot` and remove the
extension traits.
2025-02-26 15:45:49 +11:00
Tobin C. Harding 43ae9d7516
primitives: Hide script error internals
As part of the 1.0 effort and forward maintainability hide the internals
of the two error types in the `script` module. Add getters to get at the
invalid size.
2025-02-26 11:05:07 +11:00
merge-script e80ce4a89c
Merge rust-bitcoin/rust-bitcoin#4113: Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core
7ab2f5be40 Add test for sighash_single_bug incompatility fix (Liu-Cheng Xu)
5d38073afb Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core (Liu-Cheng Xu)

Pull request description:

  Close https://github.com/rust-bitcoin/rust-bitcoin/issues/4112

ACKs for top commit:
  tcharding:
    ACK 7ab2f5be40
  Kixunil:
    ACK 7ab2f5be40
  apoelstra:
    ACK 7ab2f5be4076c22e60eefcaa943444808eae3e3f; successfully ran local tests

Tree-SHA512: d47143d188653d3e845951e64e9b410fdbdac8e51906f33532b8d71519f0bd1454a46135dfdd6073a6d1ced9854dc3e13f3c35de60b7fdd45c22ef37f9a0fc75
2025-02-25 17:36:12 +00:00
leopardracer 83bd83385e
Update sighash.rs 2025-02-25 12:10:51 +02:00
leopardracer fc4ea87429
Update transaction.rs 2025-02-25 12:10:23 +02:00
leopardracer 0e70e85a1a
Update key.rs 2025-02-25 12:03:35 +02:00
Tobin C. Harding 2d6ee57d9c
Grab missing changelog
We release `v0.32.4` and `v0.32.5` already but forgot to merge the
changelog entries back into master.

Grab the missing changelog entries from the `0.32.x` release branch.
2025-02-25 19:35:43 +11:00
Liu-Cheng Xu 7ab2f5be40 Add test for sighash_single_bug incompatility fix 2025-02-25 10:56:29 +08:00
Liu-Cheng Xu 5d38073afb Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core 2025-02-25 10:54:47 +08:00
Tobin C. Harding b3f122b399
Add Timestamp newtype
Bitcoin block headers have a timestamp. Currently we are using a
`u32`. while this functions correctly it gives the compiler no chance
to enforce type safety.

Add a `Timestamp` newtype that is a thin wrapper around a `u32`.
Document it and test the API surface in `api.rs`.
2025-02-25 10:03:28 +11:00
Martin Habovstiak a8168c3f81 Add `taproot_leaf_script` methood to `Witness`
We already have `tapscript` method on `Witness` which is broken because
it doesn't check that the leaf script is a tapscript, however that
behavior might have been intended by some consumers who want to inspect
the script independent of the version. To resolve the confusion, we're
going to add a new method that returns both the leaf script and, to
avoid forgetting version check, also the leaf version.

This doesn't touch the `tapscript` method yet to make backporting of
this commit easier. It's also worth noting that leaf script is often
used together with version. To make passing them around easier it'd be
helpful to use a separate type. Thus this also adds a public POD type
containing the script and the version. In anticipation of if being
usable in different APIs it's also generic over the script type.
Similarly to the `tapscript` method, this also only adds the type and
doesn't change other functions to use it yet. Only the newly added
`taproot_leaf_script` method uses it now.

This is a part of #4073
2025-02-24 18:33:55 +01:00
Martin Habovstiak 59f21a291f Add a test case checking `taproot_control_block`
The previous commit fixed a bug when `taproot_control_block` returned
`Some` on key-spends. This adds a test case for it which succeeds when
applied after the previous commit and fails if applied before it.
2025-02-24 18:33:31 +01:00
Martin Habovstiak e810ecff7c Fix key/script spend detection in `Witness`
The `taproot_control_block` did not properly detect whether it deals
with script spend or key spend. As a result, if key spend with annex was
used it'd return the first element (the signature) as if it was a
control block.

Further, the conditions identifying which kind of spend it was were
repeated multiple times but behaved subtly differently making only
`taproot_control_block` buggy but the other places confusing.

To resolve these issues this change adds a `P2TrSpend` enum that
represents a parsed witness and has a single method doing all the
parsing. The other methods can then be trivially implemented by matching
on that type. This way only one place needs to be verified and the
parsing code is more readable since it uses one big `match` to handle
all possibilities.

The downside of this is a potential perf impact if the parsing code
doesn't get inlined since the common parsing code has to shuffle around
data that the caller is not intersted in. I don't think this will be a
problem but if it will I suppose it will be solvable (e.g. by using
`#[inline(always)]`).

The enum also looks somewhat nice and perhaps downstream consumers could
make use of it. This change does not expose it yet but is written such
that after exposing it the API would be (mostly) idiomatic.

Closes #4097
2025-02-24 18:33:13 +01:00
merge-script 81deddd0a9
Merge rust-bitcoin/rust-bitcoin#4099: Make transaction::Version field private
cb270eae8e Make transaction::Version field private (jrakibi)
6c69b66b0d Use Version constant (jrakibi)

Pull request description:

  This commit addresses #4041 by making the `transaction::Version` field private.

  This forces people to either use the associated constants (`Version::ONE/TWO/THREE`) or the `non_standard`/`from_consensus` methods for any other transaction version.

  This aligns with our approach to `block::Version`

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

Tree-SHA512: dcf5b50dfeda04e56fec350acd735dcb7099989b552afce4261d559a8a846c0eb3705369ad635ef9bbbfb2373d203a2c3641178925de6685426aa91245db9a8c
2025-02-24 16:37:16 +00:00
Jose Storopoli 82a8aefc53
sighash: Document witness_script in p2wsh 2025-02-24 08:15:42 -03:00
jrakibi cb270eae8e Make transaction::Version field private
This commit addresses #4041 by making the transaction::Version field private

Changes:
- Make the `Version` field private with `pub(crate)`
- Rename `non_standard` to `maybe_non_standard` for clarity since it accepts both standard and non-standard versions
- Add `#[inline]` attributes to small, frequently used methods:
  - `as_u32`
  - `maybe_non_standard`

Users now must use either:
- Constants (`Version::ONE/TWO/THREE`) for standard versions
- `maybe_non_standard` method for any version (standard or non-standard)
2025-02-24 06:54:21 +07:00
jrakibi 6c69b66b0d Use Version constant
Replace Version(2) with predefined Version::TWO constant
2025-02-23 04:19:48 +07:00
merge-script f71e28eea2
Merge rust-bitcoin/rust-bitcoin#4047: Properly deprecate `Hash::from_slice`
f61e93ccf1 Properly deprecate Hash::from_slice (Tobin C. Harding)
50c0af7138 Stop using Hash::from_slice (Tobin C. Harding)

Pull request description:

  The `hashes::error::FromSliceError` error is only returned from `from_slice`. We attempted to deprecate this function but it seems we only did half a job at it.

  - deprecate _all_ instances of the method/function
  - deprecate the error type
  - stop using the deprecated functions in `bitcoin`

  Close: #4053

ACKs for top commit:
  apoelstra:
    ACK f61e93ccf1db7e7e3c9604fdb09b4e25195d88b2; successfully ran local tests

Tree-SHA512: 61a0e5127019859776ffac66bd4d320c86b8462bb1e908127d0bf42896aaa8df85fd2b06850342b694ca1cd68ed50355c81cad6ae3e9a5fd6e3933efe85498ad
2025-02-19 15:36:40 +00:00
Tobin C. Harding 4259dab93a
Remove rust-ordered dependency
It has turned out that the `rust-ordered` crate and it's
`ArbitraryOrd` trait are only useful for locktimes and only marginally
useful for them at best.

Remove the `ArbitraryOrd` impls and the `rust-ordered` dependency.

This topic was discussed in various places including:

- #2500
- #4002
- #3881

Close: #4029
2025-02-18 13:36:07 +11:00
Tobin C. Harding 50c0af7138
Stop using Hash::from_slice
This function is deprecated, stop using it in favour of
`Hash::from_byte_array`.

Patch only touches test code, I'm guessing that is why lint warnings
were no showing up.
2025-02-17 12:39:11 +11:00
merge-script 15a385a5b2
Merge rust-bitcoin/rust-bitcoin#4063: Automated nightly rustfmt (2025-02-16)
0fbfe68c72 2025-02-16 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 0fbfe68c72496c3135108914cdb2c2b45e496e99; successfully ran local tests
  tcharding:
    ACK 0fbfe68c72

Tree-SHA512: 08ac71f7cf0259dcda0d7fd127ad8485e65edb06307a09ae03506f61fb917faf0c8c721b2299f15a370d6fc124cf35488f6410ddf15d522371660e6d97fd848b
2025-02-16 20:54:39 +00:00
merge-script 987a74cd96
Merge rust-bitcoin/rust-bitcoin#4007: Introduce monadic `NumOpResult` type
6244cb75fa Introduce monadic AmountOpResult (Tobin C. Harding)

Pull request description:

  We would like to return an error when doing math ops on amount types. We cannot however use the stdlib `Result` or `Option` because we want to implement ops on the result type.

  Add an `NumOpResult` type. Return this type from all math operations on `Amount` and `SignedAmount`.

  Implement `core::iter::Sum` for the new type to allow summing iterators of amounts.

  Note please this removes `AddAssign` impls for amount types.

ACKs for top commit:
  apoelstra:
    ACK 6244cb75faf62aed4b47d63a59d14cb766e4e7a8; successfully ran local tests; let's do it -- but definitely want the followup issues addressed
  Kixunil:
    ACK 6244cb75fa

Tree-SHA512: 7a105acb1aa492ab3e97d94ae182ac4c30a364edd183f71cc320cf80d85060049e8caf1e5736ef6d1af32f39c3376f21382afe35ac65ea1b8c15130c622d9d64
2025-02-16 17:04:16 +00:00
Fmt Bot 0fbfe68c72 2025-02-16 automated rustfmt nightly 2025-02-16 01:21:38 +00:00
ndungudedan 00cd247bc4
Validate compressed WIF keys
For private WIF keys corresponding to a compressed address,
the last byte of the key needs to be 0x01, but the API
doesn't enforce this when using PrivateKey::from_wif(). So,
invalid keys can be accepted.

Thus we check if the last byte is equivalent to 0x01
if the key's length is 34 (which indicates it's
compressed).
2025-02-13 11:45:25 +03:00
Andrew Toth 0acd2b5e89
primitives: store transaction::Version as u32 instead of i32 2025-02-12 09:55:33 -05:00
Tobin C. Harding fd4586eaae
Invert dependency between io and hashes
Currently in order to release `hashes v1.0` we need to 1.0 `io` as well.
For multiple reasons, many out of our control, the `io` crate may not
stabalise any time soon.

Instead we can invert the dependency between the two crates.

This is an ingenious idea, props to Kixunil for coming up with it.

Notes

- `io` does not currently re-export the `hashes` crate.
- This work highlights that we cannot call `hash_reader` on a siphash.
- The `Hmac::hash_reader` uses the default key which may not be obvious.

Signed-off-by: Tobin C. Harding <me@tobin.cc>
2025-02-11 09:17:21 +11:00
Tobin C. Harding 6244cb75fa
Introduce monadic AmountOpResult
We would like to return an error when doing math ops on amount types.
We cannot however use the stdlib `Result` or `Option` because we want to
implement ops on the result type.

Add an `AmountOpResult` type. Return this type from all math operations
on `Amount` and `SignedAmount`.

Implement `core::iter::Sum` for the new type to allow summing iterators
of amounts - somewhat ugly to use, see tests for example usage.
2025-02-10 10:06:59 +11:00
merge-script 329aaf452b
Merge rust-bitcoin/rust-bitcoin#4010: Add a tagged hash engine
6002ccdc56 Add a tagged sha256t hash engine (Tobin C. Harding)
3e8e2e46bf Use Self::Engine in GeneralHash impl (Tobin C. Harding)
a0211906fe sha256t: Remove standalone from_engine function (Tobin C. Harding)
5ce8781162 Remove the Tag::engine method (Tobin C. Harding)
ba6425947f hashes: Use associated cost for pre-tagging (Tobin C. Harding)
613fddc82b Delete deprecated sha256t_hash_newtype macro (Tobin C. Harding)

Pull request description:

  Add a new hash engine to the `sha256t` module and put the tag on it.

  Note the issue suggests adding the tag to `sha256::HashEngine` but this PR adds a new type to `sha256t` and adds the tag on it.

  Resolve: #1552

ACKs for top commit:
  apoelstra:
    ACK 6002ccdc567b26c69f6b55173f5d11b95b4d6966; successfully ran local tests
  Kixunil:
    ACK 6002ccdc56

Tree-SHA512: 8a8945bdb89841b87af2eb94073b4d01993338f34a7a3e919bccf4e8edea0fe9d5d2818b6484700b2e72143f315633338692a436149935c5156b77757b025f5e
2025-02-09 20:04:56 +00:00
Tobin C. Harding 6002ccdc56
Add a tagged sha256t hash engine
We would like it if two different pre-tagged engines were considered
different types so it is not possible to mix them up.

Add a new `sha256t::HashEngine<T>` where `T` is a tag the same as on
`sha256t::Hash<T>`.
2025-02-08 13:27:30 +11:00
Tobin C. Harding 5ce8781162
Remove the Tag::engine method
Now we have an associated const we can do away with the `engine` trait
method all together. Users can call `Hash<FooTag>::engine` instead. This
is better because its an API more similar to the other hash types and
therefor easier to discover and remember.
2025-02-08 13:27:30 +11:00
yancy ee0486a43f refactor: use convenience method to calculate fee
The FeeRate module provides a convenience method to calculate the fee.
2025-02-06 16:19:32 -06:00
merge-script 16cf1f7f2a
Merge rust-bitcoin/rust-bitcoin#3989: Do `hashes` file re-org
85e0330d7f Run the formatter (Tobin C. Harding)
7be0db730a hashes: Move bench and test code into files (Tobin C. Harding)
665fa9de99 hashes: Pull crypto out into submodule (Tobin C. Harding)
1bfd1e071a hashes: Make module subdirectories (Tobin C. Harding)

Pull request description:

  This is an attempt at point 3 in https://github.com/rust-bitcoin/rust-bitcoin/pull/3961#issuecomment-2619946074

  However instead of using `api.rs` and `implementation.rs` it uses `crypto.rs` for the cryptography stuff and leaves the rest in `mod.rs`.

  This whole PR is internal changes only. Almost entirely code moves, review is easy if you have your `diff` configured nicely.

ACKs for top commit:
  Kixunil:
    ACK 85e0330d7f
  apoelstra:
    ACK 85e0330d7f057c9fe447bff5fdb6023150ead319; successfully ran local tests; look great! thanks!

Tree-SHA512: e52e6410e86fc93ec34a72be8c64f02148f46958f8f5c1850075b1a7f41886c220f09144ccd05a98a551c356a5d25524c8884fc8578a205b27f385ec0725f13b
2025-02-06 16:08:17 +00:00
merge-script a380d4b9de
Merge rust-bitcoin/rust-bitcoin#4006: Rename fee_wu and remove fee_vb
a7526b6a70 Remove `fee_vb` (yancy)
73b14d03b9 Add `to_fee` in place of `fee_wu` (yancy)

Pull request description:

  Rename fee_wu and remove fee_vb

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3908

ACKs for top commit:
  apoelstra:
    ACK a7526b6a70642e04f0a8ad8a89aa368031fdb6cd; successfully ran local tests
  Kixunil:
    ACK a7526b6a70

Tree-SHA512: fd865525e56caddc49158fd94fb125d92fd09654d5c72609a6f8e34370e79a9be4213dbd7e69b0d8498d92fca8a970142262f56b63cebd76c200aca75b6e0ca6
2025-02-05 20:15:08 +00:00
Tobin C. Harding 85e0330d7f
Run the formatter
Run `just fmt`, no other manual changes.
2025-02-06 07:12:17 +11:00
merge-script 5fef0dd5d4
Merge rust-bitcoin/rust-bitcoin#3985: Minor: Fix policy module link
5cb1a1d565 Fix policy module link (Peter Todd)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 5cb1a1d565
  apoelstra:
    ACK 5cb1a1d5650caaaa6df00aba6d1a57340097991c; successfully ran local tests

Tree-SHA512: 6c9b20819376920ff251092bc999e7bc323c09fd7f5bc633a8e47f43d6b3ba0e875ca398e36448c02358dc2c9735b5da5aab18d497a99a2ceb6a40d167920194
2025-02-05 19:29:23 +00:00
yancy a7526b6a70 Remove `fee_vb`
This is redundant given Weight::from_vb is provided.  After converting
to a weight_unit, use to_fee().
2025-02-05 09:05:51 -06:00
yancy 73b14d03b9 Add `to_fee` in place of `fee_wu`
Use the more idiomatic to_fee instead of `fee_wu`.  Since the method
takes a strongly typed argument, remove `wu` from the method name
to improve clarity.
2025-02-04 18:34:52 -06:00
Peter Todd 5cb1a1d565
Fix policy module link 2025-02-04 21:31:44 +00:00
Jamil Lambert, PhD 55ab2f34a7
Remove manual implementation of ok
New rustc nightly gives error: manual implementation of `ok`.

Remove it.
2025-02-04 16:47:43 +00:00
Fmt Bot 282bc14d6e 2025-02-02 automated rustfmt nightly 2025-02-02 01:18:01 +00:00
merge-script 5ac27b91a1
Merge rust-bitcoin/rust-bitcoin#3984: Fix spelling of `invalid_legcay_address_length`
5fd80581eb Fix spelling of `invalid_legcay_address_length` (Peter Todd)

Pull request description:

  Incorrect spelling kept as a deprecated, hidden, function.

ACKs for top commit:
  tcharding:
    ACK 5fd80581eb
  Kixunil:
    ACK 5fd80581eb
  apoelstra:
    ACK 5fd80581eb9167195bbb4552fbd2210c40b22b7b; successfully ran local tests

Tree-SHA512: 697685a04046a90a22ba0181a64698294d7fb08918c960b707e0087864a36cf00768d7205e452becff7084a87a655d6226b7ddd0c8aa08b4bec71d23399a7f5d
2025-02-01 11:44:57 +00:00
Peter Todd 5fd80581eb
Fix spelling of `invalid_legcay_address_length`
Incorrect spelling kept as a deprecated, hidden, function.
2025-01-30 02:37:59 +00:00
Martin Habovstiak a9ffb1571c Stop using `TxOut::NULL` in tests
We want to get rid of this constant, so we replace it in tests with 0
amount, empty script. Notably, the tests were already using it as a
dummy value - the exact amount was irrelevant, so this change doesn't
break anything.
2025-01-29 23:02:41 +01:00
Martin Habovstiak 313406d6ab Optimize `encode_signing_data_to_inner`
The `encode_signing_data_to_inner` function previously constructed a
transaction internally, requiring a bunch of allocations, which it'd
then consensus-serialize into a writer (hasher). It also used a dummy
`TxOut::NULL` value which we want to get rid of.

To get rid of both allocations and the NULL value we serialize the
transaction on-the-fly. Because the encoding doesn't involve witnesses
it's not too complicated and the consensus encoding will never change so
there are no desync bugs possible. We may later change this to an
abstract transaction though.
2025-01-29 22:51:40 +01:00
merge-script 2f09da8e47
Merge rust-bitcoin/rust-bitcoin#3950: Use MAX_MONEY in serde regression test
4dad4730a8 Add unreleased changelog entry (Tobin C. Harding)
08bb57e499 Use MAX_MONEY in serde regression test (Tobin C. Harding)

Pull request description:

  We plan on enforcing MAX_MONEY in the amount types. In preparation use MAX_MONEY in the serde regression test instead of the arbitrary, and too large DEADBEEFCAFEBABE value.

ACKs for top commit:
  apoelstra:
    ACK 4dad4730a81f9f08a565f196e5699f054e28f977; successfully ran local tests

Tree-SHA512: 10fefdb75289f4c93537e031410eb729420729563b262d6104948a0176a78c4f6130956b550cc08ffc924b1bce1c42f0544542879a4d44165022196ef695718a
2025-01-26 14:54:13 +00:00
Tobin C. Harding 4dad4730a8
Add unreleased changelog entry
We want to be sure to mention the new MAX_MONEY invariant and the fact
that we silently broke serde while doing it.
2025-01-25 06:52:26 +11:00
Tobin C. Harding 08bb57e499
Use MAX_MONEY in serde regression test
We plan on enforcing MAX_MONEY in the amount types. In preparation use
MAX_MONEY in the serde regression test instead of the arbitrary, and too
large DEADBEEFCAFEBABE value.
2025-01-24 12:15:13 +11:00
Tobin C. Harding 00b71a670f
Use from_sat_unchecked for hardcoded ints
We have an `_unchecked` amount constructor that makes no assumptions
about the argument. We would like to start enforcing MAX_MONEY but the
diff to introduce this is massive. In an effort to make it smaller we
can do all the hardcoded ints first. We did this already but a bunch
more snuck in or were missed.

In any amount constructor that passes in a hardcoded const as a decimal
integer (i.e., not hex) use the `_unchecked` version.

Done in preparation for enforcing MAX_MONEY.
2025-01-24 09:05:00 +11:00
Tobin C. Harding dbec9807f9
Shorten identifiers by removing _in_sats
Using `_in_sats` is not meaningful because an `Amount` is an amount,
period.

Remove `_in_sats` and `_IN_SATS` from identifiers.

Internal change only.
2025-01-24 08:54:24 +11:00
Tobin C. Harding 154a4420fc
Stop using FQP on Amount type
Using `crate::Amount` adds no additional information - stop doing that.

Internal change only.
2025-01-24 08:54:24 +11:00
Tobin C. Harding 8e16a48252
Run the formatter
Manually run `just fmt` - no other changes.
2025-01-24 08:54:24 +11:00
Jamil Lambert, PhD 20d3f16a54
Fix clippy lint in new rustc nightly
New nightly lint warning "called `Iterator::last` on a
`DoubleEndedIterator`; this will needlessly iterate the entire iterator"

Code that gives the warning is correct, allow the lint to remove the
warning.

Update rustc nightly to 2025-01-16
2025-01-22 10:13:26 +00:00
Tobin C. Harding 7277092af4
Remove mutagen
Back in 2022 we elected to use `mutagen` for mutation testing. Since
then `cargo mutants` has progressed to a point where we would now like
to use it instead.

Remove all the `mutagen` stuff and update the lock files.

Close: #2829
2025-01-21 09:43:12 +11:00
merge-script 05c28cf31d
Merge rust-bitcoin/rust-bitcoin#3915: Add FIFTY_BTC const to the amount types
e0028239cf api: Run just check-api (Tobin C. Harding)
5eb5941215 Add FIFTY_BTC const to the amount types (Tobin C. Harding)

Pull request description:

  The mining reward for the first epoc is 50 bitcoin. For mainnet this is old news but for regtest it is still relevant.

  Add and use a new const `FIFTY_BTC` to the `Amount` type. To keep the amount types uniform also add it to the `SignedAmount`.

ACKs for top commit:
  storopoli:
    ACK e0028239cf
  jamillambert:
    ACK e0028239cf
  apoelstra:
    ACK e0028239cf207660deb2873a92bacfa1315af634; successfully ran local tests

Tree-SHA512: 623ed8b1f5fe8dd95309179308fea83d68be4349becf6305769b0378cc9032961df2c062dc2bf702fec5e2394e8abb7360d2be6f19b6cf505db8769a5ae39e16
2025-01-20 02:42:45 +00:00
Fmt Bot fec6a7669e 2025-01-19 automated rustfmt nightly 2025-01-19 01:19:33 +00:00
Tobin C. Harding 5eb5941215
Add FIFTY_BTC const to the amount types
The mining reward for the first epoc is 50 bitcoin. For mainnet this is
old news but for regtest it is still relevant.

Add and use a new const `FIFTY_BTC` to the `Amount` type. To keep the
amount types uniform also add it to the `SignedAmount`.
2025-01-17 10:08:09 +11:00
merge-script 6bc12e9363
Merge rust-bitcoin/rust-bitcoin#3864: Update for ParsePublicKeyError Display and Error impl
b11ace359a Fix up ParsePublickeyError (Innocent Onyemaenu)

Pull request description:

  Resolves #3835

  In #945fcd09 we forgot to Update impl for ParsePublicKeyError Display and Error traits

ACKs for top commit:
  tcharding:
    ACK b11ace359a
  apoelstra:
    ACK b11ace359a52d9137b8fe5919d9825d61e3e9fad; successfully ran local tests; thanks!!

Tree-SHA512: 5c4e5e113605bc5b9c3c0d2ca65a0fdae80726a0f2f791255cc1d6567812a9132ff4f5bd8f30276fef916469d5fd29f6d72ddf4adb6340e52f4e0198ac4fb371
2025-01-16 01:37:12 +00:00
Innocent Onyemaenu b11ace359a Fix up ParsePublickeyError
In #3847 we added an `InvalidCharError` into one of the variants of
`ParsePublicKeyError` but we forgot to update the trait
implementations.

Fix the `error::Error` and `Display` implementations for
`ParsePublicKeyError`. While we are at it match on `*self` as is
typical in this codebase.

 With this applied #3835 is fully resolved.

 Close: #3835
2025-01-15 21:00:55 +01:00
Tobin C. Harding 39523ea1f5
units: Remove InputString from the public API
Currently `InputString` is in the public API of `units` because of the
trait bound on `parse::int()`. We can just do the monomorphisisation
manually to remove it.

This patch renames `int` to have three different names, one for `&str`
one for `String`, and one for `Box<str>`.
2025-01-15 08:09:38 +11:00
Tobin C. Harding 668056fb36
Use Amount in examples
Use the `Amount` type as parameter in examples code instead of a `u64`.

Done as part of preparation for enforcing MAX_MONEY.
2025-01-14 09:48:48 +11:00
Fmt Bot 8bdd67a368 2025-01-12 automated rustfmt nightly 2025-01-12 01:23:13 +00:00
merge-script 93cd9a4f74
Merge rust-bitcoin/rust-bitcoin#3891: typo: `Address::is_valid_for_network`
1e0c065740 typo: Address::is_valid_for_network (Jose Storopoli)

Pull request description:

  I believe this is a typo.

ACKs for top commit:
  apoelstra:
    ACK 1e0c065740266a8ad207a95f4d964394ba3f07d7; successfully ran local tests
  tcharding:
    ACK 1e0c065740

Tree-SHA512: aff71b91dc707c0c6c723e35510d284fd9a60895b7f12828819eb0d4520216491a2cf054b59872ac4be175d566674a32bde8c170f43beb6fc15dd19e6be12e45
2025-01-11 16:01:15 +00:00
merge-script 6a4ca4e61a
Merge rust-bitcoin/rust-bitcoin#3878: bitcoin: Add an example of doing I/O to encode/decode
706a135de6 bitcoin: Add an example of doing I/O to encode/decode (Tobin C. Harding)

Pull request description:

  In an effort to improve the documentation on `bitcoin_io` add an example in `bitcoin` crate that demonstrates a few things:

  - Encode/Decode a `rust-bitcoin` type to/from a stdlib type.
  - Encode to a custom type by implementing `bitcoin_io` traits.
  - Encode to a foreign custom type by using the `bitcoin_io::bridge::FromStd` wrapper.

  Later we can link to this example online in the `bitcoin_io` docs.

ACKs for top commit:
  apoelstra:
    ACK 706a135de6ca2be0231c2f62f4f2b156e08c9a49; successfully ran local tests

Tree-SHA512: cd3ff4067d5b86031255cb31fe17dea3fd22479699d32efad93c359de465ef6250f29aa4c43ce4218ae623fa6c355661960c11908a729a895655319141b82852
2025-01-11 14:46:13 +00:00
Jose Storopoli 1e0c065740
typo: Address::is_valid_for_network
I believe this is a typo.
2025-01-10 11:11:43 -03:00
merge-script 653ea702d6
Merge rust-bitcoin/rust-bitcoin#3792: Add BIP324 V2 p2p network message support
e09bdb5f98 Add BIP324 V2 p2p network message support (Nick Johnson)

Pull request description:

  Migrating over the BIP324's library's V2 network message encoding and decoding. As discussed over in https://github.com/rust-bitcoin/rust-bitcoin/discussions/2959, this is probably the natural home for it and also cleans up some gross copy/pasta of some of the encoding/decoding chain logic.

  This patch adds `V2NetworkMessage` which wraps a `NetworkMessage`, but handles the V2 encoding and decoding. It is a parallel of the existing `RawNetworkMessage` (which mentioned before, may be better described as `V1NetworkMessage` https://github.com/rust-bitcoin/rust-bitcoin/issues/3157). A priority of this patch was to not re-invent any wheels and try to use the existing patterns as much as possible.

ACKs for top commit:
  tcharding:
    ACK e09bdb5f98
  apoelstra:
    ACK e09bdb5f98ea516382a04283373ad97a41d57c2b; successfully ran local tests; nice!

Tree-SHA512: a5078d4d3deb04c2e06ea513bbc8c97d0e6d5da5b029847a97b3f90bf55a263858dd16d88299f853aa3c468f7b9bceb3973c5652a49d3e96df3e91181b455f29
2025-01-09 18:46:39 +00:00
merge-script 9a8e61393c
Merge rust-bitcoin/rust-bitcoin#3858: Bound `Address` parsing on `NetworkValidationUnchecked`
29a71de928 Bound Address parsing on NetworkValidationUnchecked (Tobin C. Harding)
cf455d3a06 Fix typo in prifixes (Tobin C. Harding)

Pull request description:

  Currently it is not possible for downstream to use a generic on the `Address` type in structs in conjuncture with
  derives (`serde::Deserialize` and `Display`) because our impls are only done for `NetworkUnchecked` (as they should be).

  However, as observed by dpc, if we add a secondary marker trait and use it to bound the impls, implementing the new marker for `NetworkUnchecked` then downstream can use derives by way of

  ```
      #[derive(Serialize, Deserialize)]
      struct Foo<V>
          where V: NetworkValidation,
      {
          #[serde(bound(deserialize = "V: NetworkValidationUnchecked"))]
          address: Address<V>,
      }
  ```

  This is cool as hell because the `Address` type is currently a royal PITA.

  Patch 1 is trivial cleanup.

  To get past a build error in `FromStr` I used this little trick
  ```rust
              // We know that `U` is only ever `NetworkUnchecked` but the compiler does not.
              Ok(Address(address.0, PhantomData::<U>))
  ```

  Resolve: #3760
  and
  Close: #3856

ACKs for top commit:
  apoelstra:
    ACK 29a71de92817bccd49b42b1055cc570832e6b959; successfully ran local tests

Tree-SHA512: 7c158dddb9fdbaaa1e48204bbf915b18ced56f5d82ce82630db6c0b52161bcf43b3ac413fa990a23975743c56917985b2666a74f9067221f003f2dcf080f827e
2025-01-09 16:36:13 +00:00
merge-script 2056abd086
Merge rust-bitcoin/rust-bitcoin#3875: Change all occurrences of "IO" to "I/O"
316d8bcb01 Change all occurrences of "IO" to "I/O" (Jamil Lambert, PhD)

Pull request description:

  Fixes #3871

ACKs for top commit:
  tcharding:
    ACK 316d8bcb01
  apoelstra:
    ACK 316d8bcb01504420a14854d2be122d1c8cffb4a9; successfully ran local tests; lgtm

Tree-SHA512: 437a95a1c36bcd4ae27aaacdfc5e0f3463e522a222c4a6ef2c3e234be4a24be2b600687bd58b300bf2b0a0d6596ab008f60903c91646458228eb34cf510908d6
2025-01-08 22:12:24 +00:00
merge-script 72aceb32db
Merge rust-bitcoin/rust-bitcoin#3880: Derive `Clone`on consensus errors
2f91092d77 Derive Clone on consensus errors (Tobin C. Harding)

Pull request description:

  All error types in the repo use either [0]:

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

  However the `consensus` error types do not have `Clone` derived.

  Derive `Clone` on `consensus::ParseError` and `consensus::Error`.

  [0] Excluding `PushBytesError`, fixed in #3879

ACKs for top commit:
  apoelstra:
    ACK 2f91092d773e1618314b62c9e3eae17c08e1f5ad; successfully ran local tests; lgtm

Tree-SHA512: 2cae6faae97b63de311538c33eb7d1914330672744ca073bc6ea5c11ebd40a40ff3de9a38f1f5b101ff27798416d685a708599c298a6f82d7f5804575c2cb2c1
2025-01-08 20:27:32 +00:00
merge-script 31a5e71d57
Merge rust-bitcoin/rust-bitcoin#3879: Remove Copy from `PushBytesError`
0870cd1660 Remove Copy from PushBytesError (Tobin C. Harding)

Pull request description:

  The `PushBytesError` is the only error type in the codebase to derive `Copy`. Without thinking too hard this is unusual - remove it.

  Thinking a bit harder it makes the code less maintainable because we must commit to implementing `Copy`.

ACKs for top commit:
  apoelstra:
    ACK 0870cd1660edd21739cc94075e4b3a1c7f1a7d15; successfully ran local tests; lgtm

Tree-SHA512: c71db5de634dfe2bd76336e5c31fab496f2a472a8dd164034233544c15bd89c84ff986e476fa9b7b05d01aa5332dd4bc93f63a93bf7a21e9a0ec67fc145739b2
2025-01-08 19:03:12 +00:00
merge-script 515a66b854
Merge rust-bitcoin/rust-bitcoin#3839: Use `_unchecked` to construct amounts
a7c44cebf9 Use _unchecked to construct amounts (Tobin C. Harding)
09df951760 Use sat variable in tests (Tobin C. Harding)
4a5b2c60c6 Use ssat variable in tests (Tobin C. Harding)

Pull request description:

  We have a `_unchecked` constructor now for both `Amount` and `SignedAmount`. Soon we would like to start enforcing the `MAX_MONEY` invariant in both amount types. To make that change easier do a few refactorings:

  - Patch 1 and 2 introduce local variables for amount constructors.
  - Patch 3 replaces the local variables introduce in  (1) and (2) with macros
  - Patch 4 uses `_unchecked` constructor for hard coded integers

  The strange patch separation is done intentionally so we don't inadvertently reduce test coverage by using the wrong constructor. I made this mistake already in a previous PR, lesson learned.

  Note please, the macro introduced in patch 3 is in preparation for enforcing `MAX_MONEY`. The macros allow us to panic (`from_sat().unwrap()`) instead of using the `_unchecked` version.

ACKs for top commit:
  apoelstra:
    ACK a7c44cebf9975c4eeba56a65c0ea65be90e5c7f3; successfully ran local tests

Tree-SHA512: 55c2428ae231882542a4cfa724675341f7b493d158f4bec26277d3eefb04d9597cc29b05dce859661a96855fa6f4bac250d53c3dfa9f86a9611d43387ee18667
2025-01-08 04:08:39 +00:00
Tobin C. Harding 2f91092d77
Derive Clone on consensus errors
All error types in the repo use either [0]:

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

However the `consensus` error types do not have `Clone` derived.

Derive `Clone` on `consensus::ParseError` and `consensus::Error`.

[0] Excluding `PushBytesError`, fixed in #3879
2025-01-08 10:34:47 +11:00
Tobin C. Harding 0870cd1660
Remove Copy from PushBytesError
The `PushBytesError` is the only error type in the codebase to derive
`Copy`. Without thinking too hard this is unusual - remove it.

Thinking a bit harder it makes the code less maintainable because
we must commit to implementing `Copy`.
2025-01-08 10:32:51 +11:00
Tobin C. Harding 706a135de6
bitcoin: Add an example of doing I/O to encode/decode
In an effort to improve the documentation on `bitcoin_io` add an example
in `bitcoin` crate that demonstrates a few things:

- Encode/Decode a `rust-bitcoin` type to/from a stdlib type.
- Encode to a custom type by implementing `bitcoin_io` traits.
- Encode to a foreign custom type by using the `bitcoin_io::bridge::FromStd` wrapper.

Later we can link to this example online in the `bitcoin_io` docs.
2025-01-08 10:18:00 +11:00
Jamil Lambert, PhD 316d8bcb01
Change all occurrences of "IO" to "I/O" 2025-01-07 12:37:47 +00:00
Nick Johnson e09bdb5f98 Add BIP324 V2 p2p network message support
BIP324 introduced the second version of p2p network messages which
are designed to be used with the new encrypted transport. This patch
adds a V2NetworkMessage type which wraps a NetworkMessage and handles
the V2 encoding and decoding. It is a parallel of the existing
RawNetworkMessage type (which may be better described as
V1NetworkMessage). A priority of this patch was to not re-invent any
wheels and try to use the existing patterns as much as possible.
2025-01-06 20:38:57 -08:00
merge-script 70a879279b
Merge rust-bitcoin/rust-bitcoin#3859: Remove usage of impl_from_infallible in crates
f94c7185fd Remove usage of impl_from_infallible in crates (Shing Him Ng)

Pull request description:

  Fixes #3843

  tcharding Copied your commit message from the other `impl_from_infallible` commit 😄

ACKs for top commit:
  apoelstra:
    ACK f94c7185fdd62e1ed98ed4016486406146c4d4f3; successfully ran local tests; nice!
  tcharding:
    ACK f94c7185fd

Tree-SHA512: 8c58c2c87f6892855d74a3306e1027a37394961f0a26b7bd88cc1654a190dda37234e7dde51a419dcd2f1bd1dd1ccceec16bbbc6fbdd5418ad21f10531b402b3
2025-01-06 14:15:17 +00:00
Tobin C. Harding 29a71de928
Bound Address parsing on NetworkValidationUnchecked
Currently it is not possible for downstream to use a generic on the
`Address` type in structs in conjuncture with
derives (`serde::Deserialize` and `Display`) because our impls are only
done for `NetworkUnchecked` (as they should be).

However, as observed by dpc, if we add a secondary marker trait and use
it to bound the impls, implementing the new marker for
`NetworkUnchecked` then downstream can use derives by way of

```
    #[derive(Serialize, Deserialize)]
    struct Foo<V>
        where V: NetworkValidation,
    {
        #[serde(bound(deserialize = "V: NetworkValidationUnchecked"))]
        address: Address<V>,
    }
```

This is cool as hell because the `Address` type is currently a royal PITA.
2025-01-06 17:34:08 +11:00
Tobin C. Harding a7c44cebf9
Use _unchecked to construct amounts
We have a `_unchecked` constructor now for both `Amount` and
`SignedAmount`. In preparation for enforcing the `MAX_MONEY` invariant
use the `_unchecked` constructor throughout the codebase to construct
amounts from hard coded integer values.
2025-01-06 13:14:21 +11:00
merge-script 33d70529bd
Merge rust-bitcoin/rust-bitcoin#3854: Add parenthesis to explicitly show precedence
2c9fda4135 Add parenthesis to explicitly show precedence (Tobin C. Harding)

Pull request description:

  Recent clippy nightly update introduces warnings about precedence. While ours are, IMO, clear the lint docs have some cases that are not so I don't think we should ignore this lint. Specifically I could easily miss this one

    1 << 2 + 3 equals 32, while (1 << 2) + 3 equals 7

  ref: https://rust-lang.github.io/rust-clippy/master/#/precede

  Add parenthesis to explicitly show precedence. Refactor only no logic changes.

ACKs for top commit:
  apoelstra:
    ACK 2c9fda413572e251fe3fd07f679b6c786da137b3; successfully ran local tests; yeah, these all seem reasonable

Tree-SHA512: 67b8cb56c0b271d41db58f9297e4be43410c04bf7bc2308789624084e5e9fee325a2eb3be7e56bd6b3904cf19c7a8b1b426e7adef7f4af18ea8c9d24b77a9db9
2025-01-05 21:37:46 +00:00
merge-script f3e56f1c95
Merge rust-bitcoin/rust-bitcoin#3857: Automated nightly rustfmt (2025-01-05)
762f6630fe 2025-01-05 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 762f6630fe22e54e5a8b2839054e2e09a5a2db64; successfully ran local tests

Tree-SHA512: 5be013a53697ab2c3f5bb6da8c1c45124d4fee696a655544bb7fad3d28606638a639932d05a990d7e1ae7d91bcc85eb9bc136145a241b39b9089b2e7dc10f5a5
2025-01-05 18:41:02 +00:00
merge-script 8458ca5167
Merge rust-bitcoin/rust-bitcoin#3850: Fix typo in MerkleBlock::from_block_with_predicate comment
d5de89259b Fix typo in MerkleBlock::from_block_with_predicate comment (Jesus Christ)

Pull request description:

  - Corrected a grammatical error by adding "are" to the comment in MerkleBlock::from_block_with_predicate.

ACKs for top commit:
  clarkmoody:
    ACK d5de89259b
  apoelstra:
    ACK d5de89259b64ed6d4756973073f06172d1d14090; successfully ran local tests
  storopoli:
    ACK d5de89259b

Tree-SHA512: 55595e8858e90ee5ab5ef7d592cf2c47dc593f303f3f60040f1612d92abcf4a4d0f9c9ad3bf7eece54f3078a3ab8ff92b36357be58db0919888e239a8a5c1a6b
2025-01-05 16:08:50 +00:00
Shing Him Ng f94c7185fd Remove usage of impl_from_infallible in crates
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the usage of the
`impl_from_infallible` macro in the bitcoin, units, and internals crates
and just write the code.
2025-01-04 23:46:12 -06:00
Tobin C. Harding cf455d3a06
Fix typo in prifixes
Close: #3856
2025-01-05 12:36:56 +11:00
Fmt Bot 762f6630fe 2025-01-05 automated rustfmt nightly 2025-01-05 01:22:00 +00:00
Tobin C. Harding 2c9fda4135
Add parenthesis to explicitly show precedence
Recent clippy nightly update introduces warnings about precedence. While
ours are, IMO, clear the lint docs have some cases that are not so I
don't think we should ignore this lint. Specifically I could easily miss
this one

  1 << 2 + 3 equals 32, while (1 << 2) + 3 equals 7

ref: https://rust-lang.github.io/rust-clippy/master/#/precede

Add parenthesis to explicitly show precedence. Refactor only no logic
changes.
2025-01-04 15:25:00 +11:00
merge-script 72cc88d907
Merge rust-bitcoin/rust-bitcoin#3847: ParsePublicKeyError using hex::InvalidCharError
945fcd0920 fix ParsePublicKeyError using hex::InvalidCharError (Innocent Onyemaenu)

Pull request description:

  Replaced the InvalidChar variant u8 with hex::InvalidCharError

  Resolves #3835

  changed InvalidChar variant of the ParsePublicKeyError from `u8` to `hex::InvalidCharError`

  ```
  pub enum ParsePublicKeyError {
      ...
      /// Hex decoding error.
      InvalidChar(hex::InvalidCharError),
      ...
  }

  Also,

  modified the test cases to accommodate the new variant

  Why:
  - hex::InvalidCharError includes both the invalid character and its position.
  - This improves debugging and makes error messages more actionable.

ACKs for top commit:
  apoelstra:
    ACK 945fcd09209120ef8869a2e4165e866328cc9bd5; successfully ran local tests; I like it
  clarkmoody:
    utACK 945fcd0920

Tree-SHA512: c13446c099cb02b4f253f9cc559a860aff3288a2cc5eac96d3cf910bf63e78957741bbdff69b936b16b36e46b366841a5c94876d16cbc0c41aea2a70866a6e45
2025-01-04 01:05:04 +00:00
merge-script 3b2363b2c6
Merge rust-bitcoin/rust-bitcoin#3846: Remove `test_` prefix from unit tests
85e04315d5 Remove test_ prefix from unit tests (Tobin C. Harding)

Pull request description:

  There is a loose convention in Rust to not use `test_` prefix. The reason being that `cargo test` outputs 'test <test name>' using the prefix makes the output stutter.

  This patch smells a bit like code-churn but having the prefix in some places and not others is confusing to new contributors and is leading me to explain this many times now. Lets just fix it.

  Remove the prefix unless doing so breaks the code.

ACKs for top commit:
  shinghim:
    ACK 85e04315d5
  apoelstra:
    ACK 85e04315d5eb90075ce55bf18fab8876a4583def; successfully ran local tests

Tree-SHA512: d90ae5ef75cc5e5a8f43f60819544f1a447f13cbe660ba71e84b8f27bfcc04a11d3afde0ed56e4eea5c73ebc3925024b800a1b995f73142cab892f97a414f14a
2025-01-04 00:23:46 +00:00
Jesus Christ d5de89259b
Fix typo in MerkleBlock::from_block_with_predicate comment
- Corrected a grammatical error by adding "are" to the comment in MerkleBlock::from_block_with_predicate.
2025-01-03 21:00:13 +00:00
Innocent Onyemaenu 945fcd0920 fix ParsePublicKeyError using hex::InvalidCharError
What:
- Replaced the InvalidChar variant u8 with hex::InvalidCharError

Why:
- hex::InvalidCharError includes both the invalid character and its position.
- This improves debugging and makes error messages more actionable.
2025-01-03 15:33:26 +01:00
Tobin C. Harding 85e04315d5
Remove test_ prefix from unit tests
There is a loose convention in Rust to not use `test_` prefix. The
reason being that `cargo test` outputs 'test <test name>' using the
prefix makes the output stutter.

This patch smells a bit like code-churn but having the prefix in some
places and not others is confusing to new contributors and is leading me
to explain this many times now. Lets just fix it.

Remove the prefix unless doing so breaks the code.
2025-01-02 10:06:50 +11:00
Tobin C. Harding fd8d563b87
Remove macro debug_from_display
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the `debug_from_display`
macro and just write the code.

This is an API breaking change to `internals` but an internal change
only to any of the _real_ crates.
2025-01-02 07:31:13 +11:00
merge-script 21d435cf84
Merge rust-bitcoin/rust-bitcoin#3836: Update to rust-ordered 0.4.0
1a8f5b19fb Update to rust-ordered 0.4.0 (Tobin C. Harding)

Pull request description:

  We just released a version of `ordered` that makes `ArbitraryOrd` object safe - use it.

  Upgrade to the latest version of `rust-ordered` - `v0.4.0`.

ACKs for top commit:
  apoelstra:
    ACK 1a8f5b19fbc37c74bbdd7dcee55b3294046ef88e; successfully ran local tests

Tree-SHA512: 823962a9e6b956126c7ff7a63b780431e358a9f0713f30e3801de3febea1e1e569b8ba3f6e3c0dc3c38759ac233c3d0ddea2223588cd796db6849fae8b599e04
2025-01-01 16:52:11 +00:00
merge-script 16b522043d
Merge rust-bitcoin/rust-bitcoin#3789: Add hash data to Inventory's Error variant
72e97c637f Add a hash value to Inventory's Error variant (Nick Johnson)

Pull request description:

  I am working on adding BIP324 V2 p2p network message support to the p2p module and ran into a little snag. I can post a draft branch of that work if helpful, but the general strategy is to add a `V2NetworkMessage` which operates in parallel with the existing `RawNetworkMessage` type (which is a bit of misnomer and may be better described in the future as `V1NetworkMessage`, see #3157). This allows both p2p message types to hook into the existing `Encodable`/`Decodeable` chain.

  But the `Error` variant of the `Inventory` type message does not have symmetrical encoding and decoding paths. Encoding writes out a zero'd 32 bytes while decoding ignores it completely. And while it is not clear to me if there is [requirement written down](https://en.bitcoin.it/wiki/Protocol_documentation#Inventory_Vectors) anywhere, [bitcoin core does appear to always expect a hash](c1252b14d7/src/protocol.h (L499)) even with the `Error` variant where it is meaningless.

  I believe rust-bitcoin's handling of this was never a problem before because the top level `RawNetworkPackage` pulls all the required bytes off a reader before decoding them. But this is not as easy to do with the v2 p2p network messages since the length is decoded at the transport level, not the message itself. I believe it is preferable for the Decoders to *not* assume that all bytes have been pulled off already given their input stream interface. Maybe somewhat surprisingly, this is the only issue I have run into so far adding the v2 encoding and decoding paths. As it is now, the code panics with an `Unconsumed` because it hasn't pulled the dummy zero bytes off the reader.

  This patch adds the 32 byte array to the Error variant in order to make its Encoding and Decoding paths symmetrical. This also allows a reader to discard the 32 bytes when decoding a message while cleanly keeping things hooked up with the `Decodeable` chain. The hash is still not exposed to the caller.

  This is *a way* to solve my issue, but not sure if it will cause more confusion than its worth. I tried a few other strategies, but preferred this one due to how well it hooks into `Decodeable`.

ACKs for top commit:
  apoelstra:
    ACK 72e97c637fa0916be75aef28ea8169ffbbe2c4f5; successfully ran local tests
  tcharding:
    ACK 72e97c637f

Tree-SHA512: 20cb9fec0768e0fdf7c7f520a00e1a37f5ef0583e2ebc7d47583249c6829c47826d83694a56338efa5844a9fe264a77f6d04c0c46c85f8732c7585515723d7ce
2024-12-31 14:08:25 +00:00
Tobin C. Harding 1a8f5b19fb
Update to rust-ordered 0.4.0
We just released a version of `ordered` that makes `ArbitraryOrd` object
safe - use it.

Upgrade to the latest version of `rust-ordered` - `v0.4.0`.
2024-12-31 10:42:24 +11:00
Tobin C. Harding 20b798175c
Update to latest rust-ordered
I just went to town on the `rust-ordered` crate to get it ready for
releasing a `1.0` version. None of the changes effect our usage here in
`rust-bitcoin`.

Upgrade to the latest version of `rust-ordered` - `v0.3.0`.
2024-12-30 08:35:01 +11:00
merge-script 779768eff9
Merge rust-bitcoin/rust-bitcoin#3769: Change method return type for to_unsigned()
e13355318e Add From impl (yancy)
364e9ff775 Change method return type (yancy)
fdf3336ed5 Add unchecked variant (yancy)

Pull request description:

  Any SignedAmount can now be cast to Amount since the range is the same.  Specifically, the range for SignedAmount is (- 21 million, 21 million) while the range for Amount is (0, 21 million).  Therefore any value from Amount can be cast to a SignedAmount and it will work.  Note it's not the same and still requires checking when going from SignedAmount to Amount since Amount can't handle the negative range.

ACKs for top commit:
  tcharding:
    ACK e13355318e

Tree-SHA512: c016b51bdd87a12eb09d9c1a82699dad1e866bf96bd3235eeb131f216f02422acb992ddb3a8135af00bbc10e240178fde5e37fb7860d9e6eaf433cf917d4d16a
2024-12-29 14:58:47 +00:00
yancy 364e9ff775 Change method return type
Any SignedAmount can now be cast to Amount since the range is the same.
Specifically, the range for SignedAmount is (- 21 million, 21 million)
while the range for Amount is (0, 21 million).  Therefore any value from
Amount can be cast to a SignedAmount and it will work.  Note it's not
the same and still requires checking when going from SignedAmount to
Amount since Amount can't handle the negative range.

As a side effect of changing the return type, TryFrom is no longer valid
and does not compile.  Therefore in addition to changing the return
type, TryFrom is also removed.
2024-12-24 11:31:49 -06:00
merge-script 2de44ab2a6
Merge rust-bitcoin/rust-bitcoin#3798: refactor: use amount type
774f066879 refactor: Change from u64 to Amount (yancy)

Pull request description:

  Separate out refactor commit from https://github.com/rust-bitcoin/rust-bitcoin/pull/3794.  Can be merged independently.

ACKs for top commit:
  tcharding:
    ACK 774f066879
  apoelstra:
    ACK 774f066879c8ad1af81c7e46b404fa63682a0b4c; successfully ran local tests

Tree-SHA512: 9ec5121d823ee3ec506eee5b5187bd496221bd3576afcaa6daf647099720d87b58b69521f29ae9537f123e2958771bc867b123da3f2ba941cba403a6c98e46de
2024-12-24 14:53:03 +00:00
yancy 774f066879 refactor: Change from u64 to Amount
The Amount type provides better type safety and is more appropriate in
this context than u64.  Currently the checked arithmetic operations for
Amount and u64 are identical in behavior.  Therefore, this refactor does
not result in any behavior change and is purely cosmetic.
2024-12-23 13:03:53 -06:00
calciumbe bcf8580913
bitcoin: fix typo 2024-12-23 20:44:18 +08:00
Nick Johnson 72e97c637f Add a hash value to Inventory's Error variant
While the hash value of the Error variant is meaningless, the variant
still conforms to all other Inventory messages and requires a 32
byte hash to be sent over the wire. This is how bitcoin core operates.

This patch adds the 32 byte array to the Error variant in order to make
its Encoding and Decoding paths symmetrical. This also allows a reader
to discard the 32 bytes when decoding a message. The hash is still not
exposed to the caller.

This was never a problem before because the top level RawNetworkPackage
pulls all the required bytes off a reader before decoding. But this is
not as easy to do with the v2 p2p network messages.
2024-12-18 19:24:26 -08:00
merge-script f4069fcd61
Merge rust-bitcoin/rust-bitcoin#3780: Use uniform capitalisation of SegWit in rustdocs
e56f461916 Make capitalization of SegWit uniform in strings (Jamil Lambert, PhD)
3520e832ac Make capitalization of SegWit uniform in rustdocs (Jamil Lambert, PhD)

Pull request description:

  Fixes #3770

  - Searched and replaced all occurrences of `//` * `segwit` (case insensitive) with `//` * `SegWit`
  - Searched and replaced all occurrences of `"` * `segwit` (case insensitive) with `"` * `SegWit`

ACKs for top commit:
  apoelstra:
    ACK e56f461916b85928139950074d0df52caf4f919b; successfully ran local tests

Tree-SHA512: c56704102d8e86f26378bb302d5fdc7ea21d41fd49f55606e589ec32ff896f1adfb1960d8fb29abccfbf298b90fc357ed609d80fd0163dcb4ff01573646b02c3
2024-12-17 18:10:44 +00:00
merge-script 1d3f42e589
Merge rust-bitcoin/rust-bitcoin#3762: Remove double spacing in rustdocs
bb29490308 Remove double spacing in rustdocs (Jamil Lambert, PhD)

Pull request description:

  Fixes #3761

ACKs for top commit:
  apoelstra:
    ACK bb294903086e2e317c9a7a6f298a36ec17082c39; successfully ran local tests

Tree-SHA512: efc41bdfd7b46bae4ed1159c852b3a2df0a95e36e1964679640de37ae3e0cc994426e7e9d643beb5ba0f5210bb6c2a19028fc8d28a6150580bad4471d4502bb3
2024-12-17 16:38:01 +00:00
Jamil Lambert, PhD e56f461916
Make capitalization of SegWit uniform in strings 2024-12-17 14:49:01 +00:00
Jamil Lambert, PhD 3520e832ac
Make capitalization of SegWit uniform in rustdocs 2024-12-17 14:28:28 +00:00
Jamil Lambert, PhD bb29490308
Remove double spacing in rustdocs 2024-12-17 14:21:12 +00:00
Tobin C. Harding b5f553d866
hashes: Bump version to 0.16.0
We need to do a quick release because it turns out I was wrong in
thinking that making `hex` an optional dependency is not a breaking
change.

```
the package `bitcoin` depends on `bitcoin_hashes`, with features: `hex`
but `bitcoin_hashes` does not have these features. It has a required
dependency with that name, but only optional dependencies can be used as
features.
```

Add a changelog, bump the version, depend on the new version repo wide,
and update the lock files - like its our job.
2024-12-16 12:41:17 +11:00
merge-script c40e2516ae
Merge rust-bitcoin/rust-bitcoin#3737: Improve rustdocs on addresses module
f85456a726 Improve rustdocs on addresses module (Tobin C. Harding)

Pull request description:

  These docs are stale, update them.

  - Mention segwit and legacy
  - Improve example code to show feature gating
  - Fix headings to be as typical

ACKs for top commit:
  storopoli:
    ACK f85456a726
  apoelstra:
    ACK f85456a726a201b26eac58d22eb0eb6a58ad411b; successfully ran local tests

Tree-SHA512: c1cdde744fb960c4119805e6b3fb0cc971eaa94173fb85f8f7a34daab7985a0f14c9be477d7dda430c6873fed7265c598b8dcfbffe0ed6a99ca8590a2b2b724e
2024-12-15 17:32:14 +00:00
Tobin C. Harding f85456a726
Improve rustdocs on addresses module
These docs are stale, update them.

- Mention segwit and legacy
- Improve example code to show feature gating
- Fix headings to be as typical
2024-12-15 09:29:48 +11:00
merge-script 1c4d66ab39
Merge rust-bitcoin/rust-bitcoin#3749: Remove unnecessary type
3ac9b2ded2 Remove unnecessary type (Tobin C. Harding)

Pull request description:

  `hex_psbt` returns `Psbt`, no need for the explicit type.

  Internal change only.

ACKs for top commit:
  shinghim:
    ACK 3ac9b2ded2
  apoelstra:
    ACK 3ac9b2ded24d5428945538c0a674301f6094df96; successfully ran local tests

Tree-SHA512: 2be1bfa56729bf97e60d721a184d3d21bfb27115fab1dd1b9103428658b5faca012d8f01e0458be7f17dadcd8cc495b327f2b472d825d46aa5cb71d0906ecb21
2024-12-14 17:48:02 +00:00
merge-script 25236ce697
Merge rust-bitcoin/rust-bitcoin#3746: bitcoin: Improve rustdocs on extern crates
a5993426fd bitcoin: Improve rustdocs on extern crates (Tobin C. Harding)

Pull request description:

  Make slight improvement to the extern crates we re-export from `bitcoin`.

ACKs for top commit:
  apoelstra:
    ACK a5993426fddea85c29ac6cd5a0e3096988d72b03; successfully ran local tests

Tree-SHA512: bb857316fc2606aa553f15e1c9fcf8dd87374e170121fe30cf9e44ae190f873f94c113af49b096cb49a65fd68d1bf24732e3f1aba04861e2ae934414f30473c6
2024-12-14 17:03:56 +00:00
merge-script fcbe970ddb
Merge rust-bitcoin/rust-bitcoin#3734: Move script hash types to `primitives`
6e01383f16 api: Run just check-api (Tobin C. Harding)
3855d3cc83 Move script hashes to primitives (Tobin C. Harding)
d1dd63d6d4 Remove wildcard in script re-exports (Tobin C. Harding)

Pull request description:

  Woops, this should have been done before v0.101.0 was released.

  Move the `ScriptHash` and `WScriptHash` types to `primitives`.

  Requires moving constants and error types as well. We re-export the errors because they are in the `mod.rs` file so they should appear in both `primitives::script::FooError` and `bitcoin::script::FooError`.

ACKs for top commit:
  apoelstra:
    ACK 6e01383f162307a573c3454733ccae0596d3eaf6; successfully ran local tests

Tree-SHA512: 4ef838ee9c4cb3eb308ffa855ea5d6f5ad46f81c17b9413faa493a46a262afc18b47f28a0fdd5fc675eea31b895d0eb0e2505a523e820504ec88d9334d6874b4
2024-12-14 06:14:39 +00:00
merge-script 3c25a8acbc
Merge rust-bitcoin/rust-bitcoin#3753: Update to arbitrary v1.4
463fbd16f1 Update to arbitrary v1.4 (Tobin C. Harding)

Pull request description:

  Out with the old in with the new, update to the latest minor version of arbitrary.

  No real reason to do this except that I wanted to add the minor version instead of using just `1` so that we don't accidentally pull a new minor version in during a patch release (after we 1.0).

  (I'm unsure if this is necessary and did not test it against MSRV.)

ACKs for top commit:
  apoelstra:
    ACK 463fbd16f1431febe7e4fc50abd7415886542109; successfully ran local tests

Tree-SHA512: 88913f993b97abba224ba94ee9d5057894bb7142fef07e557de03a423da314a9f21632932fb64c4c27c676051bbb6e90d9b90bfa944af810e834bf42707d3cff
2024-12-14 01:39:38 +00:00
merge-script 7ab569388b
Merge rust-bitcoin/rust-bitcoin#3743: Refactor predict weight
0135cddc32 Refactor non_input_size (Tobin C. Harding)
f42f13cd8d Simplify closures in call to fold (Tobin C. Harding)

Pull request description:

  Two refactorings to make the code easier to read. No logic changes.

ACKs for top commit:
  apoelstra:
    ACK 0135cddc32462490528dea549fd8db4c0a30646e; successfully ran local tests

Tree-SHA512: 747cfe1212283a62545cb86a5af0be46592ab3b41f94ab0a2dad8f06b094c04eb9265520ece595d2aab303fddf55a652e7ef977911439602550c264bedd4385b
2024-12-13 23:58:31 +00:00
merge-script ee8430a527
Merge rust-bitcoin/rust-bitcoin#3738: Remove Weight::from_wu_usize function
3c8c956511 Remove Weight::from_wu_usize function (Tobin C. Harding)

Pull request description:

  This constructor is an anomaly in this repo. Also it is ugly to have the type parameter hard coded in the function name.

  The problem the constructor is trying to solve is that we don't like casts that don't obviously loose data. We have a solution for that already in `internals::ToU64`. This trait cannot be used in const contexts though so we do introduce a single cast in this patch.

ACKs for top commit:
  apoelstra:
    ACK 3c8c95651169fb9329ceb380162721c2d2f9b3aa; successfully ran local tests

Tree-SHA512: 168196edb7c151378d425b96ea3c9cdb0a4f6a879543e89facd02ed1fdf9bc69bde8ef862ffa0959b7c5ca21d6f4fe5ae38a933c379e7e88a946ca7cb68d61ec
2024-12-13 22:16:59 +00:00
merge-script 7977231839
Merge rust-bitcoin/rust-bitcoin#3751: Remove unnecessary rustdocs
99e2a9f530 Remove unnecessary rustdocs (Tobin C. Harding)

Pull request description:

  The `deprecated` attribute already fully explains the status of a deprecated function and the replacement - no need to repeat this.

ACKs for top commit:
  apoelstra:
    ACK 99e2a9f530eced69e9df1441e15d24e626974385; successfully ran local tests

Tree-SHA512: 133d41573a3ba97abf85100649c1e8dfe131521a627e0b7f1ab803845caf1c183b99488cb5d9f7e858cab1b35f8907a79b320ec01901b15fdd63d08f0bc1f87e
2024-12-13 20:01:34 +00:00
merge-script 66accdee08
Merge rust-bitcoin/rust-bitcoin#3748: Remove ToU64 from rustdocs
4ef06b4f2b Remove ToU64 from rustdocs (Tobin C. Harding)

Pull request description:

  The `ToU64` trait is not meant to be used publicly, remove the mention of it from rustdocs.

ACKs for top commit:
  shinghim:
    ACK 4ef06b4f2b
  apoelstra:
    ACK 4ef06b4f2b9945b46c190abd0cb3a6dfe234c7b5; successfully ran local tests

Tree-SHA512: e9acef9194cee380aa2aa44af9285d4f6e025bdf09f76e5066916c51215db15f1f920fd8ecf3cf079f0a2dddab0332b3a7df104f5bd61d12fc337b4ed163310c
2024-12-13 18:01:29 +00:00
Tobin C. Harding d066a863bf
Use backticks on amount types 2024-12-13 09:01:16 +11:00
Tobin C. Harding a5993426fd
bitcoin: Improve rustdocs on extern crates
Make slight improvement to the extern crates we re-export from
`bitcoin`.
2024-12-13 09:01:08 +11:00
Tobin C. Harding 4ef06b4f2b
Remove ToU64 from rustdocs
The `ToU64` trait is not meant to be used publicly, remove the mention
of it from rustdocs.
2024-12-13 09:00:53 +11:00
Tobin C. Harding 3ac9b2ded2
Remove unnecessary type
`hex_psbt` returns `Psbt`, no need for the explicit type.

Internal change only.
2024-12-13 09:00:41 +11:00
Tobin C. Harding 99e2a9f530
Remove unnecessary rustdocs
The `deprecated` attribute already fully explains the status of a
deprecated function and the replacement - no need to repeat this.
2024-12-13 09:00:20 +11:00
Tobin C. Harding 463fbd16f1
Update to arbitrary v1.4
Out with the old in with the new, update to the latest minor version of
arbitrary.

No real reason to do this except that I wanted to add the minor version
instead of using just `1` so that we don't accidentally pull a new minor
version in during a patch release (after we 1.0).
2024-12-12 15:44:45 +11:00
Tobin C. Harding 0135cddc32
Refactor non_input_size
Looks like the formatter has put code comments in the wrong place.

Refactor the `non_input_size` line so that the formatter does not mess
with it.
2024-12-12 15:34:22 +11:00
Tobin C. Harding f42f13cd8d
Simplify closures in call to fold
Remove unnecessary code comments and make the closure variable names more
terse with no loss of clarity.

Refactor only, no logic changes.
2024-12-12 15:34:12 +11:00
Tobin C. Harding 3c8c956511
Remove Weight::from_wu_usize function
This constructor is an anomaly in this repo. Also it is ugly to have the
type parameter hard coded in the function name.

The problem the constructor is trying to solve is that we don't like
casts that don't obviously loose data. We have a solution for that
already in `internals::ToU64`. This trait cannot be used in const
contexts though so we do introduce a single cast in this patch.
2024-12-12 15:21:29 +11:00
Tobin C. Harding 3855d3cc83
Move script hashes to primitives
Woops, this should have been done before v0.101.0 was released.

Move the `ScriptHash` and `WScriptHash` types to `primitives`.

Requires moving constants and error types as well. We re-export the
errors because they are in the `mod.rs` file so they should appear in
both `primitives::script::FooError` and `bitcoin::script::FooError`.
2024-12-12 15:14:00 +11:00
Tobin C. Harding d1dd63d6d4
Remove wildcard in script re-exports
We don't like wildcard re-exports anymore; explicitly re-export the
types from `primitives::script`.
2024-12-12 15:14:00 +11:00
merge-script e2de5162d9
Merge rust-bitcoin/rust-bitcoin#3717: base58ck: Bump version to 0.2.0
5b3e4ab95b base58ck: Bump version to 0.2.0 (Tobin C. Harding)

Pull request description:

  In preparation for release add a changelog entry, bump the version, and update the lock files.

ACKs for top commit:
  storopoli:
    ACK 5b3e4ab95b
  apoelstra:
    ACK 5b3e4ab95b015b5bba5b73db2052cbb63a901466; successfully ran local tests

Tree-SHA512: 8347b0540ccdfc29f4a03f0a3554142b4fbd39b519512dcc886c28cae75666201aa8a7e4a07999e70c0348e59f02c4c0d67541a20102153b36150f5e4748a89e
2024-12-11 16:58:08 +00:00
Tobin C. Harding adaf4ac086
Set avoid-breaking-exported-api to false
These lints are valuable, lets get at em.

Changes are API breaking but because the changes make functions consume
self for types that are `Copy` downstream should not notice the breaks.
2024-12-11 10:11:50 +11:00
Tobin C. Harding 5b3e4ab95b
base58ck: Bump version to 0.2.0
In preparation for release add a changelog entry, bump the version, and
update the lock files.
2024-12-10 13:47:29 +11:00
merge-script 8eda92ee7b
Merge rust-bitcoin/rust-bitcoin#3699: Explicitly define Ord for NodeInfo
428e9787d1 Explicitly define Ord for NodeInfo (Shing Him Ng)

Pull request description:

  Fixes #3654 by explicitly defining Ord so that we avoid potentially catastrophically changing addresses out from under users

ACKs for top commit:
  sanket1729:
    utACK 428e9787d1
  apoelstra:
    ACK 428e9787d181a462d06a18b7a45701790cbc0929; successfully ran local tests

Tree-SHA512: e900e07b2c53f643e3239bf7d26567b56275899b408a94e45f1cddd81217141c304c87159ce413a7a4660f1c09a0db2bbc1146948a2d9e7600abf14cd73ac691
2024-12-09 22:13:58 +00:00
Shing Him Ng 428e9787d1 Explicitly define Ord for NodeInfo 2024-12-09 09:15:12 -06:00
merge-script b579e1238f
Merge rust-bitcoin/rust-bitcoin#3693: Change `Amount::MAX` from `u64::MAX` to `Amount::MAX_MONEY`
6950c0a7b5 Change `Amount::MAX` to equal `MAX_MONEY` (Jamil Lambert, PhD)

Pull request description:

  As discussed in #3688 and #3691 using `u64::MAX` causes errors when converting to `f64` so `MAX_MONEY` is should be used as the maximum `Amount`.

   - `Amount::MAX` changed to equal `MAX_MONEY`
   - Add a check to add, multiply and from_str functions
   - Change tests to account for new lower maximum

  Different approach to the existing PR #3692.  I have only done Amount and not SignedAmount until there is feedback on which approach to use.

ACKs for top commit:
  tcharding:
    ACK 6950c0a7b5
  apoelstra:
    ACK 6950c0a7b507f9d70c1ebdab540634482f73b247; successfully ran local tests

Tree-SHA512: 03ebf39c47b19ba88d95235538039f28bfa29f4499618fab25c9b627684c348ed41caef682e8f0e01ca62cf9ced8a1183fe3ed861bffeb9609b09440ddfb1c92
2024-12-09 14:54:39 +00:00
Jamil Lambert, PhD 6950c0a7b5
Change `Amount::MAX` to equal `MAX_MONEY`
To prevent rounding errors converting to and from f64 change
`Amount::MAX` to `MAX_MONEY` which is below the limit in f64 that has
issues.

Add checks to `from_str_in`, `checked_add` and `checked_mul` that the
result is below MAX, where previously a u64 overflow was relied on.

Change tests to account for new lower MAX that is within the range of
SignedAmount and does not overflow so easily

Remove overflow tests

`Amount::MAX` is now below `u64::MAX` and within the range of values for
`SignedAmount`.   These tests therefore do not overflow.
In effective_value there is no error with `Amount::MAX` and the correct
value is returned.
In psbt the removed test is effectively the same as the previous test.

Modify `Amount` tests to work with new `MAX`

Tests need to be changed that checked values above the new `MAX` or
`Amount::MAX` was out of range for `SignedAmount` which it isn't anymore
2024-12-04 14:17:00 +00:00
Tobin C. Harding edab380ac0
bitcoin: Bump version to v0.33.0-alpha.0
Needs release notes but there are 20 pages of them, that feels like too
many to go through manually.

Includes a missing changelog entry in `units` for an already released
change that is included in this `bitcoin` release.
2024-12-04 15:58:36 +11:00
Shing Him Ng 8b41ecb427 Fix typo in re-export 2024-12-01 21:32:43 -06:00
merge-script 8acf3ec30a
Merge rust-bitcoin/rust-bitcoin#3684: refactor: remove unnecessary if block
b5a0731a23 refactor: remove unnecessary if block (Chris Hyunhum Cho)

Pull request description:

  This `if...else` block can be replaced with one `if` block, by changing `==` to `!=`.

ACKs for top commit:
  jamillambert:
    ACK b5a0731a23
  apoelstra:
    ACK b5a0731a23f945f7e4cc7d8f3c0f1d3f83875272; successfully ran local tests
  tcharding:
    ACK b5a0731a23

Tree-SHA512: eee0d63532953d27fe081c36180ae9adb63c0ab7f1fb8324d9fc5e4a408f421d8df894efcccfc4bd3142625d6be6ea1258c9478faa025ce147ebe1b4e9377738
2024-12-01 23:25:32 +00:00
Fmt Bot 0990b30035 2024-12-01 automated rustfmt nightly 2024-12-01 01:41:12 +00:00
Chris Hyunhum Cho b5a0731a23 refactor: remove unnecessary if block 2024-11-29 08:06:51 +00:00
merge-script 7d8b9bd883
Merge rust-bitcoin/rust-bitcoin#3673: units: Close the hex parse errors
22769268f3 units: Close the hex parse errors (Tobin C. Harding)

Pull request description:

  As part of the 1.0 effort close the errors in the `units::parse` module.

ACKs for top commit:
  apoelstra:
    ACK 22769268f34b45c0bd86c548eb13cfe1e290f0d7; successfully ran local tests; thanks!
  sanket1729:
    ACK 22769268f3

Tree-SHA512: 598ba236f8c08c3f750112e0d8b8e0aa0568be2700a328e4a2d8999ca59eada8a16a6a1d9e9121e7f42ce9bbe3da3f87221ba67c36056996a687e489f4c6007c
2024-11-29 03:41:48 +00:00
merge-script 031b402b26
Merge rust-bitcoin/rust-bitcoin#3678: Remove unused extern statement
667b7f1010 Remove unused extern statement (yancy)

Pull request description:

  `extern crate` keywords are no longer needed to import a crate as of Rust 2018

ACKs for top commit:
  tcharding:
    ACK 667b7f1010
  apoelstra:
    ACK 667b7f1010bc0745d3e39181e15ec795aeca68dc; successfully ran local tests
  sanket1729:
    ACK 667b7f1010

Tree-SHA512: d157aa881dfce12b65bb676425cb5f17fa5a193d0001f37c26f74cab1254ab08fdcc9b7462b9a74210f85028299af9e14c0071ba390642b59bb7e986ba85933d
2024-11-29 03:01:13 +00:00
merge-script 1f731e9162
Merge rust-bitcoin/rust-bitcoin#3672: units: Remove serde from amounts
77085a1fa1 units: Remove serde from amounts (Tobin C. Harding)

Pull request description:

  The `Amount` and `SignedAmount` were not supposed to implement `serde` traits by design because doing so implicitly uses sats. We provide two modules `as_sat` and `as_btc` to allow users to explicitly serialize in their preferred format.

  In commit: `d57ec019d5 Use Amount type for TxOut value field` derives were added for `serde` and we did not notice it during review.

ACKs for top commit:
  apoelstra:
    ACK 77085a1fa1f39e7358202e49189282e48a965747; successfully ran local tests

Tree-SHA512: ff4c1f34191e12d955e66737493af4b2dad761fa6f95eb334b86715906e70d2180e7f15d8b4af302d53fb0b1c8b5fb50b4924e9fcdee8824840ba555e73c1c17
2024-11-28 16:32:23 +00:00
merge-script bcc54fb9d0
Merge rust-bitcoin/rust-bitcoin#3670: Remove duplicate test case
60f43a893d Remove duplicate test case (Tobin C. Harding)

Pull request description:

  In commit:

   `a10d5e15b3 Extract the Script assembly creator from fmt::Debug`

  A test case was refactored and where there used to be two test case, one for `Debug` and one for `Display`, two identical test cases were left.

  Remove duplicate test case.

ACKs for top commit:
  apoelstra:
    ACK 60f43a893d67c221f61e289cab6394418411cf55; successfully ran local tests

Tree-SHA512: 8d21f07b33c9f88ac820422b9f5471bf53e36050a31854a7152ab14c6e25654455f4eb366ea2b497a80f741af36bf068b1df8a69321a0299c9bae60f001b354e
2024-11-28 15:20:24 +00:00
merge-script 88da0674f2
Merge rust-bitcoin/rust-bitcoin#3642: Add p2wpkh address creation example
d9b48cc2ce Add p2wpkh address creation example (yancy)

Pull request description:

  Add example for creating p2wpkh address.  It would be helpful to add examples the other address types (p2tr etc) either in separate files or in one.  Note this should be created in the Address crate however that crate is currently empty.

ACKs for top commit:
  tcharding:
    ACK d9b48cc2ce
  apoelstra:
    ACK d9b48cc2ce68b065640caa22da6fbcb1f3ad85f8; successfully ran local tests

Tree-SHA512: b383c1aca2d3669f66f0dcaf858526793ffc9e62671cf4985c44a11f7717c8e02d359b4d7d8b5822ebdde27595612e10bc831c84a2c4b654f69d4137ad16ba2f
2024-11-28 13:27:47 +00:00
merge-script 4123f2c00f
Merge rust-bitcoin/rust-bitcoin#3663: Fix internal weight calculation
f961f3c0ec Add test (yancy)
3352032892 Fix internal weight calculation (yancy)

Pull request description:

  A fix for 261c8d8ae6.  Maybe it's better to roll back that commit instead.  Clearly there needs to be a test or two added here as well since this change causes not test failures.

ACKs for top commit:
  apoelstra:
    ACK f961f3c0eca4e9b175761756f48699012b63d70a; successfully ran local tests
  sanket1729:
    utACK f961f3c0ec
  tcharding:
    ACK f961f3c0ec

Tree-SHA512: e6bb6d492b6a85f328bdfae6d0e5df9ea48c9c06bd0c310816826c893163bd0a6071af893fa7012fbd4eea0394bc8997277151486807fb8bd06ac78ce8cfc5fd
2024-11-28 12:52:53 +00:00
yancy 667b7f1010 Remove unused extern statement
extern crate keywords are no longer needed to import a crate
2024-11-27 23:06:55 -06:00
merge-script f95d877d56
Merge rust-bitcoin/rust-bitcoin#3660: Update `from_next_work_required` to take an i64 for timespan
4d0d78a3af Fix typo in from_next_work_required documentation (Shing Him Ng)
5e47c4123d Update CompactTarget::from_next_work_required to take timespan as i64 (Shing Him Ng)

Pull request description:

  [In Core](f7144b24be/src/pow.cpp (L56)), the timespan is defined as a signed int64. This PR will update `from_next_work_required` to take in an `i64` instead of a `u64`

  Fixes #3652

ACKs for top commit:
  tcharding:
    ACK 4d0d78a3af
  apoelstra:
    ACK 4d0d78a3af2ca300c6b40348724e806bd51ef27a; successfully ran local tests

Tree-SHA512: eea33ec18b081a97ae2a22c681982ad8693200ff8de3a3fc8f46b84b5e72f41589b79140ddd79ba3fce628f1539baf8387422bcc2c6b409fcadc4c2043aa2901
2024-11-28 03:17:08 +00:00
Tobin C. Harding 22769268f3
units: Close the hex parse errors
As part of the 1.0 effort close the errors in the `units::parse` module.
2024-11-28 13:36:09 +11:00
Tobin C. Harding 77085a1fa1
units: Remove serde from amounts
The `Amount` and `SignedAmount` were not supposed to implement `serde`
traits by design because doing so implicitly uses sats. We provide two
modules `as_sat` and `as_btc` to allow users to explicitly serialize in
their preferred format.

In commit: `d57ec019d5 Use Amount type for TxOut value field` derives
were added for `serde` and we did not notice it during review.
2024-11-28 12:55:43 +11:00
merge-script 3a73a480c8
Merge rust-bitcoin/rust-bitcoin#3643: Account for data pushing opcodes in `is_standard_op_return`
e0442782c8 fix(script): account for data pushing opcodes in `is_standard_op_return` (Rob N)

Pull request description:

  While I believe the original commit used 80 bytes for the entire script as the limit, Bitcoin Core as of [this commit](1101837461/src/policy/policy.h (L68-L72)) will relay OP_RETURN outputs as long as the data itself is not above 80 bytes, meaning a script of maximum size 83 bytes should be standard.

ACKs for top commit:
  tcharding:
    ACK e0442782c8
  apoelstra:
    ACK e0442782c8c04bdc48c2743e70f46e41c507e5f4; successfully ran local tests

Tree-SHA512: edd627806c9b5c85050594d45fde49ce3945b9358ae67b4bd0e22f7feb855fe5f4d90f1770870716decbc70aa1a3e0758902c74e3c010977731b823fd72ec23b
2024-11-28 01:14:26 +00:00
Tobin C. Harding 60f43a893d
Remove duplicate test case
In commit:

 `a10d5e15b3 Extract the Script assembly creator from fmt::Debug`

A test case was refactored and where there used to be two test case, one
for `Debug` and one for `Display`, two identical test cases were left.

Remove duplicate test case.
2024-11-28 11:50:42 +11:00
merge-script a22dfc2897
Merge rust-bitcoin/rust-bitcoin#3667: Fix psbt fuzz crash
9aebb96fb9 Fix psbt fuzz crash (Sanket Kanjalkar)

Pull request description:

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

  This occurs when combining two PSBTs with different xpub key sources. Added a length check before indexing into slices to prevent out-of-bounds access.

  For some reason, the precommit hook complained about non-ascii files. I don't think any of the names here are non-ascii

ACKs for top commit:
  apoelstra:
    ACK 9aebb96fb99e8e9e019663659c6eff851a62f2ce; successfully ran local tests; thanks!
  tcharding:
    ACK 9aebb96fb9

Tree-SHA512: b61274c594bc1f2ea4d04c8a7ace673a7632bb9ea31f59b56779a008c35e61281ea4f6b869990d886779e3e556932a3b2e8b015733ef18e236f12ca77e211c26
2024-11-28 00:00:12 +00:00
yancy d9b48cc2ce Add p2wpkh address creation example 2024-11-27 15:09:14 -06:00
Rob N e0442782c8
fix(script): account for data pushing opcodes in `is_standard_op_return`
While I believe the original commit used 80 bytes for the entire script as the limit,
Bitcoin Core as of [this commit](7a172c76d2/src/policy/policy.h)
will relay OP_RETURN outputs as long as the data itself is not above 80 bytes, meaning a script of maximum size 83 bytes should be standard.
2024-11-27 07:28:23 -10:00
Shing Him Ng 4d0d78a3af Fix typo in from_next_work_required documentation 2024-11-27 11:27:26 -06:00
Shing Him Ng 5e47c4123d Update CompactTarget::from_next_work_required to take timespan as i64
Bitcoin Core's consensus rules allow this timespan interval to be
negative. This commit also updates Params::pow_target_timespan to be a
u32. This field is almost compared to u64 and i64s, so changing this to
a u32 will allow users to use `.into()` for converstions
2024-11-27 11:27:22 -06:00
Sanket Kanjalkar 9aebb96fb9 Fix psbt fuzz crash
Fixes: https://github.com/rust-bitcoin/rust-bitcoin/issues/3628

This occurs when combining two PSBTs with different xpub key sources. Added a length check before indexing into slices to prevent out-of-bounds access.
2024-11-26 21:35:14 -08:00
yancy f961f3c0ec Add test
Test that behavior of this function as it interacts with predict_weight_internal
2024-11-26 16:28:11 -06:00
yancy 3352032892 Fix internal weight calculation
This function calls helper function predict_weight_internal and that
function was refactored.  The refactor changed the interface to accept
the input_weight instead of partial input_weight, however this function
was not updated with the refactor and was still passing a partial input
weight.  This commit is a followup that fixes the internal calculation
error by pass input_weight instead of partial_weight to the helper
function predict_weight_internal.
2024-11-26 16:21:26 -06:00
merge-script cefcce5bbf
Merge rust-bitcoin/rust-bitcoin#3653: Deprecate iwp weight
261c8d8ae6 Add total_weight to Input Weight Prediction (yancy)
cc4c36e8ac Deprecate weight (yancy)

Pull request description:

  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3645

  I'm not sure if `total_weight` is the most accurate.  Please advise if there's a different name that is better.

ACKs for top commit:
  apoelstra:
    ACK 261c8d8ae659d98fd3f63228f16d8f2eb4010b7e; successfully ran local tests

Tree-SHA512: f7fbcc587d3b72edf4add8a7bfecf99ee8133f679bd905c2ab6eeb959db455905c7e32ae91c95571599f09df605454f1e9953f1a47d1f1ee6d66270784c77793
2024-11-26 19:57:28 +00:00
merge-script 5958b0f8b8
Merge rust-bitcoin/rust-bitcoin#3656: Fix typo in hash tag type
d9ccb60a2f Fix typo in hash tag type (Tobin C. Harding)

Pull request description:

  In commit: `39f7dcb Reduce API surface of tagged wrapped hash types` I introduced a typo in the tagged hash tag type. Fortunately it has no effect because the tag type controls how an engine  is created (when the tag is input into the engine) and has no effect on the call to `from_engine`. However the typo makes the code confusing.

  This bug is currently unreleased.

  Fix: #3649

ACKs for top commit:
  apoelstra:
    ACK d9ccb60a2f9e89c8bb1979f032e8a222ab9b025a; successfully ran local tests

Tree-SHA512: 54cc78fb74ea119beaf4500d49a4d3954ffc3c52494b3ebc1539a3eec35078cb9388e72b42c59f371b53cced89b7b6303a0e3d3b02e59d72da182dd7bbfe2a55
2024-11-26 19:20:48 +00:00
yancy 261c8d8ae6 Add total_weight to Input Weight Prediction 2024-11-25 22:12:48 -06:00
Tobin C. Harding f9ab3aea8a
Re-export bech32 crate
`rust-bitcoin` is a lot further from 1.0 than `bech32` is. We previously
removed the re-export of `bech32` because we thought it might hold us up
releasing `rust-bitcoin 1.0` but in hindsite this was incorrect.

Having a public re-export of dependencies helps users keep their deps in
sync with those in `rust-bitcoin` and is thus useful.

Close: #3650
2024-11-26 10:13:19 +11:00
Tobin C. Harding d9ccb60a2f
Fix typo in hash tag type
In commit: `39f7dcb Reduce API surface of tagged wrapped hash types` I
introduced a typo in the tagged hash tag type. Fortunately it has no
effect because the tag type controls how an engine  is created (when the
tag is input into the engine) and has no effect on the call to
`from_engine`. However the typo makes the code confusing.

This bug is currently unreleased.

Fix: #3649
2024-11-26 10:06:58 +11:00
yancy cc4c36e8ac Deprecate weight
The name weight is misleading since it is actually only the segwit parts
of the weight calculation
2024-11-25 16:42:47 -06:00
merge-script 2a94004341
Merge rust-bitcoin/rust-bitcoin#3640: address: Add `Address::into_unchecked`
073ff81536 address: Simplify `Address::assume_checked` impl (Max Fang)
e4cf8ebc20 address: Add `Address::into_unchecked` (Max Fang)

Pull request description:

  ## Commits

  ### address: Add `Address::into_unchecked`

  Adds an ergonomic way to convert any `Address` (network can be checked
  or unchecked) into an `Address<NetworkUnchecked>` without cloning, which
  I've found useful in several contexts.

  ### address: Simplify `Address::assume_checked` impl

  Removes an unnecessary `match` which I noticed while implementing `Address::into_unchecked`.

  ## Small note on use of `Self`

  The style guide in `CONTRIBUTING.md` notes to return `Self` when possible, but that doesn't apply here as the `Address` being returned is different from the `Address` type referred to by `Self`, due to the changed `NetworkValidation` type.

ACKs for top commit:
  tcharding:
    ACK 073ff81536
  apoelstra:
    ACK 073ff81536e7a24883d6470ecf3054f4b7263186; successfully ran local tests; nice!

Tree-SHA512: dd6749cffad75d88568106169032d8af023ed56df4b1b38fa613ae7b140d1eb22933e7b0ffc1b17681a5db4b11b28d9db4efb62868755ca3c0f7cac20de0be4b
2024-11-25 13:00:00 +00:00
merge-script d662dba8b5
Merge rust-bitcoin/rust-bitcoin#3641: Fix documentation of Xpub::identifier
434e131660 Fix documentation of Xpub::identifier (LLFourn)

Pull request description:

  :^)

ACKs for top commit:
  tcharding:
    ACK 434e131660
  apoelstra:
    ACK 434e131660e82e06efd97981c1d975c04de2d010; successfully ran local tests; lol

Tree-SHA512: 5c86679a05b6cc7c77f6d6727520002ada1037b4df97c565ac383824afb4e68194a297bdaf8b0074138be54e6142b8f0819da056912c7feed0538bb1a7054254
2024-11-23 14:57:22 +00:00
merge-script e0ba1b661c
Merge rust-bitcoin/rust-bitcoin#3636: Mark `checked_` functions as `const`
a8379bf005 Mark `checked_` functions const in bitcoin. (Jamil Lambert, PhD)
fe10ff2eb7 Mark functions `const` in `units` (Jamil Lambert, PhD)

Pull request description:

  Following on #3608, #3627 and #1174 the rest of the `checked_` functions in all `rust-bitcoin` have been marked as const.  Except for tests and traits.

ACKs for top commit:
  apoelstra:
    ACK a8379bf0053e66cf5984ce449d19e54e529b6b70; successfully ran local tests; thanks! I find this much easier to read
  tcharding:
    ACK a8379bf005

Tree-SHA512: b8d97b7a3d9fc33b57349f418ccc5aac2f3e8c4145a73bf0e24e85547217d41214696e9a80b1fb5a1bae7e766aad1c2b5df6044564772fe76cec9b3628bcd8e5
2024-11-23 11:46:29 +00:00
Max Fang 073ff81536 address: Simplify `Address::assume_checked` impl
Removes an unnecessary `match`.
2024-11-19 21:15:43 -08:00
Max Fang e4cf8ebc20 address: Add `Address::into_unchecked`
Adds an ergonomic way to convert any `Address` (network can be checked
or unchecked) into an `Address<NetworkUnchecked>` without cloning, which
I've found useful in several contexts.
2024-11-19 21:15:43 -08:00
LLFourn 434e131660
Fix documentation of Xpub::identifier 2024-11-19 15:32:30 +11:00
Jamil Lambert, PhD a8379bf005
Mark `checked_` functions const in bitcoin.
Replace `?` operators, which are not allowed in const context, with
match statements.
2024-11-18 14:54:15 +00:00
Fmt Bot 500cd10802 2024-11-17 automated rustfmt nightly 2024-11-17 01:23:55 +00:00
merge-script 055a462778
Merge rust-bitcoin/rust-bitcoin#3625: Rename tests that have _test suffix
ad82ed7179 Fix spelling typo (yancy)
8fe5ffde4c Rename tests that have _test suffix (yancy)

Pull request description:

  Convention is to not include test as a suffix

  Used `git grep -n _test` and renamed all the tests that made sense to rename.  There are a number of fn `do_test(data: &[u8]) {` that I felt it would be better not to touch.  Everything else that was a testname I renamed if it had a _test suffix.

ACKs for top commit:
  apoelstra:
    ACK ad82ed71796c79399cd8a81814b1af61c4ca3582; successfully ran local tests
  tcharding:
    ACK ad82ed7179

Tree-SHA512: 9c4ee71974e39814a8a63d269e3dcf6e761312dd0903ac1e6268dc421b9ef89a63f27cade3d5a51436660bb01457ac1a2f23a628a4d11622cd4a33fa6c483934
2024-11-16 01:43:44 +00:00
merge-script d32422cdb7
Merge rust-bitcoin/rust-bitcoin#3611: Hashes: Make `hex` dependency optional
ec06028f63 hashes: Make hex dependency optional (Tobin C. Harding)
9dce0b4b8c Remove hex string trait bounds from GeneralHash (Tobin C. Harding)
766f498b33 Pull serde stuff out of impl_bytelike_traits macro (Tobin C. Harding)

Pull request description:

  This is done in 3 parts:

  1. Pull the `serde` stuff out of `impl_bytelike_traits` to fix the bug described here: https://github.com/rust-bitcoin/rust-bitcoin/issues/2654#issuecomment-2470716693
  2. Prepare the `hashes` trait by removing string/hex trait bounds from `GeneralHash` and also pull the hex/string stuff out of `impl_bytelike_traits`
  3. Make hex optional, including adding custom debug logic when `hex` feature is not enabled

  Patch 3 is tested in `hashes/embedded`, by the new `debug` unit test, and there is a `Midstate` unit test as well that covers the `Debug` impl.

  Close: #2654 - BOOM!

ACKs for top commit:
  apoelstra:
    ACK ec06028f63ba591a14c3a15cdfd410bb5ff1c09b; successfully ran local tests; nice!

Tree-SHA512: 85eb10d36a4581af6cd700f7ff876585bcc114c60e9864906e65659f3b3ee550fe6d9f40ca4230d870a9e23f0720723e11443ec329f16e40259a259b9be57466
2024-11-15 18:31:42 +00:00
Tobin C. Harding 53d3ab18b9
Bump version of bitcoin-primitives to 0.101.0
In preparation for releasing `primitives v0.101.0` bump the version
number, add a changelog entry, update the lock files, and depend on the
new version in all crates that depend on `primitives`.
2024-11-15 10:58:46 +11:00
yancy ad82ed7179 Fix spelling typo 2024-11-14 15:20:41 -06:00
Tobin C. Harding 7819e50055
Move Block to primitives
On the way re-design the API by doing:

- Introduce `Checked` and `Unchecked` tags
- Rename the `txdata` field to `transactions`
- Make the `Block` fields private
- Add getters for `header` and `transactions` fields
- Move the various `compute_` methods to be free standing functions
- Make the `check_` functions private
- Introduce extension traits
2024-11-15 07:16:21 +11:00
Tobin C. Harding 55200924e4
Move Sealed to bottom of file
In preparation for adding `Block` to the private `sealed` module move
the module down to the bottom of the file.
2024-11-15 07:14:31 +11:00
Tobin C. Harding d5b148d400
Make block_size free standing and rename
In preparation for adding a block extension trait move the only private
method off the `Block` type and make it free standing.

While we are at it just take a slice of transactions since we don't need
the block itself - rename appropriately.

Internal change only.
2024-11-15 07:14:31 +11:00
Tobin C. Harding 5016a73207
Manually implement consensus traits for Block
In preparation for hacking the `Block` type implement the consensus
traits manually instead of using the macro.

Refactor only, no logic changes.
2024-11-15 07:14:31 +11:00
yancy 8fe5ffde4c Rename tests that have _test suffix
Convention is to not include test as a suffix
2024-11-14 14:05:15 -06:00
merge-script 72760bbe96
Merge rust-bitcoin/rust-bitcoin#3606: Deprecate function
eacb1e7831 Deprecate function (yancy)

Pull request description:

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3602

ACKs for top commit:
  tcharding:
    ACK eacb1e7831
  apoelstra:
    ACK eacb1e7831bdf2058a674a106b91447c1fe9470e; successfully ran local tests; lol

Tree-SHA512: 9da7881ee62e1808d83c92e232cef42055cce6e3ba03dc1001472948380c16a1968da1d4ce4f7ad13d78d1d70e143851f2635ba167c4df34be61ecf3f2fdee4e
2024-11-14 18:21:20 +00:00
yancy eacb1e7831 Deprecate function 2024-11-13 22:31:24 -06:00
Tobin C. Harding ec06028f63
hashes: Make hex dependency optional
The only reason we need `hex-conservative` is to parse strings and
format them as hex. For users that do not require this functionality we
can make the `hex-conservative` crate an optional dependency.

The `serde` feature requires `Display` so we enable `hex` from the
`serde` feature.

If `hex` feature is not enabled we still need to be able to debug so
provide `fmt::Debug` functionality by way of macros.

Close: #2654
2024-11-14 09:36:55 +11:00
Shing Him Ng 33b9d6904c Remove From and Into test impls
The removed impls have been replaced with test-specific functions for
converting between Rust primitives and U256
2024-11-13 15:49:50 -06:00
Tobin C. Harding 9dce0b4b8c
Remove hex string trait bounds from GeneralHash
For the `hashes` crate we would like to make `hex` an optional
dependency. In preparation for doing so do the following:

- Remove the trait bounds from `GeneralHash`
- Split the hex/string stuff out of `impl_bytelike_traits` into a
  separate macro.
2024-11-13 14:01:13 +11:00
Tobin C. Harding 766f498b33
Pull serde stuff out of impl_bytelike_traits macro
The `impl_bytelike_traits` macro is public and it is used in the
`hash_newtype` macro, also public.

Currently if a user calls the `hash_newtype` macro in a crate that
depends on `hashes` without the `serde` feature enabled and with no
`serde` dependency everything works. However if the user then adds a
dependency that happens to enable the `serde` feature in `hashes` their
build will blow up because `serde` code will start getting called from
the original crate's call to `hash_newtype`.

Pull the serde stuff out of `hash_newtype` and provide a macro to
implement it `impl_serde_for_newtype`.
2024-11-13 12:28:47 +11:00
Tobin C. Harding 1ed3fc9270
Add unit test for tapscript function
Add a unit test that verifies we correctly remove the annex when getting
the tapscript from the witness stack.

Unit test written by Casey, pulled out of #3599.

Co-developed-by: Casey Rodarmor <casey@rodarmor.com>
2024-11-13 10:55:51 +11:00
Tobin C. Harding 195615c14d
Fix bug in witness stack getters
In #2646 we introduced a bug in the taproot witness stack getter
functions, of which we have three:

- `tapscript`
- `taproot_control_block`
- `taproot_annex`

Each returns `Some` if a possible bytes slice is found (with no other
guarantees).

Use `taproot_annex` combined with getters from `primitives` to implement
the other two getters. This simplifies the code and fixes the bug.

Add an additional getter to `primitives` `Witness::third_from_last`.

Fix: #3598
2024-11-13 10:55:51 +11:00
Tobin C. Harding 84ede349b0
Run the formatter
Run `just fmt`, no manual changes.
2024-11-11 14:19:17 +11:00
Tobin C. Harding 4865d60258
bitcoin: Improve the re-exports ... again
These re-exports are hard to get right. We (I) recently made an attempt
to make our stack of crates easier to navigate using the idea that
exporting a type from the crate it is defined in adds some additional
information without any loss of clarity. Note however that the module
must be re-exported from the "highest" place possible because we at
times add additional functionality as we move up the stack e.g.,
`bitcoin::merkle_tree` has logic in it that `primitives::merkle_tree`
does not. In order to future proof the codebase default to always using
the highest module up the stack even when that module adds no additional
code e.g., re-export `blockdata::fee_rate` as opposed to
`units::fee_rate` in the event that we later add logic to
`bitcoin::blockdata::fee_rate`.

This patch adds an additional re-export: `sequence` (previously missing).
2024-11-11 14:18:58 +11:00
merge-script 9d9f1d84e4
Merge rust-bitcoin/rust-bitcoin#3588: Reword `Address` constructor docs
8098d5ee05 Reword `Address` constructor docs (Jamil Lambert, PhD)

Pull request description:

  Change the wording of the Address constructor function docs to be a standard format. Following up on [#3584 comment](https://github.com/rust-bitcoin/rust-bitcoin/pull/3584#issuecomment-2457197066)

ACKs for top commit:
  apoelstra:
    ACK 8098d5ee055d672fc8f6e4eef3971b66478a61b0; successfully ran local tests
  tcharding:
    ACK 8098d5ee05

Tree-SHA512: 1027f334d69182465b2ecafd6fcf9f5deb2b299a64d12dea7f09f25379bd98b1e11be52a04fea83c2194164fd428931f6416eee52603f4bc043717b4e4d1b193
2024-11-08 15:25:27 +00:00
merge-script 0667a67b7c
Merge rust-bitcoin/rust-bitcoin#3592: Update generic param
3523b8e117 Rename generic parameter and variable (yancy)

Pull request description:

  Use W for Write

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3590

ACKs for top commit:
  tcharding:
    ACK 3523b8e117
  apoelstra:
    ACK 3523b8e1171663fc93ab25765271a1070a2d649f; successfully ran local tests

Tree-SHA512: 8dd9f38ee07e7652ebd291eb121a2630ebb09ea9ea70873f87e57241f51e5114106517511d1fe07774ec4f42920e9d0619031a1545787631a30422de12421a67
2024-11-08 14:19:00 +00:00
yancy 3523b8e117 Rename generic parameter and variable
Use W for Write
2024-11-07 16:38:13 -06:00
merge-script f963bf38b1
Merge rust-bitcoin/rust-bitcoin#3585: Fix bip34 number parsing for Block
c61df05916 Fix bip34 number parsing for Block (junderw)

Pull request description:

  Fixes #3583

ACKs for top commit:
  apoelstra:
    ACK c61df059160d3aeef73046de28cb5d5afcb962d2; successfully ran local tests
  tcharding:
    ACK c61df05916

Tree-SHA512: 3fade122f98c7c9e3b2888c33479d3a9426fb35f212ccee6b89b7cd5c92d2b58c93e165d5c59eda183c0943809d7e633d929000be92b197988a3ceb0ea37514f
2024-11-07 21:16:25 +00:00
Jamil Lambert, PhD 8098d5ee05
Reword `Address` constructor docs
Change the wording of the Address constructor function docs to be a
standard format.
2024-11-06 19:05:29 +00:00
merge-script 4797a75586
Merge rust-bitcoin/rust-bitcoin#3584: Standardize constructor type function docs
1649b68589 Standardize wording to `constructs a new` (Jamil Lambert, PhD)
27f94d5540 Replace `creates` with `constructs` (Jamil Lambert, PhD)

Pull request description:

  As discussed in issue #3575 there are various ways of saying a new object is created.

  These have all be standardized to the agreed version.

  Close #3575

ACKs for top commit:
  apoelstra:
    ACK 1649b68589834dfe9d5b63812da3e9f0e5930107; successfully ran local tests
  tcharding:
    ACK 1649b68589

Tree-SHA512: 0ed9b56819c95f1fc14da1e0fdbbe03c4af2d97a95ea6b56125f72913e8d832db5d2882d713ae139d00614e651f3834a4d72528bdf776231cceb6772bf2f9963
2024-11-06 16:05:24 +00:00
junderw c61df05916
Fix bip34 number parsing for Block 2024-11-06 14:06:18 +09:00
Jamil Lambert, PhD 1649b68589
Standardize wording to `constructs a new`
There is a range of different wordings used in the docs of constructor
type functions.

Change all to start with `Constructs a new` or `Constructs an empty`.
2024-11-05 13:02:26 +00:00
Jamil Lambert, PhD 27f94d5540
Replace `creates` with `constructs`
In functions that act like constructors there is a mixture of the usage
of `creates` and `constructs`.

Replace all occurrences of `creates` with `constructs` in the first line
of docs of constructor like functions.
2024-11-05 12:47:28 +00:00
Tobin C. Harding 915c9627f6
Put feature gate attribute under test attribute
Either way is fine, just pick one and be uniform.
2024-11-05 11:16:23 +11:00
merge-script 3b0cb0e87d
Merge rust-bitcoin/rust-bitcoin#3539: Improve the `amount` module
81d8699b55 units: Put no_std up top (Tobin C. Harding)
3e332c3839 amount: Fix docs on FromStr (Tobin C. Harding)
5bec76aa51 amount: Fix rustdocs (Tobin C. Harding)
41c80cc476 amount: Improve docs on div by weight (Tobin C. Harding)
2b4a61739a Add type to debug output for Amount (Tobin C. Harding)
42e5043b33 Add from_int_btc group of functions (Tobin C. Harding)

Pull request description:

  Improve the `amount` module by doing:

  - Patch 1: Add/update `from_int_btc` and `from_int_btc_const` functions
  - Patch 2:  Add type to debug output for `Amount`
  - Patch 3: Fix incorrect docs
  - Patch 4: Make docs use correct style and be uniform across the two amount types
  - Patch 5: Fix docs on `FromStr`

ACKs for top commit:
  apoelstra:
    ACK 81d8699b55dad570247cb14d2b3eea64270a83cf; successfully ran local tests
  jamillambert:
    ACK 81d8699b55

Tree-SHA512: ddd6e02b4cd9a9aa8cbdd2d2f7ae289a6bcca9eb002ef0c6d83a6eca950b2cd08f5918286bb33e814f321e3bfe83fdf75ae051cf8f91ee45d3e4abe76c1c2a4c
2024-11-04 23:32:41 +00:00
Tobin C. Harding 6555143478
bitcoin: Re-format crate level re-exports
Is there any advantage trying to lay out the re-exports to give users an
idea of the crate structure?

We have the explicit aim that users who depend on `bitcoin` do not ever
need to reach directly into `primitives` (or `units`) however it is kind
of nice to know where things come from, saves jumping to multiple files
looking for them (for those of us that jump to files manually).

I do not know how all the re-exports interact with other folks IDEs, I
personally open files manually and just remember where stuff is.
2024-11-04 08:49:03 +11:00
merge-script 794fb75497
Merge rust-bitcoin/rust-bitcoin#3562: Re-export `block::Header` as `BlockHeader`
85942c355d Re-export block::Header as BlockHeader (Tobin C. Harding)

Pull request description:

  For users who want to just grab stuff from the crate root it makes total sense for there to be a `BlockHeader`.

  Another nice thing, in the HMTL docs it makes BlockHeader be in the struct list right along with `Block`, `BlockHash`, and `BlockHeight`.

  Close: #3548

ACKs for top commit:
  apoelstra:
    ACK 85942c355dfdcf9ddf9a46202f4db56794dcc85d; successfully ran local tests
  jamillambert:
    ACK 85942c355d

Tree-SHA512: 6fbaf7936062323d31940179ffcbc62951b21f53955a42dca2e28476bda0cebeb041db0e1b4f1ceeac32e43d94fe5476ef055595ae88dc6cfc266d32082bdf4d
2024-11-03 17:02:38 +00:00
merge-script 26873c4ee8
Merge rust-bitcoin/rust-bitcoin#3572: Automated nightly rustfmt (2024-11-03)
5ecf7f2d67 2024-11-03 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 5ecf7f2d6795b07f8c4e3f2804e018deca80f113; successfully ran local tests

Tree-SHA512: b66b82b185120cd8f00f5bb986c914011003b727f2c172cb3da720fb2b8ac2d4027e6a9f75673ccda0a1328a77cd1bfff9aaf47506ae40a68e8984b19a250d85
2024-11-03 16:29:56 +00:00
merge-script 7210543f17
Merge rust-bitcoin/rust-bitcoin#3569: script: remove `unsafe` marker from slice-to-script conversions
4431df18fe script: remove `unsafe` marker from slice-to-script conversions (Andrew Poelstra)

Pull request description:

  The length of the slices is not a safety invariant.

  Fixes #3531

ACKs for top commit:
  tcharding:
    ACK 4431df18fe

Tree-SHA512: 85f745058bc5bf68d312f75c53cef570ead986247ca3cb3fc54b5381dd0214e7df55c1fcb99f06e693b0b9478ec6a9785186cac56886f8007ab7b827d903bd01
2024-11-03 15:06:36 +00:00
Fmt Bot 5ecf7f2d67 2024-11-03 automated rustfmt nightly 2024-11-03 01:21:14 +00:00
Andrew Poelstra 4431df18fe
script: remove `unsafe` marker from slice-to-script conversions
The length of the slices is not a safety invariant.

Fairly large diff because I had to remove a bunch of `unsafe` blocks
around calls to this function.

Fixes #3531
2024-11-02 13:45:11 +00:00
Tobin C. Harding 42e5043b33
Add from_int_btc group of functions
Add/update the from_int group of functions to provide one that errors
and one that is const and panics (errors in const context are not useful
because one cannot call `unwrap` in const context).
2024-11-02 10:19:03 +11:00
Tobin C. Harding 90b2ac03e3
hashes: Introduce impl_bytelike_traits macro
We have a couple of problems:

1. There are two macros currently for fmt stuff that do similar things,
`arr_newtype_fmt_impl` and `hex_fmt_impl` - the difference is not
immediately obvious, its the way that the byte array is iterated.

2. Our hash types are missing `AsRef<[u8; len]>` and `Borrow<[u8; len]>`.

Introduce a new macro and remove a bunch of other macros. Include
extensive docs but hide the macro from public docs because its not
really for consumers of the library.

The macro requires `$crate::hex` to point to `hex-conservative`.

Note the macro is pretty generic (as in general purpose), `hashes` might
not be the right home for it. Potentially a better place would be in
`hex` itself?
2024-11-02 08:10:43 +11:00
Tobin C. Harding 85942c355d
Re-export block::Header as BlockHeader
For users who want to just grab stuff from the crate root it makes total
sense for there to be a `BlockHeader`.

Another nice thing, in the HMTL docs it makes BlockHeader be in the
struct list right along with `Block`, `BlockHash`, and `BlockHeight`.
2024-11-02 07:38:10 +11:00
merge-script 112d75eed9
Merge rust-bitcoin/rust-bitcoin#3517: Remove `private_key_debug_is_obfuscated` test
7bff725494 Remove private_key_debug_is_obfuscated test (Diogo Canut)

Pull request description:

  Solves #3186.

ACKs for top commit:
  tcharding:
    ACK 7bff725494
  apoelstra:
    ACK 7bff725494bed02bed7f5d2af8278c89aaf73f40; successfully ran local tests; sure

Tree-SHA512: 68dcdc476ada7960eb224dd456010bc36a302472b8a8b4d1ea3ab9c6981550d7677a98516331d5e5ef636be0cdc7208c45278d70c506e04d067b997758fd4bcf
2024-11-01 14:20:46 +00:00
Tobin C. Harding 6aa8c2b023
Remove needless_borrows_for_generic_args
This has been fixed and we use nightly to lint so we have access to the
merged fix.

Removing the attribute uncovers a bunch of real lint warnings, fix
them while we are at it.
2024-11-01 14:30:45 +11:00
merge-script 4c8347a7ac
Merge rust-bitcoin/rust-bitcoin#3544: Favour `to_vec` over `to_bytes`
a51768af3f key: Deprecate to_bytes (Tobin C. Harding)
3af3239ad0 script: Re-order functions (Tobin C. Harding)
db40297f87 script: deprecate to_bytes (Tobin C. Harding)
c5cd0db493 Revert the change to to_bytes (Tobin C. Harding)
dc2ca785d2 Add to_vec and deprecate to_bytes for array types (Tobin C. Harding)
a6b7ab32a8 Move impl_array_newtype to internal_macros (Tobin C. Harding)

Pull request description:

  Use `to_vec` and deprecate `to_bytes`, the opposite of what we did in #2585.

  For functions that return a `Vec` by first allocating use function name `to_vec`. This explicitly excludes:

  - Functions that return an array (`CompressedPublicKey::to_bytes`)
  - Functions that consume self and return a `Vec` without allocating (`ScriptBuf::into_bytes`)

  See #3025 for discussion and consensus.

  Close: #3025

ACKs for top commit:
  apoelstra:
    ACK a51768af3f3d4c8e138e1ded250800810bedc903; successfully ran local tests

Tree-SHA512: ee932c13ad2e09c2b76a7833b23c859df175aa307f56e673921f3ae8b5d865518c6f999749e3b627594457b3ca33301b777177ada3520cf006acc0f14e5dacf8
2024-11-01 00:55:15 +00:00
merge-script 8bc2f39fca
Merge rust-bitcoin/rust-bitcoin#3547: Remove more wildcard re-exports
0bf1910980 Remove wildcard from script self re-exports (Tobin C. Harding)
397a4b9382 Remove wildcard in push_bytes module (Tobin C. Harding)

Pull request description:

  We thought #3436 was done (well I thought it was), turns out I was wrong.

ACKs for top commit:
  jamillambert:
    ACK 0bf1910980
  apoelstra:
    ACK 0bf1910980a13005496244ec4d4adf0553afbc73; successfully ran local tests

Tree-SHA512: 131a4aa4a907099790d14cfc2d19305943a2270cf6852c7dd92e35ea69188c9a40797fb22fd2ed8b2fefc2d6380b884401b5e32f521711f0f7b2da610d0e332f
2024-10-31 23:51:50 +00:00
Diogo Canut 7bff725494 Remove private_key_debug_is_obfuscated test
The private_key_debug_is_obfuscated test is removed because it belongs
in the secp256k1 library, not in the bitcoin library. Keeping it here
is redundant and creates unnecessary maintenance.
2024-10-31 20:31:59 -03:00
merge-script 8af8bc7886
Merge rust-bitcoin/rust-bitcoin#3543: Bump hex-conservative to 0.3.0
7f289a9fdf Bump hex-conservative to 0.3.0 (Leo Nash)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 7f289a9fdf
  apoelstra:
    ACK 7f289a9fdf86604cc1ecfb31c6df0ea5bb175465; successfully ran local tests

Tree-SHA512: bf8543ef7dc9b7ec0d11659c0a53213be50b9bbae0e6ba6970a19d32b3d89025315fcae332c23d68582ef69ea5f8e6673192ff74316a1e3dc47521312bbc469e
2024-10-31 21:05:02 +00:00
Tobin C. Harding a51768af3f
key: Deprecate to_bytes
As we have been doing in other places deprecate `to_bytes` in favour of
`to_vec`. Note this is only for functions that return a `Vec`, the `key`
module has `CompressedKey::to_bytes` still as it returns an array.
2024-11-01 07:10:41 +11:00
Tobin C. Harding db40297f87
script: deprecate to_bytes
Deprecate the `Script::to_bytes` function in favour of `to_vec` as we
are doing elsewhere.

Note that `ScriptBuf` has `into_bytes` because it does not copy.
Potentially this should be deprecated in favour of `into_vec`?

Note that in regards to the `to_` prefix this naming as valid according
to convention because the `Script` type is borrowed and `to_vec` copies
the underlying bytes.
2024-11-01 07:09:53 +11:00
Tobin C. Harding c5cd0db493
Revert the change to to_bytes
During this release cycle we deprecated `to_vec` in favour of
`to_bytes`, we have since reversed our position on the name.

Remove the deprecation of `to_bytes` from the three types that had it
and use `to_vec`.
2024-11-01 07:09:11 +11:00
Tobin C. Harding dc2ca785d2
Add to_vec and deprecate to_bytes for array types
For the array wrapper types (eg `ShortId`) created with the
`impl_array_newtype` macro deprecated the `to_bytes` function and add a
`to_vec` function.

Discussed and decided upon in issue: #3025
2024-11-01 07:08:49 +11:00
merge-script 5f3af4bab2
Merge rust-bitcoin/rust-bitcoin#3536: Use `doc(notable_trait)`
dfb76c1e15 Use doc(notable_trait) (Tobin C. Harding)

Pull request description:

  There is an unstable feature that puts up a little 'i' in a circle next to any function that returns a type that implements an notable trait. For us this means we can make the extension traits more discoverable.

  ref: https://doc.rust-lang.org/unstable-book/language-features/doc-notable-trait.html

  Close: #3232

ACKs for top commit:
  apoelstra:
    ACK dfb76c1e15933fe0058c1bb69c4b1b9acddceee8; successfully ran local tests

Tree-SHA512: 0fbc7a2a3c8c499a9276d1e86b9966a7ae6bd8a354aff5fd40aa11d07945db589b2a9c2cdfa43ddadfafcee706ae7f68cedc269f74622643307cc43cd07d554f
2024-10-31 15:01:19 +00:00
Tobin C. Harding 0bf1910980
Remove wildcard from script self re-exports
In the `script` module remove the wildcards and re-export stuff from
`self` explicitly in both `primitives` and `bitcoin`.

Internal change only, everything is re-exported.
2024-10-31 15:52:54 +11:00
Tobin C. Harding 397a4b9382
Remove wildcard in push_bytes module
Remove the wildcard even though it is only re-exporting things from
module code within the file.
2024-10-31 15:51:29 +11:00
Leo Nash 7f289a9fdf Bump hex-conservative to 0.3.0 2024-10-31 03:36:22 +00:00
Tobin C. Harding a6b7ab32a8
Move impl_array_newtype to internal_macros
The current feature gating is wrong, this bug is unreleased because it
was introduced #2585.

The `impl_array_newtype` macro is only used in the `bitcoin` crate, it
does not need to be in `internals`. Also, other crates have an `alloc`
feature which `bitcoin` does not have so if we ever need it in other
places we'll need a duplicate with the correct feature gating anyways.

Move the macro to `bitcoin::internal_macros` and remove the incorrect
`alloc` feature gating.
2024-10-31 14:15:41 +11:00
Tobin C. Harding dfb76c1e15
Use doc(notable_trait)
There is an unstable feature that puts up a little 'i' in a circle
next to any function that returns a type that implements an notable
trait. For us this means we can make the extension traits more
discoverable.

ref: https://doc.rust-lang.org/unstable-book/language-features/doc-notable-trait.html

Close: #3232
2024-10-31 13:33:28 +11:00