Commit Graph

834 Commits

Author SHA1 Message Date
Andrew Poelstra 36805b5283
Merge rust-bitcoin/rust-bitcoin#1994: Remove redundant segwit version from function names
bc398204bf Remove redundant segwit version from function names (Tobin C. Harding)

Pull request description:

  A P2TR output does not need to be clarified with version 1, it is implicit. As with p2wpkh/p2wsh and version 0.

  Remove redundant version identifiers from function names, deprecating the originals.

ACKs for top commit:
  apoelstra:
    ACK bc398204bf

Tree-SHA512: 49806c564badca25ce02161445b2b41497b565f2002aa1edfc0cf0c57b38683480deec0d9b682e18dc7e59c22128e0b641abcccc2cbedd0b5603cbcbf2fd26df
2023-09-21 16:10:50 +00:00
Andrew Poelstra f80ec98f35
Merge rust-bitcoin/rust-bitcoin#2002: Remove unnecessary reference
f17bb0d18f Remove unnecessary reference (Tobin C. Harding)

Pull request description:

  `T` is a generic that implements`AsRef<PushBytes>`, it should not be a reference. This is inline with other usages of `AsRef<PushBytes>` for example in `Builder::push_slice`.

  Found while working on #2003

ACKs for top commit:
  apoelstra:
    ACK f17bb0d18f

Tree-SHA512: 6f6ae0ba5d5010db53d9c2af107df84bc058277b2b7cc35800f4e6ed93d351838b7f101284b7d80345bee639615d27d76a2e5c4c784782c5b3e5090444defe29
2023-09-21 14:23:50 +00:00
Subhradeep Chakraborty 95b7a95fc2 test: correct psbt used for regression test 2023-09-21 11:45:18 +05:30
Tobin C. Harding e4c7e01a6f
Use the new bech32 iterator API
Use the new bech32 iterator API that Andrew and I wrote.
2023-09-21 15:10:57 +10:00
Tobin C. Harding c950ef4bbd
Add transaction::Version data type
BIP-68 activated a fair while ago (circa 2019) and since then only
transaction versions 1 and 2 have been considered standard.

Currently in our `Transaction` struct we use an `i32`, this means users
can construct a non-standard transaction if they do not first look up
what the value should be. We can help folk out here by abstracting over
the version number.

Since the version number only governs standardness elect to make the
inner `i32` public (ie., not an invariant). The aim of the type is to
make life easy not restrict what versions are used.

Add transaction::Version data type that simply provides two consts `ONE`
and `TWO`.

Add a `Default` impl on `Version` that returns `Version::TWO`.

In tests that used version 0, instead use `Version::default` because the
test obviously does not care.
2023-09-21 15:02:02 +10:00
Andrew Poelstra a2a4efbe6a
Merge rust-bitcoin/rust-bitcoin#1975: Prepare for using new bech32 release
52f2332383 Remove docs from witness version conversion functions (Tobin C. Harding)
47d6d785cb Remove bip 173/350 test vectors (Tobin C. Harding)
e0eaeaad99 Split ParseError out of Error (Tobin C. Harding)
0f536e86dc Add new UnknownAddressTypeError for parsing address type (Tobin C. Harding)
e2014cba1b Import error variants within dislay impl (Tobin C. Harding)
9d7791fcd6 Remove unnecessary self:: from error import (Tobin C. Harding)
b2e485ed51 Split the address error code out into a separate module (Tobin C. Harding)
f34ca0c52b Move address.rs to address/mod.rs (Tobin C. Harding)

Pull request description:

  In preparation for depending on the recently released version of `rust-bech32` do a bunch of preparatory fixes.

  1. Improve `address` module error handling as we are doing else where at the moment
  2. Remove bip 173 and 350 test vector tests, these are fully covered in bech32
  3. Trim down the docs on `WitnessVersion`

  This PR is the first 8 patches of https://github.com/rust-bitcoin/rust-bitcoin/pull/1951

ACKs for top commit:
  sanket1729:
    ACK 52f2332383
  apoelstra:
    ACK 52f2332383

Tree-SHA512: 67a4ea4020b4e5c9c8396e4195e06dbd1d11335788f9e52f60abbc0b399e37e5dacc9bb7fa4e88221670322fa3c3407ade059d5c709f96e2df97240f4524e08c
2023-09-20 19:54:36 +00:00
Andrew Poelstra 3b9bc9a063
Merge rust-bitcoin/rust-bitcoin#2038: Re-export Opcode
ccdcffe69c Re-export Opcode (Tobin C. Harding)

Pull request description:

  We recently rename `opcodes::All` to `Opcode` but did not re-export it from the crate root. Since it now has a nice clear name we can do so.

  Found while hacking up the `rust-bitcoincore-rpc` dependency upgrade for upcoming release `bitcoin v0.31.0`.

ACKs for top commit:
  apoelstra:
    ACK ccdcffe69c

Tree-SHA512: 8acac90d01f14245f93edc10f3483d8aa9865aca59b6ef42ab744b875558fca8ad74894ad95a83637f0cec7a2a353d74d4d6f7ee5f1d1276cc0fc3dcb6983362
2023-09-20 17:33:53 +00:00
Andrew Poelstra f4c83b4d8e
Merge rust-bitcoin/rust-bitcoin#2078: Script helper to classify bare multisig
acbf23aaa5 Add `is_multisig` helper to Script type (Clark Moody)

