Commit Graph

1059 Commits

Author SHA1 Message Date
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 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