Commit Graph

1395 Commits

Author SHA1 Message Date
Fredrik Meringdal 525ea00e0f Make Address::get_payload_bytes public 2022-04-29 11:54:53 +02:00
sanket1729 e47d89c537
Merge rust-bitcoin/rust-bitcoin#952: Remove MSRV todo comments
831b0267de Use contains() instead of manual range (Tobin C. Harding)
6410095687 Use chunks_exact (Tobin C. Harding)
3a0097ba49 Use trim_start_matches (Tobin C. Harding)
0a19710906 Use vec! macro instead of new followed by push (Tobin C. Harding)

Pull request description:

  Now that 0.28 is out we do not need to support Rust 1.29 on `master`.

  Remove trivial MSRV `TODO`s from the code. (All these changes only rely on MSRV bumping to 1.31 so are easily within bounds.)

ACKs for top commit:
  Kixunil:
    ACK 831b0267de
  sanket1729:
    ACK 831b0267de

Tree-SHA512: f1ea594216ba7dfa24696b964ce296a8aea72dd2e16e11d3a43fe8b90c851abf59b1612b2b1311146e8070112f3834762584e4f0515b8f546f72af169eb4bda9
2022-04-27 15:52:16 -07:00
sanket1729 ee411a4cc2
Merge rust-bitcoin/rust-bitcoin#853: API to find funding utxos in psbt
5afb0eaf40 API to get an iterator for funding utxos in psbt (violet360)

Pull request description:

  ### Current status
  The API returns a vector of UTXOs and has return type `Result<Vec<&TxOut>, Error>`

  ### Expected
  The return statement should be of type `sighash::Prevouts` as pointed in #849

ACKs for top commit:
  Kixunil:
    ACK 5afb0eaf40
  tcharding:
    ACK 5afb0eaf40
  sanket1729:
    ACK 5afb0eaf40. Thanks for being patient with this.

Tree-SHA512: 724fc3dffdbb1331584f89bbe84527e1af0d193a344fe43b36f2f2a628652d259001a3abf6b3909df53524cd3fbdbe3af760b7004d40d3bee1848fbb83efff5b
2022-04-27 15:40:10 -07:00
Tobin C. Harding 831b0267de Use contains() instead of manual range
We no longer support Rust 1.29, we can use `contains` for ranges instead
of doing so manually.
2022-04-27 07:59:51 +10:00
Tobin C. Harding 6410095687 Use chunks_exact
Now that we are going to bump the MSRV above 1.31 we can use
`chunks_exact`.
2022-04-27 07:59:40 +10:00
Tobin C. Harding 3a0097ba49 Use trim_start_matches
Now that we are bumping the MSRV to greater than 1.30 we can use
`trim_start_matches`.

Use `trim_start_matches` and remove the clippy directive.
2022-04-26 11:34:30 +10:00
Tobin C. Harding 0a19710906 Use vec! macro instead of new followed by push
No need to manually create a vector and push each element, just use the
`vec![]` macro.
2022-04-26 11:32:34 +10:00
mcroad 7854bd7918
Fix `no_std` MSRV
Fixes #690, #947
2022-04-25 11:14:41 -05:00
ass3rt 76fcf81474 Override default visit_byte_buf on Script
This override may avoid allocation and thus make the deserialization
faster.

Credit to Kixunil for this fix: https://github.com/rust-bitcoin/rust-bitcoin/pull/905#issuecomment-1092756343
2022-04-25 09:53:52 -05:00
ass3rt add100c20d Removed reimplementations of default methods
The default methods do the exact same thing thus our overrides are
useless, potentially even problematic.

Credit to Kixunil for this fix: https://github.com/rust-bitcoin/rust-bitcoin/pull/905#issuecomment-1092756343
2022-04-25 09:53:52 -05:00
violet360 5afb0eaf40 API to get an iterator for funding utxos in psbt 2022-04-25 18:18:11 +05:30
Andrew Poelstra 83514c87a7
Merge rust-bitcoin/rust-bitcoin#902: util::amount: Make from_sat constructor constant
31571cafbd util::amount: Make from_sat constructor constant (Steven Roose)

Pull request description:

  Currently unmergable because of MSRV but I heard talk about bumping it, so once it's bumped, this is a very much needed change :)

ACKs for top commit:
  tcharding:
    ACK 31571cafbd
  apoelstra:
    ACK 31571cafbd

Tree-SHA512: f254eb10a4349d890e29ea5fae77536429c7e731362cf2edcf2fe98ec9cbf2d8bbf65f98dfc8f0b80bac89960de688005d066a116d6cb62cca1efa9c1151f2ae
2022-04-22 23:43:52 +00:00
sanket1729 b169925c24
Merge rust-bitcoin/rust-bitcoin#966: Clean up import aliases
7307363c2e Use qualified path instead of alias (Tobin C. Harding)
80e0fb7673 Remove unnecessary 'as' statement (Tobin C. Harding)
21e1b9dbbd Use secp256k1 qualified path instead of underscore (Tobin C. Harding)

Pull request description:

  Three trivial clean ups of import aliases.

ACKs for top commit:
  apoelstra:
    ACK 7307363c2e
  sanket1729:
    ACK 7307363c2e. These are clean improvements
  Kixunil:
    ACK 7307363c2e

Tree-SHA512: f6ed3ede11d2803dbcb4584f11632fc47d28e525b5bf4de7794d400117f2d7c9ffce5bdff274877a63a519d5799bba2224fc39105d623da4bccad863005e171f
2022-04-22 12:32:18 -07:00
Martin Habovštiak 2c28d3b448
Fix handling of empty slice in Instructions
The code would've taken one element when an empty slice was requested.

Co-authored-by: Tobin C. Harding <me@tobin.cc>
2022-04-21 19:14:30 +02:00
Tobin C. Harding 7307363c2e Use qualified path instead of alias
It is more typical in this repo to use `module::Error` instead of a type
alias when importing.

Use `hex::Error` directly instead of `use hex::Error as HexError`.
2022-04-21 12:50:22 +10:00
Tobin C. Harding 80e0fb7673 Remove unnecessary 'as' statement
We can just use `self`, no idea why the `self as io` is there.
2022-04-21 12:50:22 +10:00
Tobin C. Harding 21e1b9dbbd Use secp256k1 qualified path instead of underscore
We can use the qualified path for create type aliases, this is arguably
easier to read and reduces the number of LOC.
2022-04-21 12:50:22 +10:00
Tobin C. Harding 033a12def7 Remove unused Write as _fmtWrite
We can bring the `Write` trait into scope, no need to underscore it.
2022-04-21 12:37:56 +10:00
sanket1729 30574020ef
Merge rust-bitcoin/rust-bitcoin#954: Add Script conversion method p2wpkh_script_code
d882b68a2c Add Script conversion method p2wpkh_script_code (Tobin Harding)

Pull request description:

  In order to sign a utxo that does a p2wpkh spend we need to create the
  script that can be used to create a sighash. In the libbitcoin docs this
  is referred to as the 'script code' [0] (also described in BIP143)

  The script is the same as a p2pkh script but the pubkey_hash is found in
  the scriptPubkey.

  Add a `Script` conversion method that checks if `self` is a v0 p2wpkh
  script and if so extracts the pubkey_hash and returns the required
  script.

  Includes a link to BIP143

  [0] https://github.com/libbitcoin/libbitcoin-system/wiki/P2WPKH-Transactions#spending-a-p2wpkh-output

ACKs for top commit:
  apoelstra:
    ACK d882b68a2c
  sanket1729:
    code review ACK d882b68a2c.

Tree-SHA512: 9a3244b5aac4e2911edf4d3bb634d3d2b98006b864280a2a04b45c55c263c2541bf25f01196f2a65bf9acbdd0cf28c69c3a020a7e6c8da6fddf7c7cfbb62836d
2022-04-20 14:35:59 -07:00
sanket1729 94f8c4b530
Merge rust-bitcoin/rust-bitcoin#951: Add PSBT alias
f92854a805 Add PSBT alias (Tobin Harding)