Pull request description:

  A new `is_multisig` helper method to classify bare multisig output scripts.

  The form of a valid multisig script is:
  - Pushnum `M`
  - &lt;N&gt; pubkeys
  - Pushnum `N`
  - `OP_CHECKMULTISIG`

  `N` must equal the number of pushed pubkeys, and `M` must be less than or equal to `N`.

  I've tested this against the RPC output of Core at the block level, checking that the total number of multisig outputs matches.

  ```
  Block 350338, 89 multisig
  Block 350340, 29 multisig
  Block 350341, 4 multisig
  Block 350343, 579 multisig
  Block 350344, 48 multisig
  Block 350346, 11 multisig
  Block 350347, 404 multisig
  Block 350350, 127 multisig
  Block 350351, 1 multisig
  Block 350353, 40 multisig
  Block 350356, 13 multisig
  Block 350357, 2 multisig
  Block 350358, 1 multisig
  ```

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

Tree-SHA512: b8feeaa8725ac63a658897dac3b303fc8b3d56674d796b14569548124928329993bea45482928d9ce85231f1b5837922af8c0a77b2601a92f88b5e2a9394e97f
2023-09-20 16:38:33 +00:00
Andrew Poelstra 6e6847a263
Merge rust-bitcoin/rust-bitcoin#2075: Bugfix: Script::count_sigops should not return a Result
026a55809e Fix: Script::count_sigops parsing should not return a Result (junderw)

Pull request description:

  When implementing some tests for the Transaction PR, I noticed that there were coinbase transactions that would pass Bitcoin Core parsing and fail my code.

  It turns out that the Script parsing for sigops calls `break`  to exit the loop and returns the current n value whenever there is an EarlyEndOfScript error.

  See this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/2073#issuecomment-1722403687 for some links to the relevant source.

ACKs for top commit:
  apoelstra:
    ACK 026a55809e
  tcharding:
    ACK 026a55809e

Tree-SHA512: 57c1b88add5e1c9ef9245fcec0e471db55c2f9b1b0b0f8ebd471f1bede0ca5eeb8492d8c75dea1fd43f1343037df44969c9b9fde26a7de9ac68a26dca899e47f
2023-09-20 15:53:30 +00:00
junderw cd15c746cb
Feature: Instruction can read the script number 2023-09-19 21:08:17 -07:00
Tobin C. Harding 52f2332383
Remove docs from witness version conversion functions
These docs do not add that much value, we do not typically bother
documenting `From` and `TryFrom` implementations because they are super
well known and its obvious from the function signature what is going on.
2023-09-20 13:13:56 +10:00
Tobin C. Harding 47d6d785cb
Remove bip 173/350 test vectors
The BIP-173 and BIP-350 test vectors are implemented in `rust-bech32`,
no need to duplicate those tests here.
2023-09-20 13:13:56 +10:00
Tobin C. Harding e0eaeaad99
Split ParseError out of Error
The `address::Error` is module level general, we can make the code
easier to maintain and easier to stabalize by splitting the parse error
out of the general error.

Create a `ParseError` that is returned by `FromStr for Address`. Remove
the now unused variants from the general `address::Error`.
2023-09-20 13:13:56 +10:00
Tobin C. Harding 0f536e86dc
Add new UnknownAddressTypeError for parsing address type
There is no need to use the general `address::Error` when parsing an
address type, there is only one error path.
2023-09-20 13:13:56 +10:00
Tobin C. Harding e2014cba1b
Import error variants within dislay impl
In an effort to reduce the number of lines of code import the error
variants locally within the `Display` impl on `Error`.

Refactor only, no logic changes.
2023-09-20 13:13:55 +10:00
Tobin C. Harding 9d7791fcd6
Remove unnecessary self:: from error import
`Error` is in this file, no need for `self::Error`.
2023-09-20 13:13:55 +10:00
Tobin C. Harding b2e485ed51
Split the address error code out into a separate module
Split the error code out of `address/mod.rs` and into
`address/error.rs`. Code move only, no changes other than to
imports/exports etc. to make it build.
2023-09-20 13:13:55 +10:00
Tobin C. Harding f34ca0c52b
Move address.rs to address/mod.rs
In preparation for splitting out the error code move `address.rs` to
`address/mod.rs`.

File move only, no other changes.
2023-09-20 13:13:55 +10:00
Clark Moody acbf23aaa5
Add `is_multisig` helper to Script type 2023-09-19 21:37:14 -05:00
junderw 026a55809e
Fix: Script::count_sigops parsing should not return a Result 2023-09-19 00:33:54 -07:00
Subhradeep Chakraborty 71a5fe2b54 Customize Debug implementation of absolute::LockTime 2023-09-15 22:59:26 +05:30
Riccardo Casatta 3dc75b5071
Merge rust-bitcoin/rust-bitcoin#2041: Fix deprecation notice
a0a3d4728a Fix deprecation notice (Tobin C. Harding)

