Commit Graph

484 Commits

Author SHA1 Message Date
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
Andrew Poelstra b3221b0949
Merge rust-bitcoin/rust-bitcoin#1605: `psbt` cleanup
4a03e2e721 psbt: Remove unused error variant (Tobin C. Harding)

Pull request description:

  Remove an unused error variant for PSBT code (API breaking because the error type is public).

  Woops, somehow I managed to get what was patch 1 of this series merged yesterday, I thought I left it out. Anyways, this is just the remove unused error variant now. No changes to that patch from previous versions of the PR.

ACKs for top commit:
  apoelstra:
    ACK 4a03e2e721
  Kixunil:
    ACK 4a03e2e721

Tree-SHA512: 228c661b97c6656db5a2bcc9ceb494ea485363b7f7262a97c677ee1639b5209c92ec3715ff48fdb108c95c828bfc83b6c475aa66f0ce8c5b0f286bfa7cc19554
2023-02-15 18:08:23 +00:00
Tobin C. Harding c3cc9e52ab
Fix absolute lock time examples and tests
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.
2023-02-15 13:58:50 +11:00
Andrew Poelstra 7930a9ba5c
Merge rust-bitcoin/rust-bitcoin#1641: Move deny lint checks to script
a1c3082319 Move deny lint checks to script (Harshil Jani)

Pull request description:

  Closes #1551
  Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>

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

Tree-SHA512: 68632fcff175fa7f035ea10346fc36c1d593ba797e18955b01b8f38932e10327dbb36d9cbf95f65c29f68c5106e8b3364708b75ec3fd94c41cee6c994300eca7
2023-02-14 19:05:45 +00:00
Andrew Poelstra fefaedc755
Merge rust-bitcoin/rust-bitcoin#1643: pow: Remove Mul/Div by arbitrary integer types
dd316e4d14 pow: Remove Mul/Div by arbitrary integer types (Tobin C. Harding)

Pull request description:

  When we added `Target` and `Work` types we implemented multiplication and division by anything `Into<u64>`, this is not typically done in the Rust stdlib and also is semantically incorrect for the types.

  Remove `Mul` and `Div` impls from `Target` and `Work`. Also remove `Mul<T>` for `T: Into<u64>` from the private `U256` type.

  Fix #1632

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

Tree-SHA512: ede53555844adab321ff344535b7b8bab3c5c73855823dfc3ad728b077ae199451b7e22a1d203ef73a076073b7f0cbf9637cefa5fe82fc78ab454d02fa0b62b9
2023-02-14 18:50:16 +00:00
Harshil Jani a1c3082319 Move deny lint checks to script
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-02-14 10:48:24 +05:30
Tobin C. Harding dd316e4d14
pow: Remove Mul/Div by arbitrary integer types
When we added `Target` and `Work` types we implemented multiplication
and division by anything `Into<u64>`, this is not typically done in the
Rust stdlib and also is semantically incorrect for the types.

Remove `Mul` and `Div` impls from `Target` and `Work`. Also remove
`Mul<T>` for `T: Into<u64>` from the private `U256` type.
2023-02-14 11:50:46 +11:00
Tobin C. Harding a819cf99d6
Remove FromStr impl from ScriptBuf
`FromStr` impls should roundtrip with `Display` imlps but currently our
`ScriptBuf` displays using instructions but parses hex.
2023-02-14 10:37:56 +11:00
Andrew Poelstra 5fed454949
Merge rust-bitcoin/rust-bitcoin#1644: Flatten the types directory
272cdbcf7c Flatten the types directory (Tobin C. Harding)

Pull request description:

  We recently created a `types` subdirectory under `script` to keep all the `Script` and `ScriptBuf` impls together. Turns out this additional level of subdirectory is a bit annoying and we can achieve the same grouping by just using `script/mod.rs`.

  Move code from `types/mod.rs` to `script/mod.rs`, move the two submodules up a level, remove the `types` directory.

  Fix: #1640

ACKs for top commit:
  Kixunil:
    ACK 272cdbcf7c
  apoelstra:
    ACK 272cdbcf7c

Tree-SHA512: 91fd78084829fa24f3b6420602d7d5094670647fff43e6e193d6de3126f1657132873ea133540d87db7d0d4dfc4cb9666489e39c861377085ce0254da81fd564
2023-02-13 23:20:09 +00:00
Andrew Poelstra f00b6362a0
Merge rust-bitcoin/rust-bitcoin#1636: Weight prediction
ae2aaaa436 Add `script_pubkey_lens` method (Martin Habovstiak)
cf068d16b0 Implement transaction weight prediction (Martin Habovstiak)

Pull request description:

  When creating a transaction one must know the the fee beforehand to set
  appropriate amounts for outputs and to know the fee, weight is required.
  So far we only had a method on an already-constructed transaction. This
  method clearly wasn't helpful when constructing the transaction except
  for hacks like temporarily adding an all-zeroes signature.

  This change adds a function that can compute the transaction weight
  without knowing individual bytes of the scripts, witnesses and other
  elements. It only needs to know their sizes.

  To make the API less error-prone a special, trivial, type is also added
  for computing the lengths of witnesses.

  Based on #1627

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

Tree-SHA512: 55376601c2c2826bb0909cc25ff5b65816f0b1a2d57fb2cd8831f3db5382de0f4a364d518b312f0528bb5f44c30f3f74f8d254145eed2bfd65e2332b7c4d7c8b
2023-02-13 22:40:42 +00:00
Andrew Poelstra 1451370660
Merge rust-bitcoin/rust-bitcoin#1642: Add `minimal_non_dust` to `TxOut`
6be89bf94f Add `minimal_non_dust` to `TxOut` (Martin Habovstiak)

Pull request description:

  In some scenarios it's useful to create outputs with minimal relayable value. E.g. outputs designated for fee bumping using CPFP. A method for this is useful.

  This implements a constructor of `TxOut` that computes the minimal non-dust value from the passed script.

  Closes #1459

  This one is quite easy, so if we could get it in 0.30, that'd be great.

ACKs for top commit:
  apoelstra:
    ACK 6be89bf94f
  tcharding:
    ACK 6be89bf94f

Tree-SHA512: f31ae5f649fbba95ccaabf465cb814df193e7ef89c6e0de7b316a2a484e172beada0da8851da96b195a69a4da1b0991741d4c119f9b0c94fff34150e4f033bd5
2023-02-13 20:53:04 +00:00
Tobin C. Harding 272cdbcf7c
Flatten the types directory
We recently created a `types` subdirectory under `script` to keep all
the `Script` and `ScriptBuf` impls together. Turns out this additional
level of subdirectory is a bit annoying and we can achieve the same
grouping by just using `script/mod.rs`.

Move code from `types/mod.rs` to `script/mod.rs`, move the two
submodules up a level, remove the `types` directory.
2023-02-14 06:48:10 +11:00
Martin Habovstiak ae2aaaa436 Add `script_pubkey_lens` method
In some cases people construct the transaction with a dummy fee output
value before calculating the weight. A method to create the iterator
over `script_pubkey` lengths is useful in such cases.
2023-02-13 11:16:31 +01:00
Andrew Poelstra f71da4e91a
Merge rust-bitcoin/rust-bitcoin#1579: Fix license on bip158 module
097e4e9c7f Fix license on bip158 module (Tobin C. Harding)

Pull request description:

  When we introduced the SPDX license blurb in [0] we incorrectly gave attribution to Andrew when the original file author had the attribution as "the rust-bitcoin developers". The original author [1] was Tamas Blummer and he copied this code from code he wrote and explicitly re-licenses it. In order to make the re-licensing comment a little clearer and fix the mis-attribution use Tamas' name in the attribution.

  [0] commit: `91ff2f628ce7db732d234a812e29fa8508f501a1 Introduce SPDX license identifiers`
  [1] commit: `c93a70487f81a93c7d479ae046c75590d9fb7733 Add client side block filter (BIP158) (#281)`

ACKs for top commit:
  apoelstra:
    ACK 097e4e9c7f
  Kixunil:
    ACK 097e4e9c7f

Tree-SHA512: cb80d32c739ad562b2d657a34355bb28b1dd5c477b03018fbfbb14de40e03b806663aee89b578bcd8c681b067aa8d02611d4cde36e6fb9a8fa84ad4baf2e290e
2023-02-12 21:33:47 +00:00
Andrew Poelstra f3ceed91b3
Merge rust-bitcoin/rust-bitcoin#1581: Use ignore to stop rustdoc code from being built
6d99d3c061 Use ignore to stop rustdoc code from being built (Tobin C. Harding)

Pull request description:

  Currently we have an attempted tag ```compile_fail that seems to be aiming at allowing code that does not build to exist in rustdoc. This is causing an error when running tests.

  No clue how this made it through CI.

  Use ```ignore to prevent rustdoc code from being built.

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

Tree-SHA512: 6c4b076000ba29377ac8cf942df66e849ff6421da6f9214664d487550cf45889e163b4de652079010bae327019163b63a1962ff8e6a04d918db63ffb0285ccd1
2023-02-12 21:21:34 +00:00
Andrew Poelstra c4e78661fd
Merge rust-bitcoin/rust-bitcoin#1592: Add `From<Address>` for `ScriptBuf`
5f86b3091c Add From<Address> for ScriptBuf (Tobin C. Harding)

Pull request description:

  Add an implementation of `From<Address> for ScriptBuf` that calls through to `address.script_pubkey` (which calls
  `address.payload.script_pubkey()`).

  Fix: #1457

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

Tree-SHA512: 8a45f292578765b345863946b276607d561b9bc75f6b9bb97f48b32d503143e234aedb658997db802c87289576361ec9ee6cb31fe3bbccfc06cc2fdabc7c41bb
2023-02-12 20:48:00 +00:00
Martin Habovstiak 6be89bf94f Add `minimal_non_dust` to `TxOut`
In some scenarios it's useful to create outputs with minimal relayable
value. E.g. outputs designated for fee bumping using CPFP. A method for
this is useful.

This implements a constructor of `TxOut` that computes the minimal
non-dust value from the passed script.

