Commit Graph

2070 Commits

Author SHA1 Message Date
Andrew Poelstra 4e3af5162f
units: add global `BlockMtp` type
For our relative locktime API, we are going to want to take differences
of arbitrary MTPs in order to check whether they meet some relative
timelock threshold.

However, the `locktime::absolute::Mtp` type can only represent MTPs that
exceed 500 million. In practice this is a non-issue; by consensus MTPs
must be monotonic and every real chain (even test chains) have initial
real MTPs well above 500 million, which as a UNIX timestamp corresponds
to November 5, 1985.

But in theory this is a big problem: if we were to treat relative MTPs
as "differences of absolute-timelock MTPs" then we will be unable to
construct relative timelocks on chains with weird timestamps (and on
legitimate chains, we'd have .unwrap()s everywhere that would be hard to
justify). But we need to treat them as a "difference of MTPs" in *some*
sense, because otherwise they'd be very hard to construct.
2025-05-06 15:19:35 +00:00
Erick Cestari c11772a768
Accept flexible input types for Taproot-related functions
Refactor Taproot functions to accept any type implementing `Into<XOnlyPublicKey>`,
instead of requiring `XOnlyPublicKey` directly. This improves ergonomics when working
with compatible types, avoiding unnecessary `.into()` conversions at call sites.
2025-05-06 09:01:27 -03:00
Erick Cestari 2a518d62e6
Wrap secp256k1::XOnlyPublicKey to improve error handling
This commit creates a wrapper type for XOnlyPublicKey instead of
directly re-exporting it from the secp256k1 library.
2025-05-06 09:01:17 -03:00
Jamil Lambert, PhD 2fbbc825c9
Allow uninlined format args
There is a new lint error on nightly-2025-04-25 "variables can be used
directly in the `format!` string".

Exclude the lint to allow the existing syntax in `format!` strings.
2025-05-06 09:49:02 +01:00
Daniel Roberts 7ecef176f9 Fix documentation error for `TweakedPublicKey::serialize` 2025-05-06 03:06:17 -05:00
merge-script 2d93c8e5d1
Merge rust-bitcoin/rust-bitcoin#4448: bitcoin: remove torv2 support
53d32c9e4f bitcoin: remove torv2 support (Bruno Garcia)

Pull request description:

  This PR removes support to TorV2 since it's deprecated and no longer useful to have it.

ACKs for top commit:
  apoelstra:
    ACK 53d32c9e4f0ef0f3b2c7d4dcba42e3ac5344f78a; successfully ran local tests
  tcharding:
    ACK 53d32c9e4f

Tree-SHA512: 69a2ba399d5eac7f132519ab83362fbd8739d9e975795e441cefa75896ddbf4041db2125ffde51316f9ad69aa0b62c8b226ccff042b0dae6d3c615826bc339f4
2025-05-06 02:50:48 +00:00
Tobin C. Harding 51fe619fe0
Set deprecation to released date of to_inner
In #4373 we added a couple new conversion methods and deprecated the
`to_inner` ones. During that the deprecation date was set to `0.33.0`.

We have backported the changes and will deprecate in `0.32.6` so set the
version number now so we don't forget later.
2025-05-06 09:10:07 +10:00
Bruno Garcia 53d32c9e4f bitcoin: remove torv2 support 2025-05-05 15:27:35 -03:00
merge-script ec44656933
Merge rust-bitcoin/rust-bitcoin#4438: Automated nightly rustfmt (2025-05-04)
1f19d9b4bd 2025-05-04 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 1f19d9b4bd8c55b9a7dc233c4b969d5d54d3a951; successfully ran local tests
  tcharding:
    ACK 1f19d9b4bd

Tree-SHA512: c13d24482848cc6e103304b52bd51964fbca9f3b09b5c179af7956aef0537b6cc05014a203844c93d0b21da08cd51bf4a84dc5611b61ae5684ae57b0beb2848f
2025-05-05 12:53:41 +00:00
yancy 13cbead947 Use NumOpResult instead of Option
Prefer the more descriptive NumOpResult return type over Option where
return types are fallible.

The returned type NumOpResult is already annotated as must_use per the
lint, so must_use can be removed after changing the method return types
to NumOpResult.
2025-05-05 07:18:15 -05:00
merge-script 1b04f5df65
Merge rust-bitcoin/rust-bitcoin#4431: units: rename relative locktime types, absolute::Time, and constructors
826acb8273 units: rename relative::HeightInterval constructors (Andrew Poelstra)
39b4f7670d units: rename relative::Height to HeightInterval (Andrew Poelstra)
d3619cc1bc units: deprecate relative::MtpInterval::to_consensus_u32 (Andrew Poelstra)
1a6b8b4c7a units: rename relative::Time to MtpInterval (Andrew Poelstra)
39b057fade units: rename absolute::Height consensus functions (Andrew Poelstra)
5a8f33f380 units: rename absolute::Mtp consensus functions (Andrew Poelstra)
8ffcd2cf30 units: rename absolute::Time to absolute::Mtp (Andrew Poelstra)