Pull request description:

  Recently we deprecated the `segwit_signature_hash` function but during development the deprecation notice got stale.

  Fix deprecation notice to use the actual function names.

ACKs for top commit:
  RCasatta:
    ACK a0a3d4728a
  apoelstra:
    ACK a0a3d4728a

Tree-SHA512: d84941b605c5bc6ceab75cd60eb820c1d2c16fcd1431dc3927dc22d79886d3de26fd796fab92d97e7f8d567eab0b5a1987303107720524e7b648b1168541a2ed
2023-09-12 09:01:02 +02:00
junderw c98106ef2e
Fix: IndexOutOfBoundsError Display impl has values in wrong order 2023-09-11 11:25:21 -07:00
Andrew Poelstra 5a9d70757d
Merge rust-bitcoin/rust-bitcoin#2025: Split witness version errors up
7309c7749a Split witness version errors up (Tobin C. Harding)
40db2f5ed6 witness_version: Remove rustdocs from TryFrom imlps (Tobin C. Harding)
3397ff9910 witness_version: Use Self in error From impl (Tobin C. Harding)

Pull request description:

  Done as part of the push to have small specific errors instead of large general ones.

  Split the `witness_version::Error` up into small specific errors.

  The first two patches are preparatory clean up.

ACKs for top commit:
  stevenroose:
    ACK 7309c7749a
  apoelstra:
    ACK 7309c7749a

Tree-SHA512: 9e8b4bc5db3435c88aa6de9d92f668146b20b292c3609e2d4415ff0c32a0e3923bbe765333e0d7c255c326d65680015fc9cdf3e4a994727f4d0273dc396314df
2023-09-10 15:03:33 +00:00
Andrew Poelstra 9677247b25
Merge rust-bitcoin/rust-bitcoin#2022: Make Encodable/Decodable usage uniform
4f43965ade Make Encodable/Decodable usage uniform (Tobin C. Harding)