Pull request description:

  Programmers are inherently lazy and for good reason. I'm yet to see
  anyone write `PartiallySignedTransaction` in code that uses
  `rust-bitcoin`, its too obvious to add a type alias for PSBTs, let's
  just do it ourselves to save everyone else having to do so.

  Add public type alias `Psbt` for `PartiallySignedTransaction`.

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

Tree-SHA512: 1f56ac236d34a89bbb557ada147f05d8a8ce961dad3ad921f10f26c597b91ecc8e15070f8825774745e5333ba5282962830a3cc0c53b93f147be93ab566b1b9e
2022-04-20 14:32:34 -07:00
Andrew Poelstra 954b8a9b95
Merge rust-bitcoin/rust-bitcoin#939: fix: reject message (de)serialization
548725c5fb test: reject message (de)serialization (0xb10c)
fc572aba86 fix: use var_str in 'reject' msgs (0xb10c)

Pull request description:

  [BIP-61 defines `response-to-msg`][bip61] (`Reject::message` in rust-bitcoin; the message that triggered the reject) to be a `var_str`. However, by using the `CommandString` it was (de)serialized as 12 byte string. A test is added that de- and serializes two reject messages received from an older Bitcoin Core peer.

  Reject message sending has been removed from Bitcoin Core, I'm still receiving them from older peers from time to time.

  [bip61]: https://github.com/bitcoin/bips/blob/master/bip-0061.mediawiki#common-payload

  gh-ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/323

ACKs for top commit:
  apoelstra:
    ACK 548725c5fb

Tree-SHA512: e5cbf215a471f113b4dd7f7fada162686fc6e8c7b1e2e9e641667208a36d3db610e57e8b549756ffe597656fee5444fe95466f1b88f45366595766f7c4640eea
2022-04-20 20:51:33 +00:00
Andrew Poelstra 1e58208039
Merge rust-bitcoin/rust-bitcoin#950: Fix TapTree derserialization
c97589f8de Fix TapTree derserialization (sanket1729)

Pull request description:

  Trees should only be serialized if both of the following conditions
  hold:
  1) Tree is complete binary tree(is_finalized)
  2) Tree does not have any hidden nodes

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

Tree-SHA512: 33d16f2d532cb24acba4ab847d493e550f7b279567678f3f2cd7e4161dea8b720a0e35be32b6c506e467c3526a29042aad8f4b5f45133b9a32028d4ee6a48f8e
2022-04-20 20:47:30 +00:00
Martin Habovštiak e6ff754b73 Fix doc of take_slice_or_kill
Co-authored-by: Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
2022-04-20 19:47:24 +02:00
Martin Habovstiak 0ec6d96a7b Cleanup after `Instructions` refactoring
* Changes `Option` to `Result` to avoid repeated `.ok_or(...)`
* Renames `max` to `min_push_len`
* Removes useless variable
2022-04-20 19:47:24 +02:00
Martin Habovstiak bc763259fe Move repeated code to functions in script
This creates a few primitive functions for handling iterators and uses
them to avoid repeated code. As a result not only is the code simpler
but also fixes a forgotten bound check. Thanks to a helper function
which always does bounds check correctly this can no longer be
forgotten.
2022-04-20 19:47:24 +02:00
Martin Habovstiak 1f55edf718 Use iterator in `blockdata::script::Instructions`
This refactors `blockdata::script::Instructions` to use
`::core::slice::Iter<'a, u8>` instead of `&'a [u8]` to better express
the intention and to avoid some slicing mistakes. Similarly to a
previous change this uses a macro to deduplicate the common logic and
the new `read_uint_iter` internal function to automatically advance the
iterator.

Addresses:
https://github.com/rust-bitcoin/rust-bitcoin/pull/662#pullrequestreview-768320603
2022-04-20 19:45:03 +02:00
Andrew Poelstra 6b57a02b1f
Merge rust-bitcoin/rust-bitcoin#927: Trivial improvements for TapTree type
4cdff06b1e Add convenience method TapTree:to_builder (Dr Maxim Orlovsky)
a12e7c73b6 Implement From<TapTree> for TaprootBuilder (Dr Maxim Orlovsky)
410412ff01 Rename TapTree::from_builder (Dr Maxim Orlovsky)
219273788c Rename TapTree::into_builder (Dr Maxim Orlovsky)
f9d8d0d968 Make TapTree::node_info public (Dr Maxim Orlovsky)

Pull request description:

  These are trivial fixes from extracted from now closed #922

ACKs for top commit:
  Kixunil:
    ACK 4cdff06b1e
  sanket1729:
    ACK 4cdff06b1e
  apoelstra:
    ACK 4cdff06b1e

Tree-SHA512: 6132e8c214edc6f199a5550309daf4ed5035f24f545c793d6396c393bd2f55940dc418af62aed9aff25c0c90b74ee384ace986f7201db4018c6fd52710006126
2022-04-20 17:44:27 +00:00
sanket1729 9f79f8d1ce
Merge rust-bitcoin/rust-bitcoin#936: Make TaprooBuilder::finalize able to return keyspend only
7969b7a43e Make TaprooBuilder::finalize able to return keyspend only (Jeremy Rubin)

Pull request description:

ACKs for top commit:
  JeremyRubin:
    > ACK 7969b7a
  sanket1729:
    ACK 7969b7a43e
  apoelstra:
    ACK 7969b7a43e

Tree-SHA512: 26d0b730590f610a858061394faafaa74b13dd353f34ccf1c6166d0cbb62937010eed5661a887f7bea4f983ac9eab8cdca10a5fe7bd74f2dd5701a7782cbac64
2022-04-20 09:39:20 -07:00
Dr Maxim Orlovsky 4cdff06b1e
Add convenience method TapTree:to_builder 2022-04-20 10:31:28 +02:00
Dr Maxim Orlovsky a12e7c73b6
Implement From<TapTree> for TaprootBuilder 2022-04-20 10:30:45 +02:00
Dr Maxim Orlovsky 410412ff01
Rename TapTree::from_builder 2022-04-20 10:30:45 +02:00
Dr Maxim Orlovsky 219273788c
Rename TapTree::into_builder 2022-04-20 10:28:28 +02:00
Dr Maxim Orlovsky f9d8d0d968
Make TapTree::node_info public 2022-04-20 10:28:28 +02:00
Dr Maxim Orlovsky 3c59897598
Removed IntoIterator for TapTree implementation
In the future, TapTree may iterate over different node types, and that's why it does not have `iter()` function; using instead `script_leafs`. Thus, we should not have IntoIterator implementation as well
2022-04-19 20:32:13 +02:00
Dr Maxim Orlovsky 7a5482d23a
Rename LeafInfo into ScriptLeaf 2022-04-19 20:32:13 +02:00
Dr Maxim Orlovsky 2b8d96581a
Rename TapTree::iter into TapTree::script_leaves 2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky 6f871ba47d
Add convenience LeafInfo::depth method
Without this method computation of the leaf depth requires cloning due
to the requirements of merkle_branch.into_inner()
2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky 3c502ffc2d
Making all LeafInfo fields private 2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky d655ff3e93
Make TapTreeIterator use LeafInfo
Previously used depth and script tuple missed information about the leaf version. 
All three comprises already existing type `LeafInfo` which was made public in 
previous commits.
2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky 79345fcd02
LeafInfo field accessor methods 2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky 5958466678
Make LeafInfo::leaf_hash public and change its name and return type 2022-04-19 20:31:49 +02:00
Dr Maxim Orlovsky c83893d497
Make taproot LeafInfo public
LeafInfo structure is a useful form of representing leaf script information (script, leaf version and merkle proof).
2022-04-19 20:31:49 +02:00
Tobin Harding d882b68a2c Add Script conversion method p2wpkh_script_code
In order to sign a utxo that does a p2wpkh spend we need to create the
script that can be used to create a sighash. In the libbitcoin docs this
is referred to as the 'script code' [0].

The script is the same as a p2pkh script but the pubkey_hash is found in
the scriptPubkey.

Add a `Script` conversion method that checks if `self` is a v0 p2wpkh
script and if so extracts the pubkey_hash and returns the required
script.

