Commit Graph

119 Commits

Author SHA1 Message Date
Tobin C. Harding 9dce0b4b8c
Remove hex string trait bounds from GeneralHash
For the `hashes` crate we would like to make `hex` an optional
dependency. In preparation for doing so do the following:

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

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

Pull the serde stuff out of `hash_newtype` and provide a macro to
implement it `impl_serde_for_newtype`.
2024-11-13 12:28:47 +11:00
merge-script 4797a75586
Merge rust-bitcoin/rust-bitcoin#3584: Standardize constructor type function docs
1649b68589 Standardize wording to `constructs a new` (Jamil Lambert, PhD)
27f94d5540 Replace `creates` with `constructs` (Jamil Lambert, PhD)

Pull request description:

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

  These have all be standardized to the agreed version.

  Close #3575

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

Tree-SHA512: 0ed9b56819c95f1fc14da1e0fdbbe03c4af2d97a95ea6b56125f72913e8d832db5d2882d713ae139d00614e651f3834a4d72528bdf776231cceb6772bf2f9963
2024-11-06 16:05:24 +00:00
Jamil Lambert, PhD 1649b68589
Standardize wording to `constructs a new`
There is a range of different wordings used in the docs of constructor
type functions.

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

Replace all occurrences of `creates` with `constructs` in the first line
of docs of constructor like functions.
2024-11-05 12:47:28 +00:00
Tobin C. Harding 915c9627f6
Put feature gate attribute under test attribute
Either way is fine, just pick one and be uniform.
2024-11-05 11:16:23 +11:00
Fmt Bot 5ecf7f2d67 2024-11-03 automated rustfmt nightly 2024-11-03 01:21:14 +00:00
merge-script 112d75eed9
Merge rust-bitcoin/rust-bitcoin#3517: Remove `private_key_debug_is_obfuscated` test
7bff725494 Remove private_key_debug_is_obfuscated test (Diogo Canut)

Pull request description:

  Solves #3186.

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

Tree-SHA512: 68dcdc476ada7960eb224dd456010bc36a302472b8a8b4d1ea3ab9c6981550d7677a98516331d5e5ef636be0cdc7208c45278d70c506e04d067b997758fd4bcf
2024-11-01 14:20:46 +00:00
Diogo Canut 7bff725494 Remove private_key_debug_is_obfuscated test
The private_key_debug_is_obfuscated test is removed because it belongs
in the secp256k1 library, not in the bitcoin library. Keeping it here
is redundant and creates unnecessary maintenance.
2024-10-31 20:31:59 -03:00
Tobin C. Harding a51768af3f
key: Deprecate to_bytes
As we have been doing in other places deprecate `to_bytes` in favour of
`to_vec`. Note this is only for functions that return a `Vec`, the `key`
module has `CompressedKey::to_bytes` still as it returns an array.
2024-11-01 07:10:41 +11:00
Tobin C. Harding 3e2c43b19e
Elide more lifetimes
clippy found some more lifetimes to elide.
2024-10-28 15:22:13 +11:00
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
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
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
Jiri Jakes 9db6234ea9
Show compressed public key in Debug for CompressedPublicKey 2024-08-31 22:41:12 +08: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 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
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
merge-script 15b87606bf
Merge rust-bitcoin/rust-bitcoin#3100: Bump MSRV to 1.63
c72069e921 Bump MSRV to 1.63 (Martin Habovstiak)

