Commit Graph

1705 Commits

Author SHA1 Message Date
Tobin C. Harding 24e944ed82
Introduce taproot hash type extension traits
Introduce three extension traits for the taproot hash types. All logic
for the hash types is now within the extension traits.
2024-10-22 13:50:44 +11:00
Tobin C. Harding c30ef617fb
Move combine_node_hashes out of TapNodeHash
Currently `combine_node_hashes` is an associated function, it is also
private. It is called from within other methods of the `TapNodeHash`.

In preparation for moving the `TapNodeHash` to `primitives` while
leaving all the methods in `bitcoin` in an extension trait; move the
associated function out of `TapNodeHash` and make it a stand alone
private function.
2024-10-22 13:50:43 +11:00
merge-script b4f52ac87a
Merge rust-bitcoin/rust-bitcoin#3498: Seal extension traits
e7d326f071 Seal extension traits (Tobin C. Harding)

Pull request description:

  The extension traits are temporary just while we try to stabalize `primitives`, they are not intended to be implemented by downstream.

  Seal the extension traits so that downstream crates cannot implement them.

  Fix: #3231

ACKs for top commit:
  apoelstra:
    ACK e7d326f071a368389f087ddb10ee9bbf3552c33a; successfully ran local tests; thanks! I know this is tedious and annoying

Tree-SHA512: 365979aeabb7941b9c8fa526f71aaadae3ab1cdd6a39e992c5eea2c1057b4b7c2b3a846ffd96a7eab47b9ad4e3e4de4fb141c24c62747e5cee45c74f52f9a172
2024-10-22 02:19:02 +00:00
Tobin C. Harding bbffa3db43
Remove the IO error from DecodeError
The `DecodeError` (badly named) consensus decodes an object from an
iterator that implements `Read`. The `Read` impl never returns a real IO
error, we use the `io::Error` to temporarily wrap the error returned by
the inner iterator and unwrap it in `IterReader::decode`. As such there
is no reason for the `DecodeError` to hold an `encode::Error`, it can
hold an `encode::ParseError`.

The value of this change is easily seen in the removal of calls to
`unreachable`.
2024-10-22 12:39:07 +11:00
Tobin C. Harding 713196be0d
Return DeserError from encode::deserialize
The `encode::deserialize` function never returns an I/O error. Add a new
error type that expresses this.
2024-10-22 12:04:40 +11:00
Tobin C. Harding 33566ac58c
Split encode::Error into two parts
The `consensus::encode::Error` contains an IO error but reading from a
buffer only ever errors for EOF. We converted all instances of EOF to
`MissingData` already so now we can split the IO error apart from the
actual encoding errors variants.
2024-10-22 11:58:43 +11:00
Tobin C. Harding b04142c745
Add encode::Error::MissingData variant
The `io::Error` is troublesome because it contains a bunch of stuff that
never happens when reading from a buffer. However the EOF variant can
occur if the buffer is too short. As an initial step towards reducing
usage of the `io::Error` add a `MissingData` variant to the
`encode::Error` and when converting from an IO error map to
`MissingData` if EOF is encountered.
2024-10-22 11:53:41 +11:00
Tobin C. Harding 5a42ef2850
Do not manually map IO error
We have a `From` impl for IO error but we are manually mapping.

Done in preparation for patching the `From` impl.
2024-10-22 11:51:20 +11:00
Tobin C. Harding efd7f9f06c
Add error constructor parse_failed_error
The `encode::Error::ParseFailed` variant holds an inner string, this is
suboptimal.

In an effort to patch the `encode::Error` while mimizing the diffs
required add a helper function that creates the variant. The benefit is
that later patches that effect this variant will only need to update the
constructor function and not every call site.

Internal change only.
2024-10-22 11:47:34 +11:00
Tobin C. Harding ebfef3f114
Return generic error as Some
The `std::error::Error` impl for `consensus::DecodeError` should return
the inner generic error.
2024-10-22 11:47:34 +11:00
Tobin C. Harding a6254212dc
Move consensus error code to submodule
The `consensus` module has a bunch of error types, move them all to a
separate module. Add re-exports so the types are still available at the
same place they were. Make the `error` module private and re-export all
errors from the `consensus` module root.
2024-10-22 11:47:34 +11:00
merge-script 8de5432de9
Merge rust-bitcoin/rust-bitcoin#3496: Expose units::amount::ParseError
9e607bbbe2 Expose units::amount::ParseError (Casey Rodarmor)

Pull request description:

  Fixes #3490.

ACKs for top commit:
  tcharding:
    ACK 9e607bbbe2
  apoelstra:
    ACK 9e607bbbe2738340f04dae4fab6b35e642e98f38; successfully ran local tests

Tree-SHA512: 70e4e9fae82d5aa2467be1e703d219e64fc3ab24158f109b2f4e73fb4e84d7dabcdc8d75c2363284aeb1736b023f64e8f440a2965c3734241d7748af1ad7da22
2024-10-21 20:27:32 +00:00
Jamil Lambert, PhD 9c2ac46902
Split up ParseError
ParseError is too general and the functions returning it do not have an
error path for all variants.

Split out the Bech32 and Base58 related errors into their own enums.
2024-10-21 20:55:42 +01:00
Tobin C. Harding e7d326f071
Seal extension traits
The extension traits are temporary just while we try to stabalize
`primitives`, they are not intended to be implemented by downstream.

Seal the extension traits so that downstream crates cannot implement
them.

Fix: #3231
2024-10-21 14:51:23 +11:00
Casey Rodarmor 9e607bbbe2 Expose units::amount::ParseError 2024-10-20 11:25:46 -07:00
Fmt Bot 2a08b29232 2024-10-20 automated rustfmt nightly 2024-10-20 01:21:52 +00:00
merge-script 51d503730d
Merge rust-bitcoin/rust-bitcoin#3406: Move `Witness` to `primitives`
c1eccfde25 Move Witness to primitives (Tobin C. Harding)
6ce76cd7c8 Add unstable constructor to Witness (Tobin C. Harding)

Pull request description:

  Patch 1 introduces a new policy to the codebase, we use `foo__unstable` for public unstable functions and there are zero semver guarantees if you call these functions.

  Patch 2 does the move.

  Close #3406

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

Tree-SHA512: 2388066be2b6bb2cf3d6757c8f6334beeda6115ef1ce7c537955d32aa5e466add5162d0d2adee27f416fe622fc93c4e94bd848326463ee55e08d1c0f4e03719c
2024-10-18 16:35:15 +00:00
Jamil Lambert, PhD 3d994f7bdb
Decode an address string based on prefix
When a decoding error occurs for a bech32 address string the error is
discarded and the same address string is attempted to be decoded as
base58.  This then incorrectly returns a base58 error.

Check the string prefix and decode as bech32 or base58 and return the
relevant error.  If the prefix is unknown return an `UnknownHrpError`.
2024-10-18 10:38:28 +01:00
ChrisCho-H 9d9a2d53e1 refactor: compute recid in a single line 2024-10-18 16:39:50 +09:00
Tobin C. Harding c1eccfde25
Move Witness to primitives
Move the `Witness` over to `primitives` leaving behind any method that
takes or returns a `Script` or a signature.

Includes addition of a feature gate to unit test.
2024-10-18 14:02:03 +11:00
Tobin C. Harding 6ce76cd7c8
Add unstable constructor to Witness
In preparation for moving the `Witness` to `primitives` we need a way to
construct the `Witness` when decoding. In order to maintain the current
performance and not introduce additional allocations we need to be able
to construct a `Witness` from the content buffer, this leaks the
implementation details of `Witness`.

Add a clearly marked unstable constructor to create a `Witness` from
parts. This introduces the concept of `foo__unstable` function names;
add a section to the README describing unstable functions and semver
guarantees.
2024-10-18 14:01:03 +11:00
merge-script 7af9e33f2b
Merge rust-bitcoin/rust-bitcoin#3468: Move `block::Header` to `primitives`
ff64fa3c46 Move block::Header to primitives (Tobin C. Harding)
65925117a0 Manually implement block::Header::block_hash (Tobin C. Harding)
b02ab25342 Add regression test for block_hash function (Tobin C. Harding)
5614694b69 block: Remove wildcard re-export (Tobin C. Harding)

Pull request description:

  - Patch 1: Trivial preparation.
  - Patch 2 and 3: Reduce the API surface in a disappointing way due to the orphan rule.
  - Patch 4: Do the move.

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

Tree-SHA512: 8d93de4a12c9f71f9dfbdc023fd8defb78f7d3da995490af8f83a927c2ca6338dd236bc08691311dc7345183fb6d0be61eedce3e4424d4060be2de934facd60a
2024-10-17 13:12:06 +00:00
merge-script 0f20669b40
Merge rust-bitcoin/rust-bitcoin#3472: test: add test for new sign fn
88a35c8918 test: add test for new sign fn (Chris Hyunhum Cho)

Pull request description:

  Follow up https://github.com/rust-bitcoin/rust-bitcoin/pull/3456.
  add test to verify the signature generated from newly added `sign` fn.

ACKs for top commit:
  apoelstra:
    ACK 88a35c8918f48e72f032b73a233a016a1a0056b6; successfully ran local tests; sorry, testing local CI changes..
  tcharding:
    ACK 88a35c8918

Tree-SHA512: 8c457fbabf1ff86a9369934564874e78660f706714465731c185da16eff5c88a5e680c4373effbcc273394a5653edde9f559558917fee6e438b79bfee6f63b22
2024-10-17 02:09:09 +00:00
merge-script 73bb017d78
Merge rust-bitcoin/rust-bitcoin#3474: Remove `allow_unused` flag in serde
49914c50f4 Remove `allow_unused` flag in serde (Jamil Lambert, PhD)

Pull request description:

  In serde `#[allow(unused_variables)]` has been dropped and the unused variables have been "used" when `debug_assertions` is off.

  Close #3332

ACKs for top commit:
  tcharding:
    ACK 49914c50f4
  apoelstra:
    ACK 49914c50f4 successfully ran local tests

Tree-SHA512: c83703f91abb5ce513f6403375ce3b2cb24d5200272c98f0a5e532fe41dda1aae8a5334ee8868751e156457f1cfe6b4fe08849fb5329bfbb1c12ed18364e0b1c
2024-10-16 12:03:47 +00:00
merge-script 17899d1b8c
Merge rust-bitcoin/rust-bitcoin#3475: Unify deprecated note field format
88b53a471e Unify deprecated note field format (Jamil Lambert, PhD)

Pull request description:

  Following the suggestion in Issue #3306 all the deprecated note fields have been changed to be lower case and in the format "use `abc` instead".

ACKs for top commit:
  tcharding:
    ACK 88b53a471e
  apoelstra:
    ACK 88b53a471e successfully ran local tests

Tree-SHA512: 5c20eda7140f37ce78eb58dfdf03ecc11a67fcb10f294d860e81eaaee696e3a4209516017a9885cef9bfff1aa69b845534d139578b674933770fa24d59e4275f
2024-10-16 02:10:58 +00:00
merge-script 84deb29964
Merge rust-bitcoin/rust-bitcoin#3473: Fix lint errors
8696cb4b07 Fix unused import warning (Jamil Lambert, PhD)
fbc7aa7fd5 Remove unnecessary lifetimes (Jamil Lambert, PhD)

Pull request description:

  New lint warnings from a recent nightly toolchain show some explicit lifetimes that can be omitted.

  The unnecessary lifetimes have been removed.

  An unused import warning has also been fixed by placing the use statement behind the relevant feature gate.

ACKs for top commit:
  tcharding:
    ACK 8696cb4b07
  apoelstra:
    ACK 8696cb4b07 successfully ran local tests

Tree-SHA512: 37e14ca9be2e8cfa64498084a7363783c2cca92dfce40a0a3ed9bda98f081c1fa9119e5d49d81684b6968329945c7004b86534412fd2fcd4898b862c859d42c6
2024-10-16 00:04:05 +00:00
merge-script 88af71c81f
Merge rust-bitcoin/rust-bitcoin#2906: psbt: Encode keytype as a compact size unsigned integer
2f656f77ba psbt: Use u64 for key type (Tobin C. Harding)

