Commit Graph

1152 Commits

Author SHA1 Message Date
Andrew Poelstra 750b4dfb8b
Merge rust-bitcoin/rust-bitcoin#2569: Move types to `units`
cbee9781e8 Move unit types to units (Tobin C. Harding)
5bd0d7194b Remove unused absolute::Error (Tobin C. Harding)

Pull request description:

  Move the following unit types to the new `units` crate:

  - `locktime::absolute::{Height, Time}`
  - `locktime::relative::{Height, Time}`
  - `FeeRate`
  - `Weight`

  Also move the `parse` module as well as constants as required.

  Do minimal changes to get things building:

  - Feature gate on "alloc" as needed.
  - Remove rustdocs that use `bitcoin` types.
  - Re-export units types so this is a non-breaking change.
  - Fix import paths.

  Patch 1 was originally #2526, putting it in via this PR to try and speed up the process.

  Close: #2282

ACKs for top commit:
  sanket1729:
    ACK cbee9781e8
  apoelstra:
    ACK cbee9781e8 lgtm. this is a good start. I think the LockTime types should follow Height and Time

Tree-SHA512: 6b0d63c7b054008598d7fa81be7d8c112f2778883b5529d79d446617b94b3c196c9ac735f840d1dfb488700894d3161c6976d44ab0e12ac3af4008068eac5f87
2024-03-15 22:43:51 +00: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
Andrew Poelstra bf4783db47
Merge rust-bitcoin/rust-bitcoin#2458: Support signing taproot in psbt
41e8fb0863 Support signing taproot in psbt (yu)

Pull request description:

  Hi team, I'm from Keystone Wallet team. currently rust-bitcoin does not support signing taproot transactions in psbt.
  We think this founction should be included in the psbt module, we submit this PR. Some context and discussion about this PR can be found here: #2418.

  For this PR, mostly two new functions are introduced:

  - `bip32_sign_schnorr`:  sign a taproot input.
  - `sighash_taproot`: calculate the sighash message to sign a taproot input along with the sighash type.

  Looking forward to your feedback.

ACKs for top commit:
  tcharding:
    ACK 41e8fb0863
  sanket1729:
    ACK 41e8fb0863.

Tree-SHA512: 2eb14a3204e6ed848515483778dd7986662aacb332783d187da72d29e207b78a2d427939f2b958135a32de5459221385e6f1f5bae89f491b58d8bc79f202b724
2024-03-15 07:52:27 +00:00
Andrew Poelstra e0d58a9c1c
Merge rust-bitcoin/rust-bitcoin#2576: Return error when constructing pubkey from slice
6ecc41d126 Return error when constructing pubkey from slice (Tobin C. Harding)

Pull request description:

  This PR fixes a bug introduced by me in #2473, and uncovered by #2563 - amazing that it was found so quickly!

  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?

ACKs for top commit:
  sanket1729:
    ACK 6ecc41d126
  apoelstra:
    ACK 6ecc41d126

Tree-SHA512: ae8299b21c4787a104f98533105308e8e7678cd5a29b78c30012982d741c05ba5f2bb1edd1d61d3a5ce028235d18c1511e1f94207479bc19e88cfec7a7ca1737
2024-03-14 07:19:12 +00:00
Andrew Poelstra 1ceac90bf6
Merge rust-bitcoin/rust-bitcoin#2565: Removes txid prefix in transaction IDs
56132f59d5     Remove the `:#` formatting for `hex_fmt_impl` macro (448 OG)

