Commit Graph

427 Commits

Author SHA1 Message Date
sanket1729 289dc1e7f5 Remove serde for taprootspendinfo
Implementing this for spendinfo is really complicated because it
contains some cached data without retaining the components that are used
to compute them.

Users should serde the 1) NodeInfo and 2) internal key and reconstruct
TaprootSpendInfo from it.
2023-03-01 16:58:24 -08:00
sanket1729 a397ab0c19 Remove serde for ScriptLeaf
This was incorrect and not needed. Users should not be able to create
only tree leaves directly without going through the tree construction in
rust-bitcoin
2023-03-01 16:58:24 -08:00
sanket1729 9affda3012 Introduce Hidden leaves in ScriptLeaves
Cleanly separate `TapTree` and `NodeInfo`. Fix serde not respecting
invariants for several data structures

Repurpose some tests from removed taproot builder for taptree
2023-03-01 16:58:24 -08:00
sanket1729 22bc39a143 Fix serde for TaprootMerkleBranch 2023-03-01 16:57:56 -08:00
sanket1729 38ed9bdf49 MOVE ONLY: Move TapTree to taproot module 2023-03-01 16:57:55 -08:00
Andrew Poelstra 2cf1a4c088
Merge rust-bitcoin/rust-bitcoin#1678: Improve the public API
42b07586ac Improve the public API (Tobin C. Harding)

Pull request description:

  We created the `crypto` crate as a container for cryptography modules with the idea that it may be split out into a separate crate. There is no reason for users of the lib to know about this module. Also, we have two `taproot` modules, one in `crypto` and one at the crate root, this makes for un-ergonomic usage of the lib.

  Improve the public API by doing:

  - Make the `crypto` module private (`pub(crate)`).
  - Re-export `crypto::taproot::Signature` (and `Error`) from `crate::taproot`

  Fix: #1668

ACKs for top commit:
  apoelstra:
    ACK 42b07586ac
  Kixunil:
    ACK 42b07586ac

Tree-SHA512: 5713b98b2a48d2776cdd6ca2c0e798d6e2df604d780e5182bd770fb2df807cf1f65bc24663ee6f7734fc1e0c80494c5a87dc60e44c83acefcffec7f059203a47
2023-03-01 15:42:36 +00:00
Tobin C. Harding 42b07586ac
Improve the public API
We created the `crypto` crate as a container for cryptography modules
with the idea that it may be split out into a separate crate. There is
no reason for users of the lib to know about this module. Also, we have
two `taproot` modules, one in `crypto` and one at the crate root, this
makes for un-ergonomic usage of the lib.

Improve the public API by doing:

- Make the `crypto` module private (`pub(crate)`).
- Re-export `crypto::taproot::Signature` (and `Error`) from
  `crate::taproot`
2023-03-01 09:28:42 +11:00
Andrew Poelstra 5ad2bec626
Merge rust-bitcoin/rust-bitcoin#1679: Add constant for coinbase maturity
7d1645aea0 Add constant for coinbase maturity (benthecarman)

