Commit Graph

74 Commits

Author SHA1 Message Date
Tobin C. Harding d9533523ac
Remove usage of ThirtyTwoByteHash
The `ThirtyTwoByteHash` trait is defined in `secp256k1` and used in
`hashes` as well as `bitcoin`. This means that we must use the same
version of `hashes` in both `bitcoin` and `secp256k1`. This makes doing
release difficult.

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

This is step backwards as far as type safety goes and it makes the code
more ugly as well because it uses `expect` but thems the breaks.
2023-08-23 12:21:26 +10:00
Tobin C. Harding 4300cf2210
Add p2wpkh and p2wsh signature hash functions
The word "segwit" refers to segwit v0 and taproot but currently we have
`segwit_signature_hash` that is version specific (segwit v0).

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

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

While we are at it test the bip-143 test vectors against all the
sighash types of wrapped p2wsh.
2023-08-15 11:54:08 +10:00
Andrew Poelstra f99459522d
Merge rust-bitcoin/rust-bitcoin#1914: Move type definitions of pubkey/script hash types
27b3c1e0e6 Improve the ScriptHash and WScriptHash types (Tobin C. Harding)
2197f1377f Improve PubkeyHash and WPubkeyHash (Tobin C. Harding)

Pull request description:

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

  No longer adds unit tests.

  Fix: #1909

ACKs for top commit:
  apoelstra:
    ACK 27b3c1e0e6

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

Pull request description:

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

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

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

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

Pull request description:

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

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

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

  Its all pretty trivial except:

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

  Particularly the last one might be incorrect/controversial.

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

  Thanks

ACKs for top commit:
  apoelstra:
    ACK 3c0bb63423

Tree-SHA512: 7e5f8590aec5826098d4d8d33351a41b10c42b6379ff86e5b889e73271b71921fc3ca9525baa5da53e07fa2e961e710393694e04658a8243799950b4604caf43
2023-08-02 13:18:19 +00:00
Tobin C. Harding 2197f1377f
Improve PubkeyHash and WPubkeyHash
Improve the pubkey hash types by doing:

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

Keep the current crate level re-export so this does not impact the
public API _if_ people are using the re-export but is still a breaking
change.
2023-08-02 11:27:55 +10:00
Tobin C. Harding d4e8f49fc3
Move p2p::constants::Network to crate root
The `Network` type is not a p2p construct, it is more general, used
throughout the codebase to define _which_ Bitcoin network we are
operating on.
2023-08-01 16:46:59 +10:00
Tobin C. Harding 1bac1fd518
Rename the network module to p2p
The `network` module deals with data types and logic related to
internetworking bitcoind nodes, this is commonly referred to as the p2p
layer.

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

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

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

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

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

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

Rename `NonStandardSighashType` to `NonStandardSighashTypeError`.

While we are at it make the inner type private to the crate, there is no
need to leak the inner values type.
2023-08-01 15:55:45 +10:00
Tobin C. Harding a70b1b9c6c
Use standard set of derives on all error types
As part of an ongoing effort to make our error types stable and useful
add a stand set of derives to all error types in the library.

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

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

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

Pull request description:

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

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

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

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

We just removed the deprecated code so we can now move this type to the
`sighash` module where it is used.
2023-07-27 09:21:34 +10:00
Andrew Poelstra f50bb05e99
Merge rust-bitcoin/rust-bitcoin#1949: key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey
ebcce38b4f run cargo fmt (Andrew Poelstra)
b3ac00b8b3 key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey (Steven Roose)

Pull request description:

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

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

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
Andrew Poelstra ebcce38b4f
run cargo fmt 2023-07-20 17:13:15 +00:00
Steven Roose b3ac00b8b3
key: Impl From<secp256k1::PublicKey> for bitcoin::PublicKey 2023-07-20 17:09:37 +01:00
Tobin C. Harding 9787ba6c96
Rename Script::empty to Script::new
The `empty` constructor is mis-named for the following reasons:

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

Rename the function to `new`, put it at the top of the impl block while
we are at it.
2023-07-08 11:26:30 +10:00
Tobin C. Harding e04ac1e743
Add a verify function to PublicKey
Expose signature verification functionality for ECDSA signatures on the
`PublicKey` type.

We should have an identical function on `XOnlyPublicKey` but this will
have to be done in `secp2561`.
2023-06-20 05:08:07 +10:00
Andrew Poelstra 445aa84f8c
cargo fmt 2023-06-12 13:01:12 +00:00
Steven Roose d990084481
key: Implement From<PublicKey> for XOnlyPublicKey 2023-06-06 20:24:28 +01:00
Tobin C. Harding 202d1cd581
Rename taproot::Error to SigFromSliceError
This error type is only used in the `from_slice` function. Use prefix
`Sig` because `taproot::FromSliceError` does not fully express how the
error came about.