[0] https://github.com/libbitcoin/libbitcoin-system/wiki/P2WPKH-Transactions#spending-a-p2wpkh-output
2022-04-18 10:32:05 +10:00
Tobin Harding f92854a805 Add PSBT alias
Programmers are inherently lazy and for good reason. I'm yet to see
anyone write `PartiallySignedTransaction` in code that uses
`rust-bitcoin`, its too obvious to add a type alias for PSBTs, let's
just do it ourselves to save everyone else having to do so.

Add public type alias `Psbt` for `PartiallySignedTransaction`.
2022-04-18 07:21:19 +10:00
sanket1729 c97589f8de Fix TapTree derserialization
Trees should only be serialized if both of the following conditions
hold:
1) Tree is complete binary tree(is_finalized)
2) Tree does not have any hidden nodes
2022-04-14 10:04:46 -07:00
Andrew Poelstra 8ca18f75dd
Merge rust-bitcoin/rust-bitcoin#929: Fix TapTree hidden branches bug
c036b0db6f Unit test for failing TapTree on builder containing hidden nodes. (Dr Maxim Orlovsky)
77715311cf Prevent TapTree from hidden parts (Dr Maxim Orlovsky)
b0f3992db1 Rename TaprootBuilder::is_complete into is_finalized (Dr Maxim Orlovsky)
efa800fb1f Make TapTree::from_inner return a proper error type (Dr Maxim Orlovsky)
e24c6e23e3 TapTree serialization roundtrip unit test (Dr Maxim Orlovsky)
56adfa4527 TaprootBuilder::has_hidden_nodes method (Dr Maxim Orlovsky)
e69701e089 Rename taproot `*_hidden` API into `*_hidden_nodes` (Dr Maxim Orlovsky)
6add0dd9dc Track information about hidden leaves in taproot NodeInfo (Dr Maxim Orlovsky)

Pull request description:

  Closes #928

ACKs for top commit:
  sanket1729:
    ACK c036b0db6f. Reviewed the range diff
  apoelstra:
    ACK c036b0db6f

Tree-SHA512: 3a8193e6d6dd985da30a2094d1111471b5971f422525870003b77b6ac47cd4ad6e718d46a6d86bbb5e92e5253ac53804badf67edd98bbccbdc11e6383c675663
2022-04-14 17:03:14 +00:00
Dr Maxim Orlovsky c036b0db6f
Unit test for failing TapTree on builder containing hidden nodes. 2022-04-05 22:43:52 +02:00
Dr Maxim Orlovsky 77715311cf
Prevent TapTree from hidden parts 2022-04-05 22:30:34 +02:00
Dr Maxim Orlovsky b0f3992db1
Rename TaprootBuilder::is_complete into is_finalized 2022-04-05 22:29:32 +02:00
Dr Maxim Orlovsky efa800fb1f
Make TapTree::from_inner return a proper error type 2022-04-05 22:29:20 +02:00
Dr Maxim Orlovsky e24c6e23e3
TapTree serialization roundtrip unit test 2022-04-05 22:18:23 +02:00
Dr Maxim Orlovsky 56adfa4527
TaprootBuilder::has_hidden_nodes method 2022-04-05 22:18:00 +02:00
Dr Maxim Orlovsky e69701e089
Rename taproot `*_hidden` API into `*_hidden_nodes` 2022-04-05 22:16:59 +02:00
Dr Maxim Orlovsky 6add0dd9dc
Track information about hidden leaves in taproot NodeInfo 2022-04-05 22:16:27 +02:00
0xb10c 548725c5fb
test: reject message (de)serialization
This adds tests for the previously untested reject message
(de)serialization. The two reject messages were received from an
older Bitcoin Core peer that still sends reject messages.
2022-04-05 08:35:11 +02:00
0xb10c fc572aba86
fix: use var_str in 'reject' msgs
CommandString is (de)serialized as 12 bytes. However, BIP-61 defines
the 'response-to-msg' (message that triggered the reject) field
to be a var_str [1].

[1]: https://github.com/bitcoin/bips/blob/master/bip-0061.mediawiki#common-payload
2022-04-05 08:30:16 +02:00
Tobin Harding 29843c41ef Allow deprecated function call
We have a deprecated function call because of the MSRV, tell clippy to
ignore it.
2022-04-04 18:28:09 +10:00
Jeremy Rubin 7969b7a43e Make TaprooBuilder::finalize able to return keyspend only 2022-04-03 16:24:56 -04:00
sanket1729 cb4d34fd40
Merge rust-bitcoin/rust-bitcoin#932: Derive Eq for PSBT types
603e75eb77 Derive Eq for PSBT types (Dr Maxim Orlovsky)

Pull request description:

  Closes #931

ACKs for top commit:
  apoelstra:
    ACK 603e75eb77
  sanket1729:
    utACK 603e75eb77.

Tree-SHA512: 8099e80aa2000b3d1284543b6bfab3edd45f8649519bd09b4d73d250bdb6cce5edf67a1e0e0cec61db23c358daca286061641da5ff5c2a8b4b030d1199707c94
2022-04-01 11:38:45 -07:00
sanket1729 7fa8ce0bd0
Merge rust-bitcoin/rust-bitcoin#926: Remove redundant code computing tap hashes
f3ebfd6f8b Remove repeated tap branch hash computing logic (Dr Maxim Orlovsky)
1b28375658 Abstract tap branch hash computing into a dedicated method (Dr Maxim Orlovsky)

Pull request description:

ACKs for top commit:
  apoelstra:
    ACK f3ebfd6f8b

Tree-SHA512: d66d544df324a7d25c8cc9dc48ddedf086ac87eb2ed09a8d7a568cc1488ae44e0807d53ccb7a6e61dbeef0d3d62a1cacf0d69ba7b8de9178ac5c13bae944d08b
2022-04-01 11:35:14 -07:00
Andrew Poelstra 9316c52946
Merge rust-bitcoin/rust-bitcoin#917: Rename SigHash to Sighash
46c34b3fb7 Fix code comments referring to sighash (Tobin Harding)
8f36c3979c Use sighash not sig_hash in identifiers (Tobin Harding)
c3a167b96b Rename SigHash -> Sighash (Tobin Harding)
52b711c084 Rename InvalidSigHashType -> InvalidSighashType (Tobin Harding)
b84f25584e Rename SigHashCache -> SighashCache (Tobin Harding)
e37652578b Rename PsbtSigHashType -> PsbtSighashType (Tobin Harding)
c19ec339ef Rename NonStandardSigHashType -> NonStandardSighashType (Tobin Harding)
130e27349e Rename SigHashTypeParseError -> SighashTypeParseError (Tobin Harding)
6caba2ed24 Rename SchnorrSigHashType -> SchnorrSighashType (Tobin Harding)
5522454583 Rename EcdsaSigHashType -> EcdsaSighashType (Tobin Harding)