Pull request description:

  The version 1.63 satisfies our requirements for MSRV and provides significant benefits so this commit bumps it. This commit also starts using some advantages of the new MSRV, namely namespaced features, weak dependencies and the ability to use trait bounds in `const` context.

  This however does not yet migrade the `rand-std` feature because that requires a release of `secp256k1` with the same kind of change - bumping MSRV to 1.63 and removing `rand-std` in favor of weak dependency. (Accompanying PR to secp256k1: https://github.com/rust-bitcoin/rust-secp256k1/pull/709 )

  Suggested plan:

  * merge both PRs
  * at some point release `hashes` and `secp256k`
  * remove `rand-std` from `bitcoin`
  * release the rest of the crates

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

Tree-SHA512: 0b301ef8145f01967318d3ed1c738d33e6cf9e44f835f3762122b460a536f926916dbd6ea39d6f80b4f95402cd845e924401e75427dbb0731ca5b12b4fa6915e
2024-07-28 21:11:28 +00:00
Martin Habovstiak c72069e921 Bump MSRV to 1.63
The version 1.63 satisfies our requirements for MSRV and provides
significant benefits so this commit bumps it. This commit also starts
using some advantages of the new MSRV, namely namespaced features, weak
dependencies and the ability to use trait bounds in `const` context.

This however does not yet migrade the `rand-std` feature because that
requires a release of `secp256k1` with the same kind of change - bumping
MSRV to 1.63 and removing `rand-std` in favor of weak dependency.
2024-07-27 07:24:32 +02:00
Tobin C. Harding feef34fdea
Make ScriptBuf::p2wpkh_script_code stand alone
We would like to move the `Script` type to `primitives` without moving
any key stuff, including pubkey hashes. We may later, before releasing
`primitives`, move the `WPubkeyHash` at which time this patch can be
reverted or re-implemented on `ScriptBuf`.

The `ScriptBuf::p2wpkh_script_code` function does not take `self` as a
parameter but it does return `Self` - this can trivially be made into a
standalone function.

Make `ScriptBuf::p2wpkh_script_code` a standalone function.
2024-07-23 11:24:31 -05:00
Fmt Bot 91382977fb 2024-07-07 automated rustfmt nightly 2024-07-07 01:10:59 +00:00
Jamil Lambert, PhD 175f69abeb Capitalize Merkle
Merkle is a proper noun and should be capitalized in docs and strings.

Capitalize all occurances of Merkle in docs and strings.
2024-07-01 17:42:43 +01:00
merge-script d36141b5a7
Merge rust-bitcoin/rust-bitcoin#2892: Remove wildcard imports
d099b9c195 Remove wildcard from prelude import (Jamil Lambert, PhD)

Pull request description:

  This patch replaces  `prelude::*` wildcard imports with the types actually used.  In a couple of cases `DisplayHex` was previously imported by the wildcard but was only used in the test module, an additional import was added to the test module instead of at the top where it causes an unused import warning.

  Close: #2875

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

Tree-SHA512: d59dfac0961d2649d509039a11c1b5574d81d05fef567a624cf15be2f587de796ea960ba5a08bef788199331c2f790fb06f7b393182538c7d8b1891ded119efc
2024-06-30 04:04:08 +00:00
merge-script 7ca7128b19
Merge rust-bitcoin/rust-bitcoin#2899: Pass sigs and associated types by value
dc10a49876 api: Run just check-api (Tobin C. Harding)
5e8f204581 Pass sigs and associated types by value (Tobin C. Harding)

Pull request description:

  We should pass `Copy` types by value not by reference.

  Currently this is not done in secp, but lets do it here in bitcoin.

  Pass by value:

  - `SerializedSignature`
  - bitcoin sigs
  - secp sigs
  - secp `Message`

  This is a continuation of the work to split up #2404 into manageable PRs.

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

Tree-SHA512: 8736eba067c74edb951c92357f5b3d0fc99c4fa6dc3beea579c10b3150873b74e8ec46c2c01f18818b37fca6e77c6b6edddeb6340edde6a9d8c28a4e69164c8c
2024-06-28 23:48:03 +00:00
Jamil Lambert, PhD d099b9c195 Remove wildcard from prelude import
Wildcards have been replaced with what is actually used.

In a couple of cases an additional use statement was added to the test
module to import `DisplayHex` which is only used in test, but
previously imported with the wildcard at the top.
2024-06-28 08:02:43 +01:00
Andrew Poelstra b8d85a1df0
bitcoin: remove all use of engine/from_engine on opaque hash types
In the next commits we are going to stop exposing the ability to hash
arbitrary data into wrapped hash types like Txid etc. In preparation for
this, stop using these methods internally.

This makes our internal code a little bit uglier and less DRY. An
alternative approach would be to implement the from_engine and engine
methods, but privately (and maybe having a macro to provide this). But I
think this approach is more straightforward.

The one exception is for the Taproot hashes, which are tagged hashes and
currently do not have their own engine type. I will address these in a
later PR because this one is already too big.
2024-06-24 13:57:03 +00:00
Tobin C. Harding 5e8f204581
Pass sigs and associated types by value
We should pass `Copy` types by value not by reference.

Currently this is not done in secp, but lets do it here in bitcoin.

Pass by value:

- `SerializedSignature`
- bitcoin sigs
- secp sigs
- secp `Message`
2024-06-24 10:02:55 +10:00
Tobin C. Harding a42bcdc22e
Remove usage of blockdata from paths
the `blockdata` directory is code organisation thing, all the
types/modules are re-exported from other places. In preparation for, and
to make easier, the `primitives` crate smashing work - remove all
explicit usage of `blockdata`.

Note that the few instances remain as they seem required e.g.,

  `pub(in crate::blockdata::script)`

Refactor only, no logic changes.
2024-06-20 12:00:22 +10:00
Andrew Poelstra 4defdb08fa
Merge rust-bitcoin/rust-bitcoin#2868: Pass keys by value
9f01871c11 api: Run just check-api (Tobin C. Harding)
7929b51640 Pass keys by value (Tobin C. Harding)

Pull request description:

  We should pass `Copy` types by value not by reference. Pass the key types by value.

  This is patch 1 from #2404

ACKs for top commit:
  apoelstra:
    ACK 9f01871c11 this will annoy some people but I think we should do it

Tree-SHA512: 18afab537edf4ade4dc1c1e5992e50060b8935531f1e3cbe1d3b94b2fcb87aafa39947f342e0e762835bda3b4091dd35b3b74ea79f4dbb3b21660ffd21d1f82e
2024-06-14 23:56:46 +00:00
Tobin C. Harding 7929b51640
Pass keys by value
We should pass `Copy` types by value not by reference. Pass the key
types by value.
2024-06-14 14:16:28 +10:00
Tobin C. Harding 6b7d02e5ae
Add inherent functions to hashes
Currently we have a trait `Hash` that is required for `Hmac`, `Hkdf`,
and other use cases. However, it is unegonomic for users who just want
to do a simple hash to have to import the trait.

Add inherent functions to all hash types including those created with
the new wrapper type macros.

This patch introduces some duplicate code but we are trying to make
progress in the hashes API re-write. We can come back and de-dublicate
later.

Includes making `to_byte_array`,`from_byte_array`, `as_byte_array`, and
`all_zeros` const where easily possible.
2024-06-14 10:17:00 +10:00
Jamil Lambert, PhD 5e7a638b4d fix crypto rustdocs
Fixed the rustdocs for some of the functions to be in third person, and small gramatical changes.
2024-06-06 16:54:02 +01:00
Jamil Lambert, PhD 4a9f74b55c fix missing fullstops in bitcoin rustdoc
Added missing fullstops to the rustdoc titles for everything on the main page of the bitcoin crate
2024-06-06 16:37:12 +01:00
Andrew Poelstra 45e0241267
doc: fix "lazy line continuations" in markdown
Rust nightly as of 2024-05-27 has a new lint which detects list items
which are continued by a non-indented line. Markdown treats these as
single list items, which they sometimes are, but sometimes we intended
them to be on a separate line.

Also changes the docs for `UntweakedKeypair::tap_tweak` because the
existing ones were overly technical and out-of-date.
2024-05-27 12:50:26 +00:00
Andrew Poelstra e96961f333
Merge rust-bitcoin/rust-bitcoin#2798: Standardize rustdoc subheadings
11bb1ff6ff Standardize function doc Safety, Returns and Parameters (jamil.lambert)
df83016c98 Standardize function doc Errors (jamil.lambert)
d219ceb68e Standardize function doc Examples (jamil.lambert)
233a9133d8 Standardize function doc Panics (jamil.lambert)

Pull request description:

  The subheadings in the rustdocs have been standardized according to [./CONTRIBUTING.md](https://github.com/rust-bitcoin/rust-bitcoin/blob/master/CONTRIBUTING.md):
  ```rust
  impl FooBar {
      /// Constructs a `FooBar` from a [`Baz`].
      ///
      /// # Errors
      ///
      /// Returns an error if `Baz` is not ...
      ///
      /// # Panics
      ///
      /// If the `Baz`, converted to a `usize`, is out of bounds.
      pub fn from_baz(baz: Baz) -> Result<Self, Error> {
          ...
      }
  }
  ```

ACKs for top commit:
  apoelstra:
    ACK 11bb1ff6ff
  tcharding:
    ACK 11bb1ff6ff

Tree-SHA512: 163af3cd1cfb47cea3e55eddeaeb6843ff7ec89c57354e3247d6bae85e756b183e8045c2555cfcf87e8c23c1388ff9d7592cfb6a951a37a9ec41d27263e5a2e4
2024-05-25 14:55:16 +00:00
jamil.lambert 11bb1ff6ff Standardize function doc Safety, Returns and Parameters
Changed the function docs to have the same format of
///
/// # Safety
///
/// description
2024-05-24 09:59:42 +01:00
jamil.lambert d219ceb68e Standardize function doc Examples
Changed the function docs to have the same format of
///
/// # Examples
///
/// description
2024-05-24 09:59:42 +01:00
Andrew Poelstra 814786b0a6
crypto: enable and fix accidentally disabled unit test 2024-05-21 17:17:48 +00:00
Fmt Bot a565db9fdd 2024-03-31 automated rustfmt nightly 2024-03-31 01:03:18 +00:00
Tobin C. Harding 4f68e79da0
bitcoin: Stop using base58 errors
We are currently using the `base58::Error` type to create errors in
`bitcoin`, these are bitcoin errors not `base58` errors.

Note that we add what looks like duplicate
`InvalidBase58PayloadLengthError` types but they are different because
of the expected length. This could have been a field but I elected not
to do so for two reasons:

1. We will need to do so anyways if we crate smash more
2. The `crypto::key` one can have one of two values 33 or 34.

With this applied we can remove the now unused error variants from
`base58::Error`.
2024-03-21 06:22:51 +11:00
Andrew Poelstra 42b5a6a26e
Merge rust-bitcoin/rust-bitcoin#2563: Added more tests for PublicKey::from_str
0d64ae6eb4 Added tests for PublicKey::from_str (Sh0g0-1758)

Pull request description:

  Fixes: #2550

  Added some new tests and refactored some older tests.

ACKs for top commit:
  sanket1729:
    ACK 0d64ae6eb4
  apoelstra:
    ACK 0d64ae6eb4 thanks for bearing with me!
  tcharding:
    ACK 0d64ae6eb4

Tree-SHA512: b6792590c56ccac8e8cf6f182e74cb77c4652c537c0357456ff21a7814ebcc8cf48e0fad4c8d47e6e786a50e2cbb48134cb64406bcc900b4fcad9304d9cf4167
2024-03-15 08:07:18 +00:00
Tobin C. Harding 6ecc41d126
Return error when constructing pubkey from slice
Constructing a pubkey using `PublicKey::from_slice` can fail for reasons
other than just incorrect length - we should not be using `expect` but
rather returning the error.

A purist might argue that we are now returning a nested error type with
an unreachable variant:

  `ParsePublicKeyError::Encoding(FromSliceError::InvalidLength)`

Is this acceptable or do we want to further improve this?
2024-03-13 09:22:11 +11:00
Tobin C. Harding 4bfb466bb9
Upgrade hex dependency
Upgrade to the new `hex v0.2.0` release.
2024-03-10 10:35:01 +11:00
Sh0g0-1758 0d64ae6eb4
Added tests for PublicKey::from_str 2024-03-10 04:21:32 +05:30
Liam Aharon b9f7462958
Implement infallible for errors
Creates a new macro `impl_from_infallible`, and applies it to custom
error types in the codebase.

Closes #1222.
2024-03-08 16:48:34 +11:00
Tobin C. Harding 86f8043e80
Remove Error suffix from variant
We do not use a suffix on error variants, remove it.
2024-02-28 10:48:46 +11:00
Tobin C. Harding 482c8cb7f8
Clean up error type from impls
Make the `From` impls conform to our convention.

Refactor only, no logic changes.
2024-02-28 10:48:46 +11:00
Tobin C. Harding 9d688396c9
base58: Use pub extern crate instead of module
We don't add any implementations to the `base58` types so we can just
`pub extern` the crate instead of using a module and re-exporting.
2024-02-26 08:48:30 +11:00
Tobin C. Harding 9187bf3a65
Fix new nightly warnings/errors
The latest nightly toolchain introduced a whole bunch of new warnings
and errors, mostly to do with import statements - fix them all.
2024-02-21 14:13:49 +11:00