Commit Graph

1221 Commits

Author SHA1 Message Date
Andrew Poelstra 2971740fd4
Merge rust-bitcoin/rust-bitcoin#2399: Use `Magic::BITCOIN` in unit tests
6ddb5cce37 Use Magic::BITCOIN in unit tests (Tobin C. Harding)

Pull request description:

  We are currently calling `From` to create the magic bytes, this is unnecessary since `Magic` provides consts.

  Refactor only, no logic changes.

ACKs for top commit:
  Kixunil:
    ACK 6ddb5cce37
  apoelstra:
    ACK 6ddb5cce37

Tree-SHA512: 20e2e017683f123309e3c0876bba42d86a9411bb225f07c486716184fc79837e04a832338ec8b18874ac76791260f6a4620b932ede92c8b222dac08d468cef8a
2024-01-25 15:38:22 +00:00
Andrew Poelstra 6bd8375959
Merge rust-bitcoin/rust-bitcoin#2402: Remove TODOs
5eb2de1660 Remove TODO about rand trait (Tobin C. Harding)
66cc007c2b p2p: Remove TODO comments (Tobin C. Harding)
0b5fb45ea0 consensus: Remove HEX_BUF_SIZE todo (Tobin C. Harding)
579668892a consensus: Remove TODO (Tobin C. Harding)
53beb9db30 Remove ancient todos in test code (Tobin C. Harding)
abe2241828 units: Remove "alloc" TODO (Tobin C. Harding)
5386ef0fd2 psbt: Delete TODO comments (Tobin C. Harding)
14c8a2232b examples: Remove TODO (Tobin C. Harding)

Pull request description:

  Done while working on #2368.  There are 5 left. Do we want to leave the MSRV ones in there?

  ```bash
  bitcoin/src/blockdata/weight.rs:66:                 // TODO replace with panic!() when MSRV = 1.57+
  bitcoin/src/consensus/serde.rs:101:    // TODO: statically prove impossible cases
  bitcoin/src/pow.rs:445:            // TODO: Use `carrying_mul` when stabilized: https://github.com/rust-lang/rust/issues/85532
  units/src/amount.rs:595:        // TODO replace whith unwrap() when available in const context.
  units/src/amount.rs:599:                // TODO replace with panic!() when MSRV = 1.57+
  ```

ACKs for top commit:
  Kixunil:
    ACK 5eb2de1660
  apoelstra:
    ACK 5eb2de1660

Tree-SHA512: 285b1711a6e6fba126e2c4159b25454c7f894122b76fde1d3d29e57b2ec0a6e90230e46ac79d70aa133da177c75d267fc5a13489b69881862649de771027ec8e
2024-01-25 15:06:28 +00:00
Andrew Poelstra 2de220ec6a
Merge rust-bitcoin/rust-bitcoin#2097: Add `Witness::p2tr_key_spend` function
6715e93e89 Add Witness::p2tr_key_spend function (Tobin C. Harding)

Pull request description:

  Add a function for creating the witness when doing a key path spend for a P2TR output.

  This mirrors what we did for P2WPKH when adding `Witness::p2wpkh`.

  Includes update to the taproot signing example to use the new constructor.

ACKs for top commit:
  Kixunil:
    ACK 6715e93e89
  apoelstra:
    ACK 6715e93e89

Tree-SHA512: aab51329e8fda471442bb9cebd6327636548dd157bb9842fe66993fcdd211bb04b2b829aa9d5962dd619f5c0b73d19644a44529c1a5958df1a6bc892147b44f5
2024-01-25 13:34:06 +00:00
Tobin C. Harding 5eb2de1660
Remove TODO about rand trait
This TODO applies to the whole codebase, remove it and add an issue.

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2401
2024-01-25 17:10:41 +11:00
Tobin C. Harding 66cc007c2b
p2p: Remove TODO comments
Remove three TODOs and create an issue:

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2400
2024-01-25 17:07:07 +11:00
Tobin C. Harding 0b5fb45ea0
consensus: Remove HEX_BUF_SIZE todo
Remove the todo, replace it with a comment stating that we guessed the
size and add an issue to measure and improve the value.

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2391
2024-01-25 16:59:56 +11:00
Tobin C. Harding 579668892a
consensus: Remove TODO
Remove todo in unit tests and add issue:

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2390
2024-01-25 16:59:56 +11:00
Tobin C. Harding 53beb9db30
Remove ancient todos in test code
These todos has been here since 2015 and 2016- I don't think they are
getting done anytime soon, just remove them.
2024-01-25 16:59:55 +11:00
Tobin C. Harding 5386ef0fd2
psbt: Delete TODO comments
Development for `psbt` has move to another repo, these TODO comments are
over there alread, lets just remove them from `rust-bitcoin` as part of
an effort to remove TODOs from the codebase.
2024-01-25 16:59:55 +11:00
Tobin C. Harding 14c8a2232b
examples: Remove TODO
Remove the todo from `sighash` and add issue:

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2386
2024-01-25 16:59:55 +11:00
Tobin C. Harding 6ddb5cce37
Use Magic::BITCOIN in unit tests
We are currently calling `From` to create the magic bytes, this is
unnecessary since `Magic` provides consts.

Refactor only, no logic changes.
2024-01-25 14:52:45 +11:00
Andrew Poelstra 48b42c10fb
Merge rust-bitcoin/rust-bitcoin#2395: Do minor error fixes
fb81bff61f Add a from impl for ParseIntError (Tobin C. Harding)
2130150df6 absolute: Use Self in error type (Tobin C. Harding)

Pull request description:

  While reviewing #2335 I noticed a few places that error code needed some love.

ACKs for top commit:
  Kixunil:
    ACK fb81bff61f
  Harshit933:
    ACK [`fb81bff`](fb81bff61f)
  apoelstra:
    ACK fb81bff61f

Tree-SHA512: 17f4e448862be47534d4c1c2962d5db8f90a471e53226022d7c2e153fc501705cd65b070eb17f3239fb8ad242223340f78fe828ea7df3d43707d3e42fcbef557
2024-01-24 22:17:46 +00:00
Andrew Poelstra 2f7d6451f8
Merge rust-bitcoin/rust-bitcoin#2392: Add functionality to serialize signatures to a writer
3cfd746bbc Add functionality to serialize signatures to a writer (Tobin C. Harding)

Pull request description:

  Serializing the ecdsa and taproot `Signature` straight to a writer is a useful thing to be able to do.

  Add `to_writer` to both `SerializedSignature`s and also to the `Signature`s (calling through to `SerializedSignature`).

  Remove TODO comments from code.

ACKs for top commit:
  Kixunil:
    ACK 3cfd746bbc

Tree-SHA512: 82eb6d42c7b327cdfe5e89348890e45ea39c664420f7ea17d7826a5c388c7aaae917b1334e3f3df645fc4a81a11b59d97c7d6958e99077fbd67193e2a588f2eb
2024-01-24 21:35:47 +00:00
Andrew Poelstra 53808fa9c9
Merge rust-bitcoin/rust-bitcoin#2388: Use hex-conservative to display pubkey
faa45cf10f Remove stale comment (Tobin C. Harding)
c82f26e960 Use hex-conservative to display pubkey (Tobin C. Harding)

Pull request description:

  We introduced `hex-conservative` ages ago, use it to display the `PublicKey`.

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

Tree-SHA512: 8ad14c7697314f8393ecb9a287215c505924d0655f7bf3536d4be83af983b142e06a96f802beb4548e2de051f1783549d8d1d1a8ebfb678f372a54010717752e
2024-01-24 17:33:12 +00:00
Andrew Poelstra 434773d993
Merge rust-bitcoin/rust-bitcoin#2362: Factor out `io::Error` from sighash errors
3c4f6850f4 Flatten trivial errors. (Martin Habovstiak)
a4d01d0b6c Factor out `io::Error` from sighash errors (Martin Habovstiak)

Pull request description:

  The hadnling of `io::Error` in sighash had a few problems:

  * It used `io::ErrorKind` instead of `io::Error` losing inforation
  * Changing `io::ErrorKind` to `io::Error` would disable `PartialEq`&co
  * The `Io` error wariants were duplicated

  It turns out all of these can be solved by moving the `Io` variant into a separate error.

ACKs for top commit:
  apoelstra:
    ACK 3c4f6850f4
  tcharding:
    ACK 3c4f6850f4

Tree-SHA512: b7ad6b692062d636ce29e4ebb448a8ac8ea3090feee1d349472e13f905f1f3785decc86e037d2d9658c1331a271e730076139a8d8f6c9b7dadda8b3221f6d434
2024-01-24 14:55:35 +00:00
Andrew Poelstra cf3a7bb758
Merge rust-bitcoin/rust-bitcoin#2366: Rename `txid` to `compute_txid`
7af3a58e91 Rename ntxid to compute_ntxid (yancy)
9bbf79b08c Rename wtxid to compute_wtxid (yancy)
57a7613973 Rename txid to compute_txid (yancy)

Pull request description:

  Rename txid to compute_txid and mark txid as deprecated.

  Closes #2363

ACKs for top commit:
  Kixunil:
    ACK 7af3a58e91
  apoelstra:
    ACK 7af3a58e91
  tcharding:
    ACK 7af3a58e91

Tree-SHA512: 0d9200588cd83c42b78f0ba5e6a6aa049e8360aec8b9881cf9bfbaaad0d256e6879b494fb3c4e2b06d371b9e5dde6addcc94c67a080d16a6eed5c9bc36bc845a
2024-01-24 14:07:03 +00:00
Tobin C. Harding 6715e93e89
Add Witness::p2tr_key_spend function
Add a function for creating the witness when doing a key path spend for
a P2TR output.

This mirrors what we did for P2WPKH when adding `Witness::p2wpkh`.

Includes update to the taproot signing example to use the new constructor.
2024-01-24 15:21:31 +11:00
Tobin C. Harding fb81bff61f
Add a from impl for ParseIntError
As is customary add a `From` impl for the `ParseIntError` and use `?`.
While this does not make much difference it saves devs wondering why
there is a `From` impl for one of the variants and not the other.
2024-01-24 13:31:57 +11:00
Tobin C. Harding 2130150df6
absolute: Use Self in error type
As is becoming conventional here, use `Self` in the `From` impl for
error type.
2024-01-24 13:29:20 +11:00
Tobin C. Harding 3cfd746bbc
Add functionality to serialize signatures to a writer
Serializing the ecdsa and taproot `Signature` straight to a writer is a
useful thing to be able to do.

To both ECDSA and Taproot types:

- Add `SerializedSignature::to_writer`
- Add `Signature::serialize_to_writer`

Remove TODO comments from code.
2024-01-24 13:02:08 +11:00
Tobin C. Harding faa45cf10f
Remove stale comment
This uses `hex-conservative` which is fast, remove the stale comment.
2024-01-24 12:12:38 +11:00
Tobin C. Harding c82f26e960
Use hex-conservative to display pubkey
We introduced `hex-conservative` ages ago, use it to display the
`PublicKey`.
2024-01-24 12:12:38 +11:00
yancy 7af3a58e91 Rename ntxid to compute_ntxid
Computing the ntxid is computationally expensive, so rename the method
accordingly.
2024-01-23 19:53:39 +01:00
Andrew Poelstra ccbc976261
Merge rust-bitcoin/rust-bitcoin#2382: Improve private function `use_segwit_serialization`
dae16f052c Use any method on iterator (Tobin C. Harding)
671dc0e9e0 Use better predicate name (Tobin C. Harding)

Pull request description:

  - Patch 1: Improve the name.
  - Patch 2: Use `any` instead of manual loop.

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