Pull request description:

  One encodes to a writer and decodes from a reader, most of the time in the consensus `Encodable`/`Decodable` traits we use generic `R`/`W` and variable `r`/`w` but there are other places that use other characters.

  While touching these lines note also that there are a bunch of unneeded `mut`s, I'm not sure why since usually between the compiler and the linter `mut` is handled correctly.

  Make implementations of `Encodable` and `Decodable` uniform by:
  - Use R/W and r/w for trait and variable name
  - Remove unneeded mut

  (This is split out of #1891 to assist review.)

ACKs for top commit:
  apoelstra:
    ACK 4f43965ade
  stevenroose:
    ACK 4f43965ade

Tree-SHA512: 256d080b32e60a7cabf6db4945a18d7ff5b296cb848712238e33f9b1ff3cabbe7d76723fed61a04e4b2a6c9423f12c32ec10e3ebb633761122add50e6a6cc7c9
2023-09-10 14:08:03 +00:00
Andrew Poelstra f8d7bcfce2
Merge rust-bitcoin/rust-bitcoin#2040: Use weight for block size function
a68c42e113 Remove test from Transaction test names (yancy)
f796d6fef9 Use Weight type for scaled_size (yancy)
e746341f33 Add tests for scaled_size (yancy)
97b7a2dee9 Use Weight type for block base_size (yancy)
9536a9947c Add base_size test (yancy)

Pull request description:

  Use Weight type for `base_size` in Transaction.  Also a small re-factor to remove `test_` and `_tests` from the testname for transaction tests.

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

Tree-SHA512: f4ab54143cbd9b1439912390f1e0857069a32b715477a4bc08692c5e32860a7090c95a92f78b118b17c1295c45a3bbdd209ba1d68c3a934341269235040e6911
2023-09-06 14:26:09 +00:00
Andrew Poelstra 70223027b9
Merge rust-bitcoin/rust-bitcoin#2035: Improve witness display
de95bf52cb Use checked_sub (Tobin C. Harding)

Pull request description:

  Recently we "if" guarded subtraction manually using `> 0`, we can better convey the meaning by using `checked_sub` and pattern match on the option.

  Refactor only, no logic changes.

ACKs for top commit:
  RCasatta:
    utACK de95bf52cb
  apoelstra:
    ACK de95bf52cb

Tree-SHA512: 2514cc2d8af89158e5e5e5a866f3fadb4927ba07dfb4e077fd16a98acf638588bee5ce03e2dc73fbda0b5064c30d8773d3be583c03c2a5336b8738c212a9776f
2023-09-05 13:23:38 +00:00
yancy a68c42e113 Remove test from Transaction test names 2023-09-04 15:26:44 +02:00
yancy f796d6fef9 Use Weight type for scaled_size 2023-09-04 15:26:44 +02:00
yancy e746341f33 Add tests for scaled_size 2023-09-04 15:26:44 +02:00
yancy 97b7a2dee9 Use Weight type for block base_size 2023-09-04 15:26:44 +02:00
yancy 9536a9947c Add base_size test 2023-09-04 15:26:44 +02:00
Tobin C. Harding b2a7d7023c
Rename XpubIdentifier to XKeyIdentifier
The BIP-32 extended key identifier is used to identify xpubs and xprivs,
we can rename it to show this.
2023-09-04 02:15:21 +10:00
Tobin C. Harding ffd2466ad1
Move XpubIdentifier to the bip32 module
As we have recently been doing, move the declaration of the hash type to
where it is used.

Move the `XpubIdentifier` hash declaration to the `bip32` module.

This is an API breaking change.
2023-09-04 02:13:55 +10:00
junderw 18e2854a42
Update base64 usage to 0.21.3 2023-08-30 22:47:56 -07:00
Tobin C. Harding bc398204bf
Remove redundant segwit version from function names
A P2TR output does not need to be clarified with version 1, it is
implicit. As with p2wpkh/p2wsh and version 0.

Remove redundant version identifiers from function names, deprecating
the originals.
2023-08-31 13:23:46 +10:00
Tobin C. Harding f17bb0d18f
Remove unnecessary reference
`T` is a generic that implements`AsRef<PushBytes>`, it should not be a
reference. This is inline with other usages of `AsRef<PushBytes>` for
example in `Builder::push_slice`.
2023-08-31 13:21:06 +10:00
Tobin C. Harding 4f43965ade
Make Encodable/Decodable usage uniform
One encodes to a writer and decodes from a reader, most of the time in
the consensus `Encodable`/`Decodable` traits we use generic `R`/`W` and
variable `r`/`w` but there are other places that use other characters.

While touching these lines note also that there are a bunch of unneeded
`mut`s, I'm not sure why since usually between the compiler and the
linter `mut` is handled correctly.

Make implementations of `Encodable` and `Decodable` uniform by:
- Use R/W and r/w for trait and variable name
- Remove unneeded mut
2023-08-31 13:14:02 +10:00
Tobin C. Harding 7309c7749a
Split witness version errors up
Done as part of the push to have small specific errors instead of large
general ones.

Split the `witness_version::Error` up into small specific errors.
2023-08-31 13:12:35 +10:00
Tobin C. Harding 40db2f5ed6
witness_version: Remove rustdocs from TryFrom imlps
These docs do not add much value. Done in preparation for splitting up
the `witness_version::Error` type into specific errors.
2023-08-31 13:12:35 +10:00
Tobin C. Harding 3397ff9910
witness_version: Use Self in error From impl
We can use self in error `From` impls with no loss of clarity and it is
more maintainable.
2023-08-31 13:12:35 +10:00
Tobin C. Harding de95bf52cb
Use checked_sub
Recently we "if" guarded subtraction manually using `> 0`, we can better
convey the meaning by using `checked_sub` and pattern match on the
option.

Refactor only, no logic changes.
2023-08-31 13:11:11 +10:00
Tobin C. Harding ccdcffe69c
Re-export Opcode
We recently rename `opcodes::All` to `Opcode` but did not re-export it
from the crate root. Since it now has a nice clear name we can do so.
2023-08-31 13:10:19 +10:00
Tobin C. Harding a0a3d4728a
Fix deprecation notice
Recently we deprecated the `segwit_signature_hash` function but during
development the deprecation notice got stale.

Fix deprecation notice to use the actual function names.
2023-08-31 13:06:03 +10:00
Riccardo Casatta feafac3c65
Merge rust-bitcoin/rust-bitcoin#1999: Fix witness display bug
84614d9997 Unit test debug print of witness with empty instruction (Tobin C. Harding)
e96be5ee6e Fix Witness debug display bug (Tobin C. Harding)

Pull request description:

  When we introduce a custom `Debug` implementation for the `Witness` we introduced a bug that causes code to panic if the witness contains an empty instruction.

  The bug can be verified by putting patch 2 first or by running `cargo run --example sighash` on master.

ACKs for top commit:
  apoelstra:
    ACK 84614d9997
  RCasatta:
    ACK 84614d9997

Tree-SHA512: d51891206ab15f74dda07eb29ff3f6c69dc3f983a5a5abb55685688548481a19f7c1d33aa1183a89c553ff2bc86cf41057c2bae33d75e8a7f3b801056775bf9e
2023-08-30 11:48:27 +02:00
Andrew Poelstra 7fd9b89e82
Merge rust-bitcoin/rust-bitcoin#2010: Use weight type for stripped_size
55e94b5dea Remove test from test names for Weight type (yancy)
142dde64c3 Use Weight type for stripped_size (yancy)
cb76f3ec43 Add scale_by_witness_factor to Weight type (yancy)
38c9e9947e Add witness scale factor to the Weight type (yancy)
77552987ab Add from_wu_usize to Weight type (yancy)
1a88c887f5 Rename strippedsize to stripped_size (yancy)
3369257c75 Fix grammar (yancy)

Pull request description:

  Return Weight type for the strippedize function.

ACKs for top commit:
  apoelstra:
    ACK 55e94b5dea
  tcharding:
    ACK 55e94b5dea

Tree-SHA512: ad3e4bc29380f22e20a6302c1b24c201c772be759c655c62ba4717840a01fcaa36f0f8442c9a3ba71c6400d6af47a9a815e6d90877b5f14c6883fb950b9669fd
2023-08-26 16:12:43 +00:00
Andrew Poelstra eef5cc3b92
Merge rust-bitcoin/rust-bitcoin#1978: Wrap the `bitcoinconsensus` error
29a4f9b114 Wrap the bitcoinconsensus error (Tobin C. Harding)

Pull request description:

  Currently the `bitcoinconsensus` error is part of the public API. This hinders maintainability because changes to the verison of `bitcoinconsensus` force a re-release in `rust-bitcoin`. This is an unnecessary maintenance burden, we can wrap the error instead.

ACKs for top commit:
  apoelstra:
    ACK 29a4f9b114
  sanket1729:
    utACK 29a4f9b114

Tree-SHA512: 36bc1b0ad5f5675d79eea2409844a839d862997c256e301c53c5f1af547edc9a0b83e586bd70e1b8853722cd7ef279e7515e09fbe942660f8049090d1be39d3a
2023-08-25 15:06:42 +00:00
Andrew Poelstra 4dc71b0de7
Merge rust-bitcoin/rust-bitcoin#2020: Add version bytes consts
f18f684ad2 Add version bytes consts (Tobin C. Harding)

Pull request description:

  BIP-32 defines 4 4-byte consts used as version bytes; currently we are hardcoding the version bytes in multiple places.

  Add BIP-32 version bytes consts and use them throughout the module.

ACKs for top commit:
  apoelstra:
    ACK f18f684ad2
  RCasatta:
    utACK f18f684ad2

Tree-SHA512: 50bf2d26f0f8e3528642ffcc621c03b82f536994deb808a6c84225676b4b8849db8e0d16e46f3819e0810296a422b31cf90d0595739910afdb92fb768ef7696e
2023-08-25 14:57:59 +00:00
Andrew Poelstra a458461a6b
Merge rust-bitcoin/rust-bitcoin#2004: psbt: Add IndexOutOfBounds error
66d5800ac0 psbt: Add IndexOutOfBounds error (Tobin C. Harding)

Pull request description:

  We currently have a bunch of functions that are infallible if the `index` argument is within-bounds however we return a `SignError`, this obfuscates the code.

  Add an `IndexOutOfBoundsError`. While we are at it make it an enum so users can differentiate between which vector the out of bounds access was attempted against.

ACKs for top commit:
  sanket1729:
    utACK 66d5800ac0. This is a clean improvement over existing code.
  apoelstra:
    ACK 66d5800ac0

Tree-SHA512: fa8a24990d1dcdab0c9b019fb2387b5a518b02d0a65715f0ab62519894b19c0c74750d3dcdc928626fa68b146038b907d79de3ba9712c9287db8fa64693ebc11
2023-08-25 13:58:06 +00:00
Andrew Poelstra 082bd03120
Merge rust-bitcoin/rust-bitcoin#2019: Rename xpub and xpriv types
be05f9d852 Rename xpub and xpriv types (Tobin C. Harding)

Pull request description:

  The BIP-32 extended public key and extended private key exist in the Bitcoin vernacular as xpub and xpriv. We can use these terms with no loss of clarity.

  Rename our current BIP-32 types

  - `ExtendedPubKey` to `Xpub`
  - `ExtendedPrivKey` to `Xpriv`

  This patch is a mechanical search-and-replace, followed by running the formatter, no other manual changes.

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

Tree-SHA512: 49925688783c3f37a9b92a9767a0df095323a3fa51f3d672a0b5dd1d8bca86f7facbcc33921274bc147b369de09042c4850b08c31e63f71110903435daa6c00c
2023-08-25 13:41:50 +00:00
Andrew Poelstra bd05e3f309
Merge rust-bitcoin/rust-bitcoin#2029: Fix clippy warnings
724be17394 Remove useless usage of vec! macro (Tobin C. Harding)
e84ca292d9 Clear incorrect implementation of clone warning (Tobin C. Harding)

Pull request description:

  A new version `clippy 0.1.72 (5680fa1 2023-08-23)` just came out and we get a two new warnings. Fix them up.

ACKs for top commit:
  apoelstra:
    ACK 724be17394

Tree-SHA512: f82f026773f8738d8d89710b36b979850e0e33c4d1afb4f78d2d4e957b37dd850ef9e83c9e25197dac981c7b49c448e49078777261749567303f1aac282b3d33
2023-08-25 13:27:12 +00:00
Andrew Poelstra 9dbefc6f85
Merge rust-bitcoin/rust-bitcoin#1988: Export all hash types
7bbdd9b2af Export all hash types (Tobin C. Harding)

Pull request description:

  During the 0.30.0 release we removed the re-exports of hash types. This upset some folk and since the aim of our re-exports is not exactly clean as well as the fact that the public API surface is not yet fixed just re-export all the hash types at the crate root again.

  This is #1792 but does not use a wildcard and also grabs the other hashes that we recently moved.

ACKs for top commit:
  apoelstra:
    ACK 7bbdd9b2af
  sanket1729:
    ACK 7bbdd9b2af

Tree-SHA512: addfb617fae2fce12eb9d198ffeb1b0c99792dfd757222c21c62bd4b408432de5dc93c42da7d886b43c29b2c34bd318573e813f567a29080fc264ee7beba0f70
2023-08-25 13:02:19 +00:00
yancy 55e94b5dea Remove test from test names for Weight type 2023-08-25 10:28:34 +02:00
yancy 142dde64c3 Use Weight type for stripped_size 2023-08-25 10:28:34 +02:00
yancy cb76f3ec43 Add scale_by_witness_factor to Weight type 2023-08-25 10:28:34 +02:00
yancy 38c9e9947e Add witness scale factor to the Weight type 2023-08-25 10:28:05 +02:00
yancy 77552987ab Add from_wu_usize to Weight type 2023-08-25 10:03:59 +02:00
yancy 1a88c887f5 Rename strippedsize to stripped_size 2023-08-25 10:03:39 +02:00
Tobin C. Harding 724be17394
Remove useless usage of vec! macro
Clippy emits a bunch of warnings of form:

  warning: useless use of `vec!`

As suggested, remove the vec and just use an array.
2023-08-25 12:30:04 +10:00
Andrew Poelstra cdbd4be4b1
Merge rust-bitcoin/rust-bitcoin#1917: hashes: Release `v0.13.0`
53f68383b7 hashes: Bump version to 0.13.0 (Tobin C. Harding)

Pull request description:

  In preparation for `hashes` release, bump the version. Depend on new version in `rust-bitcoin`.

  Note the `bitcoin-private` addition to the lock files is because we temporarily have two `bitcoin_hashes` dependencies and the secp one (v.0.12.0) depends on `bitcoin-private`.

ACKs for top commit:
  sanket1729:
    utACK 53f68383b7 . I am not sure about the exact nature of release dance between various crates in rust-bitcoin. This code and changelog entries looks good.
  apoelstra:
    ACK 53f68383b7

Tree-SHA512: a1933bcda1fa9a06c96a4c7079ff49f531e4f366373e98700446cecdf1eed5a104d4d4aa737202ec426cb1f1edf88eff5eee80df9f0cc3a9779c051a55f847b5
2023-08-24 17:22:10 +00:00
Tobin C. Harding 53f68383b7
hashes: Bump version to 0.13.0
Add a changelog entry and bump the version to 0.13.0

Does not include changes to `bitcoin` to depend on the new version.
2023-08-24 12:25:42 +10:00
Tobin C. Harding 66d5800ac0
psbt: Add IndexOutOfBounds error
We currently have a bunch of functions that are infallible if the
`index` argument is within-bounds however we return a `SignError`, this
obfuscates the code.

Add an `IndexOutOfBoundsErorr`. While we are at it make it an enum so
users can differentiate between which vector the out of bounds access
was attempted against.
2023-08-24 12:12:04 +10:00
Tobin C. Harding 0419fa278b
Add VarInt from implementations by way of macro
Throughout the codebase we cast values to `u64` when constructing a
`VarInt`. We can make the code marginally cleaner by adding `From<T>`
impls for all unsigned integer types less than or equal to 64 bits.
Also allows us to (possibly unnecessarily) comment the cast in a single
place.
2023-08-24 10:37:53 +10:00
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 f18f684ad2
Add version bytes consts
BIP-32 defines 4 4-byte consts used as version bytes; currently we are
hardcoding the version bytes in multiple places.

Add BIP-32 version bytes consts and use them throughout the module.
2023-08-22 13:49:46 +10:00
Tobin C. Harding be05f9d852
Rename xpub and xpriv types
The BIP-32 extended public key and extended private key exist in the
Bitcoin vernacular as xpub and xpriv. We can use these terms with no
loss of clarity.

Rename our current BIP-32 types

- `ExtendedPubKey` to `Xpub`
- `ExtendedPrivKey` to `Xpriv`

This patch is a mechanical search-and-replace, followed by running the
formatter, no other manual changes.
2023-08-22 13:47:35 +10:00
Andrew Poelstra 5bf2117dc4
Merge rust-bitcoin/rust-bitcoin#1995: Improve segwit signature hash API
4300cf2210 Add p2wpkh and p2wsh signature hash functions (Tobin C. Harding)

Pull request description:

  The word "segwit" refers to segwit v0 and taproot but these functions are version specific. Add `v0` into the function names.

  This is similar to #1994, both based on recent post of mine to bitcoin dev mailing list.

ACKs for top commit:
  stevenroose:
    ACK 4300cf2210
  apoelstra:
    ACK 4300cf2210

Tree-SHA512: 723fc302954514da0fa57a3890b9f62e9d8d1b25289b8db00611d8bc34c5000b9e54943f57b8e94befcaf72633ac078b2ff66a1da0c5bb483cfaa584e3cb6014
2023-08-21 14:53:04 +00:00
yancy 3369257c75 Fix grammar 2023-08-21 15:51:58 +02:00
Tobin C. Harding 752adff9d1
Add method calc_height
Add a private method to the `PartialMerkleTree` to calculate the height.
Enables removal of duplicate code.
2023-08-21 15:34:04 +10:00
Tobin C. Harding 46f5588646
Add unit test for calc_tree_width
Add a unit test for a `PartialMerkleTree` with node counts of 1-7
2023-08-21 15:34:04 +10:00
Andrew Poelstra 672656515e
Merge rust-bitcoin/rust-bitcoin#2001: Add txin base weight
7ec33d29eb refactor: developer doc first (yancy)
5496feb5c1 Add base weight const to TxIn (yancy)

Pull request description:

  Add a base weight const to TxIn.  I also used this const in strippedsize() and scaledsize().  As a different PR, I think strippedsize and scaledsize could return Weight instead of usize.  Also added a small commit to re-arrange commit messages.

ACKs for top commit:
  apoelstra:
    ACK 7ec33d29eb
  tcharding:
    ACK 7ec33d29eb

Tree-SHA512: b20f95605ed664b88df0a5a178d48f15f27d90eb404c9707aef010c4504d7ffd4a3565c217710b9289f87ed2a0724fd8f7cc78a79a58547fe3ee87339c0d74c1
2023-08-18 14:28:38 +00:00
yancy 7ec33d29eb refactor: developer doc first 2023-08-17 10:37:12 +02:00
yancy 5496feb5c1 Add base weight const to TxIn 2023-08-17 10:37:02 +02:00
Tobin C. Harding 84614d9997
Unit test debug print of witness with empty instruction
We recently fixed a bug that causes a panic if a `Witness` contains an
empty instruction. Add a unit test to verify it.
2023-08-17 09:36:11 +10:00
Tobin C. Harding e96be5ee6e
Fix Witness debug display bug
Currently if the witness has zero elements or any of the individual
witnesses is empty we panic. Panic is caused by subtracting 1 from a
zero length.

Check the length is non-zero before subtracting 1, print `[]` if empty.
2023-08-17 09:34:46 +10:00
Tobin C. Harding 7bbdd9b2af
Export all hash types
During the 0.30.0 release we removed the re-exports of hash types. This
upset some folk and since the aim of our re-exports is not exactly clean
as well as the fact that the public API surface is not yet fixed just
re-export all the hash types at the crate root again.

This is 1792 but does not use a wildcard and also grabs the other
hashes that we recently moved.
2023-08-15 14:53:27 +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 c276cea66d
Merge rust-bitcoin/rust-bitcoin#1744: Expose XOnlyPublicKey in crate root
1db0b21b84 Expose XonlyPublicKey and TapSighashType in crate root (sanket1729)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 1db0b21b84
  apoelstra:
    ACK 1db0b21b84

Tree-SHA512: c997024e046139d5aa89e16793e582dce6cfe8a06867aa11769c15f39f15159b6207227cd103b72dbb9e9dde7ad4451bfcc32bc04d74143bd2c8344f1ec488d1
2023-08-14 14:56:38 +00:00
sanket1729 1db0b21b84
Expose XonlyPublicKey and TapSighashType in crate root 2023-08-12 16:28:35 -07:00
Andrew Poelstra 19bb55f77e
Merge rust-bitcoin/rust-bitcoin#1991: Eliminate a heap allocation from PartialMerkleTree encoding & decoding
fa10668a35 Eliminate a heap allocation from PartialMerkleTree encoding & decoding (Steven Roose)

Pull request description:

  Just came across this and felt like doing this.

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

Tree-SHA512: 7167c63077851c4c461b33292948d9b09fe21eb45d52ed278ecf884ce15bc8b21c14040fa1eb5f50bfe51c5cde10abc133d0c59be502de139408c0d107ffa7eb
2023-08-11 22:23:02 +00:00
Steven Roose fa10668a35
Eliminate a heap allocation from PartialMerkleTree encoding & decoding
Just came across this and felt like doing this.
2023-08-11 20:51:55 +01:00
Andrew Poelstra f1b3e69f20
Merge rust-bitcoin/rust-bitcoin#1955: Rename `opcodes::All` to `Opcode`
63d0fa0164 Rename All to Opcode (Tobin C. Harding)
881d1b1a9d Move opcode aliases to the all module (Tobin C. Harding)
f3e8dbc392 Remove floating code comment (Tobin C. Harding)
dfd9384d14 opcodes: Fix whitespace (Tobin C. Harding)

Pull request description:

  I may be missing something about the `All` type but it seems it can be re-named to `Opcode` with no loss of clarity.

  The first few patches do some other clean ups, the last patch is the meat and potatoes.

ACKs for top commit:
  apoelstra:
    ACK 63d0fa0164

Tree-SHA512: c1b8be9e0c090d015c2bbfcb7ed6abe998ccada5a1eb0f6966c2a7dc462f8bbc45e6b99bf1387c736d2fca21a993a2c7fd5844c18b97a2e37ec43da517566ab1
2023-08-11 19:07:22 +00:00
yancy 443a4c5869 Remove pins from test script 2023-08-09 12:48:26 +02:00
Tobin C. Harding 29a4f9b114
Wrap the bitcoinconsensus error
Currently the `bitcoinconsensus` error is part of the public API. This
hinders maintainability because changes to the verison of
`bitcoinconsensus` force a re-release in `rust-bitcoin`. This is
an unnecessary maintenance burden, we can wrap the error instead.
2023-08-08 10:21:05 +10:00
Tobin C. Harding 63d0fa0164
Rename All to Opcode
The `opcodes::All` type can seemingly be re-named to `Opcode` with no
loss of clarity - unless I'm missing something.
2023-08-07 17:08:11 +10:00
Tobin C. Harding 881d1b1a9d
Move opcode aliases to the all module
The opcode aliases are just that, aliases to opcodes so they can live in
the `all` module along with the other opcodes.
2023-08-07 17:07:20 +10:00
Tobin C. Harding f3e8dbc392
Remove floating code comment
This code comment is not commenting anything, remove it.
2023-08-07 17:07:20 +10:00
Tobin C. Harding dfd9384d14
opcodes: Fix whitespace
Fix indentation of macro code and remove unnecessary line of whitespace.
2023-08-07 17:07:20 +10:00
Andrew Poelstra caf53d755d
Merge rust-bitcoin/rust-bitcoin#1968: Add max standard tx weight constant to transaction
2e3006a729 Add max standard tx weight constant to transaction (yancy)

Pull request description:

  Add a constant for the max transaction weight.  Similar to [max block weight](1b009b809b/bitcoin/src/blockdata/weight.rs (L35)).  This value is pulled from core  [here](44b05bf3fe/src/policy/policy.h (L27))

ACKs for top commit:
  apoelstra:
    ACK 2e3006a729
  sanket1729:
    ACK 2e3006a729

Tree-SHA512: 1583695f43387538f948be85ded7ff9a4bf9778169acb958debcbe1572a6dc8bfcd26ddfb8dbe0c030c98ab1f8a66d239a5bc663bf65ec3376a46d5f71e90894
2023-08-02 22:11:48 +00:00
Andrew Poelstra 49fa879b19
Merge rust-bitcoin/rust-bitcoin#1947: Add absolute fee rate convenience functions to `FeeRate`
9eff2f2f5e fee_rate: Add public absolute weight convenience functions (Tobin C. Harding)
f00e93bdcd Fix typos in rustdoc (Tobin C. Harding)
f3412325ea weight: Make docs uniform and terse (Tobin C. Harding)

Pull request description:

  - Patch 1 is docs cleanup
  - Patch 2 adds two functions to `FeeRate`

  From the commit log of patch 2:

      Calculating the absolute fee from a fee rate can currently be achieved
      by creating a `Weight` object and using
      `FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
      can add two public convenience functions that make discovery of the
      functionality easier.

      Add two functions for calculating the absolute fee by multiplying by
      weight units (`Weight`) and virtual bytes (by first converting to weight
      units).

  This seems like an obvious thing so I'm inclined to think that Kixunil left it out for a reason. (Mentioning you here Kix so even if this merges you'll see it in notifications later on.)

ACKs for top commit:
  sanket1729:
    utACK 9eff2f2f5e
  apoelstra:
    ACK 9eff2f2f5e

Tree-SHA512: 5b3997b721b7d7225bcf0e8a4a3efb6f207393541dbbcef533135af06a4d9c95210d450bb2322fd65a727e4560b29f0b20fb96c154d019fd4e745506213abc1c
2023-08-02 16:43:28 +00:00
Riccardo Casatta ff5fa41975
Merge rust-bitcoin/rust-bitcoin#1933: Fix links in docs
154552e334 docs: Do not link to std::option::Option (Tobin C. Harding)
24843468c3 Remove rustdocs links to serde (Tobin C. Harding)

Pull request description:

  Two minor patches to fix up docs links. These were originally done as part of #1880 but are unrelated so pushing them up separately.

ACKs for top commit:
  apoelstra:
    ACK 154552e334
  RCasatta:
    utACK 154552e334

Tree-SHA512: e45e1538c66b59d63a66898896927bb6c1336fb4c8515bb9e2204c8035870ef8e4a6fd32dfc83db2938afda67feb27c48989e382410f9e7ea7a967132941c720
2023-08-02 16:37:13 +02: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 48be5372d6
Merge rust-bitcoin/rust-bitcoin#1970: Return Weight in the TxIn/TxOut weight() methods
40c37adaf3 Return Weight in the TxIn/TxOut weight() methods (Daniela Brozzoni)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 40c37adaf3
  apoelstra:
    ACK 40c37adaf3

Tree-SHA512: 24e8962626deb016856d9b47f1c196eb2ca9923cd90b21304de7c2e0cc3d41596ad7d5373fd05f1664771f5b72b4070ca60a4845012eb5711a9fad7f672dfc42
2023-08-02 13:34:52 +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
yancy 2e3006a729 Add max standard tx weight constant to transaction 2023-08-02 10:51:55 +02:00
Tobin C. Harding 27b3c1e0e6
Improve the ScriptHash and WScriptHash types
Improve the script hash types by doing:

- Define the types in the `crypto::script` module
- Put the From impls directly below the type definitions

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:28:14 +10: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 9eff2f2f5e
fee_rate: Add public absolute weight convenience functions
Calculating the absolute fee from a fee rate can currently be achieved
by creating a `Weight` object and using
`FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
can add two public convenience functions that make discovery of the
functionality easier.

Add two functions for calculating the absolute fee by multiplying by
weight units (`Weight`) and virtual bytes (by first converting to weight
units).
2023-08-02 09:10:49 +10:00
Tobin C. Harding f00e93bdcd
Fix typos in rustdoc 2023-08-02 08:29:00 +10:00