Pull request description:

  Not sure if this is the best place to put this but it is nice to have a constant for this instead of having other libraries make their own (ie https://github.com/lightningdevkit/rust-lightning/pull/1924#pullrequestreview-1222807626)

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

Tree-SHA512: 5ac2a3359cadd303158c66ba45db8f4bf8cc80b6c19604262999ff361fd0bd98e2a4851c57da1962cb5c74f5789a85c8b3861f1742706a60ce1fbc57c3c200cc
2023-02-28 22:01:34 +00:00
benthecarman 7d1645aea0
Add constant for coinbase maturity 2023-02-28 05:37:10 -06:00
Andrew Poelstra 13143d0f6f
Merge rust-bitcoin/rust-bitcoin#1675: Add utils to convert ChainHash to a Network
56569b32ef Add utils to convert ChainHash to a Network (benthecarman)

Pull request description:

ACKs for top commit:
  Kixunil:
    ACK 56569b32ef
  tcharding:
    ACK 56569b32ef

Tree-SHA512: a489fcb1c1208db4271076d88288658988a63209e56e7433bde82d7d5719450433348fcc3cb6aae59ffa6ed8aff510d6b031c6899d5cf64c503a53b2d4c692b8
2023-02-27 23:55:24 +00:00
benthecarman 56569b32ef
Add utils to convert ChainHash to a Network 2023-02-27 12:28:04 -06:00
Andrew Poelstra 4d8ba9be31
Merge rust-bitcoin/rust-bitcoin#1577: Re-name hash inner/byte methods
161273b209 Re-name hash inner/byte methods (Tobin C. Harding)
324b6f264b Use `into` for hash argument (Tobin C. Harding)

Pull request description:

  Currently we have an associated type on hash types `Inner` with accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we provide a way to create new wrapped hash types. The use of 'inner' becomes ambiguous with the addition of wrapped types because the inner could be the inner hash type or the `Inner` byte array of the inner wrapped hash type.

  In an effort to make the API more clear and uniform do the following:

  - Rename `Inner` -> `Bytes`
  - Rename `*_inner` -> `*_byte_array`
  - Rename the inner hash to/from methods to `*_raw_hash`

  Correct method prefix `into_` -> `to_` because theses methods convert owned `Copy` types.

  Add the trait Bound `Copy` to the `Bytes` type because we rely on this trait bound for the conversion methods to be correctly named according to convention.

  Because of the dependency hole created by `secp256k1` this patch changes the secp dependency to a git tag dependency that includes changes to the hashes calls required so that we can get green lights on CI in this repo.

  Fix: #1554

ACKs for top commit:
  Kixunil:
    ACK 161273b209
  apoelstra:
    ACK 161273b209

Tree-SHA512: b51b851a1855e6a26a7ef8ccb9f554723d4cc39b368812703587a50e81e7ab49714a81696af0be743b947f09e1fca227a5331b6735912c5b0d5cd0178905f006
2023-02-27 17:28:14 +00:00
Andrew Poelstra a40427b177
Merge rust-bitcoin/rust-bitcoin#1676: Fix docs for UnknownMagic to be accurate
a4b5fb4002 Fix docs for UnknownMagic to be accurate (benthecarman)

Pull request description:

  I assume the old docs are a copy-paste error, strings are not involved when this error is encountered.

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

Tree-SHA512: b2b71f81be8a0d979b15471e7262e01284443e05626b26a19236fd25581700d9e37409576a4b73d5bb537c49ae83a4b7d40f0888dff078b07bd7550026cd778a
2023-02-27 15:54:07 +00:00
Andrew Poelstra 826a988be8
Merge rust-bitcoin/rust-bitcoin#1674: Reexport `Magic`
76c4c647cf Reexport `Magic` (Martin Habovstiak)

Pull request description:

  Writing `network::Magic` is more natural and less annoying than `network::constants::Magic`, so this change reexports it.

  Closes #1667

ACKs for top commit:
  apoelstra:
    ACK 76c4c647cf
  tcharding:
    ACK 76c4c647cf

Tree-SHA512: 1d85372ecd9723c0871faa4552ba5f93a3c95d0b213fa8aace398949f97157cdfde164bbcb2c35bae4c61ae0185e3fbdb854714cde0849c1afaf90e4e8ec223f
2023-02-27 13:51:03 +00:00
benthecarman a4b5fb4002
Fix docs for UnknownMagic to be accurate 2023-02-27 00:33:20 -06:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00
Tobin C. Harding 324b6f264b
Use `into` for hash argument
Hash types can be converted into a `Message` because `Message`
implements `From` for any type that implements `ThirtyTwoByteHash`,
which hash types do.

Use `into` to convert the hash argument to a message to sign.
2023-02-27 12:00:08 +11:00
Andrew Poelstra 5a867821aa
Merge rust-bitcoin/rust-bitcoin#1623: Improve string parsing
090dad770f Improve string parsing (Tobin C. Harding)

Pull request description:

  Currently we implement string parsing for height/time from the `absolute` module but not the `relative` module.

  Improve the macros used to implement string parsing and use the new versions to implement string parsing for the height and time types in `relative`.

  Done while reviewing data structures in relation to `serde`.

ACKs for top commit:
  apoelstra:
    ACK 090dad770f
  Kixunil:
    ACK 090dad770f

Tree-SHA512: bfa88efbaf5dc35755eb46df373a08e223f112860e8a65f58db9fdd77e2c01dc9377da735b33ef58940004fe5fe11690ac09be19591fded2c9fd04cd7d2bdf73
2023-02-25 21:16:38 +00:00
Martin Habovstiak 76c4c647cf Reexport `Magic`
Writing `network::Magic` is more natural and less annoying than
`network::constants::Magic`, so this change reexports it.

Closes #1667
2023-02-25 19:10:36 +01:00
Harshil Jani f62885890d Accept borrowed values in InputWeightPrediction::new()
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-02-25 17:20:40 +05:30
Andrew Poelstra fbb3b82b93
Merge rust-bitcoin/rust-bitcoin#1663: Create Address::matches_script_pubkey method
d71c31c235 Create Address::matches_script_pubkey method (hashmap)

Pull request description:

  to check if an address creates a particular script without allocating.

  fixes rust-bitcoin/rust-bitcoin#1604

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

Tree-SHA512: cb60a53ae2be7c47dcd27415c883a73c81d57cbbf0bc92eaf76243d79d9c8e2c2efe91bef65a7e67ed26fec376f11325709ff27025d054813b1907ea2bf4961c
2023-02-24 14:51:05 +00:00
Andrew Poelstra 9c1872f96d
Merge rust-bitcoin/rust-bitcoin#1659: Make `hash_newtype` evocative of the output
06f1f027ab Make `hash_newtype` evocative of the output (Martin Habovstiak)
b018f3e90b Remove the `$len` argument from `hash_newtype` (Martin Habovstiak)
752817e20d Stop using `$len` in `hash_newtype` (Martin Habovstiak)

Pull request description:

  The API guidelines say macro input should be evocative of the output.
  `hash_newtype` didn't have this property.

  This change makes it look exactly like the resulting struct, `$len`
  parameter was removed since it's not needed, reversing is controlled
  using an attribute. The macro is also better documented and ready to be
  extended in the future.

  The tagged SHA256 newtype is not yet modified because it has a more
  complicated input parameters.

  Closes #1648

ACKs for top commit:
  apoelstra:
    ACK 06f1f027ab

Tree-SHA512: 9762db1eca9cd749980e5d68ca286f6c926620295a602f62365f199c7b333334b976db25ba25c64e56403cd1ba046b21b99e1c73cc528ad95612ef8901f216e5
2023-02-24 13:47:22 +00:00
hashmap d71c31c235
Create Address::matches_script_pubkey method
to check if an address creates a particular script without allocating.

fixes rust-bitcoin/rust-bitcoin#1604
2023-02-24 11:07:53 +01:00
Tobin C. Harding 596e756d01
Pin syn dependency for MSRV toolchain
The recent 1.0.108 `syn` update violated our MSRV, pin `syn` in the CI
script.
2023-02-24 08:41:40 +11:00
Andrew Poelstra 3a53e7e12e
Merge rust-bitcoin/rust-bitcoin#1653: Show cache construction in rustdoc
438ee45691 Show cache construction in rustdoc (Tobin C. Harding)

Pull request description:

  To make it more clear what the cache is show the cache construction line in rustdoc.

ACKs for top commit:
  apoelstra:
    ACK 438ee45691
  Kixunil:
    ACK 438ee45691

Tree-SHA512: d6da6bad57fddf9e2f4bcfb7c9b87df38bf4b2bb914e92e52d5ae8afa3405a9793536d7164223021ab6d183ddde732cf6889370834e36f37bae470127b0271fa
2023-02-22 15:28:56 +00:00
Andrew Poelstra 17490f3734
Merge rust-bitcoin/rust-bitcoin#1654: Do trivial manifest cleanup
2620f3b69d Put optional = true at the end (Tobin C. Harding)
d86ef3b01b Put non-optional dependency with other non-optional (Tobin C. Harding)

Pull request description:

  Late stage, super anal, manifest cleanup.

  - Patch 1: put non-optional dependencies together
  - Patch 2: put `optional = true` at the end of the line

ACKs for top commit:
  apoelstra:
    ACK 2620f3b69d
  Kixunil:
    ACK 2620f3b69d

Tree-SHA512: bca2bd2eb9a50aea04cab7c6eab7c264e91937ff20473ccf4496ba380897d1d50696719aa3379bd3f29b8a3cbe43d5485ac068f0e2c126462f361fcfe9941548
2023-02-22 15:19:34 +00:00
Martin Habovstiak 06f1f027ab Make `hash_newtype` evocative of the output
The API guidelines say macro input should be evocative of the output.
`hash_newtype` didn't have this property.

This change makes it look exactly like the resulting struct, `$len`
parameter was removed since it's not needed, reversing is controlled
using an attribute. The macro is also better documented and ready to be
extended in the future.

The tagged SHA256 newtype is not yet modified because it has a more
complicated input parameters.

Closes #1648
2023-02-22 14:35:50 +01:00
Martin Habovstiak b018f3e90b Remove the `$len` argument from `hash_newtype`
Now that the `$len` argument is no longer used, remove it completely.
2023-02-22 14:35:14 +01:00
Lorenzo Maturano f69363d71a adding suggested documentation for path arg 2023-02-21 16:58:22 -03:00
Lorenzo Maturano 673ca2d2fe changing docs and examples to use reference to slice in `derive_pub` 2023-02-21 14:34:14 -03:00
Tobin C. Harding 2620f3b69d
Put optional = true at the end
In order to be uniform and make it easier to quickly see that the
dependency is optional put `optional = true` as the last item in the
config set.
2023-02-21 08:35:25 +11:00
Tobin C. Harding d86ef3b01b
Put non-optional dependency with other non-optional
We have a separation between optional dependencies and non-optional
dependencies, `hex_lit` snuck in to the wrong group.
2023-02-21 08:33:51 +11:00
Tobin C. Harding 438ee45691
Show cache construction in rustdoc
To make it more clear what the cache is show the cache construction line
in rustdoc.
2023-02-21 08:12:32 +11:00
Martin Habovstiak d83739a980 Clarify the intention of strange condition
It may not be obvious why the condition in `push_bytes` module checks
for negation of 16 and 32 bit architectures rather than 64 bit. This
adds a comment about it being conservative.
2023-02-20 19:40:19 +01:00
Andrew Poelstra 182fa9bd76
Merge rust-bitcoin/rust-bitcoin#1651: Get rid of BadFormat error
6fb2d12373 Get rid of BadFormat error (hashmap)

Pull request description:

  add additional variants instead.
  as discussed in https://github.com/rust-bitcoin/rust-bitcoin/pull/1365

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

Tree-SHA512: 2cf9146670c372a3a482448f84a30943cd2ff2fa4e724075d67a52dba5ac0ad38f99ca2af3dd3494e13584653f2b23e913e6421328d40be52e868a107fffe03b
2023-02-20 15:51:13 +00:00
hashmap 6fb2d12373
Get rid of BadFormat error
add additional variants instead.
2023-02-20 12:53:57 +01:00
Tobin C. Harding be7b3754a9
Rename schnorr module to taproot
"schnorr" is a dirty word; the current `schnorr` module defines a
`Signature` that includes a sighash type, this sighash type is a bitcoin
specific construct related to taproot. Therefore the `Signature` is
better named `taproot::Signature`. Note also that the usage of `schnorr`
in `secp256k1` is probably justified because the
`secp256::schnorr::Signature` is just doing the crypto.

While we are at it, update docs and error messages to use "taproot"
instead of "schnorr". Also change function names and identifiers that
use "schnorr".
2023-02-20 12:58:09 +11:00
Tobin C. Harding 9f39e872bc
Rename SchnorrSighashType to TapSighashType
As we did for `SchnorrSighash`, rename the `SchnorrSighashType` to
`TapSighashType`.
2023-02-20 12:58:09 +11:00
Tobin C. Harding f5c26693c5
Make match arms more terse
Add function local import statements so we can make the match arms more
terse.

Refactor only, no logic changes.
2023-02-20 12:58:09 +11:00
Tobin C. Harding 40c246743b
Split Sighash into LegacySighash and SegwitV0Sighash
Currently we have `TapSighash` that is used for taproot sighashes but
for non-taproot sighashes we use `hash_types::Sighash`. We can improve
the API by creating a `LegacySighash`, and `SegwitV0Sighash`.

Copy the original `Sighash` macro calls to create the two new types in
the `sighash` module.

While we are at it, put the `TapSighash` and `TapSighashTag` into the
`sighash` module also.
2023-02-20 12:58:09 +11:00
Tobin C. Harding e38d843536
Do not use deprecated function in rustdoc example
Currently we are use the deprecated signing method on `Transaction`, we
should use the new method on the sighash cache.
2023-02-20 12:58:08 +11:00
Tobin C. Harding 98130f49f1
Rename TapSighashHash to TapSighash
The TapSighash is the taproot sighash, no need to append `Hash` to the
identifier.
2023-02-20 12:58:08 +11:00
Tobin C. Harding 7e4da3c0ab
Move taproot keys to the keys module
We have a keys module, taproot keys should live in there.
2023-02-20 12:58:05 +11:00
Tobin C. Harding c5fe315a93
Move sighash to crypto module
There is never any use for the `sighash` module unless one is signing,
which requires the `crypto` module. The `sighash` module should
therefore live in the `crypto` module. This is not an API breaking
change because we reexport it at the crate root.
2023-02-20 12:56:34 +11:00
Martin Habovstiak bcd95fa036 Add a newtype for script-pushable slice
The code previously contained undocumented panic when trying to push
slice. This change solves it by adding a newtype that guarantees limited
length.
2023-02-18 16:35:25 +01:00
Martin Habovstiak 8fedbcbf13 Add `ecdsa::SerializedSignature`
`Signature` only supported serialization into `Vec` which required a
heap allocation as well as prevented statically proving maximum length.

Adding a specialized type that holds a byte array and size solves this.
The solution is very similar to `secp256k1::ecdsa::SerializedSignature`.
The difference is that serialized signature in this crate contains
sighash bytes flag while in `secp256k1` it doesn't.
2023-02-18 12:31:07 +01:00
Martin Habovstiak 26fc4152ec Use `PushDataLenLen` to improve confidence
Script parsing is composed of several functions which implicitly rely on
various properties. Adding a type that restricts the valid values makes
local review easier.
2023-02-18 12:24:36 +01:00
Martin Habovstiak bb2c7ec790 Introduce `hex_lit` crate
So far we deserialized hex into `Vec<u8>` at run time. This was mainly
in tests where it had negligible performance cost. However moving the
computation to compile time has a few benefits: it allows proving the
length of the decoded bytes and identifies potential typos before the
code goes through LLVM and other compilation machinery which makes
feedback faster.

This change uses the `hex_lit` crate to move computation to compile
time. It is implemented as `const` declarative macro which doesn't blow
up compilation time.
2023-02-18 12:03:08 +01:00
Andrew Poelstra 562400a7bc
Merge rust-bitcoin/rust-bitcoin#1593: hashes: Implement AsRef for fixed size arrays
a121e19e94 hashes: Implement AsRef for fixed size arrays (Tobin C. Harding)

Pull request description:

  Implement `AsRef<[u8; X]>` for hash types including wrapped hash types. Doing so means at times the compiler can no longer infer the type because we have `AsRef<[u8]` implemented also but we can use `into_inner` and `as_inner` to get the inner array if needed.

  Fix: #1462

  ## Note

  This touches code that will likely be changed by #1577 and when we do #1491 but I believe its a step forward.

ACKs for top commit:
  arturomf94:
    ACK [`a121e19`](a121e19e94)
  apoelstra:
    ACK a121e19e94
  Kixunil:
    ACK a121e19e94

Tree-SHA512: 257c44826c7649db25bb3a6f023f68b2f17b70c546a056afad044bc8a16bf61f654c3846222505aaf5e6f9a0ad1d2113272d61317b407d0ac83702e41060a1ee
2023-02-17 13:24:57 +00:00
Andrew Poelstra e4e7449562
Merge rust-bitcoin/rust-bitcoin#1647: Fix absolute lock time examples and tests
c3cc9e52ab Fix absolute lock time examples and tests (Tobin C. Harding)

Pull request description:

  An absolute lock time of 100 is nonsensical because we are well past block 100. This value was used because it makes sense for _relative_ locktimes but for absolute lock times it makes the examples and tests slightly confusing.

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

Tree-SHA512: f490ef111bce0989c4ce8300c507c21b454448af4a91b9ef7a2fc05407411ca8721c9caa3dd1f0e8c0c133c4892c5c512f2d881af2cc67ae843d87eacae76ef1
2023-02-16 00:11:00 +00:00