Pull request description:

  Currently we use `u8` for key type but it was pointed out that we should be using a `u64` and encoding it as a compact type. The reason our code works now is because the compact type encoding for a `u8` (less than

  This breaks the `serde` impl, as shown by changes to the regression tests,

  Fix: #2891

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

Tree-SHA512: ce5fe46b54cb724a0b0f9f874c037e5b5d344e5d3c380f9cdb3fdb5cbc5e31e4e32c229f5f2f72547823238934f6b0c3640c2c2ce79d98aa57bac509d130cb82
2024-10-15 18:37:20 +00:00
Jamil Lambert, PhD 88b53a471e
Unify deprecated note field format
All the deprecated note fields have been changed to be lower case and in
the format "use `a` instead".
2024-10-15 15:16:01 +01:00
Jamil Lambert, PhD 49914c50f4
Remove `allow_unused` flag in serde
In serde `#[allow(unused_variables)]` has been dropped and the unused
variables "used" when `debug_assertions` is off.
2024-10-15 14:40:54 +01:00
Jamil Lambert, PhD 8696cb4b07
Fix unused import warning
An import was only used behind a feature gate.  The feature gate has
been added to the use statement to remove the warning.
2024-10-15 14:05:19 +01:00
Chris Hyunhum Cho 88a35c8918
test: add test for new sign fn 2024-10-15 12:51:47 +09:00
Tobin C. Harding ff64fa3c46
Move block::Header to primitives
Introduce an extension trait and move the `block::Header` type to
`primitives`.

Nothing surprising here.
2024-10-15 11:04:00 +11:00
Tobin C. Harding 65925117a0
Manually implement block::Header::block_hash
We want to move the `block_hash` function to `primitives` but it uses
`Encodable` which currently lives in `bitcoin`. Just implement it
manually.

We added a regression test already in a previous commit to check that
this is correct.
2024-10-15 10:46:28 +11:00
Tobin C. Harding b02ab25342
Add regression test for block_hash function
In preparation for manually implementing `Header::block_hash` add a
regression test.
2024-10-15 10:38:50 +11:00
Tobin C. Harding 5614694b69
block: Remove wildcard re-export
Use explicit re-exports for `primitives` types when re-exporting from
the `block` module.
2024-10-15 10:01:03 +11:00
Tobin C. Harding e68da281ff
Warn on future deprecations
We use `TBD` in our `deprecated` string and it was discovered that there
is an exception on this string so as not to warn because it is used
internally by the Rust language. However there is a special lint to
enable warnings, lets use it.

Add `#![warn(deprecated_in_future)]` to the coding conventions section
of all crates except `fuzz`.
2024-10-15 07:56:06 +11:00
Tobin C. Harding 2cc944578d
Fully deprecate Hash::from_slice
We had an initial go at this but we didn't do the `Hash` trait method.
In order to do so we need to hack the serde code a fair bit, note the
public visitor types.
2024-10-15 07:56:05 +11:00
Tobin C. Harding 8bc3b2e2c4
Stop using deprecated to_vec
Recently we deprecated `to_vec` in favour of `to_bytes` however we
continued to use `to_vec` in a few places. This wasn't noticed because
of our usage of `TBD` in the `deprecated` attribute.

Use `to_bytes` instead of `to_vec`.
2024-10-15 07:49:48 +11:00
merge-script 1b23d1c66c
Merge rust-bitcoin/rust-bitcoin#3431: Move script types to `primitives`
d649c06238 Move script types to primitives (Tobin C. Harding)
ec4635904b Inline bytes_to_asm_fmt into Script::Display impl (Tobin C. Harding)

Pull request description:

  First patch removes `bytes_to_asm_fmt` as requested by Kix here: https://github.com/rust-bitcoin/rust-bitcoin/pull/3194#discussion_r1756557768

  Second patch does the move. The move is minimal but there is quite a bit of code moved in `script/mod.rs` - I believe it is as minimal as required as well.

ACKs for top commit:
  apoelstra:
    ACK d649c06238 successfully ran local tests

Tree-SHA512: 329a23948ac5617402a724b734d81cde8ab1f57ddd4860f858880618e260ea8b5cc89315de1fd93ae32787d5e8508fd604a41f003b1f5772a773b5b1648d382c
2024-10-14 00:31:38 +00:00
Tobin C. Harding d649c06238
Move script types to primitives
Move the `Script` and `ScriptBuf` types to `primitives`, nothing else.
2024-10-14 09:02:39 +11:00
Tobin C. Harding ec4635904b
Inline bytes_to_asm_fmt into Script::Display impl
This function does not need to exist anymore because it is exactly the
same as what is produced by the `Display` impl.
2024-10-14 09:02:26 +11:00
merge-script 48b2975870
Merge rust-bitcoin/rust-bitcoin#3456: feat: add sign fn for sign_message
c41a6e9b1b feat: add sign fn for sign_message (ChrisCho-H)

Pull request description:

  While it's not hard to create the signature using `secp256k1` modules with `signed_msg_hash`, it's much more convenient and safe to provide one-way function to generate signed signature(even without the understanding about the semantics of bitcoin message signing).

ACKs for top commit:
  apoelstra:
    ACK c41a6e9b1b successfully ran local tests
  tcharding:
    ACK c41a6e9b1b

Tree-SHA512: 84caea275059381040c71100badb54556dd105722f79ac43d2df7eb0e5428cf8e7acc2d7f262625dd008837099a928c3c8be858f9ab11838c2eec1786e9f1844
2024-10-13 21:13:39 +00:00
merge-script cc5b46bfc7
Merge rust-bitcoin/rust-bitcoin#3462: Automated nightly rustfmt (2024-10-13)
f1733309e0 2024-10-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:
  apoelstra:
    ACK f1733309e0 successfully ran local tests

Tree-SHA512: 465336b92d9b8a61afbc8e8d0560f3b41a732e3245d764447cc2a5429f693e29172bebeca756a91b5a1f3a16d42728960a526111bbd92316f72b46b420373641
2024-10-13 16:40:17 +00:00
Fmt Bot f1733309e0 2024-10-13 automated rustfmt nightly 2024-10-13 01:20:28 +00:00
yancy 8f79a0560e Remove unused import 2024-10-12 19:20:11 -05:00
merge-script 65e290a0ce
Merge rust-bitcoin/rust-bitcoin#3450: Add version three variant to transaction version
9e6b8faf84 feat: add version three variant to transaction version (Rob N)

Pull request description:

  Topologically restricted transactions are now considered standard as of Bitcoin 28.0.

ACKs for top commit:
  apoelstra:
    ACK 9e6b8faf84 successfully ran local tests
  tcharding:
    ACK 9e6b8faf84

Tree-SHA512: 289b986304e206802f04cee6607087167f6d91d8a81d4fc49ed01c430f4f6ad00b44646fbefdd000148fc5bfe2d257f92b386bfaf4405c482e4e438d830ab586
2024-10-12 17:55:21 +00:00
merge-script ae2358610d
Merge rust-bitcoin/rust-bitcoin#3457: Fix unused import
a250c8eee4 Fix unused imports (Shing Him Ng)

Pull request description:

  Found some unused imports while working on something unrelated

ACKs for top commit:
  apoelstra:
    ACK a250c8eee4 successfully ran local tests
  tcharding:
    ACK a250c8eee4

Tree-SHA512: ffa4fcddb91c849df7885a6d734405ab704e0353293fa3462a981e179ea49091990df4482eee2324e7b5ef250a0890b2e8983d53ec288ca6e8ada77e30c102dc
2024-10-12 14:56:48 +00:00
Andrew Poelstra 76f022d380 Merge rust-bitcoin/rust-bitcoin#3438: Implement Arbitrary for Block
0c824c9c68 Implement Arbitrary for Block (Shing Him Ng)

Pull request description:

  Implementing `Arbitrary` for `Block` and its child types

ACKs for top commit:
  apoelstra:
    ACK 0c824c9c68 successfully ran local tests
  tcharding:
    ACK 0c824c9c68

Tree-SHA512: 407acd4155ca7496bf7c59af19f103b22b4c74dd013c2e2c42aae690ba7264ecd42ed034355cf8895f2532f8cef77dfdbeac250301a6973afd5f3920be7e4310
2024-10-12 14:15:28 +00:00
ChrisCho-H c41a6e9b1b feat: add sign fn for sign_message 2024-10-12 12:37:04 +09:00
Rob N 9e6b8faf84
feat: add version three variant to transaction version 2024-10-11 06:38:58 -10:00
merge-script 03715872cd
Merge rust-bitcoin/rust-bitcoin#3432: fmt: Use style_edition = 2021
323e706113 Add rustfmt config option style_edition (Tobin C. Harding)
2e4179ed0f Run the formatter (Tobin C. Harding)
2c40b4f4ec Configure formmater to skip read_compact_size (Tobin C. Harding)

Pull request description:

  `rustfmt` is emitting:

   Warning: the `version` option is deprecated. Use `style_edition` instead.

  As suggested add a config option and set it to 2021.

  - Patch 1: Manually configure rustfmt to skip some code
  - Patch 2: Run the formmater with current configuration
  - Patch 3: Add the new config option (remove old one), introduces no new formatting requirements

ACKs for top commit:
  apoelstra:
    ACK 323e706113 successfully ran local tests

Tree-SHA512: 7f80cc89f86d2d50936e51704344955fa00532424c29c0ee3fae1a6836e24030f909b770d28da13e1c5efde3d49ad7d52c6d909d120fb09c33abf1755f62cd38
2024-10-11 12:48:50 +00:00
Shing Him Ng a250c8eee4 Fix unused imports 2024-10-10 23:42:51 -05:00
Shing Him Ng 0c824c9c68 Implement Arbitrary for Block 2024-10-10 23:30:39 -05:00
merge-script fe62d94ff7
Merge rust-bitcoin/rust-bitcoin#3296: Remove the `SliceIndex` implementation from hash types
3b7ba4f977 Remove the SliceIndex implementation from hash types (Tobin C. Harding)

Pull request description:

  If folk really want to index into a hash they can us `as_byte_array` then index that.

  Includes a bump to the version number of `hashes` to `v0.15.0` - this is because otherwise `secp` won't build since we are breaking an API that is used in the current release of secp.

  Fix: #3115

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

Tree-SHA512: 0ba93268cd8133fe683183c5e39ab8b3bf25c15bfa5767d2934d67a5f6a0d2f65f6c9304952315fe8a33abfce488d810a8d28400a28facfb658879ed06acca63
2024-10-10 22:39:21 +00:00
merge-script d5ebb19543
Merge rust-bitcoin/rust-bitcoin#3437: Move `block::Version` to primitives
70264bfcec Move block::Version to primitives (Tobin C. Harding)
819d8d72e8 Stop using private Version constructor (Tobin C. Harding)

Pull request description:

  This is a straight up move of the whole type because there are only three methods, a getter, a setter, and `is_signalling_soft_fork`.

  If we use an extension trait for `is_signalling_soft_fork` then we have to make the two private associated consts public which is in my opinion worse.

  Patch 1 is preparation, use getter/setter, patch 2 does the move.

ACKs for top commit:
  apoelstra:
    ACK 70264bfcec successfully ran local tests

Tree-SHA512: b61e853b4b96cb1cc56c7bfb67cc6c3ba7c631cb9e540393eb780dcf63bd2d934058794f2ac0145b4b5404a6a68887c3a1d225b2d87b57415474c05d3ef2811f
2024-10-10 21:28:44 +00:00
Tobin C. Harding 2f656f77ba
psbt: Use u64 for key type
Currently we use `u8` for key type but it was pointed out that we should
be using a `u64` and encoding it as a compact type. The reason our code
works now is because the compact type encoding for a `u8` (less than
253) is the same as for a `u8`.

This breaks the `serde` impl, as shown by changes to the regression tests.
2024-10-10 11:37:40 +11:00
Tobin C. Harding 2e4179ed0f
Run the formatter
Run `just fmt`, no other manual changes.
2024-10-10 10:39:53 +11:00
Tobin C. Harding 2c40b4f4ec
Configure formmater to skip read_compact_size 2024-10-10 10:39:36 +11:00
merge-script 78bcca71ca
Merge rust-bitcoin/rust-bitcoin#3443: Input weight prediction helpers for nested P2WPKH
aa1fd5f44c Update the doc for InputWeightPrediction::weight() (spacebear)
8fd53c8ecf Add nested P2WPKH helpers to InputWeightPrediction (spacebear)

Pull request description:

  Following up on https://github.com/rust-bitcoin/rust-bitcoin/issues/630#issuecomment-2392050517.

  I amended the docstring on `InputWeightPrediction::weight()` to clarify that it only returns the signature weight. I'm not sure what the rationale was for only returning partial input weight, and if there are any arguments against something like `InputWeightPrediction::total_weight()` which returns `self.weight() + Weight::from_non_witness_data_size(32 + 4 + 4)` (and maybe deprecate `weight()` eventually to prevent misuse).

ACKs for top commit:
  apoelstra:
    ACK aa1fd5f44c successfully ran local tests
  tcharding:
    ACK aa1fd5f44c

Tree-SHA512: 9bdd882164a80a1a1a44c593750aa59e470f2928c0380d4d53a0409add597844cf11945785264ebd6b281029b3f2ef1be6c77e1e9cd148192dbd601df30a60ae
2024-10-09 15:06:46 +00:00
BinChengZhao 2424b654d5 feat: rust-bitcoin supports testnet4 2024-10-05 23:47:21 +08:00
spacebear aa1fd5f44c
Update the doc for InputWeightPrediction::weight()
Clarify that the returned weight doesn't include txid, index, or
sequence weights.
2024-10-03 19:44:28 -04:00
spacebear 8fd53c8ecf
Add nested P2WPKH helpers to InputWeightPrediction
Add helpers for input weight predictions corresponding to P2WPKH nested
in a P2SH output (nested segwit).
2024-10-03 19:32:57 -04:00
BinChengZhao afa91a2030 Introduce TestnetVersion enum with only TestnetV3 2024-10-03 18:40:06 +08:00
Tobin C. Harding 3b7ba4f977
Remove the SliceIndex implementation from hash types
If folk really want to index into a hash they can us `as_byte_array`
then index that.

Includes a bump to the version number of `hashes` to `v0.15.0` - this
is because otherwise `secp` won't build since we are breaking an API
that is used in the current release of secp.

Fix: #3115
2024-10-02 10:18:45 +10:00
Tobin C. Harding 70264bfcec
Move block::Version to primitives
This is a straight up move of the whole type because there are only
three methods, a getter, a setter, and `is_signalling_soft_fork`.

If we use an extension trait for `is_signalling_soft_fork` then we
have to make the two private associated consts public which is in my
opinion worse.
2024-10-02 09:35:23 +10:00
Tobin C. Harding 819d8d72e8
Stop using private Version constructor
In preparation for moving the `block::Version` type over to `primitives`
stop using the private constructor and inner field. Use the public
getter/setter instead (`to_consensus`and `from_consensus` respectively).
2024-10-02 09:35:23 +10:00
merge-script f3fbd0e0b9
Merge rust-bitcoin/rust-bitcoin#3408: Prepare `Witness`
3f3f30d6c7 Use iter instead of accessing content field (Tobin C. Harding)
6389d1cbb3 Stop using push_slice (Tobin C. Harding)
be163eec99 Use Witness::len instead of accessing field (Tobin C. Harding)

Pull request description:

  Prepare `Witness` to be moved to `primitives`.

  This is the first three patches out of #3406. Patch 1 and 2 are internal changes, path 3 is also internal but introduces a slight perf hit by doing multiple writes.

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

Tree-SHA512: 25e2570a22797dbfa15d6e5af9b72938243192ca264bc5fe82c2f327486e52a73993b3b61ee1161e5d17b01a7f9843960cb4031e6550561de4ecafb9f935e375
2024-10-01 19:42:09 +00:00
merge-script 1c030105d7
Merge rust-bitcoin/rust-bitcoin#3428: CI fixes for latest nightly
c2674caf4c clippy: remove some newly-detected unnecessary returns (Andrew Poelstra)
053a633b27 clippy: change accidental doccomments into correct comments (Andrew Poelstra)

Pull request description:

  Blocking #3427

ACKs for top commit:
  tcharding:
    ACK c2674caf4c

Tree-SHA512: 4e24e988cb27b47354c7f7b3474fc6647386379fe2e7581dde57daab75fd11d3a511084f5d00c7421d42cd40c69ecfaf10764943d97f1594479ae250687fa279
2024-10-01 04:09:49 +00:00
Tobin C. Harding 3f3f30d6c7
Use iter instead of accessing content field
We would like to move the `Witness` to `primitives` however in the
`Encodable` implementation we are currently accessing the private
`content` field.

Instead of accessing `content` we can iterate over the witness elements
and write each individually, this has the same result but does a bunch
of additional calls to `Write::write_all` (via `emit_slice`).

This patch effects performance negatively but makes no changes to the
encoding.
2024-10-01 14:04:01 +10:00
Tobin C. Harding 6389d1cbb3
Stop using push_slice
The `Witness::push_slice` function is called by `Witness::push` after
calling `as_ref`, hence is equivalent for all types that implement
`AsRef<[u8]>`. Also, `push_slice` is a private method on `Witness`.

In preparation for moving `Witness` over to `primitives` stop using
`push_slice` in favour of `push`.

Internal change only.
2024-10-01 14:04:01 +10:00
Tobin C. Harding be163eec99
Use Witness::len instead of accessing field
In preparation for moving the `Witness` oven to `primitives` use the
`len` function instead of accessing the `witness_elements` field.

No logic change, `Witness::len()` returns `witness_elements`.
2024-10-01 14:04:01 +10:00
merge-script 12e4671810
Merge rust-bitcoin/rust-bitcoin#3388: Move `OutPoint` to `primitives`
da0795e590 primitives: Use doc links for OutPoint (Tobin C. Harding)
b079cbafee Move OutPoint to primitives (Tobin C. Harding)
f5c46cd411 Introduce OutPoint extension traits (Tobin C. Harding)
7e5bd5048d Remove docs on deprecated is_null function (Tobin C. Harding)
97b20a2316 Add additional impl block to OutPoint (Tobin C. Harding)

Pull request description:

  Just the minimal move of `OutPoint` to `primitives`.

  The last patch closes #3347

ACKs for top commit:
  apoelstra:
    ACK da0795e590 successfully ran local tests

Tree-SHA512: dced5a6d6bc6af0ce8b4ae4e52c04b45c85eb77a24bb25762ba8ab7deeab1e6b392cc4b258bb14218e8a2af999715dfed45ba94599cb16963a843995a7475917
2024-09-30 21:32:14 +00:00
merge-script 7379eba587
Merge rust-bitcoin/rust-bitcoin#3409: Improve `witness` unit tests
5fab6b178f Rename iter len unit test (Tobin C. Harding)
f6a74ef4af Refactor the serde Witness unit tests (Tobin C. Harding)
9860453b5b Improve Witness consensus encode unit test (Tobin C. Harding)
7e2899d310 Improve Witness::push unit test (Tobin C. Harding)
fe967279e5 Improve witness unit tests for single empty element (Tobin C. Harding)

Pull request description:

  In preparation for moving the `Witness` type over to `primitives` refactor and improve all the unit tests that will be moved, do not touch the ones that will stay behind.

  The first five patches are from #3406, the last is just a re-name of the test function I tried to refactor in ac6fe3a881

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

Tree-SHA512: bc00f81e3c5cc92ae58dd2fc876d368a487ae6c08cc0735d7227c3a89287e321dbfb5b571b951d0616af0ec7cf9a0ea2d0e724645b1c419933a212ece80a0fbf
2024-09-30 20:31:31 +00:00
merge-script 0b5e0bac31
Merge rust-bitcoin/rust-bitcoin#3401: Move the `block` hash types over to `primitives`
2d8c613340 Move the block hash types to primitives (Tobin C. Harding)
6b9429ac7b Remove BlockHash::all_zeros (Tobin C. Harding)
20d8dbd586 Add missing line of whitespace (Tobin C. Harding)

Pull request description:

  As an initial step in moving the `block` module, just move over the hash types `BlockHash` and `WitnessCommitment`.

  Patch 2 introduces an associated const `BlockHash::GENESIS_PREV_BLOCKHASH` and removes `all_zeros`.

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

Tree-SHA512: 64aa0ae81e1c8ab1b5d4cd8cd28e6ef04ed01bf79175dc5b1fd607a6f0967e06b0aaf4c10ad368e2b327edcad3705187b6643d5ca8647716319424f19a838ba1
2024-09-30 19:40:15 +00:00
merge-script 08eaab2ecf
Merge rust-bitcoin/rust-bitcoin#3400: Move `merkle_tree` hash types to `primitives`
9ded58fc99 Move merkle_tree hash types to primitives (Tobin C. Harding)

Pull request description:

  In preparation for moving the `block::Header` struct over to `primitives` move the `merkle_tree` hash types.

ACKs for top commit:
  apoelstra:
    ACK 9ded58fc99 successfully ran local tests

Tree-SHA512: 98017cf0403871f01a6efeda22e8f319cc8104b9bc2f3a9bae2d6a31f6df8172307466c6486a9259204166933137fa03e565e08a0c156c278cfeb72cdae09b89
2024-09-30 18:29:56 +00:00
merge-script dfa8692169
Merge rust-bitcoin/rust-bitcoin#2931: Remove `VarInt` and use `ReadExt` and `WriteExt` trait methods instead
18d8b0e469 Replace VarInt type with ReadExt and WriteExt functions (Steven Roose)
003db025c1 Return encoded length from WriteExt::emit_slice (Steven Roose)

Pull request description:

  This the meat and potatoes out of Steven's work in #2133 and also closes #1016

ACKs for top commit:
  apoelstra:
    ACK 18d8b0e469 successfully ran local tests

Tree-SHA512: 2df96c91e0fbfdc87158bde9bbdd9565f67e3f66601697d0e22341416c0cd45dd69d09637393993f350354a44031bead99fd0d2f006b4fc6e7613aedc4b0a832
2024-09-30 17:31:00 +00:00
merge-script 0b8c45ff8e
Merge rust-bitcoin/rust-bitcoin#3358: Add Xpriv::to_xpub and improve related method names
e215a39dba Improve documentation of Xpub::from_xpriv (Jiri Jakes)
0dcba98382 Add Xpriv::to_xpub (Jiri Jakes)
5a9341bfc5 Improve naming of methods on Xpub and Xpriv (Jiri Jakes)

Pull request description:

  Adds `Xpriv::to_xpub` and makes naming of methods related to extended and non-extended keys on `Xpub` and `Xpriv` consistent and easier to discover:

    - if method takes or returns extended key, its name uses `xpriv` or `xpub`
    - if method takes or returns non-extended key, its name uses `public_key` or `private_key`

  Previous naming of the methods was confusing and unclear and this PR deprecates them.

  Closes #3327.

  ### Notes for reviewers

    - `xpriv` and `xpub` could be without `x`, I opted for this version to remove any ambiguity
    - also considered `raw` or similar name (as suggested) for methods with non-extended keys, however `raw` is usually not used in this context and `to_public_key` vs. `to_x_only_public_key` did not look alright with any other name
    - if splitting this PR into two would be desirable, please let me know
    - is there a reason why `ckd_priv` is private and `ckd_pub` is public?

ACKs for top commit:
  apoelstra:
    ACK e215a39dba successfully ran local tests
  tcharding:
    ACK e215a39dba

Tree-SHA512: f76b22740b06df80dd9d01fbb991149243e47619e0bd5f1699b446456259cc6e97ecb9fca7b4881e921a9d7341ca92c6ee2dae90a417f702aff5eb760439ca42
2024-09-30 13:49:02 +00:00
Andrew Poelstra c2674caf4c
clippy: remove some newly-detected unnecessary returns 2024-09-30 12:31:17 +00:00
Andrew Poelstra 053a633b27
clippy: change accidental doccomments into correct comments 2024-09-30 12:30:57 +00:00
Jiri Jakes e215a39dba
Improve documentation of Xpub::from_xpriv 2024-09-29 11:07:54 +08:00
Jiri Jakes 0dcba98382
Add Xpriv::to_xpub 2024-09-29 11:07:49 +08:00
Jiri Jakes 5a9341bfc5
Improve naming of methods on Xpub and Xpriv
This change makes method names on Xpub and Xpriv more consistent and
easier to discover by following two patterns:

  - if the method deals with extended key, it contains 'xpub' or
    'xpriv' in its name
  - if the method deals with non-extended key, it contains
    'public_key' or 'private_key'

One exception is 'ckd_*' methods, which are lower-level and their names
come from BIP32; these keep using 'priv' and 'pub'.
2024-09-29 11:07:32 +08:00
Fmt Bot a65d2a0ee4 2024-09-29 automated rustfmt nightly 2024-09-29 01:21:14 +00:00
Shing Him Ng 74a992a5c4 Implement Arbitrary for signature types 2024-09-27 18:35:33 -05:00
Tobin C. Harding b079cbafee
Move OutPoint to primitives
Move the `OutPoint` type and associated code over to `primitives`. Take
the opportunity to re-order the code so the file reads like a story,
things are implemented below where they are called.
2024-09-27 12:43:13 +10:00
Tobin C. Harding f5c46cd411
Introduce OutPoint extension traits
In preparation for moving the `OutPoint` over to `primitives` add an
extension trait to hold everything that will stay behind.
2024-09-27 12:42:19 +10:00
Tobin C. Harding 7e5bd5048d
Remove docs on deprecated is_null function
Our `define_extension_trait` macro cannot handle examples in rustdocs,
since these docs are on the deprecated `OutPoint::is_null` function just
remove them.
2024-09-27 11:38:19 +10:00
Tobin C. Harding 97b20a2316
Add additional impl block to OutPoint
In preparation for moving the `OutPoint` to `primitives` add an
additional impl block that holds everthing that will stay here in
`bitcoin`.

Internal change only.
2024-09-27 11:30:39 +10:00
Tobin C. Harding 5fab6b178f
Rename iter len unit test
Rust convention is to not use `test_` prefix on unit tests. Also this
unit test is testing that the `ExactSizedIterator` trait is implemented
and working.

Re-name unit test to `exact_sized_iterator`.
2024-09-26 15:32:15 +10:00
Tobin C. Harding f6a74ef4af
Refactor the serde Witness unit tests
In preparation for moving unit tests to `primitives` give the serde
tests some love by doing:

- Split them up to do one thing only
- Round trip arbitrary witness
- Use better names
2024-09-26 15:32:14 +10:00
Tobin C. Harding 9860453b5b
Improve Witness consensus encode unit test
Make an effort to clean up the encoding unit test, by doing:

- Remove element accessor assertions (tested already above)
- Add roundtrip encoding assertion
2024-09-26 15:32:10 +10:00
Tobin C. Harding 7e2899d310
Improve Witness::push unit test
Make an effort to clean up the `Witness::push` unit test. This patch
dose not change the test, it only attempts to make it easier to read.
2024-09-26 15:32:02 +10:00
Chris Hyunhum Cho a33bcd3654
test: ensure push_int check i32::MIN of overflow error 2024-09-26 10:05:44 +09:00
Chris Hyunhum Cho c9988ba8cb
refactor: use match for OP_N push in push_int_unchecked 2024-09-26 09:56:12 +09:00
merge-script 3bbad77172
Merge rust-bitcoin/rust-bitcoin#3392: fix: script number overflow check for push_int
33edaf935d fix: check overflow for push_int with push_int_unchecked (Chris Hyunhum Cho)
876146154b refactor: use push_lock_time instead of push_int (Chris Hyunhum Cho)

Pull request description:

  Fix the issue https://github.com/rust-bitcoin/rust-bitcoin/issues/1530. In the discussion of https://github.com/rust-bitcoin/rust-bitcoin/issues/1530, the suggested solution is to implement `ScriptInt` type to embrace the various type of integer(`i32, u32, i64, u64, i128, u128, isize, usize...`) to support both script number and locktime number.

  However, as `push_locktime` and `push_sequence` implemented, there’s no need to support `u32` of lock time for `push_int` anymore. Therefore, I’ve just changed the type of parameter to `i32`, and only check if it’s `i32::MIN`(which overflows 4 bytes sign-magnitude integer).

  ~I also added push_uint method to use internally for `push_locktime` and `push_sequence`, which have a dependency on `push_int` method.~

  UPDATE: also add `push_int_unchecked` for those who want to push the integer out of range(and helper for `push_locktime` and `push_sequence`, which has the same functionality of former `push_int`.

ACKs for top commit:
  tcharding:
    ACK 33edaf935d

Tree-SHA512: 89b02bd3faf1e0a1ed530b7210250f0db33886d2acd553d07761f4aef0bb6388b22ddc06a88de05acfe465305db4cf34822fb6547576aae2aa224b4d0045fa07
2024-09-25 18:21:34 +00:00
Chris Hyunhum Cho 33edaf935d fix: check overflow for push_int with push_int_unchecked 2024-09-25 03:15:03 +00:00
Tobin C. Harding fe967279e5
Improve witness unit tests for single empty element
The current unit test is incorrect, the indices field of a witness with
a single element starts at 1 because 0 is encode as a single
byte (compact encoded integer).

Fix the debug test and add a test that pushes an empty slice.
2024-09-25 10:43:15 +10:00
Tobin C. Harding 2d8c613340
Move the block hash types to primitives
Move the `BlockHash` and `WitnessCommitment` hash types over to
`primitives`.
2024-09-25 06:58:55 +10:00
Tobin C. Harding 6b9429ac7b
Remove BlockHash::all_zeros
Recently we removed the `all_zeros` function from `OutPoint` in favour
of a more meaningfully named associated const. We can do the same for
`BlockHash`, the all zeros has is used for the previous blockhash of the
genesis block, add a const named as such.

In test code where we use the `all_zeros` function, just use the more
explicit form `from_byte_array([0; 32])`.
2024-09-25 06:58:14 +10:00
Tobin C. Harding 20d8dbd586
Add missing line of whitespace
As is customary put a line of whitespace before the `impl Blockhash`
block.
2024-09-25 06:57:12 +10:00
Tobin C. Harding d0a30096b4
transaction: Remove Default implementations
Currently we provide `Default` implementations for a couple of types in
the `transaction` module, the values returned are meaningless and it
seems these impls were added to make writing test code easier. In
hindsight this was the wrong thing to do.

Break the API and remove the `Default` implementations for `OutPoint`
and `TxIn`.

Add an associated const `TxIn::EMPTY_COINBASE` that is, as the name
suggests, an empty transaction input with the prevout set to all
zeros as for the coinbase transaction.
2024-09-25 06:56:42 +10:00
Tobin C. Harding 9ded58fc99
Move merkle_tree hash types to primitives
In preparation for moving the `block::Header` struct over to
`primitives` move the `merkle_tree` hash types.
2024-09-25 06:56:24 +10:00
Steven Roose 18d8b0e469
Replace VarInt type with ReadExt and WriteExt functions
At some stage we named the compact encoding `VarInt` (which makes sense
because the compact size encoding is a variable length integer encoding).
However it turns out the term "varint" is used in Core for a different
encoding so this may lead to confusion.

While we fix this naming thing observe also that the `VarInt` type is
unnecessarily complicated, all we need to be able to do is encode and
decode integers in compact form as specified by Core. We can do this
simply by extending our `WriteExt` and `ReadExt` traits.

Add `emit_compact_size` and `read_compact_size` to emit and read compact
endcodings respectively.

Includes addition of `internals::compact_size::encoded_size_const`.

Patch originally written by Steven, Tobin cherry-picked and did a bunch
of impovements after the varint vs compact_size thing (#1016).

ref: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer

Co-developed-by: Tobin C. Harding <me@tobin.cc>
2024-09-25 06:56:09 +10:00
Steven Roose 003db025c1
Return encoded length from WriteExt::emit_slice
We would like to add a `emit_varint` function, however doing so requires
that we can get access to the length of a slice when we are encoding it
so we can use `emit_slice` to implement `emit_varint`. It would be
easier to do so if `emit_slice` returned the length of the slice.

In preparation for adding `emit_varint` (and removing the `VarInt` type)
return the encoded length of a slice from `WriteExt::emit_slice`.

(Patch originally written by Steven, cherry-pick and patch description
written by Tobin.)

Co-developed-by: Tobin C. Harding <me@tobin.cc>
2024-09-25 06:56:09 +10:00
Fmt Bot 2009763ae5 2024-09-22 automated rustfmt nightly 2024-09-22 01:20:08 +00:00
merge-script f804e0027b
Merge rust-bitcoin/rust-bitcoin#3389: Ignore doc example that has a compile error
46386337b0 Ignore doc compile error (Jamil Lambert, PhD)

Pull request description:

  With the stricter doc tests required to pick up unused imports etc. the code under the `compile_fail` tag also creates an Error.

  Changed `compile_fail` to `ignore` to remove the Error.

ACKs for top commit:
  tcharding:
    ACK 46386337b0
  apoelstra:
    ACK 46386337b0 successfully ran local tests

Tree-SHA512: 67513464dede8d4bd9f9fa4363ccb58e774b180f331edc4486290784f61e931195225765768030d2709f618ba4afe17d034d4bbc4d649a02e7a34b47b48bd297
2024-09-20 14:50:48 +00:00
merge-script 855c4bb754
Merge rust-bitcoin/rust-bitcoin#3364: Remove unused inports, and fix unused variables and methods in docs
f6abdcc001 Allow unused in `macros.rs` docs (Jamil Lambert, PhD)
fd89ddf401 Remove or fix unused variables and methods in docs (Jamil Lambert, PhD)
ff6b1d4f19 Remove unused variables and methods from docs (Jamil Lambert, PhD)
e58cda6f92 Remove `unused_imports` in docs (Jamil Lambert, PhD)

Pull request description:

  As mentioned in #3362 examples in documentation are not linted in the same way as other code, but should still contain correctly written code.

  #![doc(test(attr(warn(unused))))] has been added to all lib.rs files

  In the docs throughout all crates:

  - Unused imports have been removed.

  - Unused variables, structs and enums have been used e.g. with an `assert_eq!` or prefixed with `_`

  - Unused methods have been called in the example code.

ACKs for top commit:
  tcharding:
    ACK f6abdcc001
  apoelstra:
    ACK f6abdcc001 successfully ran local tests

Tree-SHA512: c3de1775ecde6971056e9fed2c9fa1621785787a6a6ccbf3a6dbd11e18d42d4956949f3f8adfc75d94fd25db998b04adb1c346cc2c2ba47f4dc37402e1388277
2024-09-20 02:10:51 +00:00
Jamil Lambert, PhD 46386337b0
Ignore doc compile error
With the stricter doc tests required to pick up unused imports etc. the
`compile_fail` tag also creates an Error.

Changed `compile_fail` to `ignore` to remove the Error.
2024-09-19 15:58:29 +01:00
Jamil Lambert, PhD ff6b1d4f19
Remove unused variables and methods from docs
Examples in documentation are not linted in the same way as other code,
but should still contain correctly written code.

Throughout the bitcoin crate unused variables have either been prefixed
with _ or an assert used.  And unused methods have been used in the
example code.
2024-09-18 16:36:52 +01:00
merge-script 2528264d0e
Merge rust-bitcoin/rust-bitcoin#3363: Implement Arbitrary for Transaction
5b4e81b379 Implement Arbitrary for Transaction (Shing Him Ng)

Pull request description:

  Implement `Arbitrary` for Transaction and its child types

ACKs for top commit:
  apoelstra:
    ACK 5b4e81b379 successfully ran local tests
  tcharding:
    ACK 5b4e81b379

Tree-SHA512: 411b97344e01e2bf38fff994902acda51f9a4fe748573e600fbdd643951a0a56b37395594349706b13c744304bd5396fd8ee31c27e5b1ff97e1a3cebd479e7c8
2024-09-18 15:32:20 +00:00
Jamil Lambert, PhD e58cda6f92
Remove `unused_imports` in docs
Examples in documentation are not linted in the same way as other code,
but should still contain correctly written code.

unused_imports in docs have been removed in bitcoin, and a warn
attribute added to lib.rs.
2024-09-18 15:58:45 +01:00
merge-script a543b45df1
Merge rust-bitcoin/rust-bitcoin#3359: Use UFCS in macros
25d906d936 Use UFCS in macros (Liam Aharon)

Pull request description:

  Closes #3304

ACKs for top commit:
  apoelstra:
    ACK 25d906d936 successfully ran local tests
  tcharding:
    ACK 25d906d936

Tree-SHA512: a02a8507bcec73dab1ec8d49e45eab327d989c276d931520e0fff312faf7d3165292c300fb9414314f9b94b1255ee49a0fb64303df6d971c9089226b6873c36a
2024-09-18 13:35:56 +00:00
merge-script d9d12dafdd
Merge rust-bitcoin/rust-bitcoin#2850: Implement GetKey for Vec<Xpriv>
8ec3571d80 Implement GetKey for Vec<Xpriv> (Nadav Ivgi)

Pull request description:

  It appears that the `BTreeSet<Xpriv>`/`HashSet<Xpriv>` sets currently implementing `GetKey` cannot actually be constructed, because `Xpriv` does not implement `Ord` nor `Hash`. (And that the rust-bitcoin code referencing these sets should not even compile? yet evidently it does 👀 )

  This PR adds support for `Vec<Xpriv>` to enable signing with multiple `Xpriv`s, but does not address the issue with the existing sets.

  The added test case demonstrates the issue:

  ```rust
  error[E0277]: the trait bound `bip32::Xpriv: std:#️⃣:Hash` is not satisfied
      --> bitcoin/src/psbt/mod.rs:2301:24
       |
  2301 |         HashSet::new().insert(xpriv.clone());
       |                        ^^^^^^ the trait `std:#️⃣:Hash` is not implemented for `bip32::Xpriv`
       |
  note: required by a bound in `std::collections::HashSet::<T, S>::insert`
      --> /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/collections/hash/set.rs:888:5

  error[E0277]: the trait bound `bip32::Xpriv: Ord` is not satisfied
      --> bitcoin/src/psbt/mod.rs:2302:25
       |
  2302 |         BTreeSet::new().insert(xpriv.clone());
       |                         ^^^^^^ the trait `Ord` is not implemented for `bip32::Xpriv`
       |
  note: required by a bound in `std::collections::BTreeSet::<T, A>::insert`
      --> /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/alloc/src/collections/btree/set.rs:899:5

  ```

ACKs for top commit:
  apoelstra:
    ACK 8ec3571d80 successfully ran local tests
  tcharding:
    ACK 8ec3571d80

Tree-SHA512: aceb95f8eaf11f91c6829e0b5e1c0264ebffbf587fd420145a22e924cb45678b2f4334f0b7de6ed99b57f0ce24c3d61f9e5c1e348e1b40975bc515e8fd16b75d
2024-09-18 00:09:53 +00:00
merge-script 1319c65506
Merge rust-bitcoin/rust-bitcoin#3370: Document the magic bytes for witness commitment
d942882b36 Document the magic bytes for witness commitment (Peter Ryszkiewicz)

Pull request description:

ACKs for top commit:
  apoelstra:
    ACK d942882b36 successfully ran local tests; neat! I don't think I was aware of this
  tcharding:
    ACK d942882b36

Tree-SHA512: 89766e986574e49b6c5fd00844db679eb8894a591463029f7f88079fa9ca65bd56265a42dd8fa5aad20ef4744bca3259c06f8a461abd1c695fb89433ed5cc145
2024-09-17 22:20:41 +00:00
merge-script 1fe9b79f66
Merge rust-bitcoin/rust-bitcoin#3356: Fix GetKey for sets (plus some related changes)
b593c886e3 Support GetKey where the Xpriv is a direct child of the looked up KeySource (Nadav Ivgi)
055aa9d4dc Refactor GetKey to take the KeyRequest by reference (Nadav Ivgi)
d15c57bd1f Refactor GetKey for sets to internally use Xpriv::get_key() (Nadav Ivgi)
d25c62bf45 Fix GetKey for sets to properly compare the fingerprint (Nadav Ivgi)

Pull request description:

  - The first commit is the simplest fix for a bug where the fingerprint wasn't compared correctly.

  - The second & third commits are optional refactoring to reuse `Xpriv::get_key` for `$set<Xpriv>::get_key`, so the Xpriv matching logic only has to be maintained in one place.

  - The forth commit adds support for signing with `Xpriv`s that are direct children of the `KeySource` -- possibly what the original (buggy) code author had in mind?

  Of course, feel free to take just the first commit if the others seem unnecessary. The last one is kind of meh, not sure if really useful.

  Note that multi-`Xpriv` signing does not actually work until #2850 is addressed too.

ACKs for top commit:
  apoelstra:
    ACK b593c886e3 successfully ran local tests
  tcharding:
    ACK b593c886e3

Tree-SHA512: a6f84fe12b68ddb71fc722649b1596d1561ab08a07d47f2345bd8932e155571f2b1e23c32a9c03d588fb697540340cb8ca38275dfe11f4910fd48265a993762a
2024-09-17 14:27:04 +00:00
merge-script bb4a9dec52
Merge rust-bitcoin/rust-bitcoin#3360: Improve documentation test in merkle_tree
9fc2c2a5b1 Improve documentation test in merkle_tree (Jiri Jakes)

Pull request description:

  Removes unnecessary usage of vector and adds a missing assert. Closes #3353.

  Note: Kixunil in the [referred comment](https://github.com/rust-bitcoin/rust-bitcoin/pull/3288#discussion_r1756514494) wondered about missing 'unused variable' warning. It seems that rustdoc [adds](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#pre-processing-examples) allows to the documentation tests. However, these warnings [can be enabled](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#showing-warnings-in-doctests) on module level. Do we want that? I tried to enable and there are already some occurrences of unused variables and imports. Caveat: it seems that we can't deny warnings on that, only print the warnings to stdout with `--show-output`.

ACKs for top commit:
  apoelstra:
    ACK 9fc2c2a5b1 successfully ran local tests
  tcharding:
    ACK 9fc2c2a5b1

Tree-SHA512: 776177a903a6d2dcd25ba5cc8ef0db5f38d2cdb061975d3f7cc179369796af17f70e9dfe8bb4f49ff0c6df85d7e7c9d5b1d1d386a7508eee6b7dec17b7093d80
2024-09-17 11:55:32 +00:00
Shing Him Ng 5b4e81b379 Implement Arbitrary for Transaction 2024-09-16 22:02:46 -05:00
Peter Ryszkiewicz d942882b36 Document the magic bytes for witness commitment 2024-09-16 21:02:29 -05:00
merge-script 85fa0ce28d
Merge rust-bitcoin/rust-bitcoin#3357: Support `impl AsRef<[u8]>` in `signed_msg_hash`
70ccd6b629 signed_msg_hash takes impl AsRef<[u8]> (Liam Aharon)

Pull request description:

  Closes #3249

  Seems not required for Kixunil's immediate use case anymore, but figured I'd open a PR anyways.

ACKs for top commit:
  apoelstra:
    ACK 70ccd6b629 successfully ran local tests
  Kixunil:
    ACK 70ccd6b629

Tree-SHA512: b15bafd1af0749cbac36652a4e4c9ab3f91b3980bb6c3cbc1c977d339810fc15075af5d4bc6b210de364bedc496ce3759eaf4539e40d215ab66a122ba69dd56a
2024-09-15 14:56:07 +00:00
Fmt Bot 7990724ff4 2024-09-15 automated rustfmt nightly 2024-09-15 01:19:55 +00:00
Nadav Ivgi 8ec3571d80
Implement GetKey for Vec<Xpriv> 2024-09-14 11:45:07 +03:00
Jiri Jakes 9fc2c2a5b1
Improve documentation test in merkle_tree
Removes unnecessary usage of vector and adds a missing assert.
2024-09-14 15:50:56 +08:00
Liam Aharon 25d906d936
Use UFCS in macros 2024-09-14 09:46:57 +02:00
Liam Aharon 70ccd6b629
signed_msg_hash takes impl AsRef<[u8]> 2024-09-14 00:19:05 +02:00
merge-script 60e15b8007
Merge rust-bitcoin/rust-bitcoin#3194: priority: Prepare moving script types to `primitives`
8f2f4cbb3c Re-order optional dependencies (Tobin C. Harding)
95f2a8dab6 Do not access ScriptBuf inner from builder (Tobin C. Harding)
900af453ff Stop accessing inner ScriptBuf field when encoding (Tobin C. Harding)
8b82363d97 Use Script::as_bytes instead of inner when indexing (Tobin C. Harding)
b0675a4a4f Use Script::len instead of inner field (Tobin C. Harding)
374c6118dc Deprecate Script::fmt_asm and to_asm_str (Tobin C. Harding)

Pull request description:

  Move the `Script` and `ScriptBuf` types to `primitives`. There were still a few preparations required, things we had missed while creating the extension traits.

  Note also please, in the last patch, we enable `hex` from the `serder` feature. This is not the final state we want but like we did for `alloc` it is a step to reduce the size of the diff.

ACKs for top commit:
  Kixunil:
    ACK 8f2f4cbb3c
  apoelstra:
    ACK 8f2f4cbb3c successfully ran local tests

Tree-SHA512: 62a5f3c253ecb54d95c37fdc7eb955f3952909dc3bca20444b85c44665f54d5a0c48daf729bed0dd60ff3e9571b41deed039984c8b757b075ac6e136cacd17d7
2024-09-13 16:58:59 +00:00
Nadav Ivgi b593c886e3
Support GetKey where the Xpriv is a direct child of the looked up KeySource 2024-09-13 14:49:54 +03:00
Nadav Ivgi 055aa9d4dc
Refactor GetKey to take the KeyRequest by reference
To avoid cloning when looking it up in sets.
2024-09-13 14:49:54 +03:00
Nadav Ivgi d15c57bd1f
Refactor GetKey for sets to internally use Xpriv::get_key() 2024-09-13 14:49:46 +03:00
Nadav Ivgi d25c62bf45
Fix GetKey for sets to properly compare the fingerprint 2024-09-13 11:28:17 +03:00
merge-script 0c2737f25a
Merge rust-bitcoin/rust-bitcoin#3288: priority: Move txid hash types to `primitives`
0403e52ce3 Move the transaction hash types over to primitives (Tobin C. Harding)
7e454d756d Define extension traits for txid types (Tobin C. Harding)
832b726d03 Stop using all_zeros (Tobin C. Harding)
d69c241b5c Improve docs on associated consts (Tobin C. Harding)
68c9e28165 Do not use private constructor for txid hash types (Tobin C. Harding)
98328b5a7b Use as_byte_array to encode hash type (Tobin C. Harding)

Pull request description:

  Move the `Txid` and `Wtxid` hash wrapper types over to `primitives`. This introduces to `primitves` an unconditional dependency on `hashes`.

ACKs for top commit:
  Kixunil:
    ACK 0403e52ce3
  apoelstra:
    ACK 0403e52ce3 successfully ran local tests

Tree-SHA512: d14fa95bc12c2399d30d4d640b5a3fce625d51adf587a8037158f7d7e7b6288170b2d4418ca2cb68f612086ea0bdd0fae3b577f84f0d60627072fdb2217a6531
2024-09-13 03:31:00 +00:00
merge-script eacdd69688
Merge rust-bitcoin/rust-bitcoin#3344: Deprecate `OutPoint::new` constructor
e064686397 Deprecate OutPoint::new constructor (Tobin C. Harding)

Pull request description:

  The `OutPoint` type has two public fields, providing a `new` constructor that just sets these two fields adds no value.

  Done after discussion in #3340 as part of `primitives` work.

ACKs for top commit:
  Kixunil:
    ACK e064686397
  apoelstra:
    ACK e064686397 successfully ran local tests

Tree-SHA512: d80cac85093946b3678883f4e3ad7fd2052d858dbd3fab4127916b0eb6153999d30827c84ccf8c6e4412fbc3842bd127a2e4d3ca0248d2307d0bca467a2555ce
2024-09-12 17:43:48 +00:00
merge-script e027be6457
Merge rust-bitcoin/rust-bitcoin#3350: Stop using deprecated `OutPoint` functions
f811e0adb6 Stop using deprecated OutPoint functions (Tobin C. Harding)

Pull request description:

  Either our CI is failing us or `deprecated` does not work as expected, either way we should not be using the `OutPoint::null()` or `is_null` functions any more because we deprecated them already.

ACKs for top commit:
  Kixunil:
    ACK f811e0adb6
  apoelstra:
    ACK f811e0adb6 successfully ran local tests

Tree-SHA512: 64e75601ce8062da78bbd9aea97792e35c728aa31255348413fe9b57ee585974370d3f59b5467d7e4a65dc05d2718aea18bb381e128c8410fbfde4a851006416
2024-09-12 16:32:50 +00:00
Tobin C. Harding e064686397
Deprecate OutPoint::new constructor
The `OutPoint` type has two public fields, providing a `new` constructor
that just sets these two fields adds no value.
2024-09-12 11:44:27 +10:00
Tobin C. Harding 0403e52ce3
Move the transaction hash types over to primitives
Move the `Txid` and `Wtxid` hash wrapper types over to `primitives`.
This introduces to `primitves` an unconditional dependency on
`hashes`.
2024-09-11 11:02:32 +10:00
Tobin C. Harding 95f2a8dab6
Do not access ScriptBuf inner from builder
The `Builder` is staying in `bitcoin` while the `ScriptBuf` is moving to
`primitives`, so we cannot access the inner field of `ScriptBuf`.

Use the new `as_byte_vec` hack to mutate the inner `ScriptBuf` field.
2024-09-11 10:50:18 +10:00
Tobin C. Harding 900af453ff
Stop accessing inner ScriptBuf field when encoding
In preparation for moving the `ScriptBuf` type to `primitives` stop
accessing the inner field when encoding/decoding, use `as_script`
and `from_bytes` instead.
2024-09-11 10:50:18 +10:00
Tobin C. Harding 8b82363d97
Use Script::as_bytes instead of inner when indexing
In preparation for moving the `Script` type to `primitives` stop
accessing the inner field before doing slice operations, use `as_bytes`
to first get at the slice.
2024-09-11 10:50:18 +10:00
Tobin C. Harding b0675a4a4f
Use Script::len instead of inner field
In preparation for moving the `Script` type to `primitives` stop
accessing the inner field to get the length, call `len` directly.
2024-09-11 10:50:18 +10:00
Tobin C. Harding 374c6118dc
Deprecate Script::fmt_asm and to_asm_str
The `Script::fmt_asm` function is a legacy from days yore before
`Display` printed asm. We no longer need it.

Deprecate `Script::fmt_asm` and use the private `bytes_to_asm_fmt` or
`Display` impls.
2024-09-11 10:50:17 +10:00
Tobin C. Harding 7e454d756d
Define extension traits for txid types
Use the `define_extension_trait` macro to define two extension traits
for the two txid types. Each trait holds the deprecated `all_zeros`
function. There are no users of this trait in the code base.
2024-09-11 10:44:29 +10:00
Tobin C. Harding 832b726d03
Stop using all_zeros
Recently we deprecated the `all_zeros` functions on `Wtxid` and
`Txid` but for some reason our usage of them is not triggering a lint
warning.

Note please that this changes logic slightly, for example by using an
array of `0xFF` bytes instead of all zeros. Done in an effort to make it
even more obvious that the value is a dummy value and not mix it up with
the all zeros being used for coinbase thing.
2024-09-11 10:39:49 +10:00
Tobin C. Harding d69c241b5c
Improve docs on associated consts
In #3308 we added associated consts to the `Txid`, `Wtxid`, and
`OutPoint` types. During review and afterwards we realised the docs
could do with improving. Since we now want to move the types we should
do this first.

Close: #3331
2024-09-11 10:34:45 +10:00
Tobin C. Harding 68c9e28165
Do not use private constructor for txid hash types
In preparation for moving the txid hash types to `primitives` stop using
private constructors and cast the hash types as is typical.
2024-09-11 10:34:44 +10:00
Tobin C. Harding 98328b5a7b
Use as_byte_array to encode hash type
Instead of accessing the inner type of a hash wrapper type when
consensus encoding we can call `as_byte_array()`.

Done in preparation for moving `Txid` and `Wtxid` to `primitives`.

Internal change only.
2024-09-11 10:34:44 +10:00
merge-script 060ad58620
Merge rust-bitcoin/rust-bitcoin#3259: priority: Introduce and use a new `compact_size` module
d65de7c7de Introduce and use new compact_size module (Tobin C. Harding)

Pull request description:

  We would like to move the witness module to `primitives` but there is a bunch of usage of `VarInt`.

  Introduce a module that does the encoding and decoding instead, note that while the functionality is internal decoding returns an error which may one day end up in the public API. So put the module in `primitives` and make it public.

  Adds the module to `primitives`, adds a public `MAX_ENCODABLE_SIZE` variable that is commented with an issue link.

  https://github.com/rust-bitcoin/rust-bitcoin/issues/3264

ACKs for top commit:
  apoelstra:
    ACK d65de7c7de successfully ran local tests
  Kixunil:
    ACK d65de7c7de

Tree-SHA512: d9483c29b2b324e27460564a23f4639dde4037e6e773f4356216b02ebdea893a6361c342002b8e93a54de47b71ac69369431554f8cd0a2522fc451bf8493c81c
2024-09-10 13:27:24 +00:00
Tobin C. Harding f811e0adb6
Stop using deprecated OutPoint functions
Either our CI is failing us or `deprecated` does not work as expected,
either way we should not be using the `OutPoint::null()` or `is_null`
functions any more because we deprecated them already.
2024-09-10 09:32:06 +10:00
merge-script 08710d81a6
Merge rust-bitcoin/rust-bitcoin#3324: Add tests for witness_program
733505148c Add tests for witness_program (Shing Him Ng)

Pull request description:

  Add tests for witness_program

ACKs for top commit:
  tcharding:
    ACK 733505148c
  Kixunil:
    ACK 733505148c
  apoelstra:
    ACK 733505148c successfully ran local tests

Tree-SHA512: c1ee82edf22c7b39bc110d03836ba5ebfa785a63185a75c1a61781180ff907c1ea0c491c963629450f360152766845dacedccc6cf56bc3d8c607e66281427dd3
2024-09-09 20:28:24 +00:00
merge-script 9797e50ab5
Merge rust-bitcoin/rust-bitcoin#3310: Shoosh linter in bench build
fe46225ed0 Allow unused imports when running bench code (Tobin C. Harding)
eb67e873e0 Allow unused variables in release mode (Tobin C. Harding)

Pull request description:

  Two patches to clear the million warnings when running the bench code.

ACKs for top commit:
  apoelstra:
    ACK fe46225ed0 successfully ran local tests; though in the first commit you could also use `cfg_attr` FWIW
  Kixunil:
    ACK fe46225ed0

Tree-SHA512: 3f705e0441d8c0e41e9ceb5473572810ff2513f7e5531c1b7889418a3a85ac8622e50e271c7a3b5c386fb3f5629b85d4bd79739c4a02b51d58da86890721d8d2
2024-09-09 17:56:45 +00:00
merge-script cfe6c0a999
Merge rust-bitcoin/rust-bitcoin#3293: priority: Re-write (and re-name) `read_uint_iter`
0f897f80a5 Re-write (and re-name) read_uint_iter (Tobin C. Harding)

Pull request description:

  The `UintError` type (returned by `read_uint_iter`) is not that useful because one variant is unreachable. Re-write the function by doing:n

  - Re-write the function to reduce the error cases returned.
  - Re-name it to `read_push_data_len`
  - Move it to `internals`
  - Use `PushDataLenLen` enum instead of an int parameter

ACKs for top commit:
  apoelstra:
    ACK 0f897f80a5 successfully ran local tests; lol so much better than the old code
  Kixunil:
    ACK 0f897f80a5

Tree-SHA512: 095017a32c2d5bb2268cb1a059d0022e122faf8b41295f14970e7968374dd1c35c3b95357aba5aabaa17843439aebc237000009015ea9b8bc58ab1b337e8e1bc
2024-09-09 16:37:30 +00:00
merge-script 929eaf23d4
Merge rust-bitcoin/rust-bitcoin#3308: priority: Add coinbase associated consts
ea2efc155e Add coinbase associated consts (Tobin C. Harding)

Pull request description:

  Currently we have `all_zeros` functions and `null` functions but we can do better.

  Add associated consts and improve the names to better describe what these dummy zero hashes are used for.

  Deprecate related functions.

ACKs for top commit:
  Kixunil:
    ACK ea2efc155e
  apoelstra:
    ACK ea2efc155e successfully ran local tests

Tree-SHA512: bc7e840622a558bc46798e3606452ad24c16b7d23e7fe7a68fdf8a719326eb9d6d872ec1647620506f1de76b8086ae36cce0e1399e55e50bbd794efb8b4dda47
2024-09-09 16:01:43 +00:00
Tobin C. Harding 0f897f80a5
Re-write (and re-name) read_uint_iter
The `UintError` type (returned by `read_uint_iter`) is not that useful
because one variant is unreachable. Re-write the function by doing:n

- Re-write the function to reduce the error cases returned.
- Re-name it to `read_push_data_len`
- Move it to `internals`
- Use `PushDataLenLen` enum instead of an int parameter
2024-09-09 06:31:39 +10:00
Shing Him Ng 733505148c Add tests for witness_program 2024-09-08 13:58:30 -05:00
Fmt Bot 9a5ba9b6df 2024-09-08 automated rustfmt nightly 2024-09-08 01:17:12 +00:00
merge-script 9afeb711e4
Merge rust-bitcoin/rust-bitcoin#3301: Deprecate `from_slice` methods in favor of arrays
c00afe8d52 Change MessageSignatureError to secp256k1::Error (Jamil Lambert, PhD)
a20d0bc4eb Deprecate `from_slice()` in sha256.rs (Jamil Lambert, PhD)
089043546f Deprecate `from_slice` methods in favor of arrays (Jamil Lambert, PhD)

Pull request description:

  As brought up in issue #3102 support for Rust arrays is now much better so slice-accepting methods that require a fixed length can be replaced with a method that accepts an array.

  `from_slice()` methods that require a fixed length have been deprecated and where needed a `from_byte_array()` method created that accepts an array.

  There are still `from_slice` methods that rely on changes to external crates before they can be changed to arrays.

ACKs for top commit:
  apoelstra:
    ACK c00afe8d52 successfully ran local tests
  tcharding:
    ACK c00afe8d52
  Kixunil:
    ACK c00afe8d52

Tree-SHA512: c505b78d5ca57e7e1004df6761ac6760d5d9b63c93edc6ac1f9adf047bc67011883575f835b06f6d35d7f6c2b6a4c6c7f0a82a3f0e293bfb4ef58123b75d3809
2024-09-06 22:33:19 +00:00
merge-script c63695ac1e
Merge rust-bitcoin/rust-bitcoin#3286: Add additional docs to Witness
333c8ab297 Add additional docs to Witness (Tobin C. Harding)

Pull request description:

  The `Witness` struct is non-trivial, in particular it is not immediately obvious where and when the compact size encode value for each witness element is stored.

  Make an effort to improve the docs on `Witness` in relation to the compact size encoded length of each witness element.

ACKs for top commit:
  apoelstra:
    ACK 333c8ab297 successfully ran local tests
  Kixunil:
    ACK 333c8ab297

Tree-SHA512: 1c61a9ad071c035d5ad2e54446120d29ebf8cc4a779c96f04eda825890687dcbd53accc17522f57ef4ffb226eb1d85c6a3a115f27bebcfc7ad3c677033a8a414
2024-09-06 14:28:04 +00:00
Jamil Lambert, PhD c00afe8d52
Change MessageSignatureError to secp256k1::Error
`from_byte_array` cannot error due to InvalidLength so the returned
MessageSignatureError has been changed to return a secp256k1::Error,
which is the only error type returned by the function.
2024-09-06 12:49:11 +01:00
Jamil Lambert, PhD 089043546f
Deprecate `from_slice` methods in favor of arrays
Support for Rust arrays is now much better so slice-accepting methods
that require a fixed length can be replaced with a method that accepts
an array.

`from_slice()` has been deprecated and replaced with `from_byte_array()`
2024-09-06 12:33:51 +01:00
Tobin C. Harding ea2efc155e
Add coinbase associated consts
Currently we have `all_zeros` functions and `null` functions but we can
do better.

Add associated consts and improve the names to better describe what
these dummy zero hashes are used for.

Deprecate related functions.
2024-09-06 09:04:56 +10:00
Tobin C. Harding fe46225ed0
Allow unused imports when running bench code
Running the bench code results in a million warnings, instead of
solving these just allow unused imports as we do for fuzz code.
2024-09-05 12:46:13 +10:00
Tobin C. Harding eb67e873e0
Allow unused variables in release mode
In release mode we have a few unused variable warnings, lets just
allow them.

Found when running bench code, interestingly `cargo bench` must build
in release mode.
2024-09-05 12:46:13 +10:00
Tobin C. Harding d65de7c7de
Introduce and use new compact_size module
We would like to move the witness module to `primitives` but there is
a bunch of usage of `VarInt`.

Introduce a module that does the encoding and decoding instead, this
code is internal so put it in `internals`.

Note we add an unused public `MAX_ENCODABLE_SIZE` variable that is
commented with an issue link. Done like this because its quite
important that we see to it and it makes it clear that we are not and
we know about it.

 https://github.com/rust-bitcoin/rust-bitcoin/issues/3264
2024-09-05 09:49:56 +10:00
Tobin C. Harding 333c8ab297
Add additional docs to Witness
The `Witness` struct is non-trivial, in particular it is not immediately
obvious where and when the compact size encode value for each witness
element is stored.

Make an effort to improve the docs on `Witness` in relation to the
compact size encoded length of each witness element.
2024-09-05 07:42:41 +10:00
merge-script f6287fb445
Merge rust-bitcoin/rust-bitcoin#3287: Move `transaction::Version` to `primitives`
c48d9d6523 Move transaction::Version to primitives (Tobin C. Harding)
f490222068 Introduce the VersionExt trait (Tobin C. Harding)
fb89974b82 Run the formatter (Tobin C. Harding)
bb3a3ecbaa Introduce temporary module for Version (Tobin C. Harding)
1fde868f51 Separate Version impl blocks (Tobin C. Harding)

Pull request description:

  As per title, in tiny small chunks, move the `transaction::Version` over to `primitives`. Only the type, its associated consts, and its `Display` impl are moved. The two methods are left in an extension trait.

  Was originally attempted in #3253

ACKs for top commit:
  Kixunil:
    ACK c48d9d6523
  apoelstra:
    ACK c48d9d6523 successfully ran local tests

Tree-SHA512: 83415cf0762dca5c263deb743734fc7abede804a6daac31df3d0101b51c6261e6d54452eb744727ae680cacce9e4ef726a6fa253d86c4e7a5d8ec789b137566c
2024-09-04 01:21:43 +00:00
Tobin C. Harding c48d9d6523
Move transaction::Version to primitives
We would like to move the `Transaction` type to `primitives`, as a step
towards this move the `transaction::Version` and its trait imps (just
`Display`) over there.
2024-09-03 11:01:29 +10:00
Tobin C. Harding f490222068
Introduce the VersionExt trait
In preparation for moving the `transaction::Version` type to
`primitives`; add a `VersionExt` trait using our macro.
2024-09-03 10:57:58 +10:00
Tobin C. Harding fb89974b82
Run the formatter
Run `just fmt`. No manual changes.
2024-09-03 10:56:59 +10:00
Tobin C. Harding bb3a3ecbaa
Introduce temporary module for Version
In preparation for using the formatter to indent add a temporary module.
Done as part of adding an extension trait.
2024-09-03 10:56:06 +10:00
Tobin C. Harding 1fde868f51
Separate Version impl blocks
In preparation for adding an extension trait; separate the
`transaction::Version` impl blocks into stuff that will stay here and
stuff that will go to `primitives`.

Refactor only, no logic changes.
2024-09-03 10:54:48 +10:00
merge-script 3b057ad2f5
Merge rust-bitcoin/rust-bitcoin#3150: Create a macro that implements `to_hex` for types that have `core::fmt::LowerHex` implemented
30bb93c676 Implement impl_to_hex_from_lower_hex macro for types that implement fmt::LowerHex (Shing Him Ng)

Pull request description:

  Created a macro that implements `to_hex` for types that currently have `core::fmt::LowerHex` and called it on types that have `core::fmt::LowerHex` implemented. I put the macro in the `internals` crate since there are types across the whole project that can potentially use this.

  Resolves #2869

ACKs for top commit:
  Kixunil:
    ACK 30bb93c676
  apoelstra:
    ACK 30bb93c676 successfully ran local tests

Tree-SHA512: d3ebc7b5c0c23f1a8f8eef4379c1b475e8c23845e18ce514cb1e98eb63fc4f215e6bc4425f97c7303053df13374ef931ae9d9373badd7ca1975a55b0d00d0e40
2024-09-02 21:18:44 +00:00
merge-script 3bbe821ce5
Merge rust-bitcoin/rust-bitcoin#3268: Fix clippy rustdocs warnings
b6371b5801 Fix clippy rustdocs warnings (Tobin C. Harding)

Pull request description:

  A new nightly version (`nightly-2024-08-28`) introduces a few warnings because of our rustdocs. These are valid warnings and should be fixed, thanks `clippy` team.

  (The `bip152` change is a bit sloppy, open to suggestions.)

ACKs for top commit:
  apoelstra:
    ACK b6371b5801 successfully ran local tests
  Kixunil:
    ACK b6371b5801

Tree-SHA512: 503fb9d48772b74a5acdb26c0f77a85c52323c03360f983204fccee0f28bedeff142237b067caa1ce6ea04ea9842cc493e0d06dc141ca00a98151fa002b62392
2024-09-02 14:20:44 +00:00
merge-script 6a06c023e0
Merge rust-bitcoin/rust-bitcoin#3276: fix: deprecate wrong and unused max script num
345d3daa72 fix: deprecate wrong and unused max script num (ChrisCho-H)

Pull request description:

  ~~Script number can be up to 2^39 - 1 to encode locktime.~~
  ~~If it's only for the integer operation besides locktime, it must be 2^31 - 1, not 2^31.~~

  I agree with apoelstra  opinion to deprecate this value.

ACKs for top commit:
  apoelstra:
    ACK 345d3daa72 successfully ran local tests
  Kixunil:
    ACK 345d3daa72

Tree-SHA512: df4205415634d8db1412f16a75d1dbc110ed69930cefa085bdaa268a7fcaf47776dd1d8ed6965e6bbb476825b5f80e5c00bb375730ad102bae8868abe5894068
2024-09-02 00:01:32 +00:00
ChrisCho-H 345d3daa72 fix: deprecate wrong and unused max script num 2024-09-01 22:14:39 +09:00
merge-script 0a3f5330a8
Merge rust-bitcoin/rust-bitcoin#3279: Automated nightly rustfmt (2024-09-01)
fa71b0e044 2024-09-01 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 fa71b0e044 successfully ran local tests; a bit big this week

Tree-SHA512: 875e8d54a06a22ccbef690a21f9d48a0acc1e6ea073a2d16632dd7ce63400cc7f2522339b7cb808653dade407517cd03d4fece4c8dec24abacf5303418aeaa75
2024-09-01 13:03:01 +00:00
Shing Him Ng 30bb93c676 Implement impl_to_hex_from_lower_hex macro for types that implement fmt::LowerHex 2024-08-31 22:41:40 -05:00
merge-script d1e7116321
Merge rust-bitcoin/rust-bitcoin#3278: Show compressed public key in Debug for CompressedPublicKey
9db6234ea9 Show compressed public key in Debug for CompressedPublicKey (Jiri Jakes)

Pull request description:

  Currently `CompressedPublicKey` debug produces output of form:

  ```
  CompressedPublicKey(PublicKey(2f8b18dc0adcb73d75f7934d9523ea7347083e41c48115398cb37e295a0a6ffe86e0bf8b1ef65888c880c8d8813a30e69e466380cbe2daec18f3ed1e7a553ff2))
  ```

  Although it shows real internal structure together with inner uncompressed public key, it is not too helpful for the purpose of debugging _compressed_ public key.

  After this patch, `Debug` output will be equal to `Display` (it, in fact, delegates rendering to `Display`), prepended by the name of the struct:

  ```
  CompressedPublicKey(02fe6f0a5a297eb38c391581c4413e084773ea23954d93f7753db7dc0adc188b2f)
  ```

ACKs for top commit:
  Kixunil:
    ACK 9db6234ea9 unless other maintainers agree to drop the struct name.
  apoelstra:
    ACK 9db6234ea9 successfully ran local tests; we should keep the struct name since it is canonical for Debug output

Tree-SHA512: e2626678a4144357d3ff4ddd888459ae1283ea50bc7c8ef86deb9902d7d543de79109ac42883e5538f7e2f6a29426224bb43eb44a34f366821e47a9aca563753
2024-09-01 01:30:30 +00:00
Fmt Bot fa71b0e044 2024-09-01 automated rustfmt nightly 2024-09-01 01:22:04 +00:00
merge-script 753961fdb8
Merge rust-bitcoin/rust-bitcoin#3215: Improve const_assert
3c7c8c44b6 Improve const_assert (Tobin C. Harding)

Pull request description:

  Now that we can panic in const context we can improve the `const_assert` macro by adding a message string.

  Original idea by Kix:

    https://github.com/rust-bitcoin/rust-bitcoin/pull/2972#discussion_r1726328228

ACKs for top commit:
  Kixunil:
    ACK 3c7c8c44b6 in the sense that it does what it's supposed to with the only tiny issue being that the `bool` looks weird but not broken in any other way I can think of.
  apoelstra:
    ACK 3c7c8c44b6 successfully ran local tests

Tree-SHA512: 5ff721c0056f87d42c934818da6f780cd945f235291eb4b044752d67405a74f992d7f85853fec129e794ec3fcda1f319cc40daabc6a349d21bbdc977640d2572
2024-08-31 20:00:28 +00:00
merge-script 2a54c1c1f2
Merge rust-bitcoin/rust-bitcoin#3271: Move import inside feature gate
a184066660 Move import inside feature gate (Tobin C. Harding)

Pull request description:

  The `String` type is only used if the `serde` feature is enabled, move the import statement inside the already feature gated block.

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

Tree-SHA512: 688adb1e4489b0242856ac36ed9cf3503b147b84954cf1e6fd34b5e708ed6c1dd92da91e739243face1c1b4e4c8b19ce88e215117bef2a7af2e732859a6e108a
2024-08-31 18:22:51 +00:00
Jiri Jakes 9db6234ea9
Show compressed public key in Debug for CompressedPublicKey 2024-08-31 22:41:12 +08:00
merge-script 0d9e8f8c99
Merge rust-bitcoin/rust-bitcoin#3204: Do many cleanups (and bug fix)
dae42bef9d do not enable bitcoin-io by default (Antoni Spaanderman)
a14cdaf859 don't enable std by default when testing (Antoni Spaanderman)
e83830dcfc use slice instead of array to not have to hardcode the length (Antoni Spaanderman)
55749d6f61 use `hash.to_byte_array` to check equality with `test.output` (Antoni Spaanderman)
969864e3b0 use fixed size array if possible, otherwise `&'static [u8]` (Antoni Spaanderman)
28ccf70fa6 remove unnecesarry borrow operator (`&`) (Antoni Spaanderman)
fa3a3afd02 remove unnecessary slicing (Antoni Spaanderman)
22e42ab86c fix test code being unnecessarily feature gated (Antoni Spaanderman)

Pull request description:

  - remove 2 unnecessary cfg attributes from tests left over from  #3167 (it made them not dependent on `alloc` anymore)
  - simplify assertion logic by removing unnecessary conversions before comparing
  - make tests `no_std` compatible by adding imports to alloc or std
  - feature gate tests behind the `alloc` feature if they use anything from the alloc crate (like the `format!` macro)
  - `schemars` feature enables `alloc` because (for example) its trait wants implementations to return `String`
  - fix `bitcoin-io` always enabling when `std` is enabled (only useful if people depend on `hashes` only, `bitcoin` depends on `bitcoin-io` already)

ACKs for top commit:
  tcharding:
    ACK dae42bef9d
  Kixunil:
    ACK dae42bef9d
  apoelstra:
    ACK dae42bef9d successfully ran local tests

Tree-SHA512: 622fd4963ef21530a98af89bcfc71abe8723aac12d363ab88d9bd30dcf2f75392711bec10e2901fab5f1a30e11897d1aae36e22892738aa1e5670166f91fddd4
2024-08-29 21:20:10 +00:00
Tobin C. Harding 3c7c8c44b6
Improve const_assert
Now that we can panic in const context we can improve the `const_assert`
macro by adding a message string.

Original idea by Kix:

  https://github.com/rust-bitcoin/rust-bitcoin/pull/2972#discussion_r1726328228
2024-08-30 06:33:31 +10:00
Tobin C. Harding b6371b5801
Fix clippy rustdocs warnings
A new nightly version (`nightly-2024-08-28`) introduces a few warnings
because of our rustdocs. These are valid warnings and should be fixed,
thanks `clippy` team.

(The `bip152` change is a bit sloppy, open to suggestions.)
2024-08-30 05:47:31 +10:00
Tobin C. Harding a184066660
Move import inside feature gate
The `String` type is only used if the `serde` feature is enabled, move
the import statement inside the already feature gated block.
2024-08-30 05:25:51 +10:00
merge-script 98252f36df
Merge rust-bitcoin/rust-bitcoin#3255: fix: re-implement `Psbt` (de)serialization from/to readers/writers
cf129ad314 fix: re-implement (de)serialization from/to readers/writers (elsirion)

Pull request description:

  Fixes #3250.

  The serialization is less than ideal and still allocates a lot. I can understand not wanting to (ab)use the consensus encoding traits, but they have a pretty good interface, copying it and creating some `EncodePsbt` and `DecodePsbt` traits with similar interfaces would have been nice imo.

ACKs for top commit:
  Kixunil:
    ACK cf129ad314
  apoelstra:
    ACK cf129ad314 successfully ran local tests; LGTM -- I believe this is non-breaking, as does cargo-semver-checks, so we can backport this to 0.32

Tree-SHA512: d7f218164d772db3a9fb4436953c3b5fd3677b92078d0843233197629df7d852d80615a3ff38c5b70771381ba1aeb30defdc98ee63653e570bb75dc553400cad
2024-08-28 22:26:07 +00:00
Jamil Lambert, PhD 9fce57b738
Change T::from_str(s) to s.parse::<T>() in tests
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in tests.
2024-08-28 16:13:03 +01:00
Jamil Lambert, PhD 4ad86148c7
Change Address::from_str(s) to s.parse
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed for parsing a string as an `Address`.
2024-08-28 13:50:43 +01:00
Jamil Lambert, PhD c835bb9eab
Change T::from_str(s) to s.parse::<T>() in docs
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in docs.
2024-08-28 13:50:42 +01:00
merge-script e048b7b003
Merge rust-bitcoin/rust-bitcoin#3256: Change `T::from_str(s)` to `s.parse::<T>()`
a76d76eca1 Change `T::from_str(s)` to `s.parse::<T>()` (Jamil Lambert, PhD)

Pull request description:

  As mentioned in issue #3234 `s.parse::<T>()` is more idiomatic and produces more helpful error messages.

  This has been changed in the main codebase, not including examples, rustdocs, and in the `test` modules.

  `use std::str::FromStr;` has been removed where this change makes it unnecessary.

  To close the issue it may also need to be changed in the examples and the `test` modules and `contributing.md` updated.

ACKs for top commit:
  Kixunil:
    ACK a76d76eca1
  storopoli:
    ACK a76d76eca1
  tcharding:
    ACK a76d76eca1

Tree-SHA512: 18be389579b5c2d0ba567dfb02b9c8d71c108a749ec5168c60a50e7af9d7f498e364c8f4c0ce1698d960e7d146486899cd10214ebc9c96708ed158a04dfff425
2024-08-28 12:47:16 +00:00
Jamil Lambert, PhD a76d76eca1
Change `T::from_str(s)` to `s.parse::<T>()`
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in the main codebase, not including
examples, rustdocs, and in the test module.

`use std::str::FromStr;` has been removed where this change makes
it unnecessary.
2024-08-27 17:31:00 +01:00
elsirion cf129ad314
fix: re-implement (de)serialization from/to readers/writers 2024-08-27 11:49:34 +02:00
Tobin C. Harding b61adf7ca4
Introduce TxOutExt trait
In preparation for moving the `TxOut` type over to `primitives` ad an
extension trait as is becoming customary.
2024-08-27 18:44:00 +10:00
Tobin C. Harding 8b089ffe40
Run the formatter
Run `cargo +nightly fmt`, no manual changes.
2024-08-27 17:57:44 +10:00
Tobin C. Harding 02c7d504fa
Add tmp module around TxOut impl block
The two `TxOut` fields are public and there are no construtors or
getters to move, only the associated const `NULL`.

Add a tmp module around the big impl block so we can trick the formatter
into indenting before we add the extension trait.
2024-08-27 17:57:44 +10:00
Tobin C. Harding fa946796eb
Create a separate TxOut impl block
Everything except the associated const is going to stay here in
`bitcoin` when we move `TxOut` to `primitives`.
2024-08-27 17:57:41 +10:00
Antoni Spaanderman e83830dcfc
use slice instead of array to not have to hardcode the length 2024-08-26 17:22:09 +02:00
merge-script 1058cbb9f8
Merge rust-bitcoin/rust-bitcoin#3233: Automated nightly rustfmt (2024-08-25)
fbf7f41875 2024-08-25 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 fbf7f41875 successfully ran local tests

Tree-SHA512: 2612a860db6e900558d840d2bd9f1a1f6d7d5fff823b2686613aecad84268c2d0f9cb024d023f91d010652aecf0b7b7e36eb00c930a601121e61a30bfc5ddd35
2024-08-25 14:21:11 +00:00
merge-script 51af258eaa
Merge rust-bitcoin/rust-bitcoin#3015: Add Arbitrary
3e034d5ede Add Arbitrary dependency (yancy)

Pull request description:

  Adds an example draft showing what is needed to use Arbitrary for coin selection.

  Shot out to how nice Arbitrary is for fuzzing a target by taking unstructured randomness and creating structured rust-bitcoin types for fuzzing.  Is there a way we could add this to rust-bitcoin for structuring the fuzz data needed?

  This is then the example to fuzz test a SRD algo (after applying this PR to rust-bitcoin) using rust-bitcoin types :)

  ```
  #![no_main]

  use arbitrary::Arbitrary;
  use bitcoin::{Amount, FeeRate};
  use bitcoin_coin_selection::{select_coins_srd, WeightedUtxo};
  use libfuzzer_sys::fuzz_target;
  use rand::thread_rng;

  #[derive(Arbitrary, Debug)]
  pub struct Params {
      target: Amount,
      fee_rate: FeeRate,
      weighted_utxos: Vec<WeightedUtxo>,
  }

  fuzz_target!(|params: Params| {
      let Params { target: t, fee_rate: f, weighted_utxos: wu } = params;
      select_coins_srd(t, f, &wu, &mut thread_rng());
  });
  ```

ACKs for top commit:
  tcharding:
    ACK 3e034d5ede
  Kixunil:
    ACK 3e034d5ede
  apoelstra:
    ACK 3e034d5ede successfully ran local tests

Tree-SHA512: accd565815de3b37730d2ff12a24fcfc84e52ad357e5c940b1500a1e0bb17f4ff5fd6e52d31e8e96bb5290ee4fa050cfd2a9bbd6bbae13fc378f43093b64177f
2024-08-25 13:16:49 +00:00
Fmt Bot fbf7f41875 2024-08-25 automated rustfmt nightly 2024-08-25 01:14:09 +00:00
merge-script 18bdd92d34
Merge rust-bitcoin/rust-bitcoin#3216: ecdsa: Improve error types
9fb5edb39e ecdsa: Improve error types (Tobin C. Harding)

Pull request description:

  There are a couple of issues around the ECDSA signature decoding / parsing code. We have duplicate code in `from_str` and `from_slice` and both use the same error type even though it is impossible to get a hex error in `from_slice`.

  Create two errors:

  - A `DecodeError` returned by `from_slice`
  - A `FromStrError` that has a decode variant and a hex variant

  Call through to `from_slice` after parsing hex into a byte vector.

  Removes an instance of `unreachable!`.

  Fix: #1193

ACKs for top commit:
  Kixunil:
    ACK 9fb5edb39e
  apoelstra:
    ACK 9fb5edb39e successfully ran local tests

Tree-SHA512: 3b3ae31887d603f1739d261b491b99f7847987f94dbbfefb9aa84d4250736eba2d007d28746bbb064946d3055e4cca01510677bf2cdbb11bbf83d7388dbd2620
2024-08-24 17:52:08 +00:00
merge-script 837fc9c9c2
Merge rust-bitcoin/rust-bitcoin#2972: Use index size rather than pointer size to enforce convertibility of `u32` to `usize`
c427d8b213 bitcoin: Compile time assert on index size (Tobin C. Harding)
49a6acc1a0 internals: Remove double parenthesis in const_assert (Tobin C. Harding)
2300b285ef units: Remove compile time pointer width check (Tobin C. Harding)

Pull request description:

  3 patches in preparation for other size related work, this PR does not touch the `ToU64` issue which will be handled separately.

  - Patch 1: Don't check pointer width in `units` because its not consensus code
  - Patch 2: Modify internal macro `const_assert`
  - Patch 3: Use index size to enforce not building on a 16 bit machine

ACKs for top commit:
  Kixunil:
    ACK c427d8b213 though I think the last commit was kinda a waste of time and it should have been adding the trait instead or leave it for later.
  apoelstra:
    ACK c427d8b213 successfully ran local tests; unsure if we want to merg this or wait for #3215

Tree-SHA512: 823df5b6a5af3265bce2422c00d287f45816faeb5f965685650ac974a1bd441cf548e25ac2962591732ff221bee91a55703da936382eb166c014ca5d4129edf8
2024-08-24 14:16:31 +00:00