Tree-SHA512: 9b98c21cbb5fa93c011ba8bae88ab0dd2efa807d7cb220f121c56b31fbe5a9e9cd6f29badeca092e22949dc278c1a9d3dd676cd2919a11f13101d0dd83ec9313
2024-01-23 15:38:47 +00:00
Andrew Poelstra e0033367a8
Merge rust-bitcoin/rust-bitcoin#2387: Use KnowHrp instead of Network
20a5f1f35f Use KnowHrp instead of Network (Tobin C. Harding)

Pull request description:

  We have a bunch of functions that take `Network` when what they really want is something that can be converted to a `KnownHrp`.

  Make `KnownHrp` public and accept `impl Into<KnownHrp>`.

ACKs for top commit:
  Kixunil:
    ACK 20a5f1f35f
  apoelstra:
    ACK 20a5f1f35f

Tree-SHA512: d13ae989ca5136523902e938a04357776e00c650ec8699b335f04798a2fb4ea55e596b200b3ba1807d897884362ef9c419a15193ffdbd4ec26be53152a8ac1d3
2024-01-23 15:19:26 +00:00
Andrew Poelstra 7802d18557 Merge rust-bitcoin/rust-bitcoin#2381: Remove bech32 from the public API
9eeadaab98 bitcoin: Remove bech32 from the public API (Tobin C. Harding)

Pull request description:

  The only place that `bech32` appears in the pubic API is as a pub extern crate re-export. This is totally unnecessary since no other `bech32` functions or types appear in the public API.

  Removing `bech32` from the public API allows us to stabilize `rust-bitcoin` without waiting for `bech32` to stabalize - WIN.

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

Tree-SHA512: f411df7c38b417c1a4b9c175e7f7df7631d25ce23351eae8d77dff5c9aed5a4ae3b3755f0eb6e7d109f040e93d89f6777d74c2306314895f52fd349c91996c95
2024-01-23 15:09:05 +00:00
yancy 9bbf79b08c Rename wtxid to compute_wtxid
Computing the wtxid is computationally expensive, so rename the method
accordingly.
2024-01-23 15:57:50 +01:00
yancy 57a7613973 Rename txid to compute_txid
Computing the txid is computationally expensive, so rename the method
accordingly.
2024-01-23 15:57:50 +01:00
Andrew Poelstra 8b552c7706
Merge rust-bitcoin/rust-bitcoin#2393: Add kani test and remove TODO
66352cba98 Add kani test and remove TODO (Tobin C. Harding)

Pull request description:

  Add a kani test to check `div_rem`.

ACKs for top commit:
  Kixunil:
    ACK 66352cba98
  apoelstra:
    ACK 66352cba98

Tree-SHA512: 2cb277e11d9d853f92b12e3c1e93f4a1094466a7aab9a9a8d0883e015b4c0d876678d8147f107155dee11911954e7b3da6ee25e0e67f39f76a4da45b1dbc5307
2024-01-23 14:35:58 +00:00
Tobin C. Harding 66352cba98
Add kani test and remove TODO
Add a kani test to check `div_rem`.
2024-01-23 19:32:08 +11:00
Tobin C. Harding 20a5f1f35f
Use KnowHrp instead of Network
We have a bunch of functions that take `Network` when what they really
want is something that can be converted to a `KnownHrp`.

Make `KnownHrp` public and accept `impl Into<KnownHrp>`.
2024-01-23 16:53:05 +11:00
Tobin C. Harding 1fe90223aa
Remove DO_COV
We have test coverage by way of `coveralls` now. Remove the old stale
`DO_COV` stuff.

Fix: #1853
2024-01-23 14:27:10 +11:00
Tobin C. Harding dae16f052c
Use any method on iterator
We have `Iterator::any` which better shows whats going on in this loop.

Refactor only, no logic changes.
2024-01-23 10:48:33 +11:00
Tobin C. Harding 671dc0e9e0
Use better predicate name
This function is a predicate, it should have identifier `uses` not
`use`.
2024-01-23 10:46:23 +11:00
Tobin C. Harding 9eeadaab98
bitcoin: Remove bech32 from the public API
The only place that `bech32` appears in the pubic API is as a pub extern
crate re-export. This is totally unnecessary since no other `bech32`
functions or types appear in the public API.

Removing `bech32` from the public API allows us to stabilize
`rust-bitcoin` without waiting for `bech32` to stabalize - WIN.
2024-01-23 10:21:10 +11:00
Martin Habovstiak 3c4f6850f4 Flatten trivial errors.
The errors `SegwitV0Error` and `LegacyScripthashError` contained only
one variant - out of range. There will not be a new one in the future so
this change flattens it to simplify.
2024-01-22 14:20:33 +01:00
Andrew Poelstra ff51619c79
Merge rust-bitcoin/rust-bitcoin#2364: Test: add invalid segwit transaction test
fe8d559d69 test: add invalid segwit transaction test (startup-dreamer)