Pull request description:

  Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash' is a well known word in the Bitcoin ecosystem it should appear in identifiers as `Sighash`.

  Change various types, variants, and code comments to use sighash as a single word.

  - Patches 1-8 are code changes `s/SigHash/Sighash/g`
  - Patch 9 is code changes `s/sig_hash/sighash/g`
  - Patch 11 is docs fixes

  Fixes: #911

  ## Note to reviewers

  I've been particularly pedantic with the patch separation because we are so close to release.

  Done as separate patches to make review easier if review is to be done by reading the diffs. Perhaps at least one person could verify this PR programmatically by doing
  - Reset the last 2 patches (those are easy to do manually)
  - Check out master
  - Do `s/SigHash/Sighash/g` on all source files (bash function below)
  - Use `git diff branchA..branchB` to verify

  The difference between the two branches should only include comment lines (last three patches) and these seven instances of `SigHash:

  ```
  CHANGELOG.md:82:- [Add FromStr/Display implementation for SigHashType](a4a7035a94)
  CHANGELOG.md:93:- [Introduce `SigHashCache` structure](https://github.com/rust-bitcoin/rust-bitcoin/pull/390) to replace `SighashComponents` and support all sighash modes
  CHANGELOG.md:121:    - `SigHash`
  src/blockdata/transaction.rs:1190:            "SigHash_None",
  src/blockdata/transaction.rs:1191:            "SigHash_NONE",
  src/util/sighash.rs:1175:            "SigHash_None",
  src/util/sighash.rs:1176:            "SigHash_NONE",
  ```

  In case its useful, the shell function I used to do these changes is:
  ```bash
  function search-and-replace() {
      if (($# != 2))
      then
          echo "Usage: $0 <this> <that>"
          return
      fi

      local this="$1"
      local that="$2"

      # For all files containing $this, replace $this with $that.
      for file in $(git grep -l "$this")
      do
          perl -pi -e "s/$this/$that/g" "$file"
      done
  }
  ```

ACKs for top commit:
  dr-orlovsky:
    ACK 46c34b3fb7
  apoelstra:
    ACK 46c34b3fb7

Tree-SHA512: fe7e25e9cfb5155e4921de5ac185dbf9f4ca0770846d7892f6968b44fc5431f3f1a183380107449e90f7ea662094c60b118dc0468230384e8f9a8ef98d5ee0a0
2022-04-01 17:30:42 +00:00
Dr. Maxim Orlovsky 3f04c04b3d
Merge rust-bitcoin/rust-bitcoin#920: Push key xonly
f27c4a541d Added push_x_only_key(..) and its test. (mpls)

Pull request description:

  **Issue**

  I can not use [`XOnlyPublicKey`](ae985dd191/src/key.rs (L973)) in my Scripts which prevents me from working with Taproot.

  **Cause**

  The current version of [`script::Builder`](0a2d45de09/src/blockdata/script.rs (L121)) does not accept `XOnlyPublicKey`s.

  **Solution**

  So, I created a function `push_xkey(self, key: &XOnlyPublicKey)` based on the existing [`push_key`](0a2d45de09/src/blockdata/script.rs (L914)) function. I also augmented an [existing test](0a2d45de09/src/blockdata/script.rs (L1108)) in an attempt to reach testing parity with existing code.

  After toying around with `push_xkey`, it seems to work on my end.

ACKs for top commit:
  dr-orlovsky:
    ACK f27c4a541d
  sanket1729:
    utACK f27c4a541d. Thanks a lot for keeping up the iterations with prompt responses

Tree-SHA512: 064958d49edc1d3636a21e428d62c2e9bcd9b13bd226c5821db9e04ce78663a11fcf601c7667b564f88e845207219a052e1c7413f50e5d27c79003e8129825ed
2022-04-01 19:30:55 +03:00
sanket1729 efbe1417fe
Merge rust-bitcoin/rust-bitcoin#923: Taproot docs nits
da731c4825 Add further description to the NodeInfo struct (Tobin Harding)
492ccebd99 Use links for error types (Tobin Harding)
3e05887579 Use 'the' to improve sentence (Tobin Harding)

Pull request description:

  See to nits from review of https://github.com/rust-bitcoin/rust-bitcoin/pull/912

  Three minor patches to the `taproot` module docs.

  CC @dr-orlovsky

ACKs for top commit:
  dr-orlovsky:
    ACK da731c4825
  sanket1729:
    ACK da731c4825

Tree-SHA512: 17a27a19c88f9baa8127023b2ee30fc2259cb0058a92dc9d8ae595e9e02ccb047fefcba7548ff7900fffa7bc6853447183e80660b8756d90d055ab8aa96ae938
2022-04-01 08:55:57 -07:00
Dr Maxim Orlovsky 603e75eb77 Derive Eq for PSBT types 2022-04-01 11:45:32 +02:00
mpls f27c4a541d Added push_x_only_key(..) and its test. 2022-04-01 01:33:00 -05:00
Dr Maxim Orlovsky f3ebfd6f8b
Remove repeated tap branch hash computing logic 2022-03-31 15:21:36 +02:00
Dr Maxim Orlovsky 1b28375658
Abstract tap branch hash computing into a dedicated method 2022-03-31 15:16:39 +02:00
Dr Maxim Orlovsky e3f173e521
Require taproot tree depth argument always to be u8 2022-03-31 15:12:05 +02:00
Tobin Harding da731c4825 Add further description to the NodeInfo struct
Further assist devs in understanding the `NodeInfo` struct by adding
docs about when/why the struct is used.
2022-03-31 10:57:37 +11:00
Tobin Harding 492ccebd99 Use links for error types
We can help the users by linking them to errors when mentioning them in
the docs.
2022-03-31 10:57:05 +11:00
Tobin Harding 3e05887579 Use 'the' to improve sentence
Adding a 'the' makes this sentence a little better.
2022-03-31 10:56:36 +11:00
Tobin Harding 46c34b3fb7 Fix code comments referring to sighash
Recently we added a bunch of additional sighash types, some of the code
comments became stale. Use the non-specific term 'sighash type' instead
of a particular sighash identifier in comments to make the comments more
applicable.
2022-03-31 09:44:22 +11:00
Tobin Harding 8f36c3979c Use sighash not sig_hash in identifiers
Recently we update all types and docs to use `Sighash` instead of
`SigHash` because 'sighash' is a single word. We should apply the same
logic to functions and variable names.

Do not use an underscore in the identifier 'sighash'.
2022-03-31 09:42:52 +11:00
Tobin Harding c3a167b96b Rename SigHash -> Sighash
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename the `SigHash` type to `Sighash`.
2022-03-31 09:42:52 +11:00
Tobin Harding 52b711c084 Rename InvalidSigHashType -> InvalidSighashType
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename the `InvalidSigHashType` variant to `InvalidSighashType`.
2022-03-31 09:42:52 +11:00
Tobin Harding b84f25584e Rename SigHashCache -> SighashCache
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `SigHashCache` to `SighashCache`.
2022-03-31 09:42:52 +11:00
Tobin Harding e37652578b Rename PsbtSigHashType -> PsbtSighashType
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `PsbtSigHashType` to `PsbtSighashType`.
2022-03-31 09:42:18 +11:00
Tobin Harding c19ec339ef Rename NonStandardSigHashType -> NonStandardSighashType
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename the `NonStandardSigHashType` type and error variant to
`NonStandardSighashType`.
2022-03-31 09:42:18 +11:00
Tobin Harding 130e27349e Rename SigHashTypeParseError -> SighashTypeParseError
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `SigHashTypeParseError` to `SighashTypeParseError`.
2022-03-31 09:42:18 +11:00
Tobin Harding 6caba2ed24 Rename SchnorrSigHashType -> SchnorrSighashType
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `SchnorrSigHashType` to `SchnorrSighashType`.
2022-03-31 09:42:18 +11:00
Tobin Harding 5522454583 Rename EcdsaSigHashType -> EcdsaSighashType
Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `EcdsaSigHashType` to `EcdsaSighashType`.
2022-03-31 09:42:18 +11:00
Dr. Maxim Orlovsky 58a958e3f7
Merge rust-bitcoin/rust-bitcoin#912: Improve docs in taproot module
c25eddd187 Remove unnecessary documentation (Tobin Harding)
8631474f08 Improve docs in taproot module (Tobin Harding)

Pull request description:

  I should have done this PR a month ago, my bad. This one is kind of important IMO because we are going to have so many people looking at this part of the code soon as we release.

  As has been done in other places in the codebase; improve the docs in the `taproot` module by doing:

  - Use full sentences (capital letters + full stops)
  - Use back ticks and links for types where appropriate
  - Fix grammar
  - Fix stale docs
  - Use third person for describing functions
  - Use 100 character line width
  - Use markdown sections (`# Examples`, `# Returns`) where appropriate
  - Separate brief heading from extended description when appropriate
  - Use `///` for all functions/types (both private and public)

  I also did:

  - Build the docs and check all the links
  - Read all the built docs, check for sanity and pretty-ness

  Its all in one patch, I couldn't really tease it apart. I can try a bit harder if it proves too annoying to review.

ACKs for top commit:
  sanket1729:
    ACK c25eddd187
  dr-orlovsky:
    ACK c25eddd187
  apoelstra:
    ACK c25eddd187

Tree-SHA512: 72f35bf8779392060388db985df5abc42a89796eaad1eafd08ea50b635d469fbd07a53ff253cdf27ad4d4baed7d37cec6ea1da1aece3672b9447f87181e218f8
2022-03-30 19:30:14 +03:00
Tobin Harding 8d602b8778 Fix deprecated since version
We deprecated the `bip143::SigHashCache` in

```
commit 53d0e176d3
Author: <elided>
Date:   Fri Jul 16 10:44:18 2021 +0200

    Deprecate bip143::SigHashCache in favor of sighash::SigHashCache

    ...
```

This means these changes are unreleased so the deprecated since version
should be the upcoming 0.28 release.
2022-03-29 10:59:17 +11:00
Tobin Harding c25eddd187 Remove unnecessary documentation
We have some text quoted directly from BIP341, this text is on the net
if folk wish to read it, we don't need it in the source code.
2022-03-29 10:28:29 +11:00
Tobin Harding 8631474f08 Improve docs in taproot module
As has been done in other places in the codebase; improve the docs in
the `taproot` module by doing:

- Use full sentences (capital letters + full stops)
- Use back ticks and links for types where appropriate
- Fix grammar
- Fix stale docs
- Use third person for describing functions
- Use 100 character line width
- Use markdown sections (`# Examples`, `# Returns`) where appropriate
- Separate brief heading from extended description when appropriate
- Use `///` for all functions/types (both private and public)

I also did:

- Build the docs and check all the links
- Read all the built docs, check for sanity and pretty-ness
2022-03-29 10:27:45 +11:00
Dr Maxim Orlovsky 174a99cd06
Implement serde for TweakedKeyPair 2022-03-28 21:29:04 +02:00
Dr Maxim Orlovsky df3297c34e
Implement derives for TweakedKeyPair 2022-03-28 21:28:43 +02:00
Andrew Poelstra b32d40390c
Merge rust-bitcoin/rust-bitcoin#898: Make PsbtSigHashType use the same formatting as other *SigHashTypes
992857ad0a PsbtSighashType unit tests (Dr Maxim Orlovsky)
5be1cdb8c7 PsbtSigHashType Display and FromStr implementation (Dr Maxim Orlovsky)
7cdcdaad6c Support SIGHASH_RESERVED in SchnorrSigHashType::from_u8 (Dr Maxim Orlovsky)

Pull request description:

  The newly introduced `PsbtSigHashType` uses very different serde formatting from previously used `EcdsaSigHashType`; for instance it does not output human-readable sighash. This is especially obvious when printing out PSBT as JSON/YAML object and is a breaking change from the `0.27`. Serde human-readable implementation requires `Display/FromStr`, which were also absent.

ACKs for top commit:
  sanket1729:
    ACK 992857ad0a. This is much better
  apoelstra:
    ACK 992857ad0a

Tree-SHA512: 71a46471f34b5481e4c1273a66846f59d61bfd98fcb65e7823ca216ff0dd419d81ca86d99c7aaf674fcfe2b1c010e899c8e74328f60a1e809015c663c453cc89
2022-03-28 17:34:20 +00:00
Andrew Poelstra 0d5565e131
Merge rust-bitcoin/rust-bitcoin#839: feat: Add Address.is_related_to_*_key()
51fef76129 feat: Add Address.is_related_to_pubkey() (Andrew Ahlers)

Pull request description:

  ## Motivation

  This is addressing the second half of this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/684#issuecomment-1012136845

  > but would accept a PR (or two PRs) that returns Result<bool, UnsupportedAddress> and a method to check if a PublicKey is associated with an address.

  (The first half was addressed [here](https://github.com/rust-bitcoin/rust-bitcoin/pull/819))

  These changes will help build out and improve message signature verification. We don't necessarily need to add it to this crate but it allows for easy verification with something such as:
  1. recovering a pubkey
  2. checking if that pubkey relates to the given address

  ## Possible Improvements

  - There is likely a better name than `is_related_to_secp256k1_key()`
  - This could drop the `secp256k1` part of the name and take in a Pubkey enum that also supports Schnorr pubkeys and then this could be used for taproot addresses as well. This felt like a much larger change that will likely get turned down. Verifying taproot is simple enough and if absolutely desired, similar functions can be added for schnorr keys (tweaked and untweaked)

ACKs for top commit:
  Kixunil:
    ACK 51fef76129 for merging after TR
  apoelstra:
    ACK 51fef76129

Tree-SHA512: c9ab8c0f101fb4c647713e7f500656617025d8741676e8eb8a3132009dde9937d50cf9ac3d8055feb14452324a292397e46639cbaca71cac77af4b06dc42d09d
2022-03-28 17:29:48 +00:00
Dr Maxim Orlovsky 8dabe3ed64
Taproot Huffman tree builder u64->u32 fixes 2022-03-28 17:15:28 +02:00
Andrew Poelstra 7f53c2cdc1
Merge rust-bitcoin/rust-bitcoin#909: Make TaprootBuilder able to generate Huffman Tree
ec17ec356d Move with_huffman_tree logic to TaprootBuilder (Jeremy Rubin)

Pull request description:

  .

ACKs for top commit:
  apoelstra:
    ACK ec17ec356d
  dr-orlovsky:
    utACK ec17ec356d

Tree-SHA512: 67a013124267f64bfae0b2007418ad59a42ae64d8b95e23c1d86cc7d96b0dd3b48deb255ce7bb839ef9a4d4f2e3a42d691d2d2430eb7791e01f992635773cc96
2022-03-28 15:08:21 +00:00
Dr Maxim Orlovsky 992857ad0a
PsbtSighashType unit tests 2022-03-28 17:03:44 +02:00
Dr Maxim Orlovsky 5be1cdb8c7
PsbtSigHashType Display and FromStr implementation 2022-03-28 17:03:34 +02:00
Dr Maxim Orlovsky 7cdcdaad6c
Support SIGHASH_RESERVED in SchnorrSigHashType::from_u8 2022-03-28 16:53:31 +02:00
Andrew Poelstra 10949b7177
Merge rust-bitcoin/rust-bitcoin#910: Make NodeInfo API public
208eb65f1b Make NodeInfo API public (sanket1729)

Pull request description:

  Reported by @shesek. Users might find it convenient to manually construct the tree using `NodeInfo` API

  ```rust
  let leaf1 = NodeInfo::from_leaf_with_ver();
  let leaf2 = NodeInfo::from_leaf_with_ver();

  let root = NodeInfo::combine(leaf1, leaf2);
  let spend_info = TaprootSpendInfo::from_node_info(&secp, internal_key, root);
  ```

ACKs for top commit:
  dr-orlovsky:
    ACK 208eb65f1b
  apoelstra:
    ACK 208eb65f1b

Tree-SHA512: b5a6b26e0d4a637f7ad6e987976b31b00d3567feca85f1a0bf63aa03603aded0ddae6578b1cabc1056870a596b8cb1a83e4ef3f45802e03da80c3d58d9bab1f1
2022-03-28 14:02:26 +00:00
Andrew Poelstra 388897bf93
Merge rust-bitcoin/rust-bitcoin#901: TapTree iterator
e27f8ff594 TapTree iterator implementation (Dr Maxim Orlovsky)

Pull request description:

  Implemented after @sanket1729 suggestion in https://github.com/rust-bitcoin/rust-bitcoin/issues/895#issuecomment-1074366108

  Iterates all scripts present in TapTree in DFS order returning `(depth, script)` pairs.

  I propose to have it as an RC fix since this functionality is really lacking and may be required for many wallets working with Taproot PSBT even outside of the scope where I originally needed it (OP_RETURN tweaks for TapTree described in #895)

ACKs for top commit:
  sanket1729:
    utACK e27f8ff594.
  apoelstra:
    ACK e27f8ff594

Tree-SHA512: b398e468a10534561297f22dba47e340391069734a41999edd85d726890752035053690a22014402879ea40b948160f00310f78771443d382c0bbaf0201dfbe5
2022-03-28 13:45:34 +00:00
Dr. Maxim Orlovsky c3d30d51a7
Remove deprecated method use for sighash conversion
Post-merge #796 follow-up. Feel free to add other changes/nits which hadn't get into #796.
2022-03-28 09:54:13 +02:00
sanket1729 208eb65f1b Make NodeInfo API public
This allows users to create TaprootSpendInfo using NodeInfo. This
offers an alternative to TaprootBuilder.
2022-03-27 17:34:05 -07:00
Tobin Harding 8e2422f92b Add unit test for deserialize non-standard sighash
It is possible, although not immediately obvious, that it is possible to
create a `PsbtSigHashType` with a non-standard value.

Add a unit test to show this and also catch any regressions if we
accidental change this logic.
2022-03-28 10:43:37 +11:00
Tobin Harding e05776f176 Improve PsbtSigHashType conversion methods
Improve the `PsbtSigHashType` conversion methods by doing:

- Re-name `inner` -> `to_u32` as per Rust convention
- Add `from_u32` method

Note, we explicitly do _not_ use suffix 'consensus' because these
conversion methods make no guarantees about the validity of the
underlying `u32`.
2022-03-28 10:43:37 +11:00
Tobin Harding ac462897b1 Remove hungarian-ish notation
The functions `from_u32_standard` and `from_u32_consensus` smell a bit
like hungarian notation. We can look at the method definition to see
that the methods accept `u32` arguments without mentioning that in the
method names.

Remove `_u32_` from the method names. This brings the `from_*` methods
in line  with the `to_standard` method also.
2022-03-28 10:43:37 +11:00
Tobin Harding 564682627c Remove deprecated conversion method
`EcdsaSigHashType::from_u32` was deprecated in v0.26, since we are
working on the v0.28 release we can drop this method.
2022-03-28 10:43:06 +11:00
Tobin Harding d1753d7ff1 Rename as_u32 -> to_u32
Rust naming conventions stipulate that conversion methods from owned ->
owned for `Copy` types use the naming convention `to_`.

This change makes the function name objectively better, however it makes
no claims of being the 'best' name. We have had much discussion on using
`to_standard` vs `to_u32` but are unable to reach consensus.
2022-03-28 10:43:06 +11:00
Tobin Harding 2bd71c3748 Remove From<EcdsaSigHashType> for u32
We have conversion functions that include suffixes `_consensus`
and `_standard` to make it explicit what guarantees are provided by the
returned `u32` value. The `From` implementation reduces the clarity of
the API.
2022-03-28 10:43:06 +11:00
Dr. Maxim Orlovsky b7adaf69f3
Merge rust-bitcoin/rust-bitcoin#907: Add Serialize/Deserialize for TaprootSpendInfo
2b942cf506 Add Serialize/Deserialize for TaprootSpendInfo (Jeremy Rubin)

Pull request description:

  I think this is missing -- unless there is a reason not to have it?

ACKs for top commit:
  apoelstra:
    ACK 2b942cf506
  dr-orlovsky:
    ACK 2b942cf506

Tree-SHA512: d1467d8515c85a5057037b1e5bf53c1930275fbe7e4fcbc726079a47febd75d6bbce8e2d99ed4f9d8afccf6fc3782e43763a2258c4c2a934c2453920fe587e4b
2022-03-27 14:19:15 +03:00
Dr. Maxim Orlovsky 8884a61283
Merge rust-bitcoin/rust-bitcoin#908: Add serde to TweakedPublicKey
204f477a34 Add serde to TweakedPublicKey (Jeremy Rubin)

Pull request description:

ACKs for top commit:
  dr-orlovsky:
    ACK 204f477a34
  apoelstra:
    ACK 204f477a34
  sanket1729:
    utACK 204f477a34

Tree-SHA512: 61b0140549dfa641edee7e45dbb9f9c816d7319eb83d00ad9b0e7e499f3273de98b65c9905e7d50d4b6ecfd084b8848c4e612cb441124cc469142304ebff18d9
2022-03-27 13:53:30 +03:00
Andrew Poelstra 734b1deb70
Merge rust-bitcoin/rust-bitcoin#897: Check for SIGHASH_SINGLE bug in writer fn
83dda74ecb Check for SIGHASH_SINGLE bug in writer fn (Tobin Harding)

Pull request description:

  Recently we moved the logic for checking for the SIGHASH_SINGLE bug to
  the `signature_hash()` function. Although this left users of the
  `encode_signing_data_to()` function without correct handling of the bug
  there is not much else we can do but alert users to this behaviour.

  Add documentation to highlight the behaviour of `encdoe_signing_data_to`
  in regards to the sighash single bug. Requires updating docs for
  `signature_hash` also.

  Please note, uses non-conventional markdown header `# Warning`.

  Closes: #817

ACKs for top commit:
  sanket1729:
    ACK 83dda74ecb. This is much cleaner
  dr-orlovsky:
    ACK 83dda74ecb
  apoelstra:
    ACK 83dda74ecb

Tree-SHA512: 1263b06ddfbb05a293c80e7dbf6f87eac5922c501e7db1c1d26d41d3ea0172c6b7a44afc0b1843b06e78985d3ecf70a3a3feb2515d535a7413685aed0a338c64
2022-03-26 00:56:37 +00:00
Jeremy Rubin ec17ec356d Move with_huffman_tree logic to TaprootBuilder 2022-03-24 14:57:30 -07:00
Jeremy Rubin 2b942cf506 Add Serialize/Deserialize for TaprootSpendInfo 2022-03-24 14:40:27 -07:00
Jeremy Rubin 204f477a34 Add serde to TweakedPublicKey 2022-03-24 14:39:55 -07:00
Dr. Maxim Orlovsky d263c0c31e
Merge rust-bitcoin/rust-bitcoin#881: Remove feature gated enum variants
6ad2902814 Remove feature gated enum variants (Tobin Harding)

Pull request description:

  This is the updated version of #874 (which I closed, force pushed, and then was unable to re-open - my bad).

  Feature gating enum variants makes code that uses the library brittle while we do not have `non_exhaustive`, we should avoid doing so. Instead we can add a dummy type that is available when the feature is not turned on. Doing so enables the compiler to enforce that we do not create the error type that is feature gated when the feature is not enabled.

  Remove the feature gating around `bitcoinconsensus` error enum variants.

  Closes: #645

ACKs for top commit:
  sanket1729:
    tACK 6ad2902814. This is an improvment.
  dr-orlovsky:
    ACK 6ad2902814

Tree-SHA512: 07d8c6b500d2d5b92e367b89e296b86bec046bab4fe9f624eb087d52ea24a900d7f7a41a98065949c67b307a1f374a7f4cf1b77cb93b6cf19e3d779c27fd7f1d
2022-03-24 14:49:38 +02:00
Dr. Maxim Orlovsky 86c6ab7529
Merge rust-bitcoin/rust-bitcoin#903: Improve `SchnorrSigHashType`
35b682d495 Implement Display/FromStr for SchnorrSigHashType (Tobin Harding)
46c4164d67 Improve SigHashTypeParseError field (Tobin Harding)
c009210d4c Use full path for String in macro (Tobin Harding)

Pull request description:

  Implement Display/FromStr for SchnorrSigHashType

  We currently implement `Display` and `FromStr` on `EcdsaSigHashType` and use them in the `serde_string_impl` macro to implement ser/de.

  Mirror this logic in `SchnorrSigHashType`.

  Patch 1 and 2 are preparatory patches for patch 3.

  ## Notes to reviewers

  This PR has some conflicts with https://github.com/rust-bitcoin/rust-bitcoin/pull/898 but is pushing in the same direction, I'm happy to let 898 go in first and rebase on top.

ACKs for top commit:
  sanket1729:
    ACK 35b682d495. Thanks, much easier to review now that the diff is small
  dr-orlovsky:
    ACK 35b682d495

Tree-SHA512: 481f192a3064ff39acf8904737dfb25b54ef128a37e0ca765ebb39138edac772d4f01ed10aa98ff185a8ed5668d64fa5d5957206b920ffe87950cafcf5a3b516
2022-03-24 08:02:27 +02:00
sanket1729 ea80e6568a
Merge rust-bitcoin/rust-bitcoin#805: Remove impl_index_newtype macro
63e36fe6b4 Remove impl_index_newtype macro (Tobin Harding)

Pull request description:

  This macro is no longer needed since we bumped MSRV to 1.29.

  ~We can implement `SliceIndex` to get the `Index` implementations.~
  We can implement `core::ops::Index` directly since all the inner types implement `Index` already.

  Original ~Idea shamelessly stolen from @elichai [in this comment](https://github.com/rust-bitcoin/rust-bitcoin/issues/352#issuecomment-560331856).~

  New idea proposed by @Kixunil during review below. Thanks.

ACKs for top commit:
  apoelstra:
    ACK 63e36fe6b4
  dr-orlovsky:
    utACK 63e36fe6b4
  sanket1729:
    ACK 63e36fe6b4

Tree-SHA512: f7b4555c7fd9a2d458dcd53ec8caece0d12f3af77a10e850f35201bd7a580ba8fd7cb1d47a7f78ba6582e777dffa13416916ecacac6e0e874bdbb1c866132dc2
2022-03-23 19:01:56 -07:00
Tobin Harding 35b682d495 Implement Display/FromStr for SchnorrSigHashType
We currently implement `Display` and `FromStr` on `EcdsaSigHashType` and
use them in the `serde_string_impl` macro to implement ser/de.

Mirror this logic in `SchnorrSigHashType`.
2022-03-24 12:47:18 +11:00
Tobin Harding 46c4164d67 Improve SigHashTypeParseError field
In preparation for constructing an error outside of this module improve
the `SigHashTypeParseError` by doing:

- Make the field public
- Rename the field to `unrecognized` to better describe its usage
2022-03-24 12:28:59 +11:00
Tobin Harding 83dda74ecb Check for SIGHASH_SINGLE bug in writer fn
Recently we moved the logic for checking for the SIGHASH_SINGLE bug to
the `signature_hash()` function. Although this left users of the
`encode_signing_data_to()` function without correct handling of the bug
there is not much else we can do but alert users to this behaviour.

Add documentation to highlight the behaviour of `encdoe_signing_data_to`
in regards to the sighash single bug. Requires updating docs for
`signature_hash` also.

Please note, uses non-conventional markdown header `# Warning`.
2022-03-24 11:38:58 +11:00
Tobin Harding c009210d4c Use full path for String in macro
As is done in the rest of the `internal_macros` module use the fully
qualified path for the `String` type.

Done in preparation for using `serde_string_impl` in the `sighash`
module.
2022-03-24 11:01:15 +11:00
Dr Maxim Orlovsky e27f8ff594
TapTree iterator implementation 2022-03-24 00:03:54 +01:00
Steven Roose 31571cafbd
util::amount: Make from_sat constructor constant 2022-03-23 15:15:08 +00:00
Tobin Harding 6ad2902814 Remove feature gated enum variants
Feature gating enum variants makes code that uses the library brittle
while we do not have `non_exhaustive`, we should avoid doing so. Instead
we can add a dummy type that is available when the feature is not turned
on. Doing so enables the compiler to enforce that we do not create the
error type that is feature gated when the feature is not enabled.

Remove the feature gating around `bitcoinconsensus` error enum variants.

Closes: #645
2022-03-23 14:58:45 +11:00
Andrew Poelstra 1295008dc6
Merge rust-bitcoin/rust-bitcoin#861: Remove get_ prefix
3bde1a205c Remove get_ prefix (Tobin Harding)

Pull request description:

  This one might be a viewed as code churn or unnecessarily modifying the API, feel free to NACK :)

  We have a bunch of methods that use the prefix `get_`, they are not exactly getters because they do more than just access a struct fields so Rust convention relating to getters does not apply, however, the `get_` prefix does not add to the descriptiveness of name hence the shorter form can be used with no loss of clarity.

  Improve docs and deprecate any methods changed that are pubic.

ACKs for top commit:
  dr-orlovsky:
    ACK 3bde1a205c
  apoelstra:
    ACK 3bde1a205c
  sanket1729:
    ACK 3bde1a205c

Tree-SHA512: d9e618ba7fec81ad157c2c806d1db273f899d63707c78254c133b619293f9f0c9a4f3a3e091e9aad399479ff80d5d052c424501164374c21bb90fb9783a4824e
2022-03-21 21:34:54 +00:00
Andrew Poelstra 1cde71ea44
Merge rust-bitcoin/rust-bitcoin#880: Use conventional spacing for default type parameters
1629348c24 Use conventional spacing for default type parameters (Tobin Harding)

Pull request description:

  The exact code formatting we use is not as important as uniformity. Since we do not use tooling to control the formatting we have to be vigilant ourselves. Recently I (Tobin) changed the way default type parameters were formatted (arbitrarily but uniformly). Turns out I picked the wrong way, there is already a convention as shown in the rust documentation online (e.g. [1]).

  Use 'conventional' spacing for default type parameters. Make the changeacross the whole repository, found using

      git grep '\<.* = .*\>'

  [1] - https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

  **Note**

  I also audited our whole stack to make sure I had not botched this anywhere else. Apologies for the code churn.

ACKs for top commit:
  dr-orlovsky:
    utACK 1629348c24
  apoelstra:
    ACK 1629348c24

Tree-SHA512: 61c8a22acc557d8b99f7b591cf8f91b94778a954ac1c9d6cf04a2bbd10953c241e8298e71928aad3b065c98117b816b913226d973fdaa9c3a1aea8daf8bcbe72
2022-03-21 21:09:46 +00:00
Dr. Maxim Orlovsky 443a38567b
Merge rust-bitcoin/rust-bitcoin#877: Improve ClassifyContext rustdocs
51a51cd67d Improve ClassifyContext rustdocs (Tobin Harding)

Pull request description:

  Improve the rustdocs on the `ClassifyContext` enum by doing:

  - Use link for `OP_RESERVED`
  - Use term `OP_SUCCESSx` is done in BIP342 (no code link, does not exist in code).
  - Use enum::variant form for both variant mentions
  - Direct readers to BIP342 for full list of opcode re-names

ACKs for top commit:
  sanket1729:
    ACK 51a51cd67d
  apoelstra:
    ACK 51a51cd67d
  dr-orlovsky:
    ACK 51a51cd67d

Tree-SHA512: 1a9067246ef84eae39b0adef64190b9212dacb55a420909ee38c582ef1960fceb572f82d3eeff518b58fc2cceffe71b3da4e78da54cd4cb6e05a0e48a3a9d03c
2022-03-20 20:40:32 +02:00
Tobin Harding 3bde1a205c Remove get_ prefix
We have a bunch of methods that use the prefix `get_`, they are not
exactly getters because they do more than just access a struct fields so
Rust convention relating to getters does not apply, however, the `get_`
prefix does not add to the descriptiveness of name hence the shorter
form can be used with no loss of clarity.

Improve docs and deprecate any methods changed that are pubic.
2022-03-19 08:28:30 +11:00
Dr. Maxim Orlovsky ebf9162835
Merge rust-bitcoin/rust-bitcoin#860: Fix signature hash returned for sighash single bug
d1abfd9c30 Add unit test for sighash single bug (Tobin Harding)
82f29b4267 Use 1 signature hash for invalid SIGHASH_SINGLE (Tobin Harding)
3831816a73 Move test helper function (Tobin Harding)
3e21295b88 Remove unnecessary whitespace character (Tobin Harding)

Pull request description:

  Fix up the logic that handles correctly returning the special array 1,0,0,...,0 for signature hash when the sighash single bug is exploitable i.e., when signing a transaction with SIGHASH_SINGLE for an input index that does not have a corresponding transaction output of the same index.

  - Patch 1 and 2: Clean up
  - Patch 3: Implements the fix
  - Patch 4: Adds a passing test that fails if moved to before patch 3

  Resolves: #817

ACKs for top commit:
  apoelstra:
    ACK d1abfd9c30
  dr-orlovsky:
    ACK d1abfd9c30

Tree-SHA512: f2d09e929d2f91348ae0b0758b3d4be6c6ce0cb38c4988e0bebb29f5918ca8491b9e7b31fe745f7c20d9348612fe2166f0a12b782f256aad5f6b6c027c2218b7
2022-03-18 13:00:09 +02:00
Tobin Harding 1629348c24 Use conventional spacing for default type parameters
The exact code formatting we use is not as important as uniformity.
Since we do not use tooling to control the formatting we have to be
vigilant ourselves. Recently I (Tobin) changed the way default type
parameters were formatted (arbitrarily but uniformly). Turns out I
picked the wrong way, there is already a convention as shown in the rust
documentation online (e.g. [1]).

Use 'conventional' spacing for default type parameters. Make the change
across the whole repository, found using

    git grep '\<.* = .*\>'

[1] - https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
2022-03-18 10:40:51 +11:00
Tobin Harding 63e36fe6b4 Remove impl_index_newtype macro
This macro is no longer needed since we bumped MSRV to 1.29.

We can implement `core::ops::Index` directly since all the inner types
implement `Index` already.
2022-03-17 08:12:09 +11:00
Tobin Harding 51a51cd67d Improve ClassifyContext rustdocs
Improve the rustdocs on the `ClassifyContext` enum by doing:

- Use link for `OP_RESERVED`
- Use term `OP_SUCCESSx` is done in BIP342 (no code link, does not exist
  in code).
- Use enum::variant form for both variant mentions
- Direct readers to BIP342 for full list of opcode re-names
2022-03-15 14:39:24 +11:00
Martin Habovstiak 4f1200d629 Added `amount::Display` - configurable formatting
This significatnly refactors the amount formatting code to make
formatting more configurable. The main addition is the
`amount::Display` type which is a builder that can configure
denomination or other things (possibly more in the future).

Further, this makes all representations of numbers minimal by default,
so should be documented as a possibly-breaking change.

Because of the effort to support all other `fmt::Formatter` options this
required practically complete rewrite of `fmt_satoshi_in`. As a
byproduct I took the opportunity of removing one allocation from there.

Closes #709
2022-03-14 19:03:03 +01:00
Tobin Harding a77907d59c Remove unnecessary explicit type annotation
The compiler can infer this type, no need for an explicit type
annotation.
2022-03-14 13:52:25 +11:00
Tobin Harding 71cf00a314 Use less vertical lines
In this library we specifically do not use rustfmt and tend to favour
terse statements that do not use extra lines unnecessarily. In order to
help new devs understand the style modify code that seems to use an
unnecessary number of lines.

None of these changes should reduce the readability of the code.
2022-03-14 13:52:13 +11:00
Tobin Harding a5c06e0a96 Refactor vector initialisation
Vector initialisation uses neither "Block" nor "Visual" stlye, this is
irregular for no added benefit.

Elect to use "Block" style (as defined by `rustfmt`).
2022-03-14 13:52:13 +11:00
Tobin Harding aabf2d1681 Use brace not parenth fo macro arm
Macro match arms can use any parenthesis-like character (it seems),
however since we are delimiting a block of code elect to use braces.
2022-03-14 13:52:13 +11:00
Tobin Harding b021415a88 Use block stlye function call
This function uses neither "Block" nor "Visual" style (as defined by
`rustfmt`). This is unusual, code that is regular is less jarring to
read. We tent to use "Block" style for functions so elect to do that
here.
2022-03-14 13:52:13 +11:00
Tobin Harding d6462bae7b Refactor usage of +
As we do for logical operators; put the `+` operator at the start of the
line to make it more obvious and assist devs reading the code.
2022-03-14 13:52:13 +11:00
Tobin Harding 702e8bf82d Refactor consensus_encode
The implementations of `consensus_encode` use an unnecessary number of
lines. Favour more terse code with no loss of clarity.
2022-03-14 13:52:13 +11:00
Tobin Harding a8ed95ea07 Refactor where statements
Our usage of `where` statements is not uniform, nor is it inline with
the typical layout suggested by `rustfmt`.

Make an effort to be more uniform with usage of `where` statements.
However, explicitly do _not_ do every usage since sometimes our usage
favours terseness (all on a single line).
2022-03-14 13:52:13 +11:00
Tobin Harding 6d84998168 Improve braces usage
Add a pair of braces to improve readability.
2022-03-14 13:52:13 +11:00
Tobin Harding 39ec59620d Fix unusual indentation
We have a few instances of strange indentation:

- Incorrect number of characters
- Usage of neither "Block" style or "View" style (elect to use "Block")
2022-03-14 13:52:13 +11:00
Tobin Harding b9b6e7e1c6 Remove unneeded braces
Use statement contains unneeded braces, remove them.
2022-03-14 13:52:13 +11:00
Tobin Harding 5d68ad85ed Remove unneeded return statement
The last statement of a function does not need an explicit `return`
statement.
2022-03-14 13:52:13 +11:00
Tobin Harding bf4f5638e0 Refactor whitespace
Do various whitespace refactorings, of note:

- Use space around equals e.g., 'since = "blah"'
- Put return/break/continue on separate line

Whitespace only, no logic changes.
2022-03-14 13:51:50 +11:00
Tobin Harding 1c502399f1 Remove trailing whitespace
Remove trailing whitespace from all rust source files.

Done with:

find . -name *.rs | xargs perl -pli -e "s/\s*$//"
2022-03-14 13:40:44 +11:00
Tobin Harding d1abfd9c30 Add unit test for sighash single bug
When signing a transaction will result in the sighash single bug being
exploitable we should return the 'one array' (equivalent to 1 as a
uint256) as the signature hash.

Add a unit test to verify we return uint256 1 value when use of
SIGHASH_SINGLE is invalid.
2022-03-14 12:10:16 +11:00
Tobin Harding 82f29b4267 Use 1 signature hash for invalid SIGHASH_SINGLE
When signing a transaction will result in the sighash single bug being
exploitable we should return the 1 array (equivalent to 1 as a uint256)
as the signature hash.

Currently we are using the correct array value but are re-hashing it,
instead we should directly return it.
2022-03-14 12:09:39 +11:00
Tobin Harding 3831816a73 Move test helper function
Move helper function to above the test that uses it.

Refactor only, no logic changes.
2022-03-14 12:06:54 +11:00
Tobin Harding 3e21295b88 Remove unnecessary whitespace character
Typically we do not put a whitespace character before a `:` when using
explicit types.
2022-03-14 12:06:54 +11:00
Dr Maxim Orlovsky 7554d76dfe
Make Script::witness_version public 2022-03-13 14:40:03 +01:00
Andrew Poelstra 93daed95bd
Merge rust-bitcoin/rust-bitcoin#871: Delete contract hash module
7f33fe6a9b Delete contract hash module (Tobin Harding)

Pull request description:

  This module has been deprecated in commit 1ffdce9 in August 2020, it is safe to delete it now.

  Fixes: #322

ACKs for top commit:
  apoelstra:
    ACK 7f33fe6a9b
  Kixunil:
    ACK 7f33fe6a9b
  dr-orlovsky:
    ACK 7f33fe6a9b

Tree-SHA512: f218c8b0c09b14cd885cd7cf03c0a4623e5ead785decbc62a2f9610d438d5ea3efd2e2b47172a7608e33714996efa121707583d4257fa683dbfc9717988ceda6
2022-03-12 12:27:31 +00:00
Dr. Maxim Orlovsky 60d941621d
Merge rust-bitcoin/rust-bitcoin#825: test: Add a test for incorrect message signature in `is_signed_by_address`
e391ce9939 test: Add a test for incorrect message signature (Andrew Ahlers)

Pull request description:

  In response to this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/819#discussion_r801477961

  This should be straightforward. Let me know if there are any style issues. I tried to keep things similar to the existing test while cutting out any extra cruft to keep things small.

ACKs for top commit:
  apoelstra:
    ACK e391ce9939
  Kixunil:
    ACK e391ce9939
  dr-orlovsky:
    ACK e391ce9939

Tree-SHA512: 47296a7e0b2f45d5e50f507727ae4360686730a386f37dedfd1360b8cdf4b9dd3ce3bb5d05ea630177379ce4109059b6924fa362396b984ebab0ed1754318627
2022-03-12 13:17:45 +02:00