Commit Graph

2517 Commits

Author SHA1 Message Date
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
sanket1729 db23006ff5
Merge rust-bitcoin/rust-bitcoin#891: Rust-Bitcoin Logo
3d3993a3ba Add Rust-Bitcoin logo and project header. (Hunter Trujillo)

Pull request description:

  Adds a logo and a project header with some shields based on BDK's. Preview in dark mode:

  ![Screenshot from 2022-03-19 09-10-55](https://user-images.githubusercontent.com/285690/159126691-823fd6f0-295d-44bf-8a42-0b8df64d5a1f.png)

ACKs for top commit:
  dr-orlovsky:
    ACK 3d3993a3ba
  sanket1729:
    ACK 3d3993a3ba

Tree-SHA512: e1e44109584e915f6f4981ec91f04b046721e0076e6d27d7e995eb2a52f190a4de83fe9d4e8210e0f0d1c7c7ae603a1e6cc7eb91a096962df9166b922b5ce19b
2022-03-23 09:04:37 -07: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
Hunter Trujillo 3d3993a3ba
Add Rust-Bitcoin logo and project header.
Remove old shields.

Replace with new single gear logo.

Add license, readme, and source files under /logo.
2022-03-22 20:30:33 -06: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
Andrew Poelstra 7e6f514bdf
Merge rust-bitcoin/rust-bitcoin#806: Use rustfmt to hint at clean ups for the codebase
a77907d59c Remove unnecessary explicit type annotation (Tobin Harding)
71cf00a314 Use less vertical lines (Tobin Harding)
a5c06e0a96 Refactor vector initialisation (Tobin Harding)
aabf2d1681 Use brace not parenth fo macro arm (Tobin Harding)
b021415a88 Use block stlye function call (Tobin Harding)
d6462bae7b Refactor usage of + (Tobin Harding)
702e8bf82d Refactor consensus_encode (Tobin Harding)
a8ed95ea07 Refactor where statements (Tobin Harding)
6d84998168 Improve braces usage (Tobin Harding)
39ec59620d Fix unusual indentation (Tobin Harding)
b9b6e7e1c6 Remove unneeded braces (Tobin Harding)
5d68ad85ed Remove unneeded return statement (Tobin Harding)
bf4f5638e0 Refactor whitespace (Tobin Harding)
1c502399f1 Remove trailing whitespace (Tobin Harding)

Pull request description:

  `rustfmt` is still under discussion, while researching the topic I came across a maintainer of another project that does not use `rustfmt` who mentioned that he manually implemented the `rusfmt` suggestions that he liked ever month or so. This seemed like a good idea so I did it. This was extremely painful but I believe I have put together a PR that is non-controversial with well separated patches.

  Totally non urgent.

ACKs for top commit:
  apoelstra:
    ACK a77907d59c
  sanket1729:
    ACK a77907d59c.

Tree-SHA512: 27aa10d1c6d02d0e5bc335a5cda9cf2664b968c298d2ea6c653b8074abf18764a9d0f19c36222852fc23b887ab64144901dae059088e61478e9a90a042221e61
2022-03-17 17:38:11 +00: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