Pull request description:

  Tries to close #2183

  Added the test for invalid segwit transaction (witness flag is set but no witness is present) using [This suggested hex](https://github.com/rust-bitcoin/rust-bitcoin/issues/2183#issuecomment-1901207149) by Kixunil

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

Tree-SHA512: 723027e0ad9944a4763fba1e12398d7bcacdef691a40168f0be7cecdb170936f7e0c3690c4de911d086b8c5d42f5a25784f53fe096404f5cf69d6fc75c645d6e
2024-01-21 14:45:34 +00:00
startup-dreamer fe8d559d69
test: add invalid segwit transaction test 2024-01-19 23:12:08 +05:30
Martin Habovstiak a4d01d0b6c Factor out `io::Error` from sighash errors
The hadnling of `io::Error` in sighash had a few problems:

* It used `io::ErrorKind` instead of `io::Error` losing inforation
* Changing `io::ErrorKind` to `io::Error` would disable `PartialEq`&co
* The `Io` error wariants were duplicated

It turns out all of these can be solved by moving the `Io` variant into
a separate error.
2024-01-19 17:10:18 +01:00
Andrew Poelstra 01c8f2021e
Merge rust-bitcoin/rust-bitcoin#2358: Remove quadratic algorithm
a338a61cc3 Remove quadratic algorithm (Tobin C. Harding)

Pull request description:

  Currently we loop over transaction inputs within a loop over transaction inputs - ouch.

  Cache the `use_segwit_serialization` outside the iteration loop.

  Fix: #2357

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

Tree-SHA512: 91d0b46b235db57d9c28fc8da5d43a52c76a29916797a4ec44273b91eb120a928050a79cdbd704b922635dd2130db7b6e7863fd10e878eee52882c661af54c11
2024-01-19 15:27:06 +00:00
Andrew Poelstra 111094ca9e
Merge rust-bitcoin/rust-bitcoin#2329: Improve error handling in the `sighash` module
e356ff6611 Remove the now unused sighash::Error type (Tobin C. Harding)
c17324c574 Introduce segwit sighash error types (Tobin C. Harding)
f0b567313b Introduce sighash::LegacyError (Tobin C. Harding)
a1b21e2f1d Introduce sighash::TaprootError (Tobin C. Harding)
b0f20903a5 Introduce AnnexError (Tobin C. Harding)
a1a2056829 Add tx_in/tx_out accessor methods on Transaction (Tobin C. Harding)
f08aa16e91 Use Self:: in error return type (Tobin C. Harding)

Pull request description:

  Improve the error handling in the `sighash` module by adding small specific error types.

  Close: #2150

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

Tree-SHA512: e2e98a4caccae4e4acdc0e577e369fc90ee39a2206a8a1451739695fbe33ec2c3a52482b70cec8f9ee6bdb3ad7a2f4f639e8c87031878cd5d816fae24d913c42
2024-01-19 15:11:29 +00:00
Andrew Poelstra 783ba73799
Merge rust-bitcoin/rust-bitcoin#2356: Use full path in all macro usage of Result
61bf462806 Use full path in all macro usage of Result (josibake)

Pull request description:

  Follow-up to https://github.com/rust-bitcoin/rust-bitcoin/pull/2355

  Couldn't think of a clever way to do this , so just grepped for all instances of `macro_rules` and added the full path for the imports. Wasn't sure if it was necessary for `fmt::Result`, but went ahead and added the full path for consistency.

  Tested locally and confirmed this fixes the issue I was seeing.

ACKs for top commit:
  apoelstra:
    ACK 61bf462806
  Kixunil:
    ACK 61bf462806
  tcharding:
    ACK 61bf462806

Tree-SHA512: 8af105b3e6a36723804b290f8254f52e65cd42a61c323f1190e3bcbcb9e4427ff9b026a4530bafcd14aab644ccd9401fed351494457194c3a68a55f11b2a3d04
2024-01-19 13:48:33 +00:00
Tobin C. Harding e356ff6611
Remove the now unused sighash::Error type 2024-01-19 12:21:26 +11:00
Tobin C. Harding c17324c574
Introduce segwit sighash error types
Introduce two new error types to use for the segwit v0 sighash
calculation functions.
2024-01-19 12:21:26 +11:00
Tobin C. Harding f0b567313b
Introduce sighash::LegacyError
Introduce a `sighash::LegacyError` type and return it for all the
legacy sighash calculation functions.
2024-01-19 12:21:26 +11:00
Tobin C. Harding a1b21e2f1d
Introduce sighash::TaprootError
Introduce a `sighash::TaprootError` type and return it for all the
taproot sighash calculation functions.
2024-01-19 12:21:26 +11:00
Tobin C. Harding b0f20903a5
Introduce AnnexError
Split the annex related error out of the general `sighash::Error`.
2024-01-19 12:21:26 +11:00
Tobin C. Harding a1a2056829
Add tx_in/tx_out accessor methods on Transaction
In a few places in the codebase we want to grab an reference to an input
by index. To reduce code duplication add two methods on `Transaction`,
each to get a reference to an input or output respectively.

These are public methods, do not use them yet internally.
2024-01-19 12:21:25 +11:00
Tobin C. Harding a338a61cc3
Remove quadratic algorithm
Currently we loop over transaction inputs within a loop over transaction
inputs - ouch.

Cache the `use_segwit_serialization` outside the iteration loop.

Fix: #2357
2024-01-19 11:58:19 +11:00
Tobin C. Harding bd41c836ab
Fix cut'n'pasta error in map variable
We are mapping outputs not inputs.
2024-01-19 07:36:11 +11:00
josibake 61bf462806
Use full path in all macro usage of Result
If a user has defined their own alias for Result and tries to use a macro,
relative paths cause an issue. Use full paths to fix this.
2024-01-18 19:11:01 +01:00
Andrew Poelstra 9eec1082ec
Merge rust-bitcoin/rust-bitcoin#2354: Fix typos
b196f6b897 hashes: fix typos (Thabokani)
80665671cd bitcoin: fix typos (Thabokani)

Pull request description:

  bitcoin: fix typos
  hashes: fix typos

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

Tree-SHA512: 702e03a08f9500adf0ee7b7e565eeceba170691cb0ca281d8ff3ba904e857bb305c2504c48aa72a60a635508c31f98e379e31bbd5ad9685b1c241d86846ae074
2024-01-18 16:07:14 +00:00
Andrew Poelstra 1cfc7b0585
Merge rust-bitcoin/rust-bitcoin#2355: Use full path in all macro usage of Result
aa6e5cd342 Use full path in all macro usage of Result (Steven Roose)

Pull request description:

  Apparently when someone uses a custom `Result` type and then uses some of these macros, they can get type conflict errors.

  (Thanks josibake for finding this using the `sha256t_hash_newtype` macro.)

ACKs for top commit:
  josibake:
    utACK aa6e5cd342
  Kixunil:
    ACK aa6e5cd342
  apoelstra:
    ACK aa6e5cd342

Tree-SHA512: 04e51d6a4da520fd03f8c20c41707e43fc8d909de68533959373afd99e654068cedc5a6ca30bdc867a33e7e42b971a3bba623fad0fd294359948018ed55dc662
2024-01-18 15:48:21 +00:00
Steven Roose aa6e5cd342
Use full path in all macro usage of Result 2024-01-18 13:20:19 +00:00
Thabokani 80665671cd
bitcoin: fix typos 2024-01-18 14:06:23 +08:00
Tobin C. Harding 3ea44a166b
Remove usage of Cursor in pubkey sanity checks
We do not need to use `Cursor`, `io::Read` is implemented for slices of
`u8`s.
2024-01-18 09:18:56 +11:00
Tobin C. Harding 35b5350088
Remove usage of Cursor in multi key read
We do not need to know the position of the reader when reading multiple
keys, usage of `Cursor` is unnecessary.
2024-01-18 09:18:56 +11:00
Tobin C. Harding f08aa16e91
Use Self:: in error return type
As is becoming customary in this codebase use `Self::Foo` to return the
error variant in `From` impl.

Refactor only, no logic changes.
2024-01-17 13:28:31 +11:00
Tobin C. Harding 3333dbab24
Use new read_to_limit function
In the `psbt` code we have a custom `read_to_end` function, now we have
`io::Read::read_to_limit` we can remove this function.
2024-01-17 11:23:06 +11:00
Andrew Poelstra 2073a40c50
Merge rust-bitcoin/rust-bitcoin#2240: Require `BufRead` instead of `Read`
263a8b3603 Require BufRead instead of Read (Tobin C. Harding)
32d68fd1fa io: Add BufRead trait (Tobin C. Harding)

Pull request description:

  Require `BufRead` instead of `Read` for consensus decode trait.

ACKs for top commit:
  Kixunil:
    ACK 263a8b3603
  apoelstra:
    ACK 263a8b3603

Tree-SHA512: 58ad04c7267f9091738463331473bd22b61e6b06a13aec38b3602a369cd8e571d7d1388fd81dd7a0a05f2e8d5a9c35270cd8a918a4fafe636506591ed06a4cb2
2024-01-16 15:16:54 +00:00
Andrew Poelstra a3d698ac7c
Merge rust-bitcoin/rust-bitcoin#2230: Add effective value calculation
d69d62822d Add effective_value method (yancy)

Pull request description:

  Draft PR for adding effective value calculation to TxOut.  Adding this method was discussed here: https://github.com/rust-bitcoin/rust-bitcoin/pull/2217

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

Tree-SHA512: e74788fea2f97a10ccf2015406178bdac99a54207fe51f0c830937d2106d564beeebe6f70b49aded572aa17ccb42f47e25905a8f791bd9b442eff223ea59baae
2024-01-16 14:24:39 +00:00
Tobin C. Harding 263a8b3603
Require BufRead instead of Read
Our decoding code reads bytes in very small chunks. Which is not
efficient when dealing with the OS where the cost of a context switch is
significant. People could already buffer the data but it's easy to
forget it by accident.

This change requires the new `io::BufRead` trait instead of `io::Read`
in all bounds.

Code such as `Transaction::consensus_decode(&mut File::open(foo))` will
break after this is applied, uncovering the inefficiency.

This was originally Kix's work, done before we had the `io` crate.
Changes to `bitcoin` were originally his, any new mistakes are my own.
Changes to `io` are mine.

Co-developed-by: Martin Habovstiak <martin.habovstiak@gmail.com>
2024-01-16 14:36:00 +11:00
Andrew Poelstra 6702f1a144
Merge rust-bitcoin/rust-bitcoin#2342: Remove mention of core2
2dfe455161 Remove mention of core2 (Tobin C. Harding)

Pull request description:

  We no longer depend on `core2`, remove stale code comment mention of the crate.

  Fix: #2034

ACKs for top commit:
  Kixunil:
    ACK 2dfe455161
  apoelstra:
    ACK 2dfe455161

Tree-SHA512: cb723a384cd69e5b1aa70bdb25f53c818092c465783bd8a9b1ec60af488ed013d39f29057b4b09d6347b8bc52911eb6daf609bd088dec172647dbfedc2ea1791
2024-01-16 01:41:19 +00:00
Andrew Poelstra 4c9553481c
Merge rust-bitcoin/rust-bitcoin#2341: p2p: Improve nonce documentation
de9c2bc43d p2p: Improve nonce documentation (Tobin C. Harding)

Pull request description:

  Better describe what the nonce is used for.

  Note this file has not had its docs manicured so the line length is 80 still, just use the same line length instead of the conventional 100.

  Fix: #575

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

Tree-SHA512: ab18d9893fff4e673373125e607a4a60843a98cf84dc336fba9b6423da24ea3ad4c5fe5846ae8bcef51962dc2f3017157f2d7301c2c2cd1a81a37c3da6823552
2024-01-16 01:02:46 +00:00
yancy d69d62822d Add effective_value method
The effective_value method is useful for coin selection algorithms.  By
providing this effective value method, the effective value of each
output can be known during the coin selection process.
2024-01-16 00:29:26 +01:00
Tobin C. Harding 2dfe455161
Remove mention of core2
We no longer depend on `core2`, remove stale code comment mention of the
crate.

Fix: #2034
2024-01-16 09:44:00 +11:00
Tobin C. Harding de9c2bc43d
p2p: Improve nonce documentation
Better describe what the nonce is used for.

Note this file has not had its docs manicured so the line length is 80
still, just use the same line length instead of the conventional 100.

Fix: #575
2024-01-16 09:17:55 +11:00
Andrew Poelstra 6c94546360
Merge rust-bitcoin/rust-bitcoin#2248: Implement ArbitaryOrd for absolute::LockTime
518f0970c9 Implement ArbitaryOrd for absolute::LockTime (Tobin C. Harding)

Pull request description:

  At times we would like to provide types that do not implement `PartialOrd` and `Ord` because it does not make sense. I.e we do not want users writing `a < b`. This could range from kind-of-iffy to down-right-buggy (like comparing absolute locktimes).

  However this decision effects downstream users who may not care about what the ordering means they just need to use it for some other reason e.g., to use as part of a key for a `BTreeMap` (as we do in `miniscript` requiring the `AbsLockTime` type).

  A solution to this problem is to provide a wrapper data type that adds `PartialOrd` and `Ord` implementations. I wrote the `ordered` crate is for this very purpose.

ACKs for top commit:
  apoelstra:
    ACK 518f0970c9
  Kixunil:
    ACK 518f0970c9

Tree-SHA512: 05c753e650b6e2f181caf7dc363c4f8ec89237b42883bd695a64da0661436c9a7e715347f8fcf4fb19ce069cbf75a93032052e946f05fd8029f61860cf9c6225
2024-01-15 16:03:10 +00:00
Tobin C. Harding 271b45299f
Improve Signature field names
Applies to both `ecdsa::Signature` and `taproot::Signature`.

Re-name the `Signature` fields with more descriptive names. The
names used were decided upon in the issue discussion.

Impove rustdocs while we are at it.

Note, the change to `sign-tx-segwit-v0` is refactor only, the diff does
not show it but we have a local variable already called `sighash_type`
that is equal to `EcdsaSighashType::All`.

Includes a function argument rename as well, just to be uniform.

Fix: #2139
2024-01-15 10:26:40 +11:00
Andrew Poelstra 52b239ef70
Merge rust-bitcoin/rust-bitcoin#2334: Automated nightly rustfmt (2024-01-14)
e768c92ce3 2024-01-14 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK e768c92ce3 will merge with one ACK

Tree-SHA512: 76ecb717c9dee8e765fc3f4c94a58284549dc59309e35b47a183d45c482dce9ac68f2c13f010189a39c8a768410d12b895caf714c7faf409619556b8d4cfa39e
2024-01-14 15:40:26 +00:00
Fmt Bot e768c92ce3 2024-01-14 automated rustfmt nightly 2024-01-14 01:00:10 +00:00
Andrew Poelstra e762c53725
fix nightly clippy issues 2024-01-13 15:20:53 +00:00
Andrew Poelstra 5e3d1295e0
Merge rust-bitcoin/rust-bitcoin#2325: Fix some typos
04f3e939a7 Fix typos in test function names (GoodDaisy)
ed47c35b4d Fix typos in rustdocs (GoodDaisy)

Pull request description:

  1. Fix typos in rustdocs:

      hexidecimal -> hexadecimal
      lenght -> length

  2. Fix typos in test function names:

      u256_wrapping_add_wraps_at_boundry -> u256_wrapping_add_wraps_at_boundary
      u256_wrapping_sub_wraps_at_boundry -> u256_wrapping_sub_wraps_at_boundary

ACKs for top commit:
  apoelstra:
    ACK 04f3e939a7
  tcharding:
    ACK 04f3e939a7

Tree-SHA512: 3b94db3c2af71ae2f941c2662b358ea6270b5ff5a784b0c57520cad3b03c6e14adba3ee86bee21b5ba5217e40b85eb006dba16293d3842d61646bacc52fac9fe
2024-01-13 00:24:47 +00:00
GoodDaisy 04f3e939a7 Fix typos in test function names 2024-01-11 22:45:58 +08:00
GoodDaisy ed47c35b4d Fix typos in rustdocs 2024-01-11 22:45:45 +08:00
Tobin C. Harding 08d2b203a5
Remove rustdoc about attribute
Attributes are a code level thing, they should not be documented using
rustdoc.

Use code comments and simplify the comment.
2024-01-10 10:29:37 +11:00
Tobin C. Harding eea0b697bf
Fix stale docs
Recently we modified the `AddressInner` type but the docs are
stale (FTR the type is private).

Remove the stale sentence.
2024-01-10 10:29:37 +11:00
Tobin C. Harding 518f0970c9
Implement ArbitaryOrd for absolute::LockTime
At times we would like to provide types that do not implement
`PartialOrd` and `Ord` because it does not make sense. I.e., we do not
want users writing `a < b`. This could range from kind-of-iffy to
down-right-buggy (like comparing absolute locktimes).

However this decision effects downstream users who may not care about
what the ordering means they just need to use it for some other reason
e.g., to use as part of a key for a `BTreeMap` (as we do in `miniscript`
requiring the `AbsLockTime` type).

A solution to this problem is to provide a wrapper data type that adds
`PartialOrd` and `Ord` implementations. I wrote the `ordered` crate is
for this very purpose.

Feature gate a new dependency on `ordered` and implement `ArbitraryOrd`
for `absolute::LockTime`.
2024-01-09 13:15:29 +11:00
Andrew Poelstra de9f20a620
Merge rust-bitcoin/rust-bitcoin#2321: Derive `Copy` for `WitnessProgram`
b02c7d1d33 Derive Copy for WitnessProgram (Tobin C. Harding)

Pull request description:

  Recently we started using our custom `ArrayVec` for the `program` field of `WitnessProgram`, this means we can now derive `Copy`.

  Fix: #2313

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

Tree-SHA512: 5741081d578f7b056c156d046dc3b0817b4b13cf69dcc1dfb8c7f4dbe8a4f9ed6c8802aaaf2b0084dbf3984d3fde807a02dbaa8c3bd29c220b3b32d3cb7c9f38
2024-01-08 20:44:06 +00:00
Andrew Poelstra 8aab550e97
Merge rust-bitcoin/rust-bitcoin#2322: Remove Push enum
a8d50a5541 Remove Push enum (Tobin C. Harding)

Pull request description:

  The `Push` enum is only ever used to get access to one of its variants. Since it is a private type we can remove it entirely and just return `PushBytes` from the `last_pushdata` function.

  Needs careful review but I believe the function name is still correctly descriptive.

  This was discovered by of a new nightly clippy warning.

ACKs for top commit:
  apoelstra:
    ACK a8d50a5541 Looks good to me. The latest compiler complains about the currently-unused variants.
  sanket1729:
    ACK a8d50a5541.

Tree-SHA512: 7f96057b0f6f5673252578253ad4f1789793dbf6e917d3974274dedf942da27e6247946262a0669eb500d47987788fcca0e020ed16c0d672188e95ee31163242
2024-01-08 20:29:02 +00:00
Andrew Poelstra 6aaaae6ffc
Merge rust-bitcoin/rust-bitcoin#2294: Deprecate `Script::is_provably_unspendable`
089ce8f0fb Deprecate `Script::is_provably_unspendable` (Martin Habovstiak)

Pull request description:

  This method is not really that useful because it checked an arbitrary condition. There already exists `OP_RETURN` semantics and the method didn't cover all possible ways the script may be invalid.

  This deprecates the method and documents why.

  Closes #2191

ACKs for top commit:
  apoelstra:
    ACK 089ce8f0fb
  tcharding:
    ACK 089ce8f0fb
  sanket1729:
    ACK 089ce8f0fb

Tree-SHA512: 044f1c06fb8cbea4f84817be41bf10315f690b2a42748a07c1dd1eb0ba10932456780956fc628fec4bf57fe0722129537874a77be482d6660f9e02de5fc5a8a0
2024-01-08 15:12:50 +00:00
Tobin C. Harding a8d50a5541
Remove Push enum
The `Push` enum is only ever used to get access to one of its variants.
Since it is a private type we can remove it entirely and just return
`PushBytes` from the `last_pushdata` function.

Needs careful review but I believe the function name is still correctly
descriptive.
2024-01-08 14:04:55 +11:00
Tobin C. Harding b02c7d1d33
Derive Copy for WitnessProgram
Recently we started using our custom `ArrayVec` for the `program` field
of `WitnessProgram`, this means we can now derive `Copy`.

Fix: #2313
2024-01-08 13:51:12 +11:00
Fmt Bot 774b405ba9 2024-01-07 automated rustfmt nightly 2024-01-07 00:59:57 +00:00
shuoer86 3568b9b546
Fix typos 2024-01-05 23:10:31 +08:00
Andrew Poelstra b63921d625
Merge rust-bitcoin/rust-bitcoin#2306: Improve address conversion docs
03bfe1d433 Impove rustdoc on assume_checked_ref (Tobin C. Harding)
769809f1f2 Improve the docs on as_unchecked function (Tobin C. Harding)

Pull request description:

  In #1765 we added a couple of new functions.

  - Patch 1: Fix mis-documented function.
  - Patch 2: Do trivial rustdocs fix.

ACKs for top commit:
  apoelstra:
    ACK 03bfe1d433
  Kixunil:
    ACK 03bfe1d433

Tree-SHA512: 5be6b2d288c1f4e9096014acd8618dc84a3ec6f45ae38b5d44ef7f95eccc268021bc8e8435152166606d893d4238b03e59e8f9d4fc67ba9a6c33194f3f54fc40
2024-01-04 15:59:33 +00:00
conduition 01df1417c7
use arrayvec to represent witness programs 2024-01-03 17:10:57 +00:00
Andrew Poelstra 471da86e5a
Merge rust-bitcoin/rust-bitcoin#2309: Add the implementation of `Display` for `transaction::Version`
429a3ecec4 Add the implementation of `Display` for `transaction::Version` (harshit933)

Pull request description:

  Adds the implementation of `Display` trait for `transaction::Version`

  fixes #2308

  This is unrelated to the issue but can anyone suggest some good issues that needs to be fixed. I am also taking a look but I am confused as to which I would be able to solve. I am here to learn more.
  Thank you.

ACKs for top commit:
  apoelstra:
    ACK 429a3ecec4 Merry Christmas
  tcharding:
    ACK 429a3ecec4

Tree-SHA512: 9e59a8fe494b01caa8f211441744709f26df03891be171242bea4f7ccd7c3cc58b548cad241cab5270ad66fc9bb33ea7d6f98cc60d496c47647fb3396db9410f
2024-01-02 16:37:56 +00:00
yancy 278229def5 Add allow for out of bounds indexing
Out of bounds indexing is a workaround for const panic until MSRV +1.57
2024-01-01 10:35:52 +01:00
harshit933 429a3ecec4 Add the implementation of `Display` for `transaction::Version` 2023-12-22 21:08:26 +05:30
Tobin C. Harding 03bfe1d433
Impove rustdoc on assume_checked_ref
Add a newline to separate the body and heading of function rustdoc.
2023-12-20 09:34:53 +11:00
Tobin C. Harding 769809f1f2
Improve the docs on as_unchecked function
The `as_unchecked` method is never dangerous to call because an
`Address<UncheckedNetwork>` provides a subset of functionality that is
always ok to use. It is only dangerous to go the other way unchecked to
checked.
2023-12-20 09:34:24 +11:00
Martin Habovstiak 5fd731f095
Don't match on complex expression
Passing a complex expression to `match` is hard to read. Assign it to a
variable first.
2023-12-19 19:28:44 +00:00
Andrew Poelstra 6cdbb04820
clippy: whitelist uninhabited_references lint
This lint triggers on `fn input_len(&self) -> usize { match *self {} }`
where Self is an infallible type, claiming that the dereference of self
is UB. Maybe it would be, if this were possible. But it's not, and this
is literally the only point of using infallible types, so this lint is
always wrong.

Enabled in rustc 1.76 as warn by default.
2023-12-19 16:38:56 +00:00
Fmt Bot 5af7727250 2023-12-17 automated rustfmt nightly 2023-12-17 00:59:05 +00:00
Martin Habovstiak 089ce8f0fb Deprecate `Script::is_provably_unspendable`
This method is not really that useful because it checked an arbitrary
condition. There already exists `OP_RETURN` semantics and the method
didn't cover all possible ways the script may be invalid.

This deprecates the method and documents why.
2023-12-15 23:55:21 +01:00
Andrew Poelstra 9f68e6a6b5
Merge rust-bitcoin/rust-bitcoin#2286: fix : adds the arrayvec dependency
8783d526bd fix : adds the arrayvec dependency (harshit933)

Pull request description:

  This commit adds the arrayvec dependency to the sortKey.

  Potential fix #2276

ACKs for top commit:
  Kixunil:
    ACK 8783d526bd
  apoelstra:
    ACK 8783d526bd

Tree-SHA512: 35f28ade02dd526ce5dfa2f42578b36cd5af29a5a9f409da70a775bc12046674737e9bce9fabcc87f1b4669080ad10465c75601342f280c11eab11f791f44c36
2023-12-15 22:37:00 +00:00
harshit933 8783d526bd fix : adds the arrayvec dependency
This commit adds the arrayvec dependency to the sortKey.
2023-12-15 23:46:12 +05:30
Tobin C. Harding 4354f37f51
Use NetworkKind in bip32 module
BIP-32 only differentiates between mainnet and some testnet when
encoding and decoding xpubs and xprivs. As such we can use the new
`NetworkKind` type instead of `Network` throughout the `bip32` module.
2023-12-15 11:50:50 +11:00
Tobin C. Harding 35bbfcded7
Use NetworkKind in PrivateKey
We only use the network to serialize and deserialize from WIF.
For this we only really need network kind since WIF only differentiates
between mainnet and non-mainnet.
2023-12-15 11:49:43 +11:00
Tobin C. Harding d22f3828f6
Use NetworkKind in address module 2023-12-15 11:49:41 +11:00
Tobin C. Harding 6d5ef23e61
Add NetworkKind
Add a new type `NetworkKind` the describes the kind of network we are
on, ether mainnet or one of the test nets (testnet, regtest, signet).

Do not use the type yet.
2023-12-15 11:40:38 +11:00
Andrew Poelstra 3d6151b9e1
Merge rust-bitcoin/rust-bitcoin#2277: Implement `CompressedPublicKey`
a92d49fe33 Implement `CompressedPublicKey` (Martin Habovstiak)

Pull request description:

  P2WPKH requires keys to be compressed which introduces error handling even in cases when it's statically known that a key is compressed. To avoid it, this change introduces `CompressedPublicKey` which is similar to `PublicKey` except it's statically known to be compressed.

  This also changes relevant code to use `CompressedPublicKey` instead of `PublicKey`.

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

Tree-SHA512: ff5ff8f0cf81035f042dd8fdd52a0801f0488aea56f3cdd840663abaf7ac1d25a0339cd8d1b00f1f92878c5bd55881bc1740424683cde0c28539b546f171ed4b
2023-12-14 00:08:46 +00:00
Tobin C. Harding 0d9c7add4a
Remove unnecessary private extern crate
In #2278 I mistakenly left in the `use extern crate` after removing the
`pub` from it - while not incorrect it is unnecessary.
2023-12-14 08:49:44 +11:00
Andrew Poelstra 6b9f927f7f
Merge rust-bitcoin/rust-bitcoin#2278: Fully encapsulate bitcoinconsensus
43b1ed1b86 Fully encapsulate bitcoinconsensus (Tobin C. Harding)

Pull request description:

  The `bitcoinconsensus` crate is not fully under our control because it exposes code from Core, so we cannot guarantee its stability across versions. To make our semver compliance easier we can fully encapsulate the `bitcoinconsensus` crate so it does not appear in our public API.

  ### Please note that with this applied:

  - The `bitcoinconsenus` crate is no longer exported at the crate root
  - No `bitcoinconsensus` types appear in our public API

ACKs for top commit:
  Kixunil:
    ACK 43b1ed1b86
  apoelstra:
    ACK 43b1ed1b86

Tree-SHA512: 9fc4f01a35396562e980a647784b22667cbd289e45b5c122610d23a1f8bcf0fe8b9c27e33745f14ee010050d4c2d2669b679fb39c7a108e4e86d2c14fd60571a
2023-12-13 15:07:41 +00:00
Tobin C. Harding 43b1ed1b86
Fully encapsulate bitcoinconsensus
The `bitcoinconsensus` crate is not fully under our control because it
exposes code from Core, so we cannot guarantee its stability across
versions. To make our semver compliance easier we can fully encapsulate
the `bitcoinconsensus` crate so it does not appear in our public API.
However, it is useful to have the crate itself exported, here we add an
"unstable" feature and only publicly export the `bitcoinconsensus` crate
if the "unstable" feature is enabled.
2023-12-13 13:37:52 +11:00
Martin Habovstiak a92d49fe33 Implement `CompressedPublicKey`
P2WPKH requires keys to be compressed which introduces error handling
even in cases when it's statically known that a key is compressed. To
avoid it, this change introduces `CompressedPublicKey` which is similar
to `PublicKey` except it's statically known to be compressed.

This also changes relevant code to use `CompressedPublicKey` instead of
`PublicKey`.
2023-12-12 15:16:16 +01:00
Tobin C. Harding f06d12455f
bitcoin: Remove the custom sink
We have a sink in the new `io` crate, use that one and remove the one in
`bitcoin::prelude`.
2023-12-12 11:48:29 +11:00
Tobin C. Harding b503aa1544
Run the formatter
Run `cargo +nightly fmt`, no manual changes. Done separately to make
review of the last patch easier.
2023-12-12 11:48:29 +11:00
Tobin C. Harding 3ca55fb163
Remove qualifying path from Read and Write
There is no advantage in having `io::Read` as opposed to `Read` and
importing the trait. It is surprising that we do so.

Remove `io::` path from `io::Read` and `io::Write`. Some docs keep the
path, leave them as is. Add import `use io::{Read, Write}`.

Refactor only, no logic changes.
2023-12-12 11:48:29 +11:00
Tobin C. Harding ebeb21fa7a
Import fmt::Write using underscore
When we use the `fmt::Write` trait it is just to call its methods, we
can therefore, without any change to the logic, use `as _` when
importing the trait. This prevents naming conflicts.

Done in preparation for importing the `io::Write` trait.
2023-12-12 11:48:29 +11:00
Tobin C. Harding e2dbcb1d28
Use W for writer generic type
Generic types can be single letters, and a writer is conventionally, in
this codebase at least, called `W`.

Use `W` instead of `Write` with no loss of clarity.
2023-12-12 11:48:29 +11:00
Tobin C. Harding 8704d9f0ae
docs: Fix grammar 2023-12-12 11:48:29 +11:00
Andrew Poelstra 199c482b26
Merge rust-bitcoin/rust-bitcoin#1832: Remove Network from AddressInner
1ee989a3af Remove private fmt_internal function (Tobin C. Harding)
923ce7402d Remove Network from AddressInner (Tobin C. Harding)
3490433618 Return error from wpubkey_hash (Tobin C. Harding)
f7ab253ce4 Remove stale comment (Tobin C. Harding)

Pull request description:

  An `AddressInner` struct (contains `Network` field) is created when parsing address strings however address strings do not map 1:1 to `Network` because signet and testnet use the same bech32 prefix "tb".

  We can fix this by inlining the `Payload` variants into `AddressInner` and adding prefix enums for legacy addresses and an `Hrp` for bech32 addresses.

  Fix: #1819

ACKs for top commit:
  Kixunil:
    ACK 1ee989a3af
  apoelstra:
    ACK 1ee989a3af

Tree-SHA512: 1c2749dc929a1e9ad9b9feb01bec5c96b5aec07c6d646d88652deca7abe485907403116e9e29a0ab7dc06223254c4b49a384043284ec0a68fd76f9ab551e9e8a
2023-12-11 18:01:47 +00:00
Andrew Poelstra 4777ec9a90
Merge rust-bitcoin/rust-bitcoin#1225: Add `bitcoin-units` crate
396e049a7a Use InputString instead of String (Tobin C. Harding)
acacf45edf Add ParseDenominationError (Tobin C. Harding)
69e56a64ed Add bitcoin-units crate (Tobin C. Harding)
4ecb1fe7da internals: Add docs to InputString (Tobin C. Harding)
fa8d3002cd internals: Fix docs typo (Tobin C. Harding)

Pull request description:

  Create a new  `bitcoin-units` crate as described [here](https://github.com/rust-bitcoin/rust-bitcoin/issues/550#issuecomment-1012103022).

  Only the `amount` module is currently included.

  I've resolved the `Encodale/Decodable` issue by keeping the `amount` module in `bitcoin`.

ACKs for top commit:
  Kixunil:
    ACK 396e049a7a
  apoelstra:
    ACK 396e049a7a

Tree-SHA512: caf5e9da0458435ab19d00d4506896257e898525a4472d435fdac1d1a37bb747befd56993b106673f938475e5777d952a13ba04a2d3cb710d7afe7f5faebb7b8
2023-12-11 17:54:30 +00:00
Andrew Poelstra e0886e6a5f
Merge rust-bitcoin/rust-bitcoin#2263: `TaprootMerkleBranch` improvements
e1cc98986c Put `#[inline]` on trivial functions (Martin Habovstiak)
e531fa612b Move `TaprootMerkleBranch` and impl `IntoIterator` (Martin Habovstiak)
9d23c1d0a8 Implement std traits for `TaprootMerkleBranch` (Martin Habovstiak)
93b415589d Rename `inner` to `slice`/`vec` (Martin Habovstiak)
bb0f839c2f Lint with nightly (Martin Habovstiak)

Pull request description:

  This contains several improvements to `TaprootMerkleBranch` that make the API more idiomatic.

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

Tree-SHA512: b2bf52b027e7c1f8588c54e8b8d7a5fa54011dc521bd917995011d5fcc16c50a486eb89c0cdae2557a58adbe7708a4f2bc8f4c492e3d88c679f2abf85b1e7c83
2023-12-11 14:46:31 +00:00
Andrew Poelstra c53402790e
Merge rust-bitcoin/rust-bitcoin#2255: Fix: TxOut::minimal_non_dust and Script::dust_value
1b23220d10 Fix: TxOut::minimal_non_dust and Script::dust_value (Jonathan Underwood)

Pull request description:

  Fixes #2192

  TxOut::minimal_non_dust has 3 problems.

  1. There is an invisible dependency on Bitcoin Core's default minrelaytxfee value. It has been made explicit.
  2. There is an off by one error. The dust limit comparison uses < and therefore `+ 1` was not needed. It has been fixed.
  3. It was not returning 0 amount for OP_RETURN outputs.

  Script::dust_value has 2 problems.

  1. The dust amount depends on minrelaytxfee which is configurable in Bitcoin Core. This method was not configurable.
  2. The division operation was done before multiplying the byte amount, which can cause small differences when using uncommon scripts and minrelaytxfee values.

ACKs for top commit:
  Kixunil:
    ACK 1b23220d10
  apoelstra:
    ACK 1b23220d10

Tree-SHA512: eafd5112fbf773d86e094e3a69c519dd32f5074f5c9c63a8d69b1c9796579a8f2c2d11ad0995d8252c25b7fed5cd7c968ab88a70588986981a0a63649d43e197
2023-12-11 13:24:16 +00:00
Tobin C. Harding 69e56a64ed
Add bitcoin-units crate
Add a new crate `bitcoin-units`, move the `amount` module over to it and
re-export all types from `bitcoin::amount` so this as not a breaking
change.
2023-12-11 08:52:31 +11:00
Andrew Poelstra b81faab33d
Merge rust-bitcoin/rust-bitcoin#2269: Remove impossible InvalidParity error variant
c7c553ebc0 Remove impossible InvalidParity error variant (Martin Habovstiak)

Pull request description:

  Since we do `& 1`, only 0 and 1 are possible values, so the error return there can never happen. I made this explicit by manually setting the parity.

  This is a rebase of Steven's change #2163 with a rewrite of `match` to not panic.

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

Tree-SHA512: 5591fda686295f330b6da757a0052687eddec135ac947a2801343f1681bc4fd9f6cfb722ac1339ae6187a8784e7629b5f12cca32b33a81ffc8791b4407b29f85
2023-12-10 14:38:32 +00:00
Andrew Poelstra e235a80c59
Merge rust-bitcoin/rust-bitcoin#2214: Derive Debug for PrivateKey for no-std builds
3d17031725 Derive Debug for PrivateKey for no-std builds (Tobin C. Harding)

Pull request description:

  Currently we derive `impl Debug for PrivateKey` for "std" builds and manually implement an obfuscated version for "no-std" builds. Since we enable the `hashes` feature of `rust-secp` this is unnecessary because secp takes care of obfuscating the secret for us.

ACKs for top commit:
  apoelstra:
    ACK 3d17031725
  Kixunil:
    ACK 3d17031725

Tree-SHA512: 0ce394c6517c51e8964290a980cddd20186d19bcc6cbb8c71aa09b7485d6a0df373960798418184971e1c6e5a6b8f725dd44ebfa7184e31b63faf105dea69725
2023-12-10 14:29:53 +00:00
Fmt Bot 8e7afe5d4a 2023-12-10 automated rustfmt nightly 2023-12-10 00:58:56 +00:00
Andrew Poelstra aeb220ddc2
Merge rust-bitcoin/rust-bitcoin#2178: Move hash types to where they live
801c72e056 Add deprecation comment to hash_types module (Tobin C. Harding)
61351c917f Move impl_asref_push_bytes to internal_macros (Tobin C. Harding)
2b4b66dee3 Move impl_hashencode to internal_macros (Tobin C. Harding)
2a0ac1258a Move the bip158 filter hash types (Tobin C. Harding)
3107f80aac Move transaction hash types (Tobin C. Harding)
61c02ff202 Move block hash types (Tobin C. Harding)

Pull request description:

  Move hash types out of `hash_types` and into the modules where they are primarily used. Adds deprecated re-export so this is not a breaking change.

  Is an alternate solution to #2072

  Resolves: #2072

ACKs for top commit:
  apoelstra:
    ACK 801c72e056
  Kixunil:
    ACK 801c72e056

Tree-SHA512: 4ccac63553de3f7d417213429c0f5c2b7ebc3c2d77a9feb6d4a7daa233565fc62617edf6426a421d251eadc0841235a719bd7fd3f980302c7a2bf3dacb8b4a61
2023-12-10 00:18:11 +00:00
Martin Habovstiak c7c553ebc0 Remove impossible InvalidParity error variant
Since we do `& 1`, only 0 and 1 are possible values, so the error return
there can never happen. I made this explicit by manually setting the
parity.

This is a rebase of Steven's change with a rewrite of `match` to not
panic.
2023-12-09 01:27:36 +01:00
Martin Habovstiak e1cc98986c Put `#[inline]` on trivial functions
These functions either delegate to functions with identical signature or
contain condition that may be optimized-out after inlining.
2023-12-09 00:35:18 +01:00
Martin Habovstiak e531fa612b Move `TaprootMerkleBranch` and impl `IntoIterator`
Since the iterator created by `IntoIterator` should be called `IntoIter`
we move the whole `TaprootMerkleBranch` to its own module which contains
the type to avoid confusion. This has an additional benefit of reducing
the scope where the invariant could be broken. This already uncovered
that our internal code was abusing access to the private field (although
the code was correct).

To implement the iterator we simply delegate to `vec::IntoIter`,
including overriding the default method which are likely to be
implemented by `Vec` more optimally. We avoid exposing `vec::IntoIter`
directly since we may want to change the representation (e.g. to
`ArrayVec`).
2023-12-09 00:32:22 +01:00
Martin Habovstiak 9d23c1d0a8 Implement std traits for `TaprootMerkleBranch`
The type is naturally a collection of hashes so make it behave that way
by implementing `Deref`, `AsRef`, `Borrow` and their mutable versions as
well as `IntoIterator` for its reference. `IntoIterator` for itself is
not yet implemented because it's a bit more complicated.
2023-12-09 00:30:21 +01:00
Martin Habovstiak 93b415589d Rename `inner` to `slice`/`vec`
These names are more descriptive.
2023-12-08 13:22:47 +01:00
Martin Habovstiak bb0f839c2f Lint with nightly
While `clippy` now allows `TBD` to be used in `since` parameter of
`deprecated` attribute it is only available in the newest, nightly,
version. Switch `clippy` version to nightly to enable the `TBD` value.
2023-12-08 13:22:41 +01:00
Jonathan Underwood 1b23220d10
Fix: TxOut::minimal_non_dust and Script::dust_value
TxOut::minimal_non_dust has 3 problems.

1. There is an invisible dependency on Bitcoin Core's default minrelaytxfee value. It has been made explicit.
2. There is an off by one error. The dust limit comparison uses < and therefore `+ 1` was not needed. It has been fixed.
3. It was not returning 0 amount for OP_RETURN outputs.

Script::dust_value has 2 problems.

1. The dust amount depends on minrelaytxfee which is configurable in Bitcoin Core. This method was not configurable.
2. The division operation was done before multiplying the byte amount, which can cause small differences when using uncommon scripts and minrelaytxfee values.
2023-12-07 22:55:22 -07:00
Tobin C. Harding b058030384
Add deprecated bip32 types back in
In the 0.31.0 release we renamed the bip32 extended key types without
leaving the originals in there marked as deprecated. This makes for a
bad experience for devs, add them back in.
2023-12-08 06:13:46 +11:00
Andrew Poelstra db37bd27a1
Merge rust-bitcoin/rust-bitcoin#2085: Update docs on witness_mut
98ce46c009 Update docs on witness_mut (Tobin C. Harding)

Pull request description:

  Recently during the rust-bitcoin workshop at TABConf devs were thrown off by the example on `witness_mut`.  We have some work going on to add examples and a cookbook that all demonstrate usage of `witness_mut`.

  Remove the docs on `witness_mut` and direct devs to the `examples/sign-tx-*` files.

ACKs for top commit:
  apoelstra:
    ACK 98ce46c009
  Kixunil:
    ACK 98ce46c009

Tree-SHA512: e662213db4cbdaa53f6927cc1b10c1b6276f538cc6ad0d4bfff6dfcbf042f287a14bf5bfc88eeba7a32646c3d6741c5e09d11bb76666572a12a2043db55a2f38
2023-12-07 13:45:26 +00:00
Tobin C. Harding 98ce46c009
Update docs on witness_mut
Recently during the rust-bitcoin workshop at TABConf devs were thrown
off by the example on `witness_mut`.

Attempt to improve the docs on `witness_mut`.
2023-12-07 09:06:54 +11:00
Andrew Poelstra e09ef5cf12
Merge rust-bitcoin/rust-bitcoin#2156: Taproot sig on stack
0ac9ad16ce Add `taproot::SerializedSignature` (Martin Habovstiak)
dffa51e735 Move taproot module to a subdirectory (Martin Habovstiak)

Pull request description:

  Previously `taproot::Signature` could be only serialized into `Vec<u8>`
  which forced allocation. This adds a `SerializedSignature` type which
  acts like `Box<u8>` but is on stack.

  Note: the code was copied from `secp256k1::ecdsa::serialized_signature`
  with minimal changes.

ACKs for top commit:
  apoelstra:
    ACK 0ac9ad16ce
  tcharding:
    ACK 0ac9ad16ce

Tree-SHA512: e3d24f4ddd8074d477c25f5378c3f57dd266405380cc54596104effbad96e7abdb201e032b4b70cdbbaac595d9ae9c4043fca79d275ce62f9f6b221e6783956e
2023-12-06 21:52:07 +00:00
Martin Habovstiak 0ac9ad16ce Add `taproot::SerializedSignature`
Previously `taproot::Signature` could be only serialized into `Vec<u8>`
which forced allocation. This adds a `SerializedSignature` type which
acts like `Box<u8>` but is on stack.

Note: the code was copied from `secp256k1::ecdsa::serialized_signature`
with minimal changes.
2023-12-06 11:02:35 +01:00
Martin Habovstiak dffa51e735 Move taproot module to a subdirectory
This is in preparation for adding more submodules.
2023-12-06 11:02:35 +01:00
Tobin C. Harding b72f1d70e5
bitcoin: Add DO_FEATURE_MATRIX
Add a feature matrix section to the `bitcoin` CI script as we do in
`hashes`. This means:

- test with no default features
- test with all individual features
- test with all combinations of two features

Note, with this applied all features and optional dependencies are
included in `FEATURES` (excluding `secp-lowmemory`).

The feature matrix test gets run for stable and MSRV toolchains.
2023-12-06 10:09:05 +11:00
Tobin C. Harding 48879e7ad9
Remove no-std feature
Currently `bitcoin` cannot be built with no features enabled, it must
have either "no-std" or "std" enabled. This is an artifact from when
we depended on `core2` for "no-std", now that we have our own `io` crate
and we unconditionally depend on it we can remove the "no-std" feature.
2023-12-06 09:54:33 +11:00
Tobin C. Harding 801c72e056
Add deprecation comment to hash_types module
The `hash_types` module has been emptied, it now contains only
deprecated re-exports. Add a rustdoc comment stating as such.
2023-12-05 15:01:18 +11:00
Tobin C. Harding 61351c917f
Move impl_asref_push_bytes to internal_macros
We are emptying the `hash_types` module. `impl_asref_push_bytes!` is an
internal macro, as such it can live in the `internal_macros` module.

While we are at it import the macro and call it without any qualifying
path, this is typical for our usage of other internals/internal_macros
usage.
2023-12-05 15:01:17 +11:00
Tobin C. Harding 2b4b66dee3
Move impl_hashencode to internal_macros
We are emptying the `hash_types` module. `impl_hashencode!` is an
internal macro, as such it can live in the `internal_macros` module.
2023-12-05 15:01:17 +11:00
Tobin C. Harding 2a0ac1258a
Move the bip158 filter hash types
We would like all the various hash types to be defined where they
rightly live instead of in the `hash_types` module.

Move the BIP-158 filter hash types to the `bip158` module.
2023-12-05 15:01:17 +11:00
Tobin C. Harding 3107f80aac
Move transaction hash types
We would like all the various hash types to be defined where they
rightly live instead of in the `hash_types` module.

Move transaction hash types to the `transaction` module.
2023-12-05 14:57:48 +11:00
Tobin C. Harding 61c02ff202
Move block hash types
We would like all the various hash types to be defined where they
rightly live instead of in the `hash_types` module.

Move the block hash types to the `block` module. While moving, add full
stops to the rustdoc of each hash.

Re-export _all four_ types from lib.rs (previously `WitnessMerkleNode`
was not re-exported).
2023-12-05 14:57:28 +11:00
Tobin C. Harding 1ee989a3af
Remove private fmt_internal function
Just use `fmt::Display::fmt` directly since `fmt_internal` does exactly
that.

Refactor only, no logic changes.
2023-12-05 09:27:15 +11:00
Tobin C. Harding 923ce7402d
Remove Network from AddressInner
An `AddressInner` struct is created when parsing address strings however
address strings do not map 1:1 to `Network` because signet and testnet
use the same bech32 prefix "tb".

We can fix this by inlining the `Payload` variants into `AddressInner`
and adding prefix enums for legacy addresses and a `KnownHrp` for bech32
addresses.

Also enables removing the `AddressEncoding` struct as we can display the
`AddressInner` struct directly. (The `Display` impl is on `AddressInner`
and not directly on address to ignore the `NetworkValidation` wrapper,
may be able to be simplified still further.)
2023-12-05 09:27:15 +11:00
Tobin C. Harding 3490433618
Return error from wpubkey_hash
Calling `wpubkey_hash` on a key that is uncompressed is flat out an
error, really it is a programmer error at build time because a segwit
key should never be compressed, however, for historical reasons we do
not enforce this in the type system. As a step towards clarity make it
an error to call `wpubkey_hash` on a an uncompressed pubkey. This adds
documentation and potentially might assist debugging for newer devs.
2023-12-05 09:21:51 +11:00
Tobin C. Harding f7ab253ce4
Remove stale comment
This comment appears to come from before we had types for tweaked and
untweaked keys in taproot. We can remove it.
2023-12-05 09:21:51 +11:00
Tobin C. Harding 7d71fb9fdb
Re-order dependencies in manifest
Currently the feature enabling is different for "std" and "no-std",
which is again different to the order in the dependencies section. These
two things make reading the manifest harder than it needs to be.

Put the dependencies in alphabetic order in the dependencies section as
well as when enabling them.

Refactor only, no logic changes.
2023-12-04 14:21:06 +11:00
apoelstra b7604dd768 2023-12-03 automated rustfmt nightly 2023-12-03 00:58:10 +00:00
Andrew Poelstra a3f6f53d37
Merge rust-bitcoin/rust-bitcoin#2234: Inline io module in the io crate root
f764a607ac Use conventional import path for io crate (Tobin C. Harding)
5c0759a390 Inline io module in io crate root (Tobin C. Harding)
80fe9b99b2 Move public macros to a separate module (Tobin C. Harding)

Pull request description:

  Its not immediately obvious why we nest the whole `io` code in an `io` submodule within `lib.rs`. As far as I can tell we can inline it and re-export from `rust-bitcoin` same as we do for our other dependencies.

  This change would effect other users of the crate but since the `io` crate is unreleased this effects no-one except us.

  After doing this it might be because `crate::io::Foo` looks good when near `std::io::Foo`?

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

Tree-SHA512: 38888b0c23d5f2cd874f77dd332fe4fa4b9acb90e3a2dac19e62ed3d98151acd7480c719aa85434e1a3de987af2c4f565528a914a14d5fd3f0f0e410cbdf5d40
2023-11-30 19:16:17 +00:00
Tobin C. Harding f764a607ac
Use conventional import path for io crate
We have a convention in `rust-bitcoin` to use external crates directly
when importing them not via `crate::foo`.

Update all the import paths for `io` to use this form.
2023-11-29 08:48:03 +11:00
Andrew Poelstra 8d32a49ac1
Merge rust-bitcoin/rust-bitcoin#2228: Rename from_vb_const
fcc4c40a1c Rename from_vb_const (yancy)

Pull request description:

  The new function is more clear because the purpose of the function is to return a value that doesn't need to be unwrapped.  The current MSRV does not allow unwrap() in const context.

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

Tree-SHA512: 62bbd61800e9f29768d884dbe3bca14fea9b51b8f413131e0f29bfc0f0d0e20631d30489e078cc948f2dba0c5d7d9d7c229b4bb7187faef23083a88337efb6a6
2023-11-28 20:33:34 +00:00
yancy fcc4c40a1c Rename from_vb_const
The new function is more clear because the purpose of the function is to
return a value that doesn't need to be unwrapped.  The current MSRV does
not allow unwrap() in const context.
2023-11-28 09:01:03 +01:00
Tobin C. Harding 5c0759a390
Inline io module in io crate root
Its not immediately obvious why we nest the whole `io` code in an `io`
submodule within `lib.rs`. As far as I can tell we can inline it and
re-export from `rust-bitcoin` same as we do for our other dependencies.

This change would effect other users of the crate but since the `io`
crate is unreleased this effects no-one except us.
2023-11-28 14:17:14 +11:00
conduition 32eb461523 add conversions from TXIDs to merkle nodes 2023-11-27 00:12:16 +00:00
Andrew Poelstra cfa6768e79
Merge rust-bitcoin/rust-bitcoin#2221: Implement LowerHex and UpperHex for CompactTarget
58710dfb91 Implement LowerHex and UpperHex for CompactTarget (Vojtěch Toman)

Pull request description:

  closes #2219

ACKs for top commit:
  Kixunil:
    ACK 58710dfb91
  apoelstra:
    ACK 58710dfb91

Tree-SHA512: 38d8bd6573f0f033f5f0948062174f7a49db02d788a5176375a869633984451651bc738cd0588d5c6e4515f521257899bdf9f61fd4be350d80a26a96f16b19b1
2023-11-26 14:32:47 +00:00
Andrew Poelstra 4806461c75
Merge rust-bitcoin/rust-bitcoin#2188: Bump MSRV to Rust version 1.56.1
761de886be Remove imports of TryFrom and TryInto (Tobin C. Harding)
4d5415f835 Add rust-version to the workspace manifests (Tobin C. Harding)
a41e978855 Update to edition 2021 (Tobin C. Harding)
d9cc724187 Bump MSRV to Rust version 1.56.1 (Tobin C. Harding)

Pull request description:

  Rust version 1.56.0 introduced edition 2021. Shortly afterwards, on October 21 2021 Rust version 1.56.1 was released.

  Debian stable is currently shipping `rustc 1.63.0`. Our stated MSRV policy is: In Debian stable and at least 2 years old. Therefore our MSRV policy is met by Rust version 1.56.1 and we can strat to bump our MSRV org wide. Start by bumping the `rust-bitcoin` and `hashes` MSRV to Rust 1.56.1

  Start by bumping the `rust-bitcoin` and `hashes` MSRV to Rust 1.56.1, includes:

  - Update docs.
  - Update CI and remove pinning.
  - Update the build files and remove now stale cfg attributes rust_v_1_x for values less than the new MSRV.
  - Use new `IntoIterator` for arrays so we no longer need to allocate a vector to iterate.

  Links:

  - https://blog.rust-lang.org/2021/11/01/Rust-1.56.1.html
  - https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
  - https://packages.debian.org/stable/rust/rustc

ACKs for top commit:
  Kixunil:
    ACK 761de886be
  apoelstra:
    ACK 761de886be

Tree-SHA512: 3a81c8bfa37d8cec0ec794f516f014da67ae8e437decf149c9681aa547885acac0ee07ea2c0f42e4f6bfd6f7ed1695fcf4747f53cc50e5f4e70ce3fe7bcba4e9
2023-11-26 13:48:23 +00:00
Vojtěch Toman 58710dfb91
Implement LowerHex and UpperHex for CompactTarget 2023-11-25 23:26:57 +01:00
Tobin C. Harding 761de886be
Remove imports of TryFrom and TryInto
Now that MSRV is Rust 1.56.1 we no longer need to explicitly import
`TryFrom` and `TryInto`.

No clue why clippy didn't find these for us.
2023-11-24 03:52:05 +11:00
Andrew Poelstra 71d92bdbb9
Merge rust-bitcoin/rust-bitcoin#2095: bitcoin: Add signing examples
fa104aefa5 bitcoin: Add signing examples (Tobin C. Harding)

Pull request description:

  Add two signing examples to showcase signing a simple one input two output transaction using both segwit v0 outputs and taproot outputs.

  This patch is the result of the recent rust-bitcoin TABConf workshop, with bug fix by Sanket, updated to use APIs from tip of master branch.

  This code, depending on v0.30.0 is what was added to the cookbook.

ACKs for top commit:
  realeinherjar:
    ACK fa104aefa5
  apoelstra:
    ACK fa104aefa5

Tree-SHA512: ce0d5b8291c94387c68b5e1cf740d3267fc00c997af5b96f5be525f348140d9a9af17ab66d556990f09bf081a5a812374cb633ea276100c7c21f218b85eae3fd
2023-11-22 22:08:32 +00:00
Andrew Poelstra 2664f978f3
Merge rust-bitcoin/rust-bitcoin#2213: Fix InputWeightPrediction::P2WPKH_MAX constant DER sig length
f41ebc2149 Add test for input weight predictions (conduition)
4514a80a23 Fix the InputWeightPrediction constants for DER signatures (conduition)
b5ce219c62 add weight method to InputWeightPrediction (conduition)

Pull request description:

  The `P2WPKH_MAX` constant assumed DER signatures in the witness have a max length of 73. In practice, their maximum length is 72, because [BIP62](https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki) forbids nodes from relaying transactions which contain non-canonical ECDSA signatures (i.e. TX sigs must have an $s$ value of less than $\frac{n}{2}$).

  This means $s$ is never encoded with a leading zero byte, and the signature as a whole never exceeds 72 bytes in total encoded length. The `ground_p2wpkh` function was already correct; only the constant needed to be corrected.

  Technically 73 bytes *is* the upper limit for signatures, as nothing forbids miners from including such non-standard transaction signatures in blocks, but for the purposes of fee estimation and input weight prediction, 72 is the number which 99.9% of implementations should use as their ceiling. We already use it as the ceiling for the `ground_p2wpkh` function - `ground_p2wpkh(0)` returns a prediction which uses a witness signature of length 72.

  Reference:
  - https://bitcoin.stackexchange.com/questions/77191/what-is-the-maximum-size-of-a-der-encoded-ecdsa-signature
  - https://bitcoin.stackexchange.com/questions/106435/are-high-s-ecdsa-signatures-forbidden-in-segwit-witnesses
  - https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki

  To enable testing, I added a `weight()` method to `InputWeightPrediction` and made it public but i'm not sure whether it has a use-case. Let me know if I should make it private instead.

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

Tree-SHA512: 10e837bad9881c0efebb0598eaefd4ab039f2a6ececead75a68e253d84f5e85cb30496a6069eee8dfe9714773f3aa23cfe373f5d88d1c5609e1b1be1ff142e37
2023-11-22 20:36:11 +00:00
Tobin C. Harding 4d5415f835
Add rust-version to the workspace manifests
Add `rust-version = 1.56.1` to all crates in the workspace i.e.,
including `fuzz` but excluding the various test crates.
2023-11-23 06:20:04 +11:00
Tobin C. Harding a41e978855
Update to edition 2021
We just bumped the MSRV to Rust 1.56.1 which includes edition 2021.

Update all crates in this repo to use edition 2021 and build/lint
warnings.
2023-11-23 06:20:03 +11:00
Tobin C. Harding d9cc724187
Bump MSRV to Rust version 1.56.1
Rust version 1.56.0 introduced edition 2021. Shortly afterwards, on
October 21 2021 Rust version 1.56.1 was released.

Debian stable is currently shipping `rustc 1.63.0`.

Our stated MSRV policy is: In Debian stable and at least 2 years old.

Therefore our MSRV policy is met by Rust version 1.56.1 and we can strat
to bump our MSRV org wide.

Start by bumping the `rust-bitcoin` and `hashes` MSRV to Rust 1.56.1,
includes:

- Update docs.
- Update CI and remove pinning.
- Update the build files and remove now stale cfg attributes rust_v_1_x
  for values less than the new MSRV.
- Use new `IntoIterator` for arrays so we no longer need to allocate a
  vector to iterate.

Links:

- https://blog.rust-lang.org/2021/11/01/Rust-1.56.1.html
- https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
- https://packages.debian.org/stable/rust/rustc
2023-11-23 06:20:02 +11:00
Andrew Poelstra ba318f167a
Merge rust-bitcoin/rust-bitcoin#2093: Manually implement `JsonSchema`
75c490c60f hashes: Remove default features from schemars dep (Tobin C. Harding)
1105876423 Remove whitespace character from string (Tobin C. Harding)
a6d7d542ab bitcoin:: Remove dev dependency serde_derive (Tobin C. Harding)

Pull request description:

  Done while investigating removal of `serde_derive` dependency.

  - Patch 1: Do trivial dev-dep removal
  - Patch 2: Manually implement `JsonSchema` and remove default dependencies from "schemars" dependency (transitively depends on `serde_derive`)

ACKs for top commit:
  apoelstra:
    ACK 75c490c60f

Tree-SHA512: aab5bd622a76fc24259933af2f20f863d20c8ccf6e69e68246c374266c540e483ced8a769532582a184b922996857db7320a6b08ae9b5b95503eac752ef9d301
2023-11-22 18:55:41 +00:00
Andrew Poelstra aeac9bbd87
Merge rust-bitcoin/rust-bitcoin#2206: Add from_vb_const function
321d3923b8 Add from_vb_const function (yancy)

Pull request description:

  This function is can be used to construct a Weight type from_vb in const context.  Note I don't think it's possible to test the panic case since it's a compile time error work around currently to panic.

ACKs for top commit:
  tcharding:
    ACK 321d3923b8
  apoelstra:
    ACK 321d3923b8

Tree-SHA512: dc11409f0e3079400da261a8c9f580ef0527b77643ce1a5dda65c0975db19c2f2da46ac693e6a2bf49e0105b8b096e1ee51f09f5d1c78d634e4e274d7467ee05
2023-11-22 14:44:59 +00:00
conduition f41ebc2149 Add test for input weight predictions
Sanity checks the InputWeightPrediction against
a transaction which uses P2WPKH inputs.
2023-11-22 00:50:59 +00:00
conduition 4514a80a23 Fix the InputWeightPrediction constants for DER signatures
The P2WPKH_MAX constant assumed DER signatures in the witness have
a max length of 73. However, their maximum length in practice is 72,
because BIP62 forbids nodes from relaying transactions whose ECDSA
signatures are not canonical (i.e. all sigs must have an s value of
less than n/2). This means s is never encoded with a leading zero
byte, and the signature as a whole never exceeds 72 bytes in total
encoded length. The ground_p2wpkh function was already correct;
only the constant needed to be corrected.
2023-11-21 06:13:08 +00:00
conduition b5ce219c62 add weight method to InputWeightPrediction
This method computes the weight an InputWeightPrediction
would to a transaction, not including witness flag bytes.
2023-11-21 05:57:46 +00:00
yancy 321d3923b8 Add from_vb_const function
This function can be used to construct a Weight type from_vb in const
context.
2023-11-21 06:23:32 +01:00
Tobin C. Harding 3d17031725
Derive Debug for PrivateKey for no-std builds
Currently we derive `impl Debug for PrivateKey` for "std" builds and
manually implement an obfuscated version for "no-std" builds. Since
we enable the `hashes` feature of `rust-secp` this is unnecessary
because secp takes care of obfuscating the secret for us.
2023-11-21 14:51:05 +11:00
Andrew Poelstra 8aa5501827
Merge rust-bitcoin/rust-bitcoin#2202: Automated nightly rustfmt (2023-11-19)
f5882ef3c6 2023-11-19 automated rustfmt nightly (apoelstra)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK f5882ef3c6

Tree-SHA512: 4efc31ae0281dc824181177b39154f4a53475367a0e83fcbdcc4ab99cf68aefa379c850e49ac1a5d11fbd5f4fc62afa6d56393e0c6286e6ff3cae7f27583fbda
2023-11-20 14:46:26 +00:00
Tobin C. Harding a6d7d542ab
bitcoin:: Remove dev dependency serde_derive
We do not need this dependency because we can get the serde derives
directly from `serde`.

diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml
index 3868bd08..db7fb322 100644
--- a/bitcoin/Cargo.toml
+++ b/bitcoin/Cargo.toml
@@ -53,7 +53,6 @@ actual-serde = { package = "serde", version = "1.0.103", default-features = fals
 [dev-dependencies]
 serde_json = "1.0.0"
 serde_test = "1.0.19"
-serde_derive = "1.0.103"
 bincode = "1.3.1"

 [target.'cfg(mutate)'.dev-dependencies]
2023-11-20 15:17:30 +11:00
Andrew Poelstra 675da34127
Merge rust-bitcoin/rust-bitcoin#2066: Add a `bitcoin-io` crate
add371d263 Remove `core2` dependency entirely (Matt Corallo)
b7dd16da99 [IO] Use our own io::Error type (Matt Corallo)
c95b59327a Explicitly use `std::io::Error` when implementing `std` traits (Matt Corallo)
9e1cd372cb Use `io::Error::get_ref()` over `std::error::Error::source()` (Matt Corallo)
3caaadf9bb [IO] Replace the `io::Cursor` re-export with our own `Cursor` (Matt Corallo)
141343edb4 [IO] Move to custom `Read` trait mirroring `std::io::Read` (Matt Corallo)
7395093f94 Stop relying on `Take`'s `by_ref` method (Matt Corallo)
2364e1a877 Stop relying on blanket Read impl for all &mut Read (Matt Corallo)
6aa7ccf841 [IO] Replace `std::io::Sink` usage with our own trivial impl (Matt Corallo)
7eb5d65bda [IO] Provide a macro which implements `io::Write` for types (Matt Corallo)
ac678bb435 [IO] Move to custom `Write` trait mirroring `std::io::Write` (Matt Corallo)
5f2395ce56 Add missing `?Sized` bounds to `io::Write` parameters (Matt Corallo)
2348449d2a Stop relying on `std::io::Write`'s `&mut Write` blanket impl (Matt Corallo)
5e0209569c Use `io::sink` rather than our custom `EmptyWrite` utility (Matt Corallo)
a0ade883b6 [IO] Move io module into selected re-exports (Matt Corallo)
27c7c4e26a Add a `bitcoin_io` crate (Matt Corallo)

Pull request description:

  In order to support standard (de)serialization of structs, the
  `rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
  traits. This works great for environments with `std`, however sadly
  the `std::io` module has not yet been added to the `core` crate.

  Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
  used the `core2` crate to provide copies of the `std::io` module
  without any major dependencies. Sadly, its one dependency,
  `memchr`, recently broke our MSRV.

  Worse, because we didn't want to take on any excess dependencies
  for `std` builds, `rust-bitcoin` has had to have
  mutually-exclusive `std` and `no-std` builds. This breaks general
  assumptions about how features work in Rust, causing substantial
  pain for applications far downstream of `rust-bitcoin` crates.

  This is mostly done, I'm still finalizing the `io::Error` commit at the end to drop the `core2` required dep in no-std, but its getting there. Would love further feedback on the approach or code-level review on these first handful of commits.

ACKs for top commit:
  tcharding:
    ACK add371d263
  apoelstra:
    ACK add371d263
  Kixunil:
    ACK add371d263

Tree-SHA512: 18698ea8b1b65108ee0f695d5062d2562c8df2f50bf85d93442648da3b35a4184a5d5d2a493aed0adaadc83f663f0cd2ac735c34941cc9a6fa58d826e548e091
2023-11-19 14:26:19 +00:00
apoelstra f5882ef3c6 2023-11-19 automated rustfmt nightly 2023-11-19 00:58:32 +00:00
Andrew Poelstra c12debfd0c
Merge rust-bitcoin/rust-bitcoin#2172: Add `params` method to `Network`
9282cc4dad Implement standard conversions `Network`->`Params` (Martin Habovstiak)
9a8694fae5 Add `params` method to `Network` (Martin Habovstiak)

Pull request description:

  Writing `network.params()` is less annoying than `Params::network()`, so this adds it. Making it return a static could also improve performance.

  Didn't do `Params` -> `Network` conversions because of #2173

ACKs for top commit:
  tcharding:
    ACK 9282cc4dad
  apoelstra:
    ACK 9282cc4dad

Tree-SHA512: 6455956fd2c937b7212c9bab6ac7cfa05fb99b5da955f4f6690d7056cbe3902a3dadf94352c76b6866655b2e34a936191362a1cc81b33a5b252dd21dbc84d7b6
2023-11-18 18:22:07 +00:00
Andrew Poelstra c03ef3c219
Merge rust-bitcoin/rust-bitcoin#2194: Script documentation weirdness and typos
e3f2c4fa43 Fix broken link in CONTRIBUTING.md (Vojtěch Toman)
e1c4986f4a Improve Script documentation and fix typos (Vojtěch Toman)

Pull request description:

  Fixes #2193 (first commit)
  Second commit fixes broken link in `CONTRIBUTING.md`

ACKs for top commit:
  Kixunil:
    ACK e3f2c4fa43
  sanket1729:
    ACK e3f2c4fa43

Tree-SHA512: f7bd83530a071a37028c611afa1dc43de31e57b241403fa9f451be375da3db041f41dc7e07d581d770c76ab7c7a7cc68e27e68bbfc71461fe933128e7b846ae9
2023-11-16 13:25:42 +00:00
Andrew Poelstra 43cd352cf9
Merge rust-bitcoin/rust-bitcoin#2185: add input weight predictions for p2pkh outputs
c745c97e5f add input weight predictions for p2pkh outputs (conduition)

Pull request description:

  Adds input weight prediction constant and `ground_p2pkh_*` methods, mirroring those for `P2WPKH`. This seemed to be missing.

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

Tree-SHA512: 69b4484686ecf761b766d2a7d7408c784981a9ba8c4aa8abd9d8655bd9421eb882e346ece232530edf9edff602d2fe1570992a5eb7b420b928d8b13397d2f60f
2023-11-15 23:48:05 +00:00
conduition c745c97e5f add input weight predictions for p2pkh outputs
Adds missing prediction constants and const fns for
predicting the weights for P2PKH transaction inputs,
covering both compressed and uncompressed public keys.
2023-11-15 22:16:55 +00:00
Vojtěch Toman e1c4986f4a
Improve Script documentation and fix typos 2023-11-15 20:57:22 +01:00
Andrew Poelstra c0de0f7bde
Merge rust-bitcoin/rust-bitcoin#2120: Improve public re-exports
7d695f6b41 Improve public re-exports (Tobin C. Harding)
33774122e0 Remove public re-exports from private module (Tobin C. Harding)

Pull request description:

  Improve the public exports in two ways:

  1. Inline re-exports into the docs of the module that re-exports them.
  2. Separate public and private use statements

  Recently we discussed a way to separate the public and private import statements to make the code more clear and prevent `rustfmt` joining them all together.

  Separate public exports using a code block and `#[rustfmt::skip]`. Has the nice advantage of reducing the number of `#[doc(inline)]` attributes also.

  1. Modules first, as they are part of the project's structure.
  2. Private imports
  3. Public re-exports (using `rustfmt::skip` to prevent merge)

  Use the format

  ```rust
  mod xyz;
  mod abc;

  use ...;

  pub use {
      ...,
  };
  ```

  This patch introduces changes to the rendered HTML docs.

ACKs for top commit:
  apoelstra:
    ACK 7d695f6b41

Tree-SHA512: dc9121c0fe282e3035d862beadb89e2d5a374a7dab6b1c3147a9b5960f8bc2f5af49892f0f713f55c645c46f53464c32daf390c11d85c75553b3ea7e0efc8246
2023-11-15 13:51:51 +00:00
conduition 6c6c08ca50 add second test case 2023-11-10 17:19:33 +00:00
conduition 0c56131819 fix: FeeRate::checked_mul_by_weight should scale output down by 1000 2023-11-09 19:20:34 +00:00
Matt Corallo add371d263 Remove `core2` dependency entirely 2023-11-09 16:46:44 +00:00
Matt Corallo c95b59327a Explicitly use `std::io::Error` when implementing `std` traits
In the next commit, `std::io::Error` will be replaced with a
different type, and as a result `std::io::Error` must be referred
to explicitly.
2023-11-07 17:35:18 +00:00
Matt Corallo 9e1cd372cb Use `io::Error::get_ref()` over `std::error::Error::source()` 2023-11-07 17:35:18 +00:00
Matt Corallo 3caaadf9bb [IO] Replace the `io::Cursor` re-export with our own `Cursor` 2023-11-07 17:35:17 +00:00
Matt Corallo 141343edb4 [IO] Move to custom `Read` trait mirroring `std::io::Read`
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

Here we take the second big step, replacing
`{std,core2}::io::Read` with our own `bitcoin_io::io::Read`. We
provide a blanket impl for our trait for all `std::io::Read`, if
the `std` feature is enabled, allowing users who use their own
streams or `std` streams to call `rust-bitcoin` methods directly.
2023-11-07 05:51:39 +00:00
Matt Corallo 7395093f94 Stop relying on `Take`'s `by_ref` method 2023-11-07 05:51:37 +00:00
Matt Corallo 2364e1a877 Stop relying on blanket Read impl for all &mut Read 2023-11-07 05:51:36 +00:00
Matt Corallo 6aa7ccf841 [IO] Replace `std::io::Sink` usage with our own trivial impl 2023-11-07 05:51:36 +00:00
Matt Corallo 5f2395ce56 Add missing `?Sized` bounds to `io::Write` parameters
Since we are no longer relying on the blanket `io::Write` impl for
`&mut io::Write`, we should now ensure that we do not require
`Sized` for our `io::Write` bounds, as its unnecessarily
restrictive and can no longer be worked around by simply adding an
`&mut`.
2023-11-07 05:50:51 +00:00
Matt Corallo 2348449d2a Stop relying on `std::io::Write`'s `&mut Write` blanket impl
`std::io::Write` is implemented for all `&mut std::io::Write`. This
makes it easy to have APIs that mix and match owned `Write`s with
mutable references to `Write`s.

However, in the next commit we add our own `Write` trait which we
intend to implement for all `std::io::Write`. Sadly, this is
mutually exclusive with a blanket implementation on our own
`&mut Write`, as that would conflict with an `std::io::Write`
blanket impl.

Thus, in order to use the `Write for all &mut Write` blanket impl
in rust-bitcoin, we'd have to bound all `Write`s by
`std::io::Write`, as we're unable to provide a blanket
`Write for &mut Write` impl.

Here we stop relying on that blanket impl in order to introduce the
new trait in the next commit.
2023-11-07 05:50:47 +00:00
Matt Corallo 5e0209569c Use `io::sink` rather than our custom `EmptyWrite` utility 2023-11-07 05:50:45 +00:00
Matt Corallo 27c7c4e26a Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.

Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.

Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.

Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.

This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-11-07 05:50:42 +00:00
Andrew Poelstra 966b190f23
Merge rust-bitcoin/rust-bitcoin#2168: Use network when calculating difficulty
12d615d900 Use network when calculating difficulty (Tobin C. Harding)
62af5b54f3 Improve difficulty rustdocs (Tobin C. Harding)

Pull request description:

  The difficulty is a ratio of the max and current targets, since the max is network specific the difficulty calculation is also network specific.

  We already have network specific maximum target constants, use them when calculating the difficulty.

  Patch 1 is a trival docs improvement to `block::Header::difficulty`.

ACKs for top commit:
  Kixunil:
    ACK 12d615d900
  apoelstra:
    ACK 12d615d900

Tree-SHA512: 8b414c975306667309b0918109b3e5e8774496fc4c0f3413709e95ad7499bebf1a017def4c180a2bb5f1750c69bb505d94c738a28525b7ccc8b36e5e42514000
2023-11-06 14:38:00 +00:00
Andrew Poelstra 7b6de6a22b
Merge rust-bitcoin/rust-bitcoin#2170: Remove deprecated since NEXT-RELEASE
01e2233f6c Remove deprecated since NEXT-RELEASE (Tobin C. Harding)

Pull request description:

  Not sure what happened here but our release job didn't catch this? We should have updated this to "since = 0.31.0" before release. Since we only deprecate for one release lets go ahead and remove this.

ACKs for top commit:
  apoelstra:
    ACK 01e2233f6c
  clarkmoody:
    ACK 01e2233f6c
  Kixunil:
    ACK 01e2233f6c

Tree-SHA512: ad362058371e5e8ac7b577c3e32a0c65ce29e5723ff5efbccbcc57684fd61364f3caf7a4c8f0ee8c24bcb7a765bad2539a862860a902e5cec495ed9972379c2d
2023-11-06 14:04:47 +00:00
Andrew Poelstra 9f602a613b
Merge rust-bitcoin/rust-bitcoin#1979: Make `Payload` and `AddressEncoding` private
7f75447c1d Make Payload private and inline functionality (Tobin C. Harding)
b12bf07232 Make the AddressEncoding type private (Tobin C. Harding)

Pull request description:

  The `AddressEncoding` and `Payload` types are implementation details and should never have been public.    Make them private.

  Fix: #1908

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

Tree-SHA512: 37083bc759f32e5187126c4f8d39c9c9cb39bd80a92b2479128da39f4db7672fe0be24a58756a387fe944c63efb4ffacc58c1dac071f3314e882ed0f0e9f5a23
2023-11-06 13:56:56 +00:00
Tobin C. Harding 7f75447c1d
Make Payload private and inline functionality
Currently we have functions on `Address` that call through to a public
`Payload` type. The `Payload` type is an implementation detail and
should never have been public. In preparation for modifying the
`AddressInner` and removing `Payload` altogether lets move all the
functionality from `Payload` into `Address` - this is basically just
code moves so it is feasible to review with some confidence.

This is an API breaking change because it makes `Payload` private and
also removes from the pubic `Address` API functions that accept and
return `Payload`. Apart from that the changes can be seen as
refactoring.
2023-11-06 16:09:11 +11:00
Tobin C. Harding b12bf07232
Make the AddressEncoding type private
The `AddressEncoding` type exists solely to assist us in implementing
`Display` on `Address`, it may have been used in the past by alt-coins
back when we had a more tolerant outlook on supporting them. Nowadays
we explicitly do not support alts.
2023-11-06 16:09:07 +11:00