Closes #1459
2023-02-11 17:14:48 +01:00
Martin Habovstiak cf068d16b0 Implement transaction weight prediction
When creating a transaction one must know the the fee beforehand to set
appropriate amounts for outputs and to know the fee, weight is required.
So far we only had a method on an already-constructed transaction. This
method clearly wasn't helpful when constructing the transaction except
for hacks like temporarily adding an all-zeroes signature.

This change adds a function that can compute the transaction weight
without knowing individual bytes of the scripts, witnesses and other
elements. It only needs to know their sizes.

To make the API less error-prone a special, trivial, type is also added
for computing the lengths of witnesses.
2023-02-11 12:57:37 +01:00
Andrew Poelstra 9615dd12b8
Merge rust-bitcoin/rust-bitcoin#1505: Improve test coverage for docs build
41f2dcf6ae Improve test coverage for docs build (Tobin C. Harding)
b4c14a4b7c hashes: Use automatic link (Tobin C. Harding)
96e8a080d1 ci: Remove redundant || exit (Tobin C. Harding)

Pull request description:

  Currently the docs build commands in `hashes` and `bitcoin` differ, they should be the same.

  Add a command `cargo doc` to improve coverage e.g., recently we botched the feature guarding but since CI only runs `cargo rustdoc` with custom compiler conditional set we didn't catch it.

  Done after seeing: https://github.com/rust-bitcoin/rust-bitcoin/pull/1504 and CI should fail on this PR until 1504 is in.

ACKs for top commit:
  apoelstra:
    ACK 41f2dcf6ae
  Kixunil:
    ACK 41f2dcf6ae

Tree-SHA512: 7cde68292cfc6f32b75d066e188e7c418ee251f9a5abc57fbd642ba33e9cd5bd8ef7c5ba7cffd206acae6ddec2f8c3db38c8c911a4319e979158666b8225953d
2023-02-10 23:20:57 +00:00
Andrew Poelstra 4fdbf076bf
Merge rust-bitcoin/rust-bitcoin#1559: Deprecate `script::read_uint`
a7117bf8f1 Document source of logic fro read_scriptint (Tobin C. Harding)
2eb2420b40 Add comment on rountripping read/write scripint (Tobin C. Harding)
657dd51e8b Use OP_0 to better mimic bitcoin core code (Tobin C. Harding)
31d254a6a8 Fix push operators URL (Tobin C. Harding)
84cd4ca964 Deprecate script::read_uint (Tobin C. Harding)

Pull request description:

  Patch one does the deprecation, the rest of the PR is made up of tiny improvements to the code around reading/writing 'scriptint's (conceptually `CScriptNum`s). I did all this while trying to decipher the discussion on #1547.

  ### Note Please

  There are many more changes in the pipeline for all this read/write "script int" stuff. This PR was done ages ago and I believe it stall adds value.

  I re-did the whole PR manually because of the recent `script` module changes. I hope no one else has to do that - if you do please feel free to holla and I'll "rebase" your PR for you.

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

Tree-SHA512: 5e8ee7fa8d1393a1a50e4241dd947b837cc0ddd15ff1239a49e4839489459fb95d184d6773f73633d55c436310bfab0c73f806d492ed4a4215f924c6c0993936
2023-02-10 20:02:12 +00:00
Andrew Poelstra 1514205e5f
Merge rust-bitcoin/rust-bitcoin#1629: Add methods for pushing locktimes
1e0e712bb0 Add push_* methods for lock times (Tobin C. Harding)

Pull request description:

  Lock times are `u32` and can require encoding using 5 bytes.

  Add methods `push_lock_time` and `push_sequence` for pushing absolute lock times and sequence numbers. We do not push relative locktimes because they are only 16 bits from the original sequence number.

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

Tree-SHA512: 4b511679270e7ef73937259ccf7d1b9b4b7512b2464f302310519a6e02d55c9cc24e3559302aeb671156e68130478258c1c565f474880e8be708b0ee234e67ff
2023-02-10 19:21:51 +00:00
Andrew Poelstra c8e256718b
Merge rust-bitcoin/rust-bitcoin#1612: Clean up the manifest files of top level crates
8596e402f2 Fix docs.rs to use all features (Tobin C. Harding)
89086d094d hashes: Use angle braces to make hyperlinks (Tobin C. Harding)
9b5c2ad7af hashes: Clean up optional dependencies (Tobin C. Harding)
5b4f19c01f hashes: Improve std/alloc features (Tobin C. Harding)
132d2f90b6 bitcoin: Enable alloc feature in features list (Tobin C. Harding)
aa62ca224a hashes: Do not enable core2/alloc feature (Tobin C. Harding)
c15f8dee29 Improve manifest package section (Tobin C. Harding)
12f5e37ed9 Add excludes to manifests (Tobin C. Harding)

Pull request description:

  Do a complete overhaul of the manifest of the top level crates (i.e., not `embedded`, `fuzzing` ect.).

  Many of the problems being fixed here were introduced over the last year by my poor understanding of exactly what was going on with _every_ line of code in the manifests, after this PR I hope that is no longer a problem.

  I'm closing #1571 because it is now done more fully at the end of this PR.

  During review please be liberal with any questions so we can ensure everything is spot on now - as we add more crates there are going to be a proliferation of manifest files, best get it right now.

ACKs for top commit:
  apoelstra:
    ACK 8596e402f2
  Kixunil:
    ACK 8596e402f2

Tree-SHA512: 9e4ae21221cd5b185c04b6d1af73983d324e6d4e09abf94763f6eabcff420d9a3b2ec62a88cd20844fda518756862b0e64a85be54187166f3b1b2b206b82cb08
2023-02-10 17:38:35 +00:00
Andrew Poelstra 6340c8e1bb
Merge rust-bitcoin/rust-bitcoin#1388: Refactor `merkle_tree::block`
861fdd6ab1 Put the `MerkleBlock` struct at the top of the file (Tobin C. Harding)
f0d968197a Put error at the bottom of the file (Tobin C. Harding)
19e094788f Use self for Error variant imports (Tobin C. Harding)
83c2a552db Put helper function below where its called (Tobin C. Harding)
5076579fb9 Fix indentation in pmt_tests macro (Tobin C. Harding)
a7edbfb52e Move hex data to tests/data (Tobin C. Harding)

Pull request description:

  PR 2 in the `merkle_tree::block` series, used to be on top of the now merged #1374.

  Do a bunch of refactorings in preparation for more invasive changes. This is a separate PR because, other than the first patch which moves hex strings to `tests/data/` the other patches are refactoring only patches, no logic changes. However the last patch is big and will be annoying to review - sorry about that. If you really oppose this basically stylistic patch putting important things first, the opposite of C code, please say and I'll try to stop doing it.

ACKs for top commit:
  Kixunil:
    ACK 861fdd6ab1
  apoelstra:
    ACK 861fdd6ab1

Tree-SHA512: 3da0a600898f490b602ab05a396061587d86ffef55697877885a8c611eff96e7382a2d816fe9594c100d378dc56fe7fdc88009a0343bc602b7f4c180836adbd3
2023-02-10 14:30:28 +00:00
Tobin C. Harding 090dad770f
Improve string parsing
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`.
2023-02-10 12:28:25 +11:00
Tobin C. Harding 1e0e712bb0
Add push_* methods for lock times
Lock times are u32 and can necessitate encoding using 5 bytes. As such
they are "special".

Add methods `push_lock_time` and `push_sequence` for pushing absolute
lock times and sequence numbers. We do not push relative locktimes
because they are only 16 bits from the original sequence number.
2023-02-10 12:23:15 +11:00
Tobin C. Harding a7117bf8f1
Document source of logic fro read_scriptint
Our `script::read_scriptint` function is based on the constructor
code (incl. call to `set_vch`) code from Bitcoin Core. Add rustdoc
comment saying so, emit a link because there are already multiple links
to `script.h` in this file (one just right below the added comment).
2023-02-10 12:02:32 +11:00
Tobin C. Harding 2eb2420b40
Add comment on rountripping read/write scripint
We only support reads of upto 4 bytes where as Bitcoin Core allows
reading a `CScriptNum` with more bytes than that. Add a rustdoc
comment (incl. link to Bitcoin Core) mentioning that.
2023-02-10 12:02:00 +11:00
Tobin C. Harding 657dd51e8b
Use OP_0 to better mimic bitcoin core code
Our `Builder::push_int` method is the same as Bitcoin Core `CScript`
`push_int64` method. We currently use `OP_FALSE` (equivalent to `OP_0`)
but recently we added `OP_0`, lets use it to make our code better mimic
Core (also saves devs checking that `OP_FALSE` is the same as `OP_0`).
2023-02-10 12:01:23 +11:00
Tobin C. Harding 31d254a6a8
Fix push operators URL
The URL is wrong (section `#Push_operators` should be
`#push-operators`), also should use angle brackets not back ticks.
2023-02-10 11:59:37 +11:00
Tobin C. Harding 84cd4ca964
Deprecate script::read_uint
There is no current usage for reading an unsigned script integer, seems
like this is kruft from days gone past.
2023-02-10 11:58:25 +11:00
Tobin C. Harding 861fdd6ab1
Put the `MerkleBlock` struct at the top of the file
The `MerkleBlock` struct is the main type in this file, put it at the
top of the file. This leaves the next most important type,
`PartialMerkleTree` below that.

Refactor only, no logic changes.
2023-02-10 11:53:05 +11:00
Tobin C. Harding f0d968197a
Put error at the bottom of the file
The `Error` is not interesting code, put it at the bottom of the file.