Use specific identifier for the error, this aids usage but also prevents
us later adding "random" other variants into this error and using it in
other functions.
2023-06-02 15:48:45 +10:00
Tobin C. Harding 29678cb82b
Correctly document InvalidSighashType variant
The rustdoc on the `taproot::Error::InvalidSighashType` is wrong, fix
it.
2023-06-02 15:05:41 +10:00
Tobin C. Harding 13d5c0536b
Remove explicit error conversion
We provide a `From<secp255k1::Error>` impl so we do not need to
explicitly convert the error return, just use `?`.
2023-06-02 15:05:38 +10:00
Tobin C. Harding d86517ae4f
taproot: Use error variants locally
Add 'use Error::*' locally to make the code more terse.
2023-06-02 14:48:22 +10:00
Tobin C. Harding 042dcaa4b7
Remove doc(hidden) from error conversion functions
Give people access to the error type conversion docs, its no harm and it
may be useful when the compiler does not give enough information.
2023-05-30 15:54:40 +10:00
Andrew Poelstra d93e781148
Merge rust-bitcoin/rust-bitcoin#1838: Move and rename TxOut default trait to a const called NULL
75b3f19b96 Move and rename TxOut default trait to a const called NULL (yancy)

Pull request description:

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

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

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

Pull request description:

  added a function to generate a private key

  Resolves: #1823

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

Tree-SHA512: 29ba54be8cb777e71a4683835686cbf2978b23736f629d7bbff468074235fece261ca170c23f358d1bd878987566d09e4488c3f1a106c59a5c8bdf52b98abffe
2023-05-08 03:13:51 +00:00
kshitjj 995c797e0d feat: generate PrivateKey 2023-05-07 21:56:39 +05:30
yancy d57ec019d5 Use Amount type for TxOut value field 2023-05-04 17:09:08 +02:00
Tobin C. Harding 984fe69448
bitcoin: Remove attribution from all files
Currently we have a mishmash of attribution lines accompanying the SPDX
identifier. These lines are basically meaningless because:

- The date is often wrong
- The original author attributed is not the only contributor to a file
- The term "rust bitcoin developers" is basically just noise

Just remove all the attribution lines and be done with it. While we are
at it add an SPDX line to the few files missing it, whether this license
nonsense is even needed is left as an argument for another day.
2023-05-01 09:22:48 +10:00
Andrew Poelstra e83a2d3422
Merge rust-bitcoin/rust-bitcoin#1742: Use package in manifest and shorten import
fabcde036f Use package in manifest and shorten import (Tobin C. Harding)

Pull request description:

  We can use `package` to rename `bitcoin_hashes` to `hashes` and `bitcoin_internals` to `internals`. This makes imports more terse with no loss of meaning.

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

Tree-SHA512: bc5bff6f7f6bf3b68ba1e0644a83da014081d8c6c9d578c21cb54fdd56a018f68733dd1135d05b590ba193ed9efd12fa9019182c1fed347e604d8548f6ef9103
2023-04-05 14:20:06 +00:00
Martin Habovstiak 91f45a214f Replace hardcoded values with compile-time hashing
Previous changes enabled passing the string used as a tag into
`sha256t_hash_newtype!` macro rather than hard-coding midstate. This
commit takes advantage of it and replaces the hard-coded values with
compile-time executed (`const`) hashing.
2023-04-02 17:01:10 +02:00
Martin Habovstiak 095b7958dd Make `sha256t_hash_newtype!` evocative of the output.
The Rust API guidelines state that macros should be evocative of the
output, which is a sensible recommendation. We already had this for
`hash_newtype!` macro but didn't for sha256t version.

This changes the macro to have this syntax:

```rust
sha256t_hash_newtype! {
    // Order of these structs is fixed.
    /// Optional documentation details here. Summary is auto-generated.
    /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

    /// Documentation here
    #[hash_newtype(forward)] // optional, default is backward
    /*pub*/ struct HashType(/* attributes allowed here */ _);
}
```

Closes #1427
2023-04-02 17:00:52 +02:00
Tobin C. Harding a189942c64
Use doc_auto_cfg
If we use `#![cfg_attr(docsrs, feature(doc_auto_cfg))]` instead of
`#![cfg_attr(docsrs, feature(doc_cfg))]` we no longer need to manually
mark types with `#[cfg_attr(docsrs, doc(cfg(feature = "std")))]`.

Sweeeeeet.
2023-03-29 14:50:33 +11:00
Tobin C. Harding fabcde036f
Use package in manifest and shorten import
We can use `package` to rename `bitcoin_hashes` to `hashes` and
`bitcoin_internals` to `internals`. This makes imports more terse with
no loss of meaning.
2023-03-28 12:20:04 +11:00
Tobin C. Harding 122188f7dd
Use shorter import statements
As per discussion [0] use the shorter form for importing crates that we
re-export (`hashes` and `secp256k1`).

[0] https://github.com/rust-bitcoin/rust-bitcoin/discussions/1661
2023-03-22 14:09:58 +11:00
Tobin C. Harding 3ec8a12428
crypto: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:23 +11:00
Tobin C. Harding 314e6786b4
crypto: Add rustfmt::skip attributes
In preparation for formatting the `crypto` module add a couple of `skip`
attributes to keep arrays formatted 8 bytes per line.
2023-03-21 08:33:22 +11:00
Tobin C. Harding 42b07586ac
Improve the public API
We created the `crypto` crate as a container for cryptography modules
with the idea that it may be split out into a separate crate. There is
no reason for users of the lib to know about this module. Also, we have
two `taproot` modules, one in `crypto` and one at the crate root, this
makes for un-ergonomic usage of the lib.

Improve the public API by doing:

- Make the `crypto` module private (`pub(crate)`).
- Re-export `crypto::taproot::Signature` (and `Error`) from
  `crate::taproot`
2023-03-01 09:28:42 +11:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00