Pull request description:

  This PR does a whole bunch of renames to the `units` locktime modules. These modules contain the underlying units for locktime types and hopefully aren't used directly too often, which should minimize the disruption from these renames.

  This fixes a number of issues:

  * Confusion between blocktimes and MTPs (which in fairness, *are* blocktimes, but they're not the one that users are likely to reach for)
  * Constructor and conversion names that imply there is a "consensus encoding" for these bare units, which corresponded to the consensus encoding of the corresponding locktimes/sequence numbers
  * `from_*` methods without `to_*` methods and vice-versa (overlaps with the above)
  * the horribly named `value` method on relative heights and times (but not absolute ones)

  This PR does **not** remove the `MtpAndHeight` type, nor does it add constructors for `Mtp` from lists of blocktimes. This is because the PR was too big already and I felt I should split it up.

  Alternate to #4427

ACKs for top commit:
  tcharding:
    ACK 826acb8273

Tree-SHA512: 6e0491e17927625cde85c2cf92ff152a10613e632474122a626ee31b662d21c09fcb9fa3014c44708c97536535a33845cbbcd81e73dcdf98e9ee9fd6143c698f
2025-05-05 09:08:30 +10:00
Fmt Bot 1f19d9b4bd 2025-05-04 automated rustfmt nightly 2025-05-04 01:41:13 +00:00
merge-script 082075304e
Merge rust-bitcoin/rust-bitcoin#4373: Add as_inner method to TweakedKeypair
3bb6c73f2d Add methods to retrieve inner types (Shing Him Ng)

Pull request description:

  Resolves #4345

ACKs for top commit:
  tcharding:
    ACK 3bb6c73f2d
  apoelstra:
    ACK 3bb6c73f2d3edd1165b7b7f3a833fa471786e166; successfully ran local tests; should backport to 0.32.x

Tree-SHA512: c89017bbc2126ec62c756c4ee9b49dcc8b94a3063a8155aadcf7c69a6f0bc9337baedffe7f52a4ab6f0b738302bea683391d394483c4c7eefbb622b97d34d26c
2025-05-03 23:24:19 +00:00
Andrew Poelstra 39b4f7670d
units: rename relative::Height to HeightInterval
This is disruptive, but makes the type name consistent with
`MtpInterval` and also greatly improves clarity, helping to distinguish
between absolute and relative locktimes and reminding the author (and
reviewer) of locktime code that this needs to be a diff.
2025-05-03 03:12:08 +00:00
Andrew Poelstra 1a6b8b4c7a
units: rename relative::Time to MtpInterval
The name `Time` is misleading. In fact this represents an interval
between MTPs.
2025-05-03 03:12:07 +00:00
Andrew Poelstra 8ffcd2cf30
units: rename absolute::Time to absolute::Mtp
This is not a generic UNIX timestamp, but rather a MTP restricted to
have values between 500 million and u32::MAX. Most importantly, it is
*not* a blocktime, which is what is implied by its name and
constructors.
2025-05-02 17:44:58 +00:00
Shing Him Ng 3bb6c73f2d Add methods to retrieve inner types
For TweakedKeypair, `to_inner` is also renamed to `to_keypair` to maintain
consistency. Similarly, `to_inner` is renamed to `to_x_only_pubkey` for
TweakedPublicKey
2025-05-01 08:24:43 -05:00
merge-script ee037042ae
Merge rust-bitcoin/rust-bitcoin#4387: bip32: overhaul error types and add a "maximum depth exceeded" error
b9a12043b0 bip32: return error when trying to derive a path too deep (Andrew Poelstra)
73781e047b bip32: rename Error to ParseError (Andrew Poelstra)
a66ad97fb6 bip32: split InvalidChildNumber and InvalidChildNumberFormat out of error (Andrew Poelstra)
a891fb9b74 bip32: remove unused error variants (Andrew Poelstra)
f0a237c001 bip32: split out DerivationError from the main error enum (Andrew Poelstra)
32d96f6c33 bip32: make Xpriv::new_master be infallible (Andrew Poelstra)
0e5e021b69 bip32: change several cryptographically unreachable paths to expects (Andrew Poelstra)

Pull request description:

  This PR makes a first pass at splitting the `bip32::Error` type into multiple distinct types -- one for derivation (which can fail if you try to derive a hardened child of an xpub, or if you try to derive too many layers), one for parsing child numbers or derivation paths, and one for parsing xkeys. Along the way it cleans up a ton of weird things and typos, e.g. the psbt `GetKeyError` having an unused `Bip32` variant whose display text references "bip 23".

  Because all the error types get renamed, every part of this PR is an API break, but only the last commit is a "real" API break which uses the new `DerivationError::MaximumDepthExceeded` error variant to return an error when trying to derive a path of length 256 or longer. This means that `Xpriv::derive_xpriv` again returns an error result.

  I will make a simpler version of this last commit suitable for backporting to 0.32.x. (In 0.32.x `Xpriv::derive_priv` returns an error, so we can change it to error out on max-depth-exceeded without breaking the API. Sadly most users are likely to be unwrapping the error because in 0.32.x currently the error path is cryptographically unreachable...but at least this way the panic will be in their control rather than ours.)

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

ACKs for top commit:
  tcharding:
    ACK b9a12043b0

Tree-SHA512: 688826126ff24066c6de9de3caa73db68c516ba8893d64d9226a498774a2fb9db7d7fd797375c6b3f820588c178632e1e6e8561022dfa7042a560582bf1509b4
2025-04-30 13:12:01 +00:00
Andrew Poelstra b9a12043b0
bip32: return error when trying to derive a path too deep
This restores the Result return from derive_xpriv which we had removed
in a previous PR, but such is life.

Fixes #4308
2025-04-29 23:09:32 +00:00
Andrew Poelstra 73781e047b
bip32: rename Error to ParseError
The bip32::Error enum is now exclusively used for errors related to
parsing and decoding. It is still a little messy (mainly: it contains a
base58 variant which is used when parsing a string but not when decoding
from bytes) but much cleaner than it was.
2025-04-29 23:09:30 +00:00
Tobin C. Harding 5d5a19793a
Run the formatter 2025-04-28 13:44:24 +10:00
Tobin C. Harding c27b95fb0d
Make script to/from hex use consensus encoding
I'm not sure why we do not use consensus encoding currently for encoding
and decoding scripts to/from hex strings. Many tests include hard coded
hex which do not include the length prefix.

- Add a pair of encoding functions to encode/decode to/from hex without
the length prefix.
- Make `to_hex` and `from_hex` expect the length prefix i.e., use
consensus encoding.

This makes the API easier to use because the various encoding APIs can
be use together now eg `consensus::encode_hex` and `ScriptBuf::from_hex`.
2025-04-28 13:39:44 +10:00
Fmt Bot 6737c3a0e5 2025-04-27 automated rustfmt nightly 2025-04-27 01:36:56 +00:00
merge-script 5871c51888
Merge rust-bitcoin/rust-bitcoin#4394: chore: fix docs for `WitnessProgram` and extend test for P2A
8eeceed450 test: extend `valid_v1_witness_programs` test to include P2A (Luis Schwab)
647526dd1d chore: fix docs for `impl WitnessProgram` and P2A (Luis Schwab)

Pull request description:

  Closes #4124.

  This PR fixes documentation on `impl WitnessProgram` by replacing instances of `address` to `[WitnessProgram]`, adds punctuation and capitalization where it was lacking and extends the `valid_v1_witness_programs` test to include the P2A output.

ACKs for top commit:
  Kixunil:
    ACK 8eeceed450
  apoelstra:
    ACK 8eeceed450f7414c8a286a9e47b6f04b652b18ef; successfully ran local tests

Tree-SHA512: 6e62a8de7135da04d6330d2b5596a2cd19da8a849f8c8c892f53578a8690152b23facf58149d4139ae088f1ab297d3526094617c3549e688819e9b1f3688de8b
2025-04-26 19:52:20 +00:00
merge-script 7e79a8b401
Merge rust-bitcoin/rust-bitcoin#4391: test: push minimality check for zero(empty)
afd4ec8c5e test: push minimality check for zero(empty) (ChrisCho-H)

Pull request description:

  Following https://github.com/rust-bitcoin/rust-bitcoin/pull/4368.
  I omitted to test OP_0(empty bytes) and can be covered by this PR.

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

Tree-SHA512: a3643227f9dfde71d5c5707bf11804e0e26eff43346c0443abdd805f0ffad284c3090e22a0bda34e54e1185a980adc7511724db401c04b55a8be79d67a3fce6d
2025-04-26 15:16:03 +00:00
merge-script 41e9976998
Merge rust-bitcoin/rust-bitcoin#4395: fix error in comment pow.rs
6ddbcb0283 fix error pow.rs (Alex Pikme)

Pull request description:

  hi! found typo

  Matagen -- typo
  Mutagen -- fix

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

Tree-SHA512: deee4b2be35d0d786b7e4da7b9a662ba7ffe0503d13f374e712f5e2679ad679290470916da7ee3c64456d940b012a56fc42a28cd26a57e4889d047b14127c554
2025-04-25 15:50:59 +00:00
merge-script 872fc038fe
Merge rust-bitcoin/rust-bitcoin#4392: Fix minor typos and improve clarity in documentation and comments
c009a42e60 Update internal_macros.rs (GarmashAlex)
a4253fa5d9 Update mod.rs (GarmashAlex)
604b095540 Update serialize.rs (GarmashAlex)
024f87e655 Update error.rs (GarmashAlex)
1af34f92c5 Update message_compact_blocks.rs (GarmashAlex)
f554b01e82 Update params.rs (GarmashAlex)

Pull request description:

  This PR addresses several minor issues across the codebase, including:
  - Fixing typographical errors in comments and documentation (e.g., "deserilaization" → "deserialization", "send" → "sent").
  - Improving sentence clarity and grammar in doc comments (e.g., correcting sentence structure and word choice).
  - Enhancing code readability without changing any logic or functionality.

  These changes are purely cosmetic and aimed at improving maintainability and developer experience.

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

Tree-SHA512: 915e2c9444d8f2810ba5cd51d3066685aea5a39d98303c793a854aea6da016cab2c457dd71c0b6549d29d6443db1292ebdb06d25f693741b2eca3979bf67cfab
2025-04-25 13:26:27 +00:00
Alex Pikme 6ddbcb0283
fix error pow.rs 2025-04-25 10:56:28 +02:00
Luis Schwab 8eeceed450
test: extend `valid_v1_witness_programs` test to include P2A 2025-04-24 22:51:41 -03:00
Luis Schwab 647526dd1d
chore: fix docs for `impl WitnessProgram` and P2A 2025-04-24 22:51:32 -03:00
merge-script aadea3eeb2
Merge rust-bitcoin/rust-bitcoin#4386: fix typo in serialized_signature.rs
3c6f45294b fix typo in serialized_signature.rs (Bilog WEB3)

Pull request description:

  `signtature` --> `signature` --fix typo

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

Tree-SHA512: c0d93efc95397eb6b817906ceef02083b529ef0f8b2d13afa675710d5ef78850dbcdc0e42f05a2956ba2949eb792cc92ff37359a540d246affec1f9604a44e1c
2025-04-24 19:40:12 +00:00
merge-script 5f4075a071
Merge rust-bitcoin/rust-bitcoin#4250: Introduce and use `test_hex_unwrap` macro in `internals`
d6296cd3d1 Remove usage of hex::test_hex_unwrap (Tobin C. Harding)
37035e20e8 Simplify and improve transaction benchmarks (Tobin C. Harding)

Pull request description:

  We have the `hex_lit` dependency for converting a hex string literal to an array.

  Currently we have a `test_hex_unwrap` macro in the `hex v0.3.0` release but not on either `master`
  or the upcoming `v1.0.0-alpha.0` release. This is making PRs around releasing and depending on the
  release more noisy than required.

  Introduce a `test_hex_unwrap` macro in internals for usage when the input is not a string literal.

  Use `hex_lit::hex` where possible (often needing an additional call to

ACKs for top commit:
  apoelstra:
    ACK d6296cd3d1989cf28d67a5329ad60da4f814ba92; successfully ran local tests
  Kixunil:
    ACK d6296cd3d1

Tree-SHA512: eab3573f6b7fee408ae11821b77e56cbaddf7cc4540bdc31ed7ef9eb3f25987f50e484f1553aaaa9709367e614eb77ed36250875d0faf5a51ab3fe709d4d4054
2025-04-24 17:19:37 +00:00
merge-script 04f706f6bd
Merge rust-bitcoin/rust-bitcoin#4383: rustdocs: Make headings consistent
c4d9c1b9f8 Use a consistent rustdoc heading level of H1 `#` (Jamil Lambert)
6325a7cdea Change rustdoc heading level of references (Jamil Lambert)
f22e997587 Use parameters instead of arguments in rustdocs (Jamil Lambert)
e2c7be6d2f Fix typo (Jamil Lambert)

Pull request description:

  In the rustdocs both `# Parameters` and `# Arguments` are used to mean the same thing.  In a previous PR #2792 it was decided to go with Parameters everywhere.  Since then there have been a few additions of "Arguments" into the rustdocs.

  There is also a mix in the usage of `#`, `##` or `###` for headings.  Noticed here https://github.com/rust-bitcoin/rust-bitcoin/pull/2792#issuecomment-2125775974.

   - Fix a typo found when looking into this.

   - Change all occurances to use `# Parameters` in rustdocs.

   - Change all heading levels to H1 `#`

   - Change all subheading levels to H3 `###` to make the small difference in the rendered font size noticable

  Closes #4380

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

Tree-SHA512: c8cc77ccf7e2003dd2dd1d309268624576e3bf390cd8ac61b0a7bb1141ca05377c83627576b0b7ff258b8e51c2d255097a4363fbdd1b368db7d32ac32ece58a1
2025-04-24 16:44:17 +00:00
GarmashAlex c009a42e60
Update internal_macros.rs 2025-04-24 16:46:01 +03:00
GarmashAlex a4253fa5d9
Update mod.rs 2025-04-24 16:40:00 +03:00
GarmashAlex 604b095540
Update serialize.rs 2025-04-24 16:37:25 +03:00
GarmashAlex 024f87e655
Update error.rs 2025-04-24 16:30:44 +03:00
GarmashAlex 1af34f92c5
Update message_compact_blocks.rs 2025-04-24 16:28:38 +03:00
GarmashAlex f554b01e82
Update params.rs 2025-04-24 15:53:27 +03:00
ChrisCho-H afd4ec8c5e test: push minimality check for zero(empty) 2025-04-24 20:52:37 +09:00
aagbotemi dedb42dd71
fix(taproot): remove unused error variant
- clippy::enum_variant_names allow attribute added
- cargo formatting changes reversed
2025-04-23 15:51:09 +01:00
Andrew Poelstra a66ad97fb6
bip32: split InvalidChildNumber and InvalidChildNumberFormat out of error
Currently in this module we have a distinction between an "index" which
is a number between [0, 2^31 - 1] which indexes into the set of normal
or hardened child numbers. We also have a child *number*, which within
the library is an opaque type, but can be freely converted into/out of a
u32 in the consensus encoding which uses the MSB as a normal/hardened
flag and the other bits to encode the index.

Probably we want to change ChildNumber::From<u32> to some sort of
from_consensus_u32 method, but that's out of scope for this PR. What is
*in scope*, though is fixing the error types.

In the existing code we have three problems:

* Our error type for a bad index is called InvalidChildNumber, rather
  than InvalidIndex, and the error message reflects this, which is wrong
  and confusing. (Some with InvalidChildNumberFormat.)
* The InvalidChildNumberFormat is always constructed from a ParseIntError
  from stdlib, but we always throw that away rather than preserving it.
* These two error variants only appear when parsing child numbers, or
  derivation paths which are lists of child numbers, but they are part
  of the main error enum.
2025-04-22 14:46:34 +00:00
Andrew Poelstra a891fb9b74
bip32: remove unused error variants 2025-04-22 14:46:18 +00:00
Andrew Poelstra f0a237c001
bip32: split out DerivationError from the main error enum
This is a breaking change to the API of the deprecated `derive_pub`
function, but it's in a way that's unlikely to break consumers (who are
probably just .expect'ing the result) so we will retain the deprecated
function.
2025-04-22 14:45:57 +00:00
Andrew Poelstra 32d96f6c33
bip32: make Xpriv::new_master be infallible
The only error path for this is cryptographically unreachable and was
removed in a previous commit.
2025-04-22 14:45:36 +00:00
Andrew Poelstra 0e5e021b69
bip32: change several cryptographically unreachable paths to expects
These paths cannot be reached. In general, key derivation cannot fail
for cryptographic reasons.
2025-04-22 14:44:46 +00:00
Bilog WEB3 3c6f45294b
fix typo in serialized_signature.rs 2025-04-22 16:15:02 +02:00
Jamil Lambert c4d9c1b9f8
Use a consistent rustdoc heading level of H1 `#`
There was and inconsistent usage of `#`, `##` and `###` in rustdoc
headings.  The difference in the rendered rustdocs is a minimal font
size change.

Change all headings to be H1 `#`.

Change all subheadings to be `###` to have a noticeable difference in
font size in the rendered docs.
2025-04-22 13:06:32 +01:00
Fallengirl 55119c6a7b
fix errors input.rs 2025-04-21 23:23:59 +02:00
merge-script 2d8ebb79c3
Merge rust-bitcoin/rust-bitcoin#4375: Add ControlBlock constructor that takes a hex string
3319e6ef6d Add ControlBlock constructor that takes a hex string (Shing Him Ng)

Pull request description:

  Closes #4362

ACKs for top commit:
  Kixunil:
    ACK 3319e6ef6d
  apoelstra:
    ACK 3319e6ef6dd964f40e6e880843648c112a6b74ed; successfully ran local tests

Tree-SHA512: 230e4607402b3df6a8c5fe1e03209573baffbd08ca9d28e1208bff1464668a083ddb5ae72781eceb2546bc99b150dd2f832d122570d55aa323c603481c5eff93
2025-04-21 16:57:41 +00:00
merge-script 4a5ea9af53
Merge rust-bitcoin/rust-bitcoin#4370: Accessing XOnlyPublicKey from bitcoin::taproot
87d1210156 accessing XOnlyPublicKey from bitcoin::taproot (aagbotemi)

Pull request description:

  This PR fixes #4363

  `XOnlyPublicKey` is now made available from `bitcoin::taproot`.

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

Tree-SHA512: 7ea0ee330d32e5413d06deb70d920396ebd275ce65841d6eadac318aab4e728629c57c828722df4e1cb3130bb72f4170f32218ec25ec243d99f0094094d8f9da
2025-04-21 15:33:07 +00:00
merge-script 353992f33a
Merge rust-bitcoin/rust-bitcoin#4368: test: add test for push slice minimal
2b37583ca5 test: add test for push slice minimal (ChrisCho-H)

Pull request description:

  Following https://github.com/rust-bitcoin/rust-bitcoin/pull/4322.
  Test `push_slice` and `push_slice_nom_minimal` from OP_1...OP_16 and OP_1NEGATE.

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

Tree-SHA512: 6d8e96d522d317d7ede2f1bc133050d98ccbc1816c59fe3589003de249828367314f751d3a75d6b58b0c90b1b735f85650c0d7acebaf5da21a38fb651ab4177d
2025-04-21 14:54:24 +00:00
merge-script b67b9de765
Merge rust-bitcoin/rust-bitcoin#4369: Rename `try_into_taptree` to `try_into_tap_tree`
73317c1c31 rename try_into_taptree into try_into_tap_tree - docs(taproot): hide deprecated try_into_taptree (aagbotemi)

Pull request description:

  The PR introduces a new function `try_into_tap_tree()` to replace `try_into_taptree()`. `try_into_taptree()` has been deprecated.

  This PR fixes #4364

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

Tree-SHA512: 5d853ea05a8cacfc0a27a93bed4351409abf5f93e53b308d62efdc3966c8403a4a857e39882eb84e511b637763ddb9629be832c2079057d81fa1c1984bcb8ad7
2025-04-21 14:21:58 +00:00
Shing Him Ng 3319e6ef6d Add ControlBlock constructor that takes a hex string 2025-04-20 23:13:01 -05:00
merge-script 8276e4d0e5
Merge rust-bitcoin/rust-bitcoin#4372: Automated nightly rustfmt (2025-04-20)
2c1ce53deb 2025-04-20 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 2c1ce53deb2fb2dc56546c9151c5fb61520f4713; successfully ran local tests

Tree-SHA512: 566535deece199d3a19910d6b033f8bf3ddbce27092f218041ebb1694b5f6184e4851ae9a54ddd0887c56f73e8083441e1769785109688c3c6f2a57120805229
2025-04-20 20:11:56 +00:00
merge-script aa804ee1b1
Merge rust-bitcoin/rust-bitcoin#2456: Use InputWeightPrediction to calculate effective_value
f6105ea417 Use InputWeightPrediction to calculate effective_value (yancy)

Pull request description:

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

  Also, what about moving `effective_value` to `InputWeightPrediction`?

  Marking as a draft until we can add api changes again.

ACKs for top commit:
  apoelstra:
    ACK f6105ea4171a85ce21443d7eb76b7aa9cadab53a; successfully ran local tests; yeah, this API does look nicer
  Kixunil:
    ACK f6105ea417

Tree-SHA512: 20592e49cb93343b1aefa340c3c870e2e21c747711da68a6aa57342f59ff2981c30e9c91de7eab32bcd11da33f040f9df62008db991d93b549079f91a6908055
2025-04-20 19:36:22 +00:00
Fmt Bot 2c1ce53deb 2025-04-20 automated rustfmt nightly 2025-04-20 01:36:29 +00:00
aagbotemi 73317c1c31
rename try_into_taptree into try_into_tap_tree
- docs(taproot): hide deprecated try_into_taptree
2025-04-19 16:54:01 +01:00
aagbotemi 87d1210156
accessing XOnlyPublicKey from bitcoin::taproot 2025-04-19 16:26:19 +01:00
ChrisCho-H 2b37583ca5 test: add test for push slice minimal
Test push_slice and push_slice_nom_minimal from OP_1...OP_16 and OP_1NEGATE
2025-04-19 14:23:31 +09:00
merge-script c85648cfd4
Merge rust-bitcoin/rust-bitcoin#4322: fix: enforce standard minimal push for push_slice
354e1e42ad fix: enforce minimal push for push_slice (ChrisCho-H)

Pull request description:

  Currently `push_slice` doesn't check the standard minimal push rule, which could result in possible money loss(e.g. if non minimal push is used in output script of p2wsh or p2tr).
  Introduce `push_slice_non_minimal` to provide the way to push as now, and change `push_slice` logic to follow standard minimal push rule.

  99a4ddf5ab/src/script/script.cpp (L366)

ACKs for top commit:
  tcharding:
    ACK 354e1e42ad
  apoelstra:
    ACK 354e1e42ad98c7968827d03be563bc14d764a983; successfully ran local tests

Tree-SHA512: 01f53dbd2a1a3c9a9e4387f0aaa7801f9c4570996054503a38d09ed1646ab65a3249d227adcf4139ac37a111d795e6cf986c4c273d5660c20820d1b36ba46f57
2025-04-18 14:23:53 +00:00
merge-script 89d7b3fe24
Merge rust-bitcoin/rust-bitcoin#4358: Capitalize Taproot in rustdocs
c4ff2baa5a Capitalize Taproot in rustdocs (Jamil Lambert)

Pull request description:

  Some recent additions to the rustdocs used a lowercase t in taproot.

  Capitalize all cases of Taproot in rustdocs to be consistent.

  Closes #4354

ACKs for top commit:
  tcharding:
    ACK c4ff2baa5a
  apoelstra:
    ACK c4ff2baa5a7c841ecdfe0e8a92b0a1a664f203ae; successfully ran local tests

Tree-SHA512: 620ccf769a6dad3bcdfec8a0460f6f1bbf90780e280ca94317f6048117aaad06c41a90cd3ee00d670f799459316c1f84e96e4ce3ef3a44818243b21c2f41924b
2025-04-18 13:04:17 +00:00
Alex Pikme 79f6530c86
fix rejectection to rejection message_network.rs 2025-04-17 21:13:12 +02:00
Jamil Lambert c4ff2baa5a
Capitalize Taproot in rustdocs
Some recent additions to the rustdocs used a lowercase t in taproot.

Capitalize all cases of Taproot in rustdocs to be consistent.
2025-04-17 18:02:35 +01:00
ChrisCho-H 354e1e42ad fix: enforce minimal push for push_slice 2025-04-17 06:03:57 +09:00
merge-script 7d05078b6a
Merge rust-bitcoin/rust-bitcoin#4349: minor docstring fixups message.rs
793920d6bf minor docstring fixups message.rs (Bilog WEB3)

Pull request description:

  Please fix this error, thank you

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

Tree-SHA512: 5e152299ceab2962ac841a3935877a2264654e7f1d296c0486b2548f57f9cc9106590f6fc92b0a4adaba44d909457e932cd706e76ab814067c5972afa4d8ab93
2025-04-16 00:13:03 +00:00
merge-script 9ae40372b2
Merge rust-bitcoin/rust-bitcoin#4346: chore: Fix the typos in the comments and variables
d0e1cd72fe chore: Fix the typos in the comments and variables (dufucun)

Pull request description:

  Fix the typos in the comments and variables

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

Tree-SHA512: 267d2b6b47e5a4f9466507e9dceb62ce94c848b3edefee65b85c3ce31560d1be880ef1c03ffd7dac54198f1f470695b70e06a505f91174f2990639bc20bf86e4
2025-04-15 18:49:14 +00:00
merge-script ee43f9235b
Merge rust-bitcoin/rust-bitcoin#4342: Remove Option return from `minimal_non_dust`
0498f7b7b7 Remove Option return from `minimal_non_dust` (jrakibi)

Pull request description:

  Closes #4221

  This removes the `Option` return type from `minimal_non_dust

  Overflow is only possible in 2 cases:
  - `dust_relay_fee` would need to be excessively high
  - script size would have to exceed ~6.15 × 10¹⁵ bytes (≈ 6 petabytes)

  we now panic with the same message we had before in  cf12ba262a/bitcoin/src/blockdata/script/borrowed.rs (L412)

ACKs for top commit:
  tcharding:
    ACK 0498f7b7b7
  apoelstra:
    ACK 0498f7b7b7d43cc015d6788efe826df25d6156a5; successfully ran local tests

Tree-SHA512: 826a5d4ebb9c237cdd261f7d8b25fb2118cfba7d79b031839a619e12c440cbd34bbf830ffe513c104ef34e8ae50320e314c736a55be9ba7a82ae50f6022b9cf0
2025-04-15 17:19:54 +00:00
Bilog WEB3 793920d6bf
minor docstring fixups message.rs 2025-04-15 16:28:26 +02:00
merge-script a7c7ff94a6
Merge rust-bitcoin/rust-bitcoin#4333: Automated nightly rustfmt (2025-04-13)
19c5fcc78f 2025-04-13 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:
  tcharding:
    ACK 19c5fcc78f

Tree-SHA512: 1008800b18e1efc14e773d4364ccbda0f8382e93c55feec22f6570d26cc7ad826c984c2fd4e17feabf37cd91e1eb17bcb019d7b0310568616bf3ca594abddf3c
2025-04-15 14:05:51 +00:00
dufucun d0e1cd72fe chore: Fix the typos in the comments and variables
Signed-off-by: dufucun <dufuchun@sohu.com>
2025-04-15 18:31:52 +08:00
jrakibi 0498f7b7b7 Remove Option return from `minimal_non_dust`
we replace Option<Amount> return type with Amount in minimal_non_dust

- Use `.expect("dust_relay_fee or script length should not be absurdly large")` to handle overflow from .checked_mul()

`.expect()` is only triggered if the value calculateed overflows u64
such an overflow would require a script size exceeding ~6.15 petabytes
2025-04-14 20:07:41 +08:00
Fmt Bot 19c5fcc78f 2025-04-13 automated rustfmt nightly 2025-04-13 02:50:07 +00:00
Maxim Evtush a697af9755
Update tests.rs 2025-04-12 16:09:18 +02:00
Tobin C. Harding d6296cd3d1
Remove usage of hex::test_hex_unwrap
We have the `hex_lit` dependency for converting a hex string literal
to an array.

Currently we have a `test_hex_unwrap` macro in the `hex v0.3.0` release
but not on either `master` or the upcoming `v1.0.0-alpha.0` release.
This is making PRs around releasing and depending on the release more
noisy than required.

Use `hex_lit::hex` where possible (often needing an additional call to
`to_vec()`) and where not possible use `Vec::from_hex`.
2025-04-11 06:49:06 +10:00
Tobin C. Harding 37035e20e8
Simplify and improve transaction benchmarks
We can use `deserialize_hex` when outside of the actual benchmark code
to simplify the functions.

Also add an additional test that benchmarks `deserialize_hex`.
2025-04-11 06:48:04 +10:00
merge-script d4c10a8af0
Merge rust-bitcoin/rust-bitcoin#4317: add copy trait to address and AddressInner
52f9c13358 Implement Copy for Address and AddressInner (tushar994)

Pull request description:

  solves #4237

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

Tree-SHA512: 007092c57b893cfa62a6185c21b8839daf946a5b8ca505fc7d5f3b78c056fc5ce44cfb32936469f1955d535f4798506ea20a936218f0bf6808b17506e87836f2
2025-04-10 18:57:38 +00:00
yancy f6105ea417 Use InputWeightPrediction to calculate effective_value
InputWeightPrediction can be used to determine the weight needed
to calculate the effective_value.  This simplifies the process for api
consumers by allowing an easier interface with which to use to predict
the weight.  Prior to this change, it was required to manually calculate
the predicted weight for whichever input type was to be used.
2025-04-10 07:25:12 -05:00
tushar994 52f9c13358 Implement Copy for Address and AddressInner
Enables by-value Address semantics for easier use; large sizes can use Box.
2025-04-09 12:25:43 +05:30
merge-script 8599ceefe3
Merge rust-bitcoin/rust-bitcoin#4309: Automated nightly rustfmt (2025-04-06)
4c35e06c01 2025-04-06 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:
  tcharding:
    ACK 4c35e06c01
  apoelstra:
    ACK 4c35e06c0164f4cc18550e4a8e8eec9b79daf4f2; successfully ran local tests; nicee

Tree-SHA512: a989a58511e3e1ecfc14885aa0a3b1c1d8c54bdf49221d39f88a4cc230c8808701ddad4ef2d62b30e6819f75f86e77796848304a170720ba5c3c38d0125f3d1f
2025-04-07 20:56:12 +00:00
Bilog WEB3 0a1efdaa66
fix correction in comment mod.rs 2025-04-06 13:38:37 +02:00
Fmt Bot 4c35e06c01 2025-04-06 automated rustfmt nightly 2025-04-06 01:26:49 +00:00
merge-script 9f7f659591
Merge rust-bitcoin/rust-bitcoin#4302: Add push_relative_lock_time() and deprecate push_sequence()
ebaf162a96 Add push_relative_lock_time() and deprecate push_sequence() (Erick Cestari)

Pull request description:

  This pr improves the script builder API to better align with Bitcoin semantics when working with relative timelocks:

  - Add `push_relative_lock_time()` method that takes a `relative::LockTime` parameter, which correctly represents the semantic meaning when working with CHECKSEQUENCEVERIFY

  - Deprecate `push_sequence()` in favor of `push_relative_lock_time()` to avoid confusion between sequence numbers and relative timelocks

  This addresses a potential confusion point in the API where developers might incorrectly push raw sequence numbers in scripts when what they actually need is to push a relative locktime value that will be checked against the transaction's sequence numbers by CHECKSEQUENCEVERIFY.

  Closes #4301

ACKs for top commit:
  apoelstra:
    ACK ebaf162a962494329c6cb5f6d375a6a4a97fe83b; successfully ran local tests
  tcharding:
    ACK ebaf162a96

Tree-SHA512: 52c37b6e8bbcaa3f9346c5fd5db26eba69169bce13f915906df95fdc65204067fd75f803f8b5adad76978c9baad553c99281628736db4d1d317b149ab257d81f
2025-04-03 19:23:52 +00:00
merge-script f7006e3d15
Merge rust-bitcoin/rust-bitcoin#4304: minor docstring fixups
157fe48dfd minor docstring fixups (planetBoy)

Pull request description:

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

Tree-SHA512: 29fe6168ff729f0f65f32a2c6ad28d45e36e0761cac4455b57b891f9c0bd2622db51a21b4961d33fa5a8934302eefca4a77c20732bf047e2721a5bc5d655c340
2025-04-02 16:51:06 +00:00
planetBoy 157fe48dfd
minor docstring fixups 2025-04-02 12:26:53 +02:00
Bilog WEB3 ab63ec9768
fix correction typos 2025-04-01 17:38:24 +02:00
Erick Cestari ebaf162a96 Add push_relative_lock_time() and deprecate push_sequence()
This commit improves the script builder API to better align with Bitcoin
semantics when working with relative timelocks:

- Add push_relative_lock_time() method that takes a relative::LockTime
  parameter, which correctly represents the semantic meaning when working
  with CHECKSEQUENCEVERIFY

- Deprecate push_sequence() in favor of push_relative_lock_time() to avoid
  confusion between sequence numbers and relative timelocks

This addresses a potential confusion point in the API where developers
might incorrectly push raw sequence numbers in scripts when what they
actually need is to push a relative locktime value that will be checked
against the transaction's sequence numbers by CHECKSEQUENCEVERIFY.
2025-03-31 14:01:14 -03:00
Fmt Bot b8910e201e 2025-03-30 automated rustfmt nightly 2025-03-30 01:27:51 +00:00
merge-script cb5f84c21f
Merge rust-bitcoin/rust-bitcoin#4290: fix err `P2WPKH` to `P2WSH` witness_version.rs
7b193b5125 fix err P2WPKH to P2WSH (planetBoy)

Pull request description:

  The correction is important because “P2WPK” is not a valid name. In the BIP141 specifications, the correct terms are “P2WPKH” and “P2WSH”.

ACKs for top commit:
  Kixunil:
    ACK 7b193b5125
  apoelstra:
    ACK 7b193b5125336263f672f2e2c69447cc3ae58926; successfully ran local tests

Tree-SHA512: 951bcde2c28e2086a69043c1ed27bde0935df0918f418c5f6f89ed476ba9e182e99eec545a438f79ca4e1704ce496d443b5bc9e368a53dd583a884f1da405865
2025-03-28 18:32:34 +00:00
merge-script a2408e9b0c
Merge rust-bitcoin/rust-bitcoin#4281: Return `ControlBlock` from `Witness::taproot_control_block`
492073f288 Strengthen the type of `taproot_control_block()` (Martin Habovstiak)
e8a42d5851 Unify/reduce usage of `unsafe` (Martin Habovstiak)
d42364bd9d Swap around the fields in `Address` (Martin Habovstiak)
7a115e3cf1 Make `Address` obey sanity rules (Martin Habovstiak)
bc6da1fe07 Swap around the fields in `sha256t::Hash` (Martin Habovstiak)
8ee088df74 Make `sha256t` obey sanity rules (Martin Habovstiak)

Pull request description:

  Well, I thought this PR will be just the last commit... 😅

  Anyway, this implements a bunch of changes to allow returning `ControlBlock` from `Witness` method(s). One cool side effect is that this PR also reduces the number of `unsafe` blocks.

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

Tree-SHA512: 11979517cc310abf25644fc93a75deccacae66af8ba2d9b4011fdc3f414b15fac7e748399c7eef492ca850c11b7aacc3f24ec46fccf95e6d57a400212979637e
2025-03-28 15:06:43 +00:00
planetBoy 7b193b5125 fix err P2WPKH to P2WSH 2025-03-27 22:20:32 +02:00
Martin Habovstiak 492073f288 Strengthen the type of `taproot_control_block()`
The type returned by `Witness::taproot_control_block()` was just `&[u8]`
which wasn't very nice since users then had to manually decode it which
so far also required allocation. Thanks to previous improvements to
`ControlBlock` it is now possible to return a `ControlBlock` type
directly.

To avoid expensive checks, this change adds a new type
`SerializedXOnlyPublicKey` which is a wrapper around `[u8; 32]` that is
used in `ControlBlock` if complete checking is undesirable. It is then
used in the `ControlBlock` returned from
`Witness::taproot_control_block`. Users can still conveniently validate
the key using `to_validated` method.

It then uses this type in the recently-added `P2TrSpend` type. As a side
effect this checks more properties of `Witness` when calling unrelated
methods on `Witness`. From correctness perspective this should be OK: a
witness obtained from a verified source will be correct anyway and, if
these checks were done by the caller, they can be removed.

From performance perspective, if the `Witness` was obtained from a
verified source (e.g. using Bitcoin Core RPC) these checks are wasted
CPU time. But they shouldn't be too expensive, we already avoid
`secp256k1` overhead and, given that they always succeed in such case,
they should be easy to branch-predict.
2025-03-27 20:15:53 +01:00
Martin Habovstiak e8a42d5851 Unify/reduce usage of `unsafe`
Since the introduction of `Script` `unsafe` started slowly creeping in
as more types with similar semantics were added. The `unsafe` in these
cases is just for trivial conversions between various pointer-like
types. As such, it's possible to move these into a single macro that
takes care of the conversions at one place and avoid repeating the same
`unsafe` code in the codebase. This decreases the cost of audits which
now only need to happen in `internals`, focuses any changes to happen in
that single macro and decreases the chance that we will mess up
similarly to the recent `try_into().expect()` issue (but this time with
UB rather than panic).

The new macro accepts syntax very similar to the already-existing struct
declarations with these differences:

* The struct MUST NOT have `#[repr(transparent)]` - it's added by the
  macro
* If the struct uses `PhantomData` it must be the first field and the
  real data must be the second field (to allow unsized types).
* The struct must be immediately followed by an impl block containing at
  least on conversion function.
* If the struct has generics the impl block has to use the same names of
  generics.
* The conversion functions don't have bodies (similarly to required
  trait methods) and have a fixed set of allowed signatures.
* Underscore (`_`) must be used in place of the inner type in the
  conversion function parameters.

The existing code can simply call the macro with simple changes and get
the same behavior without any direct use of `unsafe`. This change
already calls the macro for all relevant existing types. There are still
some usages left unrelated to the macro, except one additional
conversion in reverse direction on `Script`. It could be moved as well
but since it's on a single place so far it's not really required.
2025-03-27 20:15:37 +01:00
merge-script 459a7db038
Merge rust-bitcoin/rust-bitcoin#4285: chore: remove explicit into iteration
e966335447 chore: remove explicit into iteration (jike)

Pull request description:

  ```
  warning: it is more concise to loop over containers instead of using explicit iteration methods
     --> bitcoin/src/psbt/serialize.rs:209:21
      |
  209 |         for cnum in self.1.into_iter() {
      |                     ^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&self.1`
  ```

ACKs for top commit:
  Kixunil:
    ACK e966335447
  apoelstra:
    ACK e9663354476b0b9b2ec52ce034de5ab9223f9d29; successfully ran local tests

Tree-SHA512: 32fced1a5aba2caa6e48a2e586bf00dcf3d4b82b6b530d9c7f535b530737a40e24d5c05a7cea40747df4c7c4698ec141683b9416583b185b1ff6461f3c8f667f
2025-03-26 17:39:40 +00:00
merge-script e77d6b3360
Merge rust-bitcoin/rust-bitcoin#4166: Remove bip158 types from crate root
fb29aadc47 Remove bip158 types from crate root (Tobin C. Harding)

Pull request description:

  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.

ACKs for top commit:
  apoelstra:
    ACK fb29aadc47367ba8579f0004e97cd1fd7fd8236f; successfully ran local tests
  Kixunil:
    ACK fb29aadc47

Tree-SHA512: 8a2edaad858b18feded8cc9e1d15f03a76980bd41524fa34b91b4055b236788c6d828940c6293e086a8c8e33baadc5765a1a60920513fdff2de22e9d94c0e541
2025-03-26 15:06:55 +00:00
merge-script 143531de7c
Merge rust-bitcoin/rust-bitcoin#4279: `Witness` api improvements and test cleanups
84bee2f7b0 Simplify `Witness` construction in tests (Martin Habovstiak)
3551ec2c69 Don't access internalls of `Witness` in tests (Martin Habovstiak)
c8078360d2 Impl `PartialEq` between `Witness` and containers (Martin Habovstiak)
587a66da47 Add a bunch of missing conversions for `Witness` (Martin Habovstiak)

Pull request description:

  This is supposed to go in front of #4250

  `Witness` lacked a bunch of APIs that were making it harder to use and test, so this also adds them in addition to cleaning up tests. (I only realized they are missing when I tried to clean up tests and got a bunch of errors.)

ACKs for top commit:
  tcharding:
    ACK 84bee2f7b0
  apoelstra:
    ACK 84bee2f7b06a7bd1f435aaad18fa76a15188326e; successfully ran local tests

Tree-SHA512: 7973f2a56b070babba7b4c632f45858154ccd00f8e77956ad2d28cb66e1fd18ff60d92c031ba3b76d0958e4acd34adfca10607fa26ec569dfd52ba1c1e2c79eb
2025-03-26 03:38:39 +00:00
jike e966335447 chore: remove explicit into iteration 2025-03-26 11:07:30 +08:00
merge-script cf79f05ad6
Merge rust-bitcoin/rust-bitcoin#4271: Make code style consistent and fix naming in macro Array implementations
e744347022 Make usage of Self and type uniform across both modules (Erick Cestari)
dfb49f014c Rename impl_try_from_array to impl_from_array (Erick Cestari)

Pull request description:

  This PR makes two main changes:

  1. Standardizes the function signatures in the `Amount` and `SignedAmount` implementations by consistently using `Self` as the return type instead of the concrete type names. This improves code consistency, maintainability, and follows Rust's idiomatic practices.
  2. Renames `impl_try_from_array` to `impl_from_array` to better reflect its functionality.

  ### Changes
  **Consistent usage of Self instead of concrete types**

  - Replace all occurrences of `-> Amount` with `-> Self `in unsigned.rs
  - Replace all occurrences of `-> SignedAmount` with `-> Self` in signed.rs
  - Make similar replacements for Option/Result return types
  - Use `Self::` instead of explicit type name for static method calls

  **Function rename**

  Renamed `impl_try_from_array` to `impl_from_array` for better clarity

  ### Related Issues

  Closes #4210

  Closes #4241

ACKs for top commit:
  Kixunil:
    ACK e744347022
  tcharding:
    ACK e744347022
  apoelstra:
    ACK e744347022d1ad1e0ca0a83ec9350501af08297b; successfully ran local tests

Tree-SHA512: 3113f3ccf595b298afe6b23514f1de790284df7fcb55a13658aabe3ef4fcea0e401b65b0a2c67ac18da87a1bcd247bd1f1484856fe03470b98dfa2614958a3bb
2025-03-25 19:39:23 +00:00
merge-script 87889955f9
Merge rust-bitcoin/rust-bitcoin#4238: Add XOnlyPublicKey support for PSBT key retrieval and improve Taproot signing
069d2fd07e Add XOnlyPublicKey support for PSBT key retrieval and improve Taproot signing (Erick Cestari)

Pull request description:

  The `bip32_sign_schnorr` function was previously only attempting to retrieve private keys using `KeyRequest::Bip32`, which limited the ability to sign Taproot inputs with key maps that don't support BIP32 derivation paths.

  ## Changes
  - Added new `KeyRequest::XOnlyPubkey` variant to support direct retrieval using XOnly public keys
  - Implemented `GetKey` for `HashMap<XOnlyPublicKey, PrivateKey>` for more efficient Taproot key management
  - Modified `HashMap<PublicKey, PrivateKey>` implementation to handle XOnlyPublicKey requests by checking both even and odd parity variants
  - Added comprehensive tests for both key map implementations

  These improvements enable wallet implementations to store keys indexed by either `PublicKey` or `XOnlyPublicKey` and successfully sign PSBTs.

  Closes #4150

ACKs for top commit:
  Kixunil:
    ACK 069d2fd07e
  apoelstra:
    ACK 069d2fd07e7d6dad1401fce6ab28ab1dc9f3c60f; successfully ran local tests

Tree-SHA512: 0ae07309b772f1a53e7da45073f7e2337cc332ab2335925d623d0e1ad1503aab77673bbbd64e5533ae7fc8d57f3577db0ae7ac3b05279de92d3b34ab8eeae90f
2025-03-24 20:15:21 +00:00
Martin Habovstiak d42364bd9d Swap around the fields in `Address`
There's a restriction that for structs containing unsized types the
unsized type has to be the last field. `Address` is not an unsize type
but we are going to introduce a macro that will assume this order to
work equally well with both sized and unsized types. Thus we swap it
upfront here.
2025-03-24 14:30:56 +01:00
Martin Habovstiak 7a115e3cf1 Make `Address` obey sanity rules
`Address` was directly accessing its internals in multiple places. This
makes maintenance harder, so change it to use methods instead.
2025-03-24 14:30:24 +01:00
Martin Habovstiak 84bee2f7b0 Simplify `Witness` construction in tests
The `Witness`-related tests were constructing `Witness` in
over-complicated way by serializing `Vec<Vec<u8>>` and then
deserializing `Witness` even though they were not supposed to test
serialization but Taproot accessor methods. This was difficult to
understand and maintain.

This change simplifies them to just construct the `Witness` from array
of `Vec<u8>`s using the recently-added constructors. Note that we
already have serialization tests written separately so we're not losing
meaningful coverage here.
2025-03-23 21:23:56 +01:00
Erick Cestari dfb49f014c Rename impl_try_from_array to impl_from_array 2025-03-21 14:26:31 -03:00
merge-script 294a58c3f9
Merge rust-bitcoin/rust-bitcoin#4128: Remove `From<hash>` for not-general-hash types
db9ec3bed8 Remove From<newtype> for $hash (Tobin C. Harding)
6b2b89c2f7 Remove From<hash> for not-general-hash types (Tobin C. Harding)
200ff47327 Use compute_merkle_root (Tobin C. Harding)

Pull request description:

  The `hash_newtype` macro is explicitly designed to produce a hash that is not a general purpose hash type to try and prevent users hashing arbitrary stuff with it. E.g., `Txid` isn't meant to be just hash arbitrary data. However we provide a `From` impl that will convert any instance of the inner hash type into the new type. This kind of defeats the purpose. We provide `from_byte_array` and `to_byte_array` to allow folk to 'cast' from one hash type to another if they really want to and its ugly on purpose.

  Also, it is becoming apparent that we may be able to remove the `hashes` crate from the public API of `primitives` allowing us to stabalise `primitives` without stabalising `hashes`.

  For both these reasons remove the `From` impl from the `hash_newtype` macro. Note that deprecating doesn't seem to work so we just delete it.

ACKs for top commit:
  Kixunil:
    ACK db9ec3bed8
  apoelstra:
    ACK db9ec3bed8d6164a0345ba8db1e2162626db7cc5; successfully ran local tests

Tree-SHA512: 90bc325821cd2d72bbaef5b3cfef2d299192d1e7999cd4f96b6b69b8872e419964e431e91674c59bfdd2e9a5959dbc13ee89d5f87d03e96785044c616db19d72
2025-03-21 15:49:01 +00:00
Erick Cestari 069d2fd07e Add XOnlyPublicKey support for PSBT key retrieval and improve Taproot signing
This commit enhances PSBT signing functionality by:

1. Added new KeyRequest::XOnlyPubkey variant to support direct retrieval using XOnly public keys
2. Implemented GetKey for HashMap<XOnlyPublicKey, PrivateKey> for more efficient Taproot key management
3. Modified HashMap<PublicKey, PrivateKey> implementation to handle XOnlyPublicKey requests by checking both even and odd parity variants

These changes allow for more flexible key management in Taproot transactions.
Specifically, wallet implementations can now store keys indexed by either
PublicKey or XOnlyPublicKey and successfully sign PSBTs with Taproot inputs.

Added tests for both implementations to verify correct behavior.

Added test for odd parity key retrieval.

Closes #4150
2025-03-21 11:28:54 -03:00
Tobin C. Harding 6b2b89c2f7
Remove From<hash> for not-general-hash types
The `hash_newtype` macro is explicitly designed to produce a hash that
is not a general purpose hash type to try and prevent users hashing
arbitrary stuff with it. E.g., `Txid` isn't meant to be just hash
arbitrary data. However we provide a `From` impl that will convert any
instance of the inner hash type into the new type. This kind of defeats
the purpose. We provide `from_byte_array` and `to_byte_array` to allow
folk to 'cast' from one hash type to another if they really want to and
its ugly on purpose.

Also, it is becoming apparent that we may be able to remove the `hashes`
crate from the public API of `primitives` allowing us to stabalise
`primitives` without stabalising `hashes`.

For both these reasons remove the `From` impl from the `hash_newtype`
macro. Note that deprecating doesn't seem to work so we just delete it.
2025-03-21 09:12:25 +11:00
Tobin C. Harding 200ff47327
Use compute_merkle_root
Remove manual implementation of merkle root calculation and just use the
function we already have.

Refactor only, no logic change.
2025-03-21 09:09:49 +11:00
Martin Habovstiak 437562e71c Add official BIP32 test vectors for invalid keys
These are defined in the BIP as invalid. The previous commit fixed a bug
where invalid key was parsed as valid and this bug can be caught by
these vectors. Therefore, if this commit is ordered before the last one
the test will fail.
2025-03-20 20:19:51 +01:00
Martin Habovstiak 5dd0c9253d Remove a bunch of `try_into().expect()`
Previously we've used `try_into().expect()` because const generics were
unavailable. Then they became available but we didn't realize we could
already convert a bunch of code to not use panicking conversions. But we
can (and could for a while).

This adds an extension trait for arrays to provide basic non-panicking
operations returning arrays, so they can be composed with other
functions accepting arrays without any conversions. It also refactors a
bunch of code to use the non-panicking constructs but it's certainly not
all of it. That could be done later. This just aims at removing the
ugliest offenders and demonstrate the usefulness of this approach.

Aside from this, to avoid a bunch of duplicated work, this refactors
BIP32 key parsing to use a common method where xpub and xpriv are
encoded the same. Not doing this already led to a mistake where xpriv
implemented some additional checks that were missing in xpub. Thus this
change also indirectly fixes that bug.
2025-03-20 20:19:50 +01:00
merge-script 20c50e337f
Merge rust-bitcoin/rust-bitcoin#4248: Alloc-free (AKA zero-copy) control block
9ea2e9262f Don't use references to `TaprootMerkleBranchBuf` (Martin Habovstiak)
c528f52894 Change `Deref::Target` of `TaprootMerkleBranchBuf` (Martin Habovstiak)
04a4efbe63 Introduce unsized `TaprootMerkleBranch` (Martin Habovstiak)
370c2597c6 Add `as_mut_slice` to `TaprootMerkleBranchBuf` (Martin Habovstiak)
33d75659da Push `merkle_branch` module one level deeper. (Martin Habovstiak)
277045bad7 Add `Buf` suffix to `TaprootMerkleBranch` (Martin Habovstiak)

Pull request description:

  This implements a bunch of changes needed to make `ControlBlock` alloc-free. In particular, this allows constructing `Witness` without the intermediate allocation. It is also a step towards having `P2TrSpend` public.

  Closes #1614

  This also intentionally does **not** address decoding of `ControlBlock` from `Witness` since I'm not sure about the API.

  Rationale for doing the `Buf` rename: while doing it with `Script` was very painful it shouldn't be here since it's not used that often and also we can just backport the first commit with deprecated type alias. I was thinking of having `TaprootMerkleBr` but it'd be inconsistent and the name is silly.

  (Also if anyone is wondering why I did this: I was too exhausted to do more important stuff but felt like doing something nice and easy like this.)

ACKs for top commit:
  tcharding:
    ACK 9ea2e9262f
  apoelstra:
    ACK 9ea2e9262fbc04ea6fad33047de0fc1ead999dc7; successfully ran local tests

Tree-SHA512: c5e3ea61d10fbe0cbce5e900943e3cef77a175a62043c500b3ff6df57a96f00692d80fb1c4dd75bca9a704201baab6ddfcc430b12c7ecabc43968198466fed9d
2025-03-20 19:12:00 +00:00
Andrew Poelstra 05c8b043ff
tests: replace Amount::from_sat_unchecked with from_sat.unwrap
There are only 7 instances of this so just call .unwrap() on each one.
2025-03-18 19:27:53 +00:00
Andrew Poelstra beaa2db7e5
amount: add from_sat_i32 and from_sat_u32 methods for small constants
We have a ton of calls to `from_sat_unchecked` for small constants which
were clearly in range, e.g. in fee.rs. Add a new constfn for these
cases. Don't bother making a generic Into<u32>/Into<u16> variant because
there isn't an obvious name for it.

There are 7 instances where we're using this method with values that are
out of range, which we leave as from_sat_unchecked for now.
2025-03-18 19:27:53 +00:00
Martin Habovstiak 9ea2e9262f Don't use references to `TaprootMerkleBranchBuf`
The new unsized type is more flexible and so are the references to it.
Just like we pass around `&str` instead of `&String` we should be
passing `&TaprootMerkleBranch` instead of `&TaprootMerkleBranchBuf`.
2025-03-18 16:33:05 +01:00
Martin Habovstiak c528f52894 Change `Deref::Target` of `TaprootMerkleBranchBuf`
`TaprootMerkleBranchBuf` previously derefed to a slice which lost the
information about length being valid. This commit changes the type
which, while API-breaking, is not disruptive because the type has API
very similar to slice.
2025-03-18 16:32:55 +01:00
Martin Habovstiak 04a4efbe63 Introduce unsized `TaprootMerkleBranch`
`TaprootMerkleBranchBuf` being a vec introduced intermediate allocation
when creating or decoding `Witness`. However the representation on the
wire is the same as in-memory (aside from `#[repr(transparent)]`) so
this allocation wasn't really needed.

This commit introduces `TaprootMerkleBranch` type which is unsized and
can be used in place of `TaprootMerkleBranchBuf` within `ControlBlock`.
Aside from removing the intermediate allocation, this improves the API a
bit: the conversion from array to other type is no longer needed because
it's performed by `ControlBlock` in its methods. Thus, consumers who
have an array can simply set it as `merkle_branch` field and then encode
the `ControlBlock` into witness. A convenience method is also provided
to push the `ControlBlock` along with other parts at the end of the
`Witness`.
2025-03-18 16:31:46 +01:00
Martin Habovstiak 370c2597c6 Add `as_mut_slice` to `TaprootMerkleBranchBuf`
`TaprootMerkleBranchBuf` already had `as_slice` method and `DerefMut`
but was missing `as_slice_mut`, so this change adds it.
2025-03-18 16:20:16 +01:00
Martin Habovstiak 33d75659da Push `merkle_branch` module one level deeper.
This moves the content of the module into `buf` submodule making future
changes clearer.
2025-03-18 16:20:16 +01:00
Martin Habovstiak 277045bad7 Add `Buf` suffix to `TaprootMerkleBranch`
This type actually contains a `Vec` but we would prefer to have an
unsized type. Rename it first so that we can reuse the name later.
2025-03-18 16:20:16 +01:00
merge-script cfccd389a9
Merge rust-bitcoin/rust-bitcoin#4242: Automated nightly rustfmt (2025-03-16)
a74e08a53d 2025-03-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:
  tcharding:
    ACK a74e08a53d
  apoelstra:
    ACK a74e08a53df42a822b3e4840f37bf72b24418fe1; successfully ran local tests

Tree-SHA512: ca233e517c6734181e86940ba92bde1c2b138b66ff27e80a10f98d5ae1aac5ac73ec46633bba2a94ba143d01602764bfe6ffad2378cdf8bbfe4210be620113ea
2025-03-18 12:27:46 +00:00
merge-script 78538bc8a6
Merge rust-bitcoin/rust-bitcoin#4251: Enable getting the network kind from an address
f4f79f88eb Enable getting the network kind from an address (Tobin C. Harding)

Pull request description:

  Users may wish to ask of an address 'what kind of address is this?' We have the `NetworkKind` struct that abstracts over the answer but currently no API to ask the question.

  The address may have been parsed or constructed and weather the network has been checked already is immaterial. Hence we add the function for both `NetworkChecked` and `NetworkUnchecked` addresses.

  Fix: #4247

ACKs for top commit:
  apoelstra:
    ACK f4f79f88eb2c6c80c46c95c69fcc43b17d306be2; successfully ran local tests
  Kixunil:
    ACK f4f79f88eb

Tree-SHA512: 57bdf7a0f2ae8bf599b3830d10201af3f6312a802ab72c0d86e346af660cbc4f430954e46d6698032a062514ec3ee1ee7edc732beff79af99a84ce718a519afa
2025-03-17 23:22:15 +00:00
merge-script ac502b0fc3
Merge rust-bitcoin/rust-bitcoin#4246: fix spelling issues
441e173792 Update owned.rs (planetBoy)
dd6687d992 Update sighash.rs (planetBoy)

Pull request description:

  bitcoin/examples/sighash.rs
  inde - index `fix errors`

  bitcoin/src/blockdata/script/owned.rs
  paics - panics `fix errors`

ACKs for top commit:
  Kixunil:
    ACK 441e173792
  tcharding:
    ACK 441e173792

Tree-SHA512: a040cfd58f0ce17d8698da6f815705da77557d99d6c1b0e7d30696ff38a72857ed17e93aea169890983bdd6c36b9b8617e435178b94014a5bba3eacc7f4286b0
2025-03-17 21:31:12 +00:00
Tobin C. Harding f4f79f88eb
Enable getting the network kind from an address
Users may wish to ask of an address 'what kind of address is this?' We
have the `NetworkKind` struct that abstracts over the answer but
currently no API to ask the question.

The address may have been parsed or constructed and weather the network
has been checked already is immaterial. Hence we add the function for
both `NetworkChecked` and `NetworkUnchecked` addresses.

Fix: #4247
2025-03-17 12:27:11 +11:00
planetBoy 441e173792
Update owned.rs 2025-03-16 16:14:13 +01:00
healthyyyoung 427bfb82d7 chore: spellchecker 2025-03-16 07:23:18 +00:00
Fmt Bot a74e08a53d 2025-03-16 automated rustfmt nightly 2025-03-16 01:25:25 +00:00
merge-script 0ca9fcfd0e
Merge rust-bitcoin/rust-bitcoin#4157: Enforce MAX_MONEY invariant in amount types
ab4ea7c13d Enforce the MAX_MONEY invariant in amount types (Tobin C. Harding)

Pull request description:

  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 eliminated 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

  Close #620

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

Tree-SHA512: bec963d8ea69e202f399cd19bca864b06f3e86323d376c2d2126d74093598f8bbbf19792b2327dba0862ef6f0201202778014a2be7a14991f02917d8ca312afb
2025-03-13 23:35:40 +00:00
merge-script d1365eb376
Merge rust-bitcoin/rust-bitcoin#4219: Rename then new `Timestamp` type
c707b959b7 Rename timestamp module to time (Tobin C. Harding)
e2dee4900f Re-name Timestamp to BlockTime (Tobin C. Harding)

Pull request description:

  Done in two patches so we can bikeshed the name of the type and separately the name of the module.

  - Rename type: `Timestamp` to `BlockTime`
  - Rename module: `timestamp` to `time`

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

Tree-SHA512: de3855b38445a58b6767a6081919eecb81c6c12aee3f6699f3bfa10efaf5770b54fb412da23991a9ee734e14dfb642af670f0218d1886cdc8c8d3f393ef65d7e
2025-03-13 15:13:31 +00:00
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
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
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 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
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