Refactor only, no logic changes.
2023-02-10 11:53:05 +11:00
Tobin C. Harding 19e094788f
Use self for Error variant imports
Use `self` to import the error variants to make it more explicit.
2023-02-10 11:53:05 +11:00
Tobin C. Harding 83c2a552db
Put helper function below where its called
Reading the code is arguably easier if we have seen the call site before
seeing the function, saves having to think what the function does.
2023-02-10 11:53:04 +11:00
Tobin C. Harding 5076579fb9
Fix indentation in pmt_tests macro
The macro has unusual indentation, fix to to be more regular.
2023-02-10 11:53:03 +11:00
Tobin C. Harding a7edbfb52e
Move hex data to tests/data
We have a test directory for holding test data, use it to for the hex
used for testing the `MerkleBlock`.
2023-02-10 11:53:02 +11:00
Andrew Poelstra b6387db47f
Merge rust-bitcoin/rust-bitcoin#1627: Add `Weight` and `FeeRate` newtypes
70cf4515db Add `Weight` and `FeeRate` newtypes (Martin Habovstiak)

Pull request description:

  Use of general-purpose integers is often error-prone and annoying. We're working towards improving it by introducing newtypes.

  This adds newtypes for weight and fee rate to make fee computation easier and more readable. Note however that this dosn't change the type for individual parts of the transaction since computing the total weight is not as simple as summing them up and we want to avoid such confusion.

  Part of #630
  Replaces #1607 (I want to get this in quickly and don't want to be blocked on DanGould's availability.)

ACKs for top commit:
  apoelstra:
    ACK 70cf4515db
  tcharding:
    ACK 70cf4515db

Tree-SHA512: ab9cc9f554a52ab0109ff23565b3e2cb2d3f609b557457b4afd8763e3e1b418aecbb3d22733e33304e858ecf900904a1af6e6fdc16dc21483b9ef84f56f103b2
2023-02-10 00:33:56 +00:00
Andrew Poelstra 2290e90b71
Merge rust-bitcoin/rust-bitcoin#1600: Refactor script module
a9108d3939 Refactor script module (Tobin C. Harding)

Pull request description:

  The `script` module is large and unwieldy.

  Refactor the `script` module, splitting it up into a tree of modules. Here are a few of the changes and their stated benefits

  - Split the two script types out into separate files: Readers of the methods can then tell immediately from the file name which type they are reading.
  - Put all the impls for the two script types together: Makes parsing the API easier because one can more quickly see which traits are implemented on what i.e., all the `AsRef` imlps are grouped together.
  - Put the impls for the two script types in order, first `Script` then `ScriptBuf`: Makes it easier for us to see if we missed something.
  - Put the `Builder` and `Instruction` (and associated) types in their own modules: Some devs find long files hard to navigate, so far there hasn't been too much push back against short files.
  - Put tests in a separate file: This idea was recently discussed.

  This is only moving code and fixing import statements etc. No other changes to the code.

  ## Note to reviewers

  This PR is impossible to review from the diff because it moves so much code. Perhaps better to look at the resulting `src/blockdata/script/` directory and see if you like it.

  #### Motivation

  While adding script tagging I was having difficulty navigating the script module.

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

Tree-SHA512: 19123c8cfbdce6c42b322fa75a74073a0114b0ed21bd06ca5727981b3573b74cf05075723b774b92ae2b497e20644fca6e2fac14e30cc44f2802dde5aa567f66
2023-02-09 23:26:58 +00:00
Tobin C. Harding 8596e402f2
Fix docs.rs to use all features
Many moon ago we were unable to build with `--all-features`, this is no
longer the case.

Instruct docs.rs to build the docs with `--all-features`.
2023-02-10 08:04:11 +11:00
Martin Habovstiak 70cf4515db Add `Weight` and `FeeRate` newtypes
Use of general-purpose integers is often error-prone and annoying. We're
working towards improving it by introducing newtypes.

This adds newtypes for weight and fee rate to make fee computation
easier and more readable. Note however that this dosn't change the type
for individual parts of the transaction since computing the total weight
is not as simple as summing them up and we want to avoid such confusion.

Part of #630
2023-02-08 23:17:30 +01:00
Tobin C. Harding 132d2f90b6
bitcoin: Enable alloc feature in features list
In `bitcoin` when we use the `core2` dependency we always need the
"alloc" feature. Enabling "alloc" when enabling "core2" in the "no-std"
feature is confusing because it makes it seem that we don't always need
it.

Set usage of the "alloc" feature of `core2` in the `features` list.
2023-02-08 08:24:50 +11:00
Tobin C. Harding aa62ca224a
hashes: Do not enable core2/alloc feature
`core2` is for Read/Write, nothing to do with allocation and we do not
use the "alloc" feature of `core2` in `hashes`.

Fix core2 dependency/features by doing:

- Explicitly enable "bitcoin_hashes/core2" in `bitcoin`.
- Do not enable "core2/alloc" in `hashes`
2023-02-08 08:24:49 +11:00
Tobin C. Harding c15f8dee29
Improve manifest package section
Improve all manifest package sections by doing:

- Order the list of options uniformly
- Remove unnecessary homepage option (currently same as repo)
- Add categories section
2023-02-08 08:24:49 +11:00
Tobin C. Harding 12f5e37ed9
Add excludes to manifests
We can check which files are included in the packaged release with
 `cargo package --list `.

Add an `exclude` section to each manifest that excludes `tests/` and
`contrib/`. Not all crates have a `tests/` directory yet but they should
so add the exclude anyway to future proof the crates.
2023-02-08 08:24:49 +11:00
Andrew Poelstra cc3b5a080b
Merge rust-bitcoin/rust-bitcoin#1621: Rename `from_slice` methods to `decode`
32d2d62e0f Rename from_slice methods to decode (Tobin C. Harding)

Pull request description:

  The `TaprootMerkleBranch` and `ControlBlock` both have methods on them called `from_slice` but these methods do more that just basic copy from a slice. `decode` is a more descriptive name.

  Deprecate the `from_slice` methods and implement `decode`, on other changes to the logic.

  cc sanket1729

ACKs for top commit:
  apoelstra:
    ACK 32d2d62e0f
  Kixunil:
    ACK 32d2d62e0f

Tree-SHA512: e8c089545411a214ef9393f65d3990be46983000bd045182cc27dd70b62273bf48ac97adaf89d1e7fc807c72964a01eef176c7685684e8f87a01c219746d6d3d
2023-02-07 16:49:55 +00:00
Andrew Poelstra a58460bc3c
Merge rust-bitcoin/rust-bitcoin#1611: Instruct devs to use nightly for embedded
55d17f2c18 Instruct devs to use nightly for embedded (Tobin C. Harding)

Pull request description:

  The embedded test crate requires usage of the nightly toolchain, fix the docs to show this.

ACKs for top commit:
  apoelstra:
    ACK 55d17f2c18
  Kixunil:
    ACK 55d17f2c18

Tree-SHA512: 5d3d611ff4331d8475f77d260ebaa3da3ae71960b709eb4603056d7034885306b8bcc39be488f3391697d47a1074a695979fb50b6cb31af414f873da8e82bdbd
2023-02-07 15:24:38 +00:00
Andrew Poelstra 6db85c1d7e
Merge rust-bitcoin/rust-bitcoin#1601: Add '_ back into the BitStreamWriter
86f372774b Add '_ back into the BitStreamWriter (Tobin C. Harding)

Pull request description:

  Recently we merged `commit 53d4fe66b57c255086def2b5e47afaddee776b75` to fix CI even though a better approach is to use `'_` because it assists reading the code (shows that the bit stream writer is not writing from a reference since its writing a `Copy` type `n`).

  Add back in the `'_` (I forget what its called).

ACKs for top commit:
  apoelstra:
    ACK 86f372774b
  Kixunil:
    ACK 86f372774b

Tree-SHA512: 2a9989164562dbe7bf133e3aeb090fbff7831bfeefb0ac8431e75b17d57184c4d60ac206578c6ebbcff903a3832502a162027ed9f37e5ed87e42a6bf61efa594
2023-02-07 14:34:41 +00:00
Tobin C. Harding a9108d3939
Refactor script module
The `script` module is large and unwieldy.

Refactor the `script` module, splitting it up into a tree of modules.
Here are a few of the changes and their stated benefits

- Split the two script types out into separate files: Readers of the
methods can then tell immediately from the file name which type they are
reading.
- Put all the impls for the two script types together: Makes parsing the
API easier because one can more quickly see which traits are implemented
on what i.e., all the `AsRef` imlps are grouped together.
- Put the impls for the two script types in order, first `Script` then
`ScriptBuf`: Makes it easier for us to see if we missed something.
- Put the `Builder` and `Instruction` (and associated) types in their
own modules: Some devs find long files hard to navigate, so far there
hasn't been too much push back against short files.
- Put tests in a separate file: This idea was recently discussed.

This is only moving code and fixing import statements etc. No other
changes to the code.
2023-02-07 16:07:54 +11:00
Tobin C. Harding 32d2d62e0f
Rename from_slice methods to decode
The `TaprootMerkleBranch` and `ControlBlock` both have methods on them
called `from_slice` but these methods do more that just basic copy from
a slice. `decode` is a more descriptive name.

Deprecate the `from_slice` methods and implement `decode`, on other
changes to the logic.
2023-02-07 14:54:08 +11:00
Andrew Poelstra 611772ca0e
Merge rust-bitcoin/rust-bitcoin#1624: Improve `sighash` module documentation
75b266a129 Improve `sighash` module documentation (Martin Habovstiak)

Pull request description:

  "Sighash" is a technical term that newbies in Bitcoin may not know and could get lost when trying to find how to sign a transaction. This change attempts to make it more obvious that this module is needed for signing.

  Closes #1463

ACKs for top commit:
  tcharding:
    ACK 75b266a129
  apoelstra:
    ACK 75b266a129

Tree-SHA512: 7157566c1639c63ce0fba2832e8e5e846e689d89e24077ed7769b721c5db4613cd7fd8d91464992eb78de74b42912ca877e7182a9c3c9c8848bf94d89767b8cc
2023-02-07 01:21:51 +00:00
Andrew Poelstra 0aba8e56f6
Merge rust-bitcoin/rust-bitcoin#1625: Improve `SighashCache` API
b3188bbac3 Add `Transaction` accessors to `SighashCache` (Martin Habovstiak)
7c6854fe02 Use `Borrow` instead of `Deref` in `SighashCache` (Martin Habovstiak)

