This new method will check the last witness element to see if it starts with 0x50, and
depending on the result it will return the second to last or third to last witness
element according to BIP341.
In its current state, Witness can not know what type of script it is fulfilling,
so it is up to the caller to verify if the previous output is a taproot output or not.
Arbitrary indexing into Witness fixes the API of last and second_to_last to be more flexible.
This patch started off as an addition of third_to_last, but ended up evolving
into arbitrary indexing to allow for future use cases.
A list of the indices of the start byte for each witness element is stored as an ordered
contiguous group of u32s represented as 4 bytes each in the Vec<u8> contents.
The bytes are stored using to_ne_bytes for performance reasons. A helper function is added
to the tests to allow for easier contruction of the contents Vec in test vectors. u32 was
chosen because 22 bits are needed to store 4,000,000 which is the maximum weight limit for
a block. This might need to be reworked in the event of consensus limits increasing, but
u32 can hold 1000x the current limit, so it should be fine for the forseeable future.
The push and consensus_deserialize functions utilize rotate_left and rotate_right to move
the indices to the end of the new allocation. Depending on the size of the data, this
might be more of a performance hit than just allocating a new temporary Vec to store the
indices and append them after parsing is completed. However, for a majority of cases
rotating the indices should be faster. Suggestions to use VecDeque instead of Vec for
contents necessitate other considerations, since it is not a public facing change,
those optimizations can be dealt with in future patches.
The Index<usize> trait is implemented using the new nth method with expect.
The Iter struct is reworked to make use of the new data representation. This new data
structure makes it trivial to implement DoubleEndedIterator and other such traits, but
I have decided to leave this as out of scope for this patch.
This transaction broke past versions of `rust-bitcoin` and LND so this
adds a test to avoid reintroducing the problem in the future.
See also https://github.com/romanz/electrs/issues/783
In order that we can safely change/maintain de/serialization code we
need to have regression tests with hard coded serializations for each
type that implements serde.
It is enough to test a single serde data format, use JSON for `opcodes`
and bincode for other types.
Do regression testing in a newly added `tests` module.
The `base58` module is for encoding and decoding, it makes sense for the
public functions to be called `encode` and `decode`. We also have some
functions that operate on data with a checksum, for these it makes sense
to tack `check` onto the _end_ of the function name.
With this applied the public API is:
- decode
- decode_check
- encode
- encode_check
- encode_check_to_fmt
Code is arguably easier to read if the most important stuff comes first.
In the old days, when writing C, we had to put definitions before they
were used but in Rust this is not the case
Re-order the `base58` file so that the public API functions are up the top
then other helper functions are defined _after_ they are called.
Refactor only, no logic changes.
Currently we are manually adding `0x` in calls to `write!`, this is
unnecessary since the alternate form already adds the `0x`.
Was verified with
```
#[test]
fn bad_checksum_error_hex_format() {
let want = "invalid base58 character 0xab";
let got = format!("{}", Error::BadByte(0xAB));
assert_eq!(got, want)
}
```
Use alternate form to print hex.
The key related errors are incorrect because they are circular, we have
a base58 error variant in `key::Error` and two key error variants in
`base58::Error`.
Remove the key errors from the `base58::Error` type.
The function call `calculate_root_inline` calculates the merkle root
using the input array as a scratch buffer, i.e., we trash the data
during recursive calls to `merkle_root_r`.
Add explicit documentation to the function so its super clear not to use
the hashes again after calling this function.
Recently we renamed the `hash` module to `merkle_root`, this makes the
public functions provided stutter if used with one layer of path as is
Rust convention:
`merkle_root::bitcoin_merkle_root`
We can improve on this by renaming the functions to 'calculate', then we
get
- `merkle_root::calculate()`
- `merkle_root::calculate_inline()`
The `util::hash` module provides two functions for computing a merkle
root from a list/iterator of hashes.
Rename the module to `merkle_root` and move it to the crate root,
deprecate the original functions.
Done as part of flattening the `util` module.
d2ed0fe022 Add `impl IntoIterator for &'_ Witness` (Martin Habovstiak)
Pull request description:
It is considered idiomatic for types that have `iter()` method to also implement `IntoIterator` for their references. `Witness` was missing this so it is added here.
ACKs for top commit:
apoelstra:
ACK d2ed0fe022
tcharding:
ACK d2ed0fe022
Tree-SHA512: fc891109696de4f349324d6ddc160249ef22510622d9ce72a65b18f085d86b0de0f3ecb4f7060e1eaf716a908029865cd21cda5a6598fc4c16d0540152d9a4c9
2674327c93 Remove the endian module (Tobin C. Harding)
Pull request description:
Now we have MSRV of 1.41.1 we can use the `from_le_bytes` and `to_be_bytes` methods implemented on standard integer types, these became available in Rust 1.32.
Remove the `endian` module replacing its logic with calls to methods on the respective stdlib integer types.
ACKs for top commit:
Kixunil:
ACK 2674327c93
apoelstra:
ACK 2674327c93
Tree-SHA512: 7cdaf278c9d162cb0080bb6b9ea80ab55f881bfcd389f8b968f8cfaeebb0d27d3b5b46e9677a376bc6b7d4068cf094f50560ed4ae7bc817c50da688f70a7af25
It is considered idiomatic for types that have `iter()` method to also
implement `IntoIterator` for their references. `Witness` was missing
this so it is added here.
b8bd31d5a8 Promote rust-miniscript finalizer (DanGould)
16bf6f68dd Test PSBT integration (DanGould)
6b6ef528a4 Add OP_0 alias for OP_PUSHBYTES_0 (DanGould)
72935a0f6e Move test_data/* tests/data (Tobin Harding)
Pull request description:
resolves#892
* Initial patch adds OP_0 alias for OP_PUSHBYTES_0 as [approved here](https://github.com/rust-bitcoin/rust-bitcoin/pull/935#discussion_r854539416)
* Second patch is the bulk of this work. It tests BIP 174 PSBT integration defined at [The BIP's Test Vectors Section](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#test-vectors) using #957
* Third patch points users to rust-miniscript for a more useful finalizer
ACKs for top commit:
tcharding:
ACK b8bd31d5a8
sanket1729:
ACK b8bd31d5a8. Thanks for sticking with this PR. This looks awesome after #957
Tree-SHA512: dc68e524d4349530b082baf5032460fa56593b0ef192125c0b7d7e00954e5593f386b7f1984fc00106b4b9eafbf29cc80ab368dbd26b710eba0962dbd89e0013
Now we have MSRV of 1.41.1 we can use the `from_le_bytes` and
`to_be_bytes` methods, these became available in Rust 1.32.
Remove the `endian` module replacing its logic with calls to methods on
the respective stdlib integer types.
c4084b91fb Fix broken build due to conflict between #1340 and #999 (Andrew Poelstra)
Pull request description:
I will test merge commits more thoroughly before signing off on them in future, sorry.
ACKs for top commit:
DanGould:
tACK c4084b91fb
tcharding:
ACK c4084b91fb
sanket1729:
ACK c4084b91fb
Tree-SHA512: 51ece3aa43045e81138d21b8402b1ec1559a0b37bdfc4c5246ff46fd085364517449a2e20e625934cbc0c96f18eb2fc6121a6e993fd5b9535ae54c863d032a0b
dea9b1d1e0 Re-export base64 when enabled (Alekos Filini)
Pull request description:
`base64` used to be exported until commit 23ee0930c7 which removed the `pub extern crate` declaration.
ACKs for top commit:
Kixunil:
ACK dea9b1d1e0
apoelstra:
ACK dea9b1d1e0
Tree-SHA512: 2f32b6676aab9881bab9eb0ae61910ec0d4b60cb17c8a7bf8155ec4a13e50abce0061b52f4e81b106b938e99cb68329d027291c1702213cfa2a46734ebadb488
1a89d5230c examples: Add taproot-psbt workflow example (Duncan Dean)
Pull request description:
Will address #893.
Currently includes a BIP86 example (no spendable script path)
Working on script path and key path spending when both are possible spending paths.
ACKs for top commit:
tcharding:
ACK 1a89d5230c
apoelstra:
ACK 1a89d5230c
Tree-SHA512: 31d23914faedb2632d517f9a827075f1bf387df6d98f151000f70546d1e67ac332e698c6a01bda40a9baf5b25bff0114928edc0879c5e01753ecfc6ad182fe26
`default-features = false` was missing previously but blindly adding it
would lead to subtle risk of breaking when a crate not needing `std`
depends on `bitcoinconsensus` and simultaneously another crate not
needing `bitcoinconsensus` depends on `std` and another crate depends on
them both.
This change fixes it by introducing `bitcoinconsensus-std` feature flag
and provides a fallback if the flag is off. Unfortunately the fallback
has to use a bit of reasonable `unsafe` due to limitations of upcasting.
The only safe alternatives are not do it and provide worse experience
for crates that are affected by the problem above or break the API,
which couldn't be backported and would be more annoying to use.
Closes#1343
fd7f8daeff Move sighash module to crate root (Tobin C. Harding)
Pull request description:
Done as part of the effort to flatten the `util` module.
The `sighash` module can stand alone in the crate root, it provides a discreet set of functionality - the `SighashCache` and associated types.
Marking as high priority because this is part of flattening `util` which is a required step before we start crate smashing.
ACKs for top commit:
Kixunil:
ACK fd7f8daeff
apoelstra:
ACK fd7f8daeff
Tree-SHA512: e812ca903f7dccfa5a06084e23f93f617d016583bdf082d7a36ca8e67e49f1d140a3e138b93939e816861460ff2c04d49d5e37a555dd853dca1c76dbccd910bf
b05ba16a05 ci: Remove serde version pinning (Tobin C. Harding)
Pull request description:
The MSRV break in serde is fixed now, remove the serde version pinning.
Fix: #1256
ACKs for top commit:
Kixunil:
ACK b05ba16a05 if CI passes.
apoelstra:
ACK b05ba16a05
Tree-SHA512: 2046b443500a848cb7b22576473f3b09ece4538dd3d30d7a06b09b28d6fe26f5c4d482f70ec0ec1b79469c221dc9c09e8b05b83debb71fd27f7dc30571cbbcfe
c34d5f8f85 Implement PartiallySignedTransaction::fee (hashmap)
Pull request description:
to calculate fee if previous outputs are available.
Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/1220
ACKs for top commit:
Kixunil:
ACK c34d5f8f85 if CI passes
tcharding:
ACK c34d5f8f85
apoelstra:
ACK c34d5f8f85
Tree-SHA512: 697b837de2fb21bbd5d489c524c06a56bb35b73c0f32cc5b0500f5508f3c539b21d327cd556a04ee847ccf8d98829da994d90c19e80c457ddba2cd9d3469476e
Done as part of the effort to flatten the `util` module.
The `sighash` module can stand alone in the crate root, it provides a
discreet set of functionality - the `SighashCache` and associated types.
c3e4399519 Remove usage of opcodes::all (Tobin C. Harding)
Pull request description:
We have all of the opcodes defined in a submodule called `all`, this allows wildcard imports without bringing in the other types in the `opcodes` module.
Use wildcard import `use crate::blockdata::opcodes::all::*` instead of fully qualifying the path to opcodes.
### Original PR description (left here so the thread of discussion below makes sense)
The `all` module adds no value, we can remove it without loss of meaning or clarity, doing so makes the code less verbose.
EDIT: After review, now includes importing with wildcard and removing the `opcodes::` path from any type starting with `OP_`.
Idea stolen from: https://github.com/rust-bitcoin/rust-bitcoin/pull/525 (patch 7)
ACKs for top commit:
Kixunil:
ACK c3e4399519
apoelstra:
ACK c3e4399519
Tree-SHA512: 300511d909a25e82c563b18da1b52bcf65653cd3efd8ff32dd5b9e839dacd57924953c1745dfb5e9301fa4f9fc0cd61a075f3a3fd94f6a5a9730bca5155dfd96
We have all of the opcodes defined in a submodule called `all`, this
allows wildcard imports without bringing in the other types in the
`opcodes` module.
Use wildcard import `use crate::blockdata::opcodes::all::*` instead of
fully qualifying the path to opcodes.
7e39082eec Improve doc of `Script::push_verify` (Martin Habovštiak)
Pull request description:
This rewords the doc to have a reasonable summary, adds a little background explaining the opcode behavior and the effect of the function when called multiple times.
Closes#1154
ACKs for top commit:
tcharding:
ACK 7e39082eec
apoelstra:
ACK 7e39082eec
Tree-SHA512: 7f0142c9fcec8ef5b30779f1d22922219180aa103ce2f3039412b1d6b46aa7ee2522181e23a76f9ba5fd84720ef3ff3daa8233d71cf10008f5e3b805b5a5c470
7d851b42ee Move serde_string_* macros to the serde_utils module (Tobin C. Harding)
53b681b838 Move const_assert to bitcoin_internals (Tobin C. Harding)
5a8a5ff6c9 Move debug_from_display to bitcoin_internals (Tobin C. Harding)
a2f08f2bc6 Improve docs on impl_array_newtype macro (Tobin C. Harding)
771cdde282 Move impl_array_newtype to bitcoin_internals (Tobin C. Harding)
Pull request description:
Move macros out of `internal_macros`, done in an effort to work towards removing the `internal_macros` module since we have `bitcoin_internals` now.
ACKs for top commit:
apoelstra:
ACK 7d851b42ee
Kixunil:
ACK 7d851b42ee
Tree-SHA512: b31b3a5b4d18a2dbe3f358bff62ae6ca4041d432c755e9c45b0241d48903e02c95e79ec72a7478b9d2a53486ce9eef19bfe3b8905aba19036e59c0719f193ce7
This rewords the doc to have a reasonable summary, adds a little background explaining the opcode behavior and the effect of the function when called multiple times.
Closes ##1154
Done as part of flattening the `util` module. Simply move the `amount`
module out of the `util` module and to the crate root. Justified by the
fact that the `Amount` type is more-or-less a "primitive" bitcoin type.
dd8730e14f Use new PSBT signing API in example (Tobin C. Harding)
d2367fb187 Add PSBT sign functionality (Tobin C. Harding)
b80e5aeaab Re-order import statements (Tobin C. Harding)
Pull request description:
Add an API for signing inputs to the `PSBT` struct. This is work based on code in `rust-miniscript` and the API design suggestions below from @sanket1729 and @Kixunil.
Please note, this adds an `unimplemented!` call for taproot inputs. ECDSA signing is complete.
Includes a patch adding the psbt example from https://github.com/rust-bitcoin/rust-bitcoin/pull/940 updated to use this new api. Run `cargo run --example psbt --features=bitcoinconsensus` to test it out.
ACKs for top commit:
dunxen:
ACK dd8730e
apoelstra:
ACK dd8730e14f
sanket1729:
reACK dd8730e14f
Tree-SHA512: 6345571e53cd3aa4b7ad962536da47ae03ab7c0b088107dc4104676bdb64fcf892e8fa60e0b716f3ef158d88d7058938bf267046721ccf74b2d1b092e9b9aaaa