Pull request description:

  This commit attempts to solve #2505  by ensuring that formatting is not forced using the `:#` in the hex macro code generating in macro rule `hex_fmt_impl` in the hashes/utils.rs file.

  The write! macro forces all formatting to add the prefix `0x` by adding an alternate by (#) default

  ```rust
  impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
              #[inline]
              fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
                  write!(f, "{:#}", self) // <-- This is where the formatting is being forced.
              }
          }
  ```

  By removing this formatting, the `:#` must be specified by the user in order for a prefix to be added.

  ```rust
  let outpoint = bitcoin::OutPoint::default();
      println!("{:?}", &outpoint);
      println!("{:#?}", &outpoint);
      println!("{:#}", &outpoint);
      println!("{:x}", &outpoint.txid);
      // `{:#}` must be specified to pretty print with a prefix
      println!("{:#}", &outpoint.txid);
      dbg!(&outpoint);
      dbg!(&outpoint.txid);
  ```

  The PR also adds testcase for this when running `cargo test` .

ACKs for top commit:
  tcharding:
    ACK 56132f59d5
  apoelstra:
    ACK 56132f59d5

Tree-SHA512: 9e4fc9f30ab0b3cf2651d3c09f7f01d8245ac8ea7ae3a82bb4efd19f25c77662bf279020a31fa61b37587cc0c74284696c56045c59f1ba63b2dd42a210d98ebc
2024-03-13 17:28:09 +00:00
Andrew Poelstra d2617f99b2
Merge rust-bitcoin/rust-bitcoin#2530: Improve leaf errors
f8de7954b2 Remove unused pow::TryFromError type (Tobin C. Harding)
43c5eb765c Fix witness_version leaf error type (Tobin C. Harding)
2af764e859 hashes: Fix leaf error type (Tobin C. Harding)

Pull request description:

  In light of recent discussion go over the codebase and look for some places that the leaf errors are wrong. Does not do the whole code base, excludes `p2p` and a couple of other places.

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

Tree-SHA512: 2905878363869ee205cce49c58c060c712c9b7b55965ee60bb856128842968a4be86c93a194ffffdb35e215b2bea8ad33b04ee47e8e17cc784b0641ea48518e5
2024-03-13 15:03:57 +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
448 OG 56132f59d5
Remove the `:#` formatting for `hex_fmt_impl` macro
This fixes the issue where pretty debug like `dbg` or `{:#}` introduce the use of
    `0x` prefix to hex encoded transaction ID.

    The transaction id is being forced to pretty print inside the `hex_fmt_impl` macro
    using `{:#}` in the line `write!(f, "{:#}", self)` debug formatter.

    Resolves: #2505
2024-03-12 11:48:05 +03:00
Tobin C. Harding f8de7954b2
Remove unused pow::TryFromError type 2024-03-12 12:14:26 +11:00
Tobin C. Harding 43c5eb765c
Fix witness_version leaf error type
Leaf error types should typically have private fields, provide accessor
functions, and not use `non_exhaustive`.
2024-03-12 12:14:14 +11:00
Tobin C. Harding be329c2d7b
Upgrade bitcoinconsenus
Upgrade to the most recent `bitcoinconsensus` version that excludes
Taproot verification i.e., one version before latest.
2024-03-12 12:04:32 +11:00
Tobin C. Harding cbee9781e8
Move unit types to units
Move the following unit types to the new `units` crate:

- `locktime::absolute::{Height, Time}`
- `locktime::relative::{Height, Time}`
- `FeeRate`
- `Weight`

Also move the `parse` module as well as constants as required.

Do minimal changes to get things building:

- Feature gate on "alloc" as needed.
- Remove rustdocs that use `bitcoin` types.
- Re-export units types so this is a non-breaking change.
- Fix import paths.
2024-03-12 11:59:39 +11:00
Tobin C. Harding 5bd0d7194b
Remove unused absolute::Error
The `absolute::Error` is not used, we originally intended it as possibly
useful for users of the library. We have not made effort in other
modules to provide such errors - lets remove it.
2024-03-12 09:13:09 +11:00
Andrew Poelstra a124ff41c4
Merge rust-bitcoin/rust-bitcoin#2473: Upgrade to `hex v0.2.0`
f337dec2b1 hashes: Remove unnecessary feature guard from test (Tobin C. Harding)
0cea90d505 Test hashes honour Formatter::precision (Tobin C. Harding)
4bfb466bb9 Upgrade hex dependency (Tobin C. Harding)
f0558e8eb9 Use fmt_hex_exact (Tobin C. Harding)
6820f51408 hashes: Add fmt roundtrip tests (Tobin C. Harding)
e302e30e7c Import with super::* in unit test (Tobin C. Harding)

Pull request description:

  Upgrade to use the newly released `hex` code.

  - Patch 1: Does trivial preparatory cleanup
  - Patch 2: Adds some unit tests to check we roundtrip hashes correctly (added because in the test PR I had the `Midstate` iml wrong and it was not being caught).
  - Patch 3: Uses macro in place of `forward_hex` and `backward_hex` - needs concept review, I hacked this without understanding why the functions existed in the first place.
  - Patch 4: Does the upgrade, I've attempted to make minimal changes, so there is room for a bunch of cleanups if/when this merges.
  - Patch 5: Adds a unit test to verify that we can close #2494
  - Patch 6: Removes unnecessary feature gate from unit test.

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

Tree-SHA512: 7913d1b3079cf5ba1b0e70f5c33e091c5ef1258026c8f27bbe8a050100bbc7622b6555d560b15be3b3d90d47ce873f137a73cf2d772108d2915fb30ed129bded
2024-03-11 21:01:24 +00:00
Andrew Poelstra 24b19d7776
Merge rust-bitcoin/rust-bitcoin#2433: Split relative locktime error up
3c8edae25b Split relative locktime error up (Tobin C. Harding)

Pull request description:

  The `relative` module has a single general error type, we are moving away from this style to specific error types.

  Split the `relative::Error` up into three error structs.

  I forget the policy on public inner fields.

ACKs for top commit:
  sanket1729:
    utACK 3c8edae25b
  apoelstra:
    ACK 3c8edae25b

Tree-SHA512: f3079f81a825125f1efe54657fbba64618530b25aecaa3844902900517bf23bec26ff5399cf22f4e63e44316ebb603e8692cbaece2782ecafe09ffed3eab553c
2024-03-11 17:42:54 +00:00
Tobin C. Harding 0cea90d505
Test hashes honour Formatter::precision
Test that the new version of `hex` honours `Formatter::precision` for
new wrapped hash types (ie, types created with `hashes::hash_newtype`).

Fix: #2494
2024-03-10 10:35:02 +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
Andrew Poelstra b3273cfcb8
Merge rust-bitcoin/rust-bitcoin#2562: Replaced Deprecated Function
08a9962035 Replaced Deprecated Function (Sh0g0-1758)

Pull request description:

  Changed deprecated Function with a supported one.

ACKs for top commit:
  apoelstra:
    ACK 08a9962035 yep, this seems reasonable. Thanks!
  tcharding:
    ACK 08a9962035

Tree-SHA512: fd0a55ab25cd15a3254a6c353e4906b4f4c74175d648e1f532be8b8642490e5187b96aa0aa7365771016e10a481054d3377b9074b93cd001da515177315b0d92
2024-03-09 23:10:54 +00:00
Tobin C. Harding 3c8edae25b
Split relative locktime error up
The `relative` module has a single general error type, we are moving
away from this style to specific error types.

Split the `relative::Error` up into three error structs.

Note the change of parameter `h` to `height`, and using `h` as the
pattern matched variable - this makes sense because it gives the
variable with large scope the longer name.
2024-03-10 09:57:11 +11:00
Sh0g0-1758 0d64ae6eb4
Added tests for PublicKey::from_str 2024-03-10 04:21:32 +05:30
Andrew Poelstra f69417f8bc
Merge rust-bitcoin/rust-bitcoin#2396: Add consts to Params for individual networks
3a56ecc677 Add consts to Params for individual networks (Tobin C. Harding)

Pull request description:

  Add consts to the `Params` type for the individual networks.

ACKs for top commit:
  apoelstra:
    ACK 3a56ecc677
  Kixunil:
    ACK 3a56ecc677
  sanket1729:
    ACK 3a56ecc677

Tree-SHA512: 0d265a14dd6a591a267da5381d3dcfd0d313f950dec4922f96d25349047d0c8a366c41dcdc1fc523fe4b178ec6a00b717bda25286625e222194f345cee5e7a97
2024-03-09 13:14:10 +00:00
Andrew Poelstra e58975adbc
Merge rust-bitcoin/rust-bitcoin#2555: Add ServiceFlags::P2P_V2
5818e04328 Add ServiceFlags::P2P_V2 (Ava Chow)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 5818e04328
  sanket1729:
    ACK 5818e04328
  Kixunil:
    ACK 5818e04328

Tree-SHA512: 077424c984a11af93e65873d57cdf03fe9fcc479ca270bcbe6383afeb5d2161882edf72b27ef3b5abd7a2b58efff8b95cf41e3f68221c91a3b3dc6a6b7ce9f60
2024-03-09 13:00:09 +00:00
Sh0g0-1758 08a9962035
Replaced Deprecated Function 2024-03-09 17:18:00 +05:30
Ava Chow 5818e04328 Add ServiceFlags::P2P_V2 2024-03-08 15:07:34 -05:00
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
Andrew Poelstra 1ac7c292b1
Merge rust-bitcoin/rust-bitcoin#2546: Fix CJDNS marker byte check
ec67456172 Fix CJDNS marker byte check (Ava Chow)

Pull request description:

  Only the first byte of a CJDNS address is 0xfc, the second byte should be ignored.

  See https://github.com/hyperboria/peers for examples of CJDNS addresses.

ACKs for top commit:
  apoelstra:
    ACK ec67456172
  sanket1729:
    urACK ec67456172.
  Kixunil:
    ACK ec67456172

Tree-SHA512: 0da1054a8e997b6bf6e0aaedc40943edb6a6c7b23f660c92b34dc9395e6153e7e10b0268335a77fbcb5bb635352e1ed92839b350188fd6d33dabe558e88c00bb
2024-03-07 14:43:52 +00:00
Andrew Poelstra ea6aa99ae4
Merge rust-bitcoin/rust-bitcoin#2492: Remove the FromHexStr trait
b873a3cd44 Do infallible int from hex conversions (Tobin C. Harding)
4d762cb08c Remove the FromHexStr trait (Tobin C. Harding)
026537807f Remove mention of packed (Tobin C. Harding)

Pull request description:

  The `FromHexStr` trait is used to parse integer-like types, however we can achieve the same using inherent methods.

  Move the hex parsing functionality to inherent methods, keeping the same behaviour in regard to the `0x` prefix.

  Patch 1 is trivial preparatory cleanup.

ACKs for top commit:
  apoelstra:
    ACK b873a3cd44
  sanket1729:
    ACK b873a3cd44

Tree-SHA512: a280169b68304fcc1a531cc9ffb6914b70238efc4c2241a766105053911a373a0334b73e5ea3525c331ccb81ce98c43fea96dae77668804e608376a48d5ed8ac
2024-03-07 14:36:53 +00:00
Ava Chow ec67456172 Fix CJDNS marker byte check
Only the first byte of a CJDNS address is 0xfc, the second byte should
be ignored.
2024-03-06 13:53:09 -05:00
geekvest a6adfd845c fix some comments
Signed-off-by: geekvest <cuimoman@sohu.com>
2024-03-03 13:41:23 +08:00
Tobin C. Harding 3a56ecc677
Add consts to Params for individual networks
Add consts to the `Params` type for the individual networks.
2024-02-29 08:54:54 +11:00
Andrew Poelstra e386cbfadf
ci: delete *test.sh files
These are not run in CI since #2353 and are likely to go out of date. If
we want a script that users can run locally then we should create a new
script that wraps our current CI.
2024-02-28 20:45:56 +00: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
Andrew Poelstra 36aa627d83
Merge rust-bitcoin/rust-bitcoin#2508: Add `NetworkValidationError`
7e2a81d03b Remove unused address::Error type (Tobin C. Harding)
a92dc9c35c Add NetworkValidationError (Tobin C. Harding)

Pull request description:

  Replaces #2502 because there is going  to be way too much arguing on this to bother a newer contributor with.

  This PR takes into consideration #2507 but does not improve the issue, it also does not make it worse. I propose to do this and then consider #2507 since this is a step forwards IMO.

  Remove the `address::Error` because its not good. Add a `NetworkValidationError` and return it from `require_network` - leaving the door open for resolving Kix's issue in 2507.

ACKs for top commit:
  Kixunil:
    ACK 7e2a81d03b
  apoelstra:
    ACK 7e2a81d03b

Tree-SHA512: 0a220dbec1457f35e3d95f32399f258e65c0477e8b4d14dbe2dfad0a44966ab0339d4c2d9828da318bf131db45cecb2447c0e32d5669a5f4c4739b90c83d3c0d
2024-02-27 14:35:42 +00:00
Tobin C. Harding 7e2a81d03b
Remove unused address::Error type 2024-02-27 11:10:03 +11:00
Tobin C. Harding a92dc9c35c
Add NetworkValidationError
The `require_network` function can fail in one way only, add a specific
error for the failure.
2024-02-27 11:08:38 +11:00
Andrew Poelstra 42e8f537e6
Merge rust-bitcoin/rust-bitcoin#2504: base58: Use pub extern crate instead of module
9d688396c9 base58: Use pub extern crate instead of module (Tobin C. Harding)

Pull request description:

  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.

ACKs for top commit:
  Kixunil:
    ACK 9d688396c9
  apoelstra:
    ACK 9d688396c9

Tree-SHA512: 521af0fd1ae365a6d060dd3c38fc18c1fb3a6c46adb7cba64e53128c7a898c766bcc603078b4cb8a3672df96559633495b23203a33147b5b4959b0c690ab7451
2024-02-27 00:04:00 +00:00
Tobin C. Harding b873a3cd44
Do infallible int from hex conversions
We have three integer wrapping types that can be created from hex
strings where the conversion from an integer is infallible:

- `absolute::LockTime`
- `Sequence`
- `CompactTarget`

We would like to improve our handling of the two prefix characters (eg
0x) by making it explicit.

- Modify the inherent `from_hex` method on each type to error if the
input string does not contain a prefix.

- Add an additional inherent method on each type `from_unprefixed_hex`
that errors if the input string does contain a prefix.

This patch does not touch the wrapper types that cannot be infallibly
constructed from an integer (i.e. absolute `Height` and `Time`).
2024-02-27 10:40:52 +11:00
Tobin C. Harding 4d762cb08c
Remove the FromHexStr trait
The `FromHexStr` trait is used to parse integer-like types, however we
can achieve the same using inherent methods.

Move the hex parsing functionality to inherent methods, keeping the same
behaviour in regard to the `0x` prefix.
2024-02-27 10:09:20 +11:00
Tobin C. Harding 026537807f
Remove mention of packed
We removed the `PackedLockTime`, remove all mentions of the word packed.
2024-02-27 10:09:20 +11:00
Tobin C. Harding 4e557fa4e6
Update bech32 dependency
Update `bech32` to the newly released version `0.11.0`.
2024-02-26 15:31:51 +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
Andrew Poelstra d85817b880
Merge rust-bitcoin/rust-bitcoin#2497: Add the `FromScriptError` for handling errors in `address`
c2d658ac05 Add `P2shError` for handling errors related to P2sh (harshit933)
5182a8d7a8 Remove unused variants from `Address::Error` (harshit933)
05b24946eb Add the `FromScriptError` for handling errors in `address` (harshit933)

Pull request description:

  This commit adds the `FromScriptError` struct to handle the errors while generating address from any script. It includes :
  - Unrecognized script error.
  - Witness Program error.
  - Witness Version error.

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

Tree-SHA512: 891eed787129aaf1b664cc16d325178d5d2f77cc41a0543a3d9d1a5af1b58188daece1f6a653bdc6b76b82db0490a39e9bba7fc090e3727d15ee9b8977733698
2024-02-24 15:59:45 +00:00
Andrew Poelstra 94938ea247
Merge rust-bitcoin/rust-bitcoin#2446: Make constructors const
ac88bc03fd Make constructors const (Tobin C. Harding)

Pull request description:

  Audit the codebase for any function that starts with `/// Creates` and see if we can make it const. Inline them at the same time.

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

Tree-SHA512: 0c71e38018e74b3ce1aae871fc6208b87655a0970ae6cca7a538b9f896c95542c6905eb4d7e02539847e88d8a22a873039a5734130c2a46efb4d1b2b9ffd9f4a
2024-02-24 15:48:46 +00:00
Tobin C. Harding ac88bc03fd
Make constructors const
Audit the codebase for any function that starts with `/// Creates` and
see if we can make it const. Inline them at the same time.
2024-02-24 06:04:41 +11:00
harshit933 c2d658ac05 Add `P2shError` for handling errors related to P2sh
Added a new `P2shError` struct for handling errors emmited while
generating addresses from P2sh scripts.
2024-02-23 19:33:44 +05:30
Tobin C. Harding aa8ba118ae
Add a new base58 crate
Add a new `base58` crate to the workspace and move the `bitcoin::base58`
module to it.

Done as part of crate smashing, specifically so that we can make `bip32`
into a separate crate.
2024-02-23 12:54:24 +11:00
harshit933 5182a8d7a8 Remove unused variants from `Address::Error` 2024-02-23 03:36:35 +05:30
harshit933 05b24946eb Add the `FromScriptError` for handling errors in `address`
This commit adds the `FromScriptError` struct to handle the errors
while generating address from any script. It includes :
- Unrecognized script error.
- Witness Program error.
- Witness Version error.
2024-02-23 02:21:11 +05:30
yu 41e8fb0863 Support signing taproot in psbt 2024-02-22 10:42:44 +08:00