Pull request description:

  This changes the bound from `Deref<Target = Transaction>` to `Borrow<Transaction>` (with respective `mut` changes) and adds accessors.

  Closes #1423 (PSBT stuff will be separate issue).

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

Tree-SHA512: 9db2c5890b26e9eefd483d697b42e84b1d7d3b8676fc39b4f39075c149e12697aa538828a757f9187578a958d72a592bb913f8f5788c93feb273db5370979d99
2023-02-07 01:02:09 +00:00
Andrew Poelstra 44ec22f396
Merge rust-bitcoin/rust-bitcoin#1613: Implement from arrays for `TaprootMerkleBranch`
118a593c89 Implement from arrays for TaprootMerkleBranch (Tobin C. Harding)

Pull request description:

  The `TaprootMerkleBranch` contains a vector of `TapNodeHash`s, as such it can trivially be constructed from an array of the same type.

  Implement `From` for all array sizes 1 - 128 inclusive.

  Fix: #1469

ACKs for top commit:
  Kixunil:
    ACK 118a593c89
  apoelstra:
    ACK 118a593c89

Tree-SHA512: dd497abd9143ea8b43485133beaccac9049fb915a95a3422d41c1f99961b59ec95df93efe759aa02f62ba1cf3e1afc4597671f1202ff0fa78eeee8b305d21305
2023-02-07 00:41:43 +00:00
Tobin C. Harding 118a593c89
Implement from arrays for TaprootMerkleBranch
The `TaprootMerkleBranch` contains a vector of `TapNodeHash`s, as such
it can trivially be constructed from an array of the same type.

Implement `From` for all array sizes 1 - 128 inclusive.
2023-02-07 08:10:59 +11:00
Martin Habovstiak 75b266a129 Improve `sighash` module documentation
"Sighash" is a technical term that newbies in Bitcoin may not know and
could get lost when trying to find how to sign a transaction. This
change attempts to make it more obvious that this module is needed for
signing.

Closes #1463
2023-02-06 22:07:21 +01:00
Tobin C. Harding 69688b6487
Merge rust-bitcoin/rust-bitcoin#1610: Add KeyPair import to rustdoc example
277e8e96bd Add KeyPair import to rustdoc example (Tobin C. Harding)

Pull request description:

  Recently, and bizarrely, a PR merged that broke `cargo test --doc`.

  Add an import for `KeyPair` to the `schnorr` rustdoc example.

ACKs for top commit:
  apoelstra:
    ACK 277e8e96bd
  Kixunil:
    ACK 277e8e96bd

Tree-SHA512: ad214b668827b35848cc7b260cbd2104a916a82a5a6d242bdc498c62edc9a0e864f4bdb4abcade42924dbaf951223ae80feacbe68d8a4ccb4562d8ead50b23a9
2023-02-07 08:05:35 +11:00
Andrew Poelstra 12298ddc14
Merge rust-bitcoin/rust-bitcoin#1608: Set `rustv_1_53` in build script
bb612fdafa Set rustv_1_53 in build script (Tobin C. Harding)

Pull request description:

  The rust version is supposed to be set by the build script so that users automagically get features matching the toolchain in use. Currently we have a feature in the manifest for `rustv_1_53` instead setting a compiler conditional configuration option in the build script. This causes `cargo +1.41.1 --all-features check` to fail.

  ## Note

  I don't see `rustv_1_46` used anywhere, do we need that still?

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

Tree-SHA512: f74195d4ee5a5bc5f209e99d30789df3552cef10aee5ea8b61a5a701b753999c34d04be9fe0321ccee7a8ec14fa5a05e0b454b9dc5f8deddd7b5b8d4f3d7e744
2023-02-06 14:11:01 +00:00
Martin Habovstiak b3188bbac3 Add `Transaction` accessors to `SighashCache`
It may be useful to access the transaction stored in `SighashCache`
during signing or afterwards, especially when the transaction is stored
without indirection (to enable long-lived storage).

This change adds the appropriate accessors.
2023-02-06 12:14:30 +01:00
Martin Habovstiak 7c6854fe02 Use `Borrow` instead of `Deref` in `SighashCache`
The requirement for a type dereferencing to `Transaction` prevented
storing the cache in long-lived data without resorting to silly
wrappers. Since `Borrow` is implemented both for `T` and for smart
pointers it's a more flexible bound which this change implements.

While this is technically breaking, all usual non-generic code will
continue to work beause smart pointers generally have `Borrow`
implemented.
2023-02-06 12:14:25 +01:00
Tobin C. Harding aae03999a5
Do not export unusual hash types at crate root
Currently we use a wildcard to export all the hash types in
`hash_types`. We are moving to a world were we only export
normal/standard types from the crate root.

Remove the reexport of the following hash types:

- `FilterHash`
- `FilterHeader`
- `TxMerkleNode`
- `WitnessCommitment`
- `WitnessMerkleNode`
- `XpubIdentifier`
- `Sighash`

Fix: #1541
2023-02-04 13:34:20 +11:00
Andrew Poelstra 732dd038ff
Merge rust-bitcoin/rust-bitcoin#1618: Add newline in rustdoc
f0e4e38844 Add newline in rustdoc (Tobin C. Harding)

Pull request description:

  Docs created with the `sha256t_hash_newtype` macro are missing a newline between the doc heading and doc main section.

  Note that the strings used span multiple lines and therefor the subsequent lines must be aligned with the start of the line (not indented).

  Fix: #1540

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

Tree-SHA512: 240c68864da63688c400498903d5cc345bee224dcd3235df0127dcf391c66ee08c487d31fe59f890009c674574810b689d9a53628d07d8cdd46b79bc0ac3eb2b
2023-02-03 20:08:02 +00:00
Tobin C. Harding f0e4e38844
Add newline in rustdoc
Docs created with the `sha256t_hash_newtype` macro are missing a newline
between the doc heading and doc main section.

Note that the strings used span multiple lines and therefor the
subsequent lines must be aligned with the start of the line (not
indented).

Fix: #1540
2023-02-03 14:11:53 +11:00
Tobin C. Harding 55d17f2c18
Instruct devs to use nightly for embedded
The embedded test crate requires usage of the nightly toolchain, fix the
docs to show this.
2023-02-03 08:44:47 +11:00
DanGould 0ffd928a7d
Carry ConsensusEncoding(encode::Error)
When psbt::Error was Eq, it could not have associated dyn error types.
2023-02-02 13:05:59 -05:00
DanGould 126cbb00ef
Associate io::Error with psbt::Error
In order to associate the error, psbt::Error must not derive so many
traits. Tests are also adjusted for the new error type.
2023-02-02 13:02:33 -05:00
Tobin C. Harding 277e8e96bd
Add KeyPair import to rustdoc example
Recently, and bizarrely, a PR merged that broke `cargo test --doc`.

Add an import for `KeyPair` to the `schnorr` rustdoc example.
2023-02-02 12:22:47 +11:00
Tobin C. Harding bb612fdafa
Set rustv_1_53 in build script
The rust version is supposed to be set by the build script so that users
automagically get features matching the toolchain in use. Currently we
have a feature in the manifest for `rustv_1_53` instead setting a
compiler conditional configuration option in the build script. This
causes `cargo +1.41.1 --all-features check` to fail.
2023-02-02 10:12:16 +11:00
Tobin C. Harding 4a03e2e721
psbt: Remove unused error variant
This error variant is never used, remove it.
2023-02-02 08:10:55 +11:00
Andrew Poelstra f52301151c
Merge rust-bitcoin/rust-bitcoin#1565: Reduce usage of `FromHex`
32ca6cc320 Remove hex_from_slice and display Sighash forwards (Tobin C. Harding)
a308e1e2ea Remove FromHex for all types except Vec and array (Tobin C. Harding)
3e70c01826 Manually format a bunch of vecs (Tobin C. Harding)
83e1c40c4d Remove script:: prefix from unambiguous types (Tobin C. Harding)
5ab5c264d2 Use fully qualified path in macro (Tobin C. Harding)
7e85452cd9 hashes: Implement std::error::Error (Tobin C. Harding)
5e3abc5e11 Fix feature gating on unit tests (Tobin C. Harding)
3344cf6be2 Favour $reverse instead of $reversed (Tobin C. Harding)

Pull request description:

  This work started out, as the branch name suggests, as an effort to use the `hex_lit` crate. But once I got to this stage it seems that the `hex!` macro we have provides different, useful, functionality than the `hex_lit::hex!` macro (it allows usage with non-consts). So I'm unsure if we want to remove it now.

  - Patches 1 - 6 are preparatory clean ups
  - Patch 7 reduces usage of  `FromHex`, please see git log for full description
  - Patch 8 removes `hex_from_slice` and fixes a bug in how we display `Sighash`

ACKs for top commit:
  apoelstra:
    ACK 32ca6cc320
  Kixunil:
    ACK 32ca6cc320

Tree-SHA512: 11b45b39ec2fc0f837d7395b5fb86de7cc44641fd51cf7e93394a635e6a8fb1c7ac441a6070d5516dae60e084c04cc6e8b605a5167093f964679e445ef60c271
2023-02-01 18:09:56 +00:00
Andrew Poelstra ef53871754
Merge rust-bitcoin/rust-bitcoin#1583: Add conversions for `TweakedKeyPair` -> `TweakedPublicKey`
facaefc49c Add conversions for TweakedKeyPair -> TweakedPublicKey (Tobin C. Harding)
2407f241e4 Remove sep256k1 path from Parity (Tobin C. Harding)

Pull request description:

  It is trivially possible to get `TweakedPublicKey` from a `TweakedKeyPair`, add conversion methods for doing so.

  Patch 1 is preparatory cleanup. Please note `From` is not implemented because the conversion returns the `Parity` also.

  Fix: #1452

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

Tree-SHA512: 597026c481fe2622a625cbeb381cac345af6f49f4a115418b69817345fc3c2140bbdbc5208eae1149d7d171f94c776365d302ffe1f9c01d944e738807db28a89
2023-02-01 17:58:33 +00:00
Tobin C. Harding 32ca6cc320
Remove hex_from_slice and display Sighash forwards
`Sighash` should be displayed forwards according to BIP143. Currently we
are displaying it backwards (as we do for double SHA256). This is
working because parse using `Vec::from_hex`.

We have the means to parse hex strings directly for hashes, we no longer
need `hex_from_slice`.

BIP143 test vectors display double SHA256 forwards but we display
backwards, this is acceptable because there is no fixed display in the
ecosystem for double SHA256 hashes. In order to overcome this we parse
test vector hex strings with into `Vec` when needed.
2023-02-01 08:26:46 +11:00
Tobin C. Harding a308e1e2ea
Remove FromHex for all types except Vec and array
Remove `FromHex` from hash and script types

- Remove the `FromHex` implementation from hash types and `ScriptBuf`
- Remove the `FromStr` implementation from `ScriptBuf` because it does not
  roundtrip with `Display`.
- Implement a method `from_hex` on `ScriptBuf`.
- Implement `FromStr` on hash types using a fixed size array.

This leaves `FromHex` implementations only on `Vec` and fixed size arrays.
2023-02-01 08:26:46 +11:00
Tobin C. Harding 3e70c01826
Manually format a bunch of vecs
In preparation for modifying some unit test data structures, manually
format the code so it is uniform.

Move elements added to a vec with `vec!` onto a new line so they all
line up and one can better see what fields go where.

Refactor only, no logic changes.
2023-02-01 08:22:47 +11:00
Tobin C. Harding 83e1c40c4d
Remove script:: prefix from unambiguous types
There is no need to qualify `Script` and `ScriptBuf` with the `script::`
prefix, remove it.
2023-02-01 08:22:46 +11:00
Andrew Poelstra ca902e65f8
Merge rust-bitcoin/rust-bitcoin#1596: Implement fmt traits for ScriptBuf
ed6f6d11dd Implement fmt traits for ScriptBuf (Tobin C. Harding)

Pull request description:

  We can improve ergonomics of the `script` module by implementing the `fmt` traits on `ScriptBuf`, trivial because we can call through to the `Script` implementations.

  Fix: #1585

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

Tree-SHA512: 878a1522af4ed1e10d1d8d60d150e6571008c008b5e5c662c67462f9e09075b4f1fe4e399ed50e98cd7253b6815937c6732cd1ce02b74a5be017d5b8fcdbbd2f
2023-01-31 14:40:50 +00:00
Tobin C. Harding ed6f6d11dd Implement fmt traits for ScriptBuf
We can improve ergonomics of the `script` module by implementing the
`fmt` traits on `ScriptBuf`, trivial because we can call through to the
`Script` implementations.
2023-01-31 17:48:01 +11:00
Tobin C. Harding facaefc49c Add conversions for TweakedKeyPair -> TweakedPublicKey
It is trivially possible to get `TweakedPublicKey` from a
`TweakedKeyPair`, add conversion methods for doing so.
2023-01-31 17:21:56 +11:00
Tobin C. Harding 2407f241e4 Remove sep256k1 path from Parity
The `Parity` type is unambiguous, no need to use the `secp256k1` path
all the time just import the type.

Refactor only, no logic changes.
2023-01-31 17:21:56 +11:00
Tobin C. Harding 7dde3b3b22 Make max/min_value functions const
The `max_value` and `min_value` functions only exist to be
compatible/uniform with Rust 1.41.1 they will never change and they just
return a constant value. They can therefore be made const functions.
2023-01-31 08:35:32 +11:00
Tobin C. Harding 86f372774b Add '_ back into the BitStreamWriter
Recently we merged `commit 53d4fe66b57c255086def2b5e47afaddee776b75` to
fix CI even though a better approach is to use `'_` because it assists
reading the code (shows that the bit stream writer is not writing from a
reference since its writing a `Copy` type `n`).

Add back in the `'_` (I forget what its called).
2023-01-31 08:07:05 +11:00
Andrew Poelstra a43de831e4
Merge rust-bitcoin/rust-bitcoin#1584: Add new hex parse error variant
877f9af364 Add new hex parse error variant (Tobin C. Harding)

Pull request description:

  Recently we used an error type that holds only one expected hex string length when parsing but for `PublicKey`s we have two (66 and 130). Add a new error variant to express the error. Requires adding a variant to `bip32` for the same thing.

  Fix: #1281

ACKs for top commit:
  Kixunil:
    ACK 877f9af364
  apoelstra:
    ACK 877f9af364

Tree-SHA512: c1ca493ee30418bd82bc326b35c18731260e4217c371f37301a73c64f9a6631163801acc217c6c2c7b14f632a2ad5043174266c1b4fdce127698e68ab8494f20
2023-01-30 20:55:08 +00:00
Andrew Poelstra 4ad0c63774
Merge rust-bitcoin/rust-bitcoin#1574: Add standard constants to lock times
3c0598b399 Add standard constants to lock times (Tobin C. Harding)

Pull request description:

  Some of the lock time structs (`Height`, `Time` ect.) are missing standard constants for min, max ect.

  Add standard constants taking into consideration the various locktime corner cases.

  Add `max_value` and `min_value` to be consistent with Rust 1.41.1 (incl. `Sequence`).

  Fix: #1451

  This PR is not complex in itself but **locktimes are notoriously complex, please wait for 3 acks before merging** - and ack'ing makes no guarantee that reviewer got all corner cases :)

  There is no rush on this one, apoelstra, Kixunil, sanket1729 please just review when your brain is fresh.

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

Tree-SHA512: aa3d112db83b4785edb0a7a517cc335ded59f5967eb39b8979a6d68f9bba4644a27e5ca400fcabf368a1f8c0eecdef0b87b1011933ac7fd96b467b8501533203
2023-01-30 16:36:07 +00:00
Tobin C. Harding 877f9af364 Add new hex parse error variant
Recently we used an error type that holds only one expected hex string
length when parsing but for `PublicKey`s we have two (66 and 130). Add a
new error variant to express the error. Requires adding a variant to
`bip32` for the same thing.

Fix: #1281
2023-01-28 07:53:47 +11:00
Tobin C. Harding 53d4fe66b5 Remove unnecessary lifetime
Clippy emits:

  warning: the following explicit lifetimes could be elided: 'a

As suggested, remove the unnecessary lifetime.
2023-01-27 08:34:12 +11:00
Andrew Poelstra f90338021b
Merge rust-bitcoin/rust-bitcoin#1591: Improve downflow of information in `psbt::Error`
68abfdb0b9 Better downflow of information in `psbt::Error` (Arturo Marquez)

Pull request description:

  See third point in https://github.com/rust-bitcoin/rust-bitcoin/issues/837

  Closes: https://github.com/rust-bitcoin/rust-bitcoin/issues/1589

ACKs for top commit:
  apoelstra:
    ACK 68abfdb0b9
  tcharding:
    ACK 68abfdb0b9

Tree-SHA512: 8accfd6a1ae9c413b48a5a5861ec036a166a5cd5a73ee242169ee9d34a91f85f4f152e2feba628e20b8b89d7177694c3d2af60551a37c2d9c9a4408036f55262
2023-01-26 13:35:45 +00:00
Tobin C. Harding a121e19e94
hashes: Implement AsRef for fixed size arrays
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.
2023-01-26 11:00:57 +11:00
Tobin C. Harding 5f86b3091c
Add From<Address> for ScriptBuf
Add an implementation of `From<Address> for ScriptBuf` that calls
through to `address.script_pubkey` (which calls
`address.payload.script_pubkey()`).

Fix: #1457
2023-01-26 10:28:40 +11:00
Arturo Marquez 68abfdb0b9
Better downflow of information in `psbt::Error`
See third point in `https://github.com/rust-bitcoin/rust-bitcoin/issues/837`
2023-01-24 15:49:23 -06:00
Tobin C. Harding 3c0598b399
Add standard constants to lock times
Some of the lock time structs (`Height`, `Time` ect.) are missing
standard constants for min, max ect.

Add standard constants taking into consideration the various locktime
corner cases.

Add `max_value` and `min_value` to be consistent with Rust 1.41.1 (incl.
`Sequence`).

Fix: #1451
2023-01-25 07:43:48 +11:00
Andrew Poelstra ac65c338ab
Merge rust-bitcoin/rust-bitcoin#1575: Add documentation to `Sequence::is_final`
a762a89b48 Add documentation to Sequence::is_final (Tobin C. Harding)
b1490a26ea Move enables_absolute_lock_time method (Tobin C. Harding)

Pull request description:

  The term "final" is an archaic Bitcoin term however it is well used, it exists in Bitcoin Core code as well as in various bips. To help folks new to Bitcoin add documentation to the `is_final` method including historical notes.

  Note, this does _not_ deprecate `is_final` - while writing the notes I found the term "final" in enough official places that I think its fair game to keep the term, some things people just have to learn, we can definitely help with that learning though.

  Fix: #1198

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

Tree-SHA512: 895fbdce90223d90c0a68fb1e3d6b7aada4a3606d1294ea4df1f4194681a79d970b0434e7bb078f6d5cbf413b3550e72560d6d5cf811a5a959adf53f7f778ab2
2023-01-24 15:52:15 +00:00
Andrew Poelstra 96865e3b23
Merge rust-bitcoin/rust-bitcoin#1580: Delegate debug for ScriptBuf to Script
8c0e5213d3 Delegate debug for ScriptBuf to Script (Tobin C. Harding)

Pull request description:

  Currently the derived implementation of `Debug` for `ScriptBuf` prints the inner vector of u8s as integers, this is ugly and hard to read. The `Script` implementation of `Debug` prints the script opcodes and data as hex, we can just delegate to it.

  With this applied we get debug output of form:

      Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 3bde42dbee7e4dbe6a21b2d50ce2f0167faa8159 OP_EQUALVERIFY OP_CHECKSIG)

  Fix: #1516

ACKs for top commit:
  Kixunil:
    ACK 8c0e5213d3
  apoelstra:
    ACK 8c0e5213d3

Tree-SHA512: ca07d9fb191f4e0379cbd96b2944e6881094a8334d39b97209b6bf452a3c15d4aede53b9c88176b9b7667b7a539d47897940bc561dc9f8cd83ce1990a08047e1
2023-01-24 15:30:41 +00:00
Andrew Poelstra 07a5cccd42
Merge rust-bitcoin/rust-bitcoin#1582: Take `Into<secp256k1::PublicKey>` in PublicKey constructors
1d3d5a9c5b Take Into<secp256k1::PublicKey> in PublicKey constructors (Tobin C. Harding)
b13a76407b keys: Clean up test imports (Tobin C. Harding)

Pull request description:

  We can make the API more ergonomic by taking a generic argument that implements `Into<secp256k1::PublicKey>` in the `bitcoin::PublicKey` constructors.

  The only thing than this is useful for is passing in `KeyPair` and the `From` implementation already exists. Add a unit test to verify.

  Fix: #1453

  ## Note

  As per the discussion in #1453 I checked secp and bitcoin for all keys that can be converted using `From` and it turns out its only `KeyPair` which already has `From` impls - good rust-bitcoin devs :)

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

Tree-SHA512: b5e5272561de15cdcfb15913aa5d42ddc96bf2fd5835068a5a9aa0274074ffa698ec9e81707f102b7d1b244f1abd0fdbd0eb4b6b505c84c3d5719dcb01d46efb
2023-01-24 15:03:06 +00:00
Andrew Poelstra 1b8f52a804
Merge rust-bitcoin/rust-bitcoin#1567: Use `sha256d::Hash` type for sighash encoding
49e8b8da32 Use write_all for sighash encoding (Tobin C. Harding)

Pull request description:

  From BIP143:

  > If sighash type is SINGLE and the input index is smaller than the number of outputs, hashOutputs is the double SHA256 of the output amount with scriptPubKey of the same index as the input;

  Currently we are using a `Sighash` which wraps double sha256 so while technically correct this means we are relying on `Sighash` to implement `Encodable`. We can remove this requirement by directly using the `sha256d::Hash` type to hash the outputs data.

  Fix: #1549

ACKs for top commit:
  Kixunil:
    ACK 49e8b8da32
  apoelstra:
    ACK 49e8b8da32

Tree-SHA512: 8dd0037245a7cf180ba8a6eceeadad912d4adc14fc3f49df9008856de262624666d7d575195eea4868b2a5252dc565590e6be78471053b5e6367f3d2363310e8
2023-01-24 14:52:25 +00:00
Andrew Poelstra f6d983b2ef
Merge rust-bitcoin/rust-bitcoin#1532: Improve Psbt error handling
e7bbfd3913 Improve Psbt error handling (DanGould)

Pull request description:

  ## Separate `encode::Error` and `psbt::Error` recursive dependency

  This initial work attempts to fix #837's first 2 points

  > - The current psbt::serialize::Deserialize has an error type of consensus::encode::Error. I think we should cleanly separate consensus encoding errors from application-level encoding errors like psbt.
  > - There is a recursive dependence between encode::Error and psbt::Error which would need to be cleanly dissected and separated so that there is no dependence or only one-way dependence.

  ## Better `ParseError(String)` types

  arturomf94 how compatible do your #1310 changes look to address #837's third point with this design?

  > - There are a lot ParseError(String) messages that could use a better type to downflow the information.

  I think your prior art would completely address this issue now.

  ## On handling `io::Error` with an associated error

  `encode::Error` has an `Io` variant. now that `Psbt::deserialize` returns `psbt::Error` and produces an `io::Error`, we need an `Io` variant on `psbt::Error`. Except that doing so breaks  `#[derive(Eq)]` and lots of tests for `psbt::Error`.

  Kixunil, I'm trying to understand your feedback regarding a solution to this problem.

  > I believe that the best error untangling would be to make decodable error associated.

  > I meant having associated `Error` type at `Decodable` trait. Encoding should only fail if the writer fails so we should have `io::Error` there (at least until we have something like `genio`).
  >
  > > [it] is a problem to instantiate consensus::encode::Error in [the psbt] module for `io::Error`?
  >
  > It certainly does look strange. Maybe we should have this shared type:
  >
  > ```rust
  > /// Error used when reading or decoding fails.
  > pub enum ReadError<Io, Decode> {
  >     /// Reading failed
  >     Io(Io),
  >     /// Decoding failed
  >     Decode(Decode), // consensus and PSBT error here
  > }
  > ```
  >
  > However this one will be annoying to use with `?` :( We could have `ResultExt` to provide `decode()` and `io()` methods to make it easier.
  >
  > If that's not acceptable then I think deduplicated IO error is better.

  Kixunil didn't we just get rid of Psbt as `Decodable`? Would this make more sense to have as an error associated with `Deserialize`? Or did we do the opposite of what we should have by making Psbt only `Serialize`/`Deserialize` because of #934, where only consensus objects are allowed to be `Decodable`? I wonder if we prioritized that strict categorization and are stuck with worth machinery because of it. My goal with #988 was to get to a point where we could address #837 and ultimately implement PSBTv2.

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

Tree-SHA512: 32975594fde42727ea9030f46570a1403ae1a108570ab115519ebeddc28938f141e2134b04d6b29ce94817ed776c13815dea5647c463e4a13b47ba55f4e7858a
2023-01-24 14:18:25 +00:00
Andrew Poelstra d8d34116ad
Merge rust-bitcoin/rust-bitcoin#1564: Address validity invariant cleanups
44d3ec487d Rename Payload::as_bytes to inner_prog_as_bytes (sanket1729)
a446df583c Make Payload non-exhaustive (sanket1729)
6ebc9de252 Introduce WitnessProgram struct and cleanup Address validity invariants (sanket1729)
41652caf05 Introduce is_spend_standard method (sanket1729)

Pull request description:

  Fixes #1561.

  Highlights:

  - Segwitv0 programs with lengths apart from 20 or 32 are invalid `Address` struct. Such Addresses are useless and we should not parse/create them.
  - Renamed `is_standard` to `is_spend_standard`.

ACKs for top commit:
  apoelstra:
    ACK 44d3ec487d
  tcharding:
    ACK 44d3ec487d

Tree-SHA512: 1ee36f7ea25c65619ddf7d643d025690096876843dbe6fbdf877ce23e88049d79b0bbd78cee6cf4b415bca028b3634bb70c9f52d1098bd90558e6ba7f8731332
2023-01-24 13:25:49 +00:00
Andrew Poelstra c4b8d452da
Merge rust-bitcoin/rust-bitcoin#1544: amount parses 0 msats
0cf6da4f03 amount parses 0 msats (bnabi)

Pull request description:

  Closes #1529

  0 msats should be parasable.

ACKs for top commit:
  Kixunil:
    ACK 0cf6da4f03
  tcharding:
    ACK 0cf6da4f03
  apoelstra:
    ACK 0cf6da4f03

Tree-SHA512: aeb15ed46f21b3e75bbd80094a103900ad0f42995abdd32476db771c623aea6e3c557a4180f1d9a92d0ad62e703b9ad838d9be1b43d61624dd6e6bce31a8b81b
2023-01-23 22:12:24 +00:00
sanket1729 44d3ec487d Rename Payload::as_bytes to inner_prog_as_bytes
Also make it private
2023-01-23 13:45:46 -08:00
sanket1729 a446df583c Make Payload non-exhaustive
It is unlikely that we will see another segwit type fork. But we never
know, best to mark Payload as [non-exhaustive]
2023-01-23 13:45:46 -08:00
sanket1729 6ebc9de252 Introduce WitnessProgram struct and cleanup Address validity invariants
Addresses with Segwitv0 not having len 20/32 are invalid and cannot be
constructed. Also cleans up a API bug in
ScriptBuf::new_witness_prog(ver, prog) allowing prog of invalid lenghts.
2023-01-23 13:45:46 -08:00
Andrew Poelstra 4b02d90ce4
Merge rust-bitcoin/rust-bitcoin#1578: Implement `Debug` for generic `Address<V: NetworkValidation>`
ebfbe74243 Implement `Debug` for generic `Address<V: NetworkValidation>` (Jiri Jakes)

Pull request description:

  Previously `Debug` was implemented for both `Address<NetworkChecked>` and `Address<NetworkUnchecked>`, but not for cases when the `NetworkValidation` parameter was generic. This change adds this ability. Based on Kixunil's tip.

  With previous implementation, the `test_address_debug()` resulted in error:

  ![image](https://user-images.githubusercontent.com/1381856/213907042-f1b27f41-fa46-4fa0-b816-cc4df53f5d29.png)

  The added `Debug` on `NetworkChecked` and `NetworkUnchecked` are required by compiler.

  ---

  While dealing with derives and impls, I also attempted to turn all the derives on `Address` into manual impls (see Kixunil's suggestion in https://github.com/rust-bitcoin/rust-bitcoin/pull/1489#discussion_r1052448057). The motivation behind this was the possibility to remove derives on `NetworkChecked` and `NetworkUnchecked`, too. However, even with manual impls, all the traits on `NetworkChecked` and `NetworkUnchecked` were still required by compiler in this sort of situations (see also the rest of the same discussion linked above). I do not fully understand why, perhaps limitation of this way of sealing traits?

  It can be demonstrated by removing `Debug` derivation on `NetworkUnchecked` and `NetworkChecked` in this PR and running `test_address_debug()`.

  Therefore, if we want to allow users of the library to define types generic in `NetworkValidation` and at the same time derive impls, it seems to me that `NetworkChecked` and `NetworkUnchecked` will have to have the same set of impls as `Address` itself.

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

Tree-SHA512: 87f3fa4539602f31bf4513a29543b04e943c3899d8ece36d0d905c3b5a2d76e29eb86242694b5c494faa5e54bb8f69f5048849916c6438ddd35030368f710353
2023-01-23 14:49:22 +00:00
Tobin C. Harding 1d3d5a9c5b
Take Into<secp256k1::PublicKey> in PublicKey constructors
We can make the API more ergonomic by taking a generic argument that
implements `Into<secp256k1::PublicKey>` in the `bitcoin::PublicKey`
constructors.

The only thing than this is useful for is passing in `KeyPair` and the
`From` implementation already exists. Add a unit test to verify.

Fix: #1453
2023-01-23 14:46:53 +11:00
Tobin C. Harding b13a76407b
keys: Clean up test imports
Clean up the test import statements in the `keys` module. Done in
preparation for adding a unit test.
2023-01-23 14:46:53 +11:00
Tobin C. Harding 6d99d3c061
Use ignore to stop rustdoc code from being built
Currently we have an attempted tag ```compile_fail that seems to be
aiming at allowing code that does not build to exist in rustdoc. This is
causing an error when running tests.

No clue how this made it through CI.

Use ```ignore to prevent rustdoc code from being built.
2023-01-23 13:26:15 +11:00
Tobin C. Harding 8c0e5213d3
Delegate debug for ScriptBuf to Script
Currently the derived implementation of `Debug` for `ScriptBuf` prints
the inner vector of u8s as integers, this is ugly and hard to read. The
`Script` implementation of `Debug` prints the script opcodes and data as
hex, we can just delegate to it.

With this applied we get debug output of form:

    Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 \
    3bde42dbee7e4dbe6a21b2d50ce2f0167faa8159 OP_EQUALVERIFY OP_CHECKSIG)

Fix: #1516
2023-01-23 12:49:11 +11:00
Tobin C. Harding 097e4e9c7f
Fix license on bip158 module
When we introduced the SPDX license blurb in [0] we incorrectly gave
attribution to Andrew when the original file author had the attribution
as "the rust-bitcoin developers". The original author [1] was Tamas
Blummer and he copied this code from code he wrote and explicitly
re-licenses it. In order to make the re-licensing comment a little
clearer and fix the mis-attribution use Tamas' name in the attribution.

[0] commit: `91ff2f628ce7db732d234a812e29fa8508f501a1 Introduce SPDX license identifiers`
[1] commit: `c93a70487f81a93c7d479ae046c75590d9fb7733 Add client side block filter (BIP158) (#281)`
2023-01-23 10:47:50 +11:00
Tobin C. Harding a762a89b48
Add documentation to Sequence::is_final
The term "final" is an archaic Bitcoin term however it is well used, it
exists in Bitcoin Core code as well as in various bips. To help folks
new to Bitcoin add documentation to the `is_final` method including
historical notes.
2023-01-23 09:18:23 +11:00
Tobin C. Harding 6e56feed57
bip158: Replace usage of HashSet with BTreeSet
The `bip158` module uses a `HashSet` and in order to do so requires the
`hashbrown` dependency for "no-std" builds.

We can replace the usage of `HashSet` with a `BTreeSet` in `bip158` and
remove the `hashbrown` dependency entirely.

This patch makes no claims about performance cost or benefit of this
change. The patch also makes no claims about the validity of the current
`HashSet` usage.

The `hashbrown` dependency and `HashSet` usage can be trivially added
back in if someone comes up with perf data to back it up.
2023-01-23 08:38:08 +11:00
Jiri Jakes ebfbe74243 Implement `Debug` for generic `Address<V: NetworkValidation>`
Previously `Debug` was implemented for both `Address<NetworkChecked>`
and `Address<NetworkUnchecked>`, but not for cases when the
`NetworkValidation` parameter was generic. This change adds this
ability.
2023-01-22 16:23:10 +08:00
bnabi 0cf6da4f03 amount parses 0 msats 2023-01-22 12:08:25 +05:30
Tobin C. Harding b1490a26ea
Move enables_absolute_lock_time method
In preparation for deprecating the `is_final` method; move the
`enables_absolute_lock_time` method to be directly above the `is_final`
method.

Refactor only, no logic changes.
2023-01-22 17:04:58 +11:00
Tobin C. Harding 49e8b8da32
Use write_all for sighash encoding
`Sighash` does not need to implement `Encodable` because it is
claimed (I don't know exactly myself) that `Sighash` is never consensus
encode in Bitcoin.

We are currently relying on `Sighash` to implement `Encodable` when
encoding creating the segwit v0 sighash for a single input.

For reference, from BIP143:

 If sighash type is SINGLE and the input index is smaller than the
 number of outputs, hashOutputs is the double SHA256 of the output
 amount with scriptPubKey of the same index as the input;

We can use `write_all` directly to write the hashed bytes and remove the
implementation of `Encodable` from the `Sighash` type.

While we are at it, use `write_all` to write the zero hash also to make
the code more uniform and understandable.

Fix: #1549
2023-01-21 10:56:59 +11:00
sanket1729 41652caf05 Introduce is_spend_standard method 2023-01-20 07:31:21 -08:00
DanGould e7bbfd3913
Improve Psbt error handling
Remove recursive dependence between encode::Error and psbt::Error.
Separate consensus encoding errors from Psbt application errors.
2023-01-17 16:43:39 -05:00
Andrew Poelstra 649bf023af
Merge rust-bitcoin/rust-bitcoin#1537: Export the DisplayHex trait from within prelude
70fe07f1ce Export the DisplayHex trait from within prelude (Tobin C. Harding)

Pull request description:

  We use `internals::hex::display::DisplayHex` in many places, we can improve ergonomics of the `internals` crate by re-exporting it from the `prelude` module.

ACKs for top commit:
  Kixunil:
    ACK 70fe07f1ce
  apoelstra:
    ACK 70fe07f1ce

Tree-SHA512: 96a89135cb0b829b7b5926a3b344f78e178b5b48e772a69da5133fab6d2e14e7b7bbaa56b7a417a5c1a64337546a1c7bac32307d3a1f27aa199ed61f590902bf
2023-01-16 15:21:33 +00:00
Tobin C. Harding 41f2dcf6ae
Improve test coverage for docs build
Currently the docs build commands in `hashes` and `bitcoin` differ, they
should be the same.

Add a command `cargo doc` to improve coverage e.g., recently we botched
the feature guarding but since CI only runs `cargo rustdoc` with custom
compiler conditional set we didn't catch it.

Run docs in CI using nightly and stable toolchains as required.
2023-01-16 13:56:56 +11:00
Tobin C. Harding 96e8a080d1
ci: Remove redundant || exit
We already use `set -ex`, adding an explicit exit on command fail is
redundant.
2023-01-16 13:56:26 +11:00
Andrew Poelstra 006fe3c223
Merge rust-bitcoin/rust-bitcoin#1539: Add a rustdoc test to Denomination
a7dd4b5ab0 Add a rustdoc test to Denomination (Tobin C. Harding)

Pull request description:

  Add a rustdoc test to the `Denomination` type to show basic usage.

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

Tree-SHA512: 08f15c4641e70f043276b873e60fcf0e195fe50b6c5c18a245d2d609f4a4a4badc291aae1be532fc4890a91b2057cd308c86d0d3b85770b600a07499303a7bc4
2023-01-15 23:00:51 +00:00
Tobin C. Harding 308c727c82
pow: Add more mutation testing
Recently we introduced some mutation testing to the `pow` module but
testing is never done - add more `mutate` attributes and add unit tests
to ensure all mutants are killed.

Of note, the `from_compact` and `to_compact_lossy` functions are not
done, doing so results in a bunch of surviving mutants.
2023-01-13 08:23:14 +11:00
Tobin C. Harding 948b04927d
Remove unnecessary parenthesis
Plain integer does not require parenthesis, remove them.
2023-01-13 08:22:45 +11:00
Tobin C. Harding c3021d852a
Fix typo in code comment
Fix a trivial grammatical mistake in code comment.
2023-01-13 08:21:34 +11:00
Tobin C. Harding a7dd4b5ab0
Add a rustdoc test to Denomination
Add a rustdoc test to the `Denomination` type to show basic usage.
2023-01-13 07:19:30 +11:00
Jiri Jakes bef7c6e687 Use marker type to enforce validation of `Address`'s network
Parsing addresses from strings required a subsequent validation of
network of the parsed address. However, this validation was not
enforced by compiler, one had to remember to perform it.

This change adds a marker type to `Address` that will assist the
compiler in enforcing this validation.
2023-01-11 19:27:10 +08:00
sanket1729 ac6340943c
Merge rust-bitcoin/rust-bitcoin#1538: A crash course in rust perf (#988 nits)
1a409ecc8e Use &[u8] instead of Cursor as Read (DanGould)
81ca10701a Use as_ref() instead of costly clone() (DanGould)
debcce6a03 Improve magic bytes push performance (DanGould)

Pull request description:

  A follow up to address [nits in  #988](https://github.com/rust-bitcoin/rust-bitcoin/pull/988#pullrequestreview-1233687052)

ACKs for top commit:
  tcharding:
    ACK 1a409ecc8e
  elichai:
    ACK 1a409ecc8e
  sanket1729:
    utACK 1a409ecc8e

Tree-SHA512: bc6c538d8f92b0efa8063ba33bb47f0c9dc2f99eb5db267b66aa01fc70fd634695a0cd9c27fb0f33e3498628729a8e0f622d9876dc1c83fab4a1e46484d425b2
2023-01-11 02:38:33 -08:00
DanGould 1a409ecc8e
Use &[u8] instead of Cursor as Read 2023-01-09 23:21:20 -05:00
DanGould 81ca10701a
Use as_ref() instead of costly clone() 2023-01-09 23:17:04 -05:00
DanGould debcce6a03
Improve magic bytes push performance 2023-01-09 23:10:07 -05:00
Tobin C. Harding 70fe07f1ce
Export the DisplayHex trait from within prelude
We use `internals::hex::display::DisplayHex` in many places, we can
improve ergonomics of the `internals` crate by re-exporting it from the
`prelude` module.
2023-01-10 09:56:41 +11:00
sanket1729 f39cd88f5f Use TapNodeHash in NodeInfo
This cleans up some ugly code where we had to use sha256::Hash for
combining TapLeafHash and TapBranchHash
2023-01-09 13:01:44 -08:00
sanket1729 5ff2635585 Rename TapBranchHash -> TapNodeHash 2023-01-09 12:42:46 -08:00
Andrew Poelstra 1f7affbc95
Merge rust-bitcoin/rust-bitcoin#1531: Remove `ToHex`
1b0988833a Remove `ToHex` (Martin Habovstiak)

Pull request description:

  The `ToHex` trait was replaced by either simple `Display`/`LowerHex` where appropriate or `DisplayHex` from `bitcoin_internals` which is faster.

  This change replaces the usages and removes the trait.

ACKs for top commit:
  tcharding:
    ACK 1b0988833a
  apoelstra:
    ACK 1b0988833a

Tree-SHA512: a1b508e24ac247a0692c01b7cb1e7fa8f23fbfa3d6c3d5dfe669eec01f940a96c3f88508cb0b2e3529eebd9cb51e7d41435dbd5f4cbaf3bc14b9c7e7d790308b
2023-01-09 12:50:37 +00:00
sanket1729 cc1e6c02c8
Merge rust-bitcoin/rust-bitcoin#1523: Fix LeafVersion serde
a400757676 Add failing tests from serde-json (sanket1729)
b3246bf73f Fix LeafVersion serde (sanket1729)

Pull request description:

  The default implementation maps to visit_u64. The current implementation
  does not roundtrip with many deserializers, including serde_json. See
  the failing test in the second commit

ACKs for top commit:
  Kixunil:
    tACK a400757676
  tcharding:
    ACK a400757676

Tree-SHA512: a6307b799b0bb4af398addc9ddbff0d811b632d1cc6ab4bdd77aaf3f151e48dd1cd10e3f90a71ef8fc3feb1cd988f7a719f92ec745b5fdf2ae0f0ad97ab2fa10
2023-01-09 01:40:43 -08:00
sanket1729 a400757676 Add failing tests from serde-json 2023-01-08 06:47:40 -08:00
Martin Habovstiak 1b0988833a Remove `ToHex`
The `ToHex` trait was replaced by either simple `Display`/`LowerHex`
where appropriate or `DisplayHex` from `bitcoin_internals` which is
faster.

This change replaces the usages and removes the trait.
2023-01-07 19:50:03 +01:00
Andrew Poelstra 5f2beb8ae2
Merge rust-bitcoin/rust-bitcoin#1527: Rename `push_scriptint` and make it private
94b678e73f Rename `push_scriptint` and make it private (Martin Habovstiak)

Pull request description:

  `push_scriptint` is a significant footgun with an unclear name. This renames it and unpublishes to avoid mistakes by downstream crates.

  Closes #1517

ACKs for top commit:
  apoelstra:
    ACK 94b678e73f

Tree-SHA512: 9e1772c6fb326d8b0c78d702ad9926a79a91589feb8650aed7c5e75bfbdbf0164357b4d5b190877c40b8469e0e3be3d3453fe407741b5dae0c5758176f756417
2023-01-06 14:57:00 +00:00
Andrew Poelstra d71c428949
Merge rust-bitcoin/rust-bitcoin#1528: [WIP] 1514: fix formatting in opcodes
77799bf5cb 1514: fix formatting in opcodes, unify definition and display (bnabi)

Pull request description:

  See  #1514.

ACKs for top commit:
  Kixunil:
    ACK 77799bf5cb
  apoelstra:
    ACK 77799bf5cb

Tree-SHA512: beab4e5ff0f2c995cfb120e5aa04fdeda740ce5136e60da390cf0156c372e3807263a8cdf3eea2762a0fb68b222fed56efdc5ad686b9f481fabe3fccf227fed3
2023-01-06 13:53:21 +00:00
bnabi 77799bf5cb 1514: fix formatting in opcodes, unify definition and display 2023-01-05 14:28:20 +05:30
Martin Habovstiak 94b678e73f Rename `push_scriptint` and make it private
`push_scriptint` is a significant footgun with an unclear name. This
renames it and unpublishes to avoid mistakes by downstream crates.

Closes #1517
2023-01-04 22:58:27 +01:00
Casey Rodarmor 16c49df688 Accept amounts with denominations with and without spaces 2023-01-04 11:07:03 -08:00
Andrew Poelstra deaf21dd84
Merge rust-bitcoin/rust-bitcoin#988: Replace consensus `Encodable`/`Decodable` Psbt Serialization
c4363e5ba1 Deserialize Psbt fields, don't consensus_encode (DanGould)
c1dd6ad8a2 Serialize Psbt fields, don't consensus_encode them (DanGould)
1b7b08aa5d De/serialize Psbt without consensus traits (DanGould)

Pull request description:

  fix https://github.com/rust-bitcoin/rust-bitcoin/issues/934

  Instead of using consensus {de,en}code, serialize high-level structures (PartiallySignedTransaciton, Output, Input) borrow the signature from `Serialize`, `Deserialize` traits and implement them on Psbt:

  ```rs
  impl Psbt {
      /// Serialize a value as raw data.
      fn serialize(&self) -> Vec<u8>;

      /// Deserialize a value from raw data.
      fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error>;
  }
  ```

ACKs for top commit:
  apoelstra:
    ACK c4363e5ba1
  tcharding:
    ACK c4363e5ba1
  sanket1729:
    ACK c4363e5ba1. One small comment, but can be addressed in follow up if there is nothing else from other reviewers.
  Kixunil:
    ACK c4363e5ba1

Tree-SHA512: d8dd5ef1189b36fde08969b1ec36006a6fc55ae990f62ea744e6669e03a7f9e90e1d5907be7eac48ee1af23bc20a62aa7e60ff1ff78080d0e923bb9ccedcd432
2023-01-04 18:32:59 +00:00
sanket1729 b3246bf73f Fix LeafVersion serde
The default implementation maps to visit_u64. The current implementation
does not roundtrip with many deserializers, including serde_json. See
failing test in the subsequent commit
2023-01-01 11:42:29 -08:00
sanket1729 98203bc8b3
Merge rust-bitcoin/rust-bitcoin#1438: Replace `Vec::from_hex` with `hex!`
089a1e452d Replace `Vec::from_hex` with `hex!` (Martin Habovstiak)

Pull request description:

  This makes the code less noisy and is a preparation for changing it to `const`-based literal. Because of the preparation, places that used variables to store the hex string were changed to constants.

  There are still some instances of `Vec::from_hex` left - where they won't be changeable to `const` and where `hex!` is unavailable (integration tests). These may be dealt with later.

  See also #1189

  Note that while the change appears big it's nearly entirely mechanical, so should be pretty easy to review. (But I don't feel it's `trivial`.)

ACKs for top commit:
  apoelstra:
    ACK 089a1e452d
  tcharding:
    ACK 089a1e452d

Tree-SHA512: c55357b8cffc86f8e107c0f8390490ede75948260018f63dc926455700cbcaf422f6ae72444f93943065e6f4ffa4335bee9160a64184ea4f8e91721f30a46ace
2023-01-01 02:24:01 -08:00
Martin Habovstiak 089a1e452d Replace `Vec::from_hex` with `hex!`
This makes the code less noisy and is a preparation for changing it to
`const`-based literal. Because of the preparation, places that used
variables to store the hex string were changed to constants.

There are still some instances of `Vec::from_hex` left - where they
won't be changeable to `const` and where `hex!` is unavailable
(integration tests). These may be dealt with later.

See also #1189
2022-12-31 21:10:19 +01:00
Martin Habovstiak 8d58ee2ca3 Change `#[cfg(docsrs)]` to `#[cfg(doc)]` on `use`
The additional `use` items were added to improve the style of
documentation. Because they were only used for doc they were `cfg`ed.
But because this is independent from being built by `docs.rs` the `cfg`
should've been `doc` not `docsrs`.

IOW `docsrs` means roughly `all(doc, nightly)` and the added items are
unrelated to `nightly`.
2022-12-31 20:58:17 +01:00
Andrew Poelstra 3b11ff6607
Merge rust-bitcoin/rust-bitcoin#1387: Add rand-std feature
941083ec4e Remove rand-std dev-dependency from secp256k1 (Tobin C. Harding)
f71335f971 Add rand-std feature (Tobin C. Harding)

Pull request description:

  This PR uncovered incorrect feature gating in `secp256k1`, fixed in https://github.com/rust-bitcoin/rust-secp256k1/pull/519

  Currently we enable "secp256k1/rand-std" in the "rand" feature, this is incorrect because it means "rand" implies "std" which it does not.

  Add a "rand-std" feature that turns on "seck256k1/rand-std" and make the "rand" feature turn on "seck256k1/rand".

  Fix: #1384

ACKs for top commit:
  sanket1729:
    ACK 941083ec4e
  apoelstra:
    ACK 941083ec4e

Tree-SHA512: e1d12196766c2b3082b488fe7d0c03a865ebbfc70ffa6f128c57074df14da71e56c31ea92982991d376ac62fa86c6639049ce17aac93613b523ddcc99677c269
2022-12-30 23:53:54 +00:00
Andrew Poelstra 3a867ee8ce
Merge rust-bitcoin/rust-bitcoin#1503: Fix wrong newtype APIs
64f7d2549e Fix wrong newtype APIs (Martin Habovstiak)

Pull request description:

  This fixes several API bugs:

  * Use `TryFrom` instead of `From` for fallible conversions
  * Move byte conversion methods from `impl_array_newtype` to `impl_bytes_newtype`
  * Add missing trait impls like `AsRef`, `Borrow`, their mutable versions and infallible conversions from arrays

  Closes #1336

  Notably this change is much less bad than even I expected and some places actually get cleaner than before.

ACKs for top commit:
  tcharding:
    ACK 64f7d2549e
  sanket1729:
    code review ACK 64f7d2549e
  apoelstra:
    ACK 64f7d2549e

Tree-SHA512: 0c5ab19aa1a01ccf34ad1c2f3f66bd003a3142a840cb7cf57e1449e721f6eed1523555c62f0e3391012708767ff86315ff7edce9afd85017a67c61cfe57dddbc
2022-12-30 23:10:39 +00:00