Commit Graph

359 Commits

Author SHA1 Message Date
Tobin Harding 4031fbf4ba Use expect for concensus_encode on sinks
Calls to `unwrap` outside of tests are typically unfavourable.

Sink writers do not error. We can use `expect` with a descriptive
message string to indicate this.
2021-11-25 10:04:21 +11:00
Tobin Harding fa513bb5b5 Use expect for concensus_encode on engines
Calls to `unwrap` outside of tests are typically unfavourable.

Hash engines do not error when calling `consensus_encode`. Instead of
the current usage of `unwrap` we can use `expect` with a descriptive
string as is done in other parts of the codebase.
2021-11-25 10:01:41 +11:00
Tobin Harding 3f5caa501f Clean up module level rustdocs
Docs can always do with a bit of love.

Clean up the module level (`//!`) rustdocs for all public modules.

I claim uniform is better than any specific method/style. I tried to fit
in with what ever was either most sane of most prevalent, therefore
attaining uniformity without unnecessary code churn (one exception being
the changes to headings described below).

Notes:

* Headings - use heading as a regular sentence for all modules e.g.,

```
//! Bitcoin network messages.
```

as opposed to
```
//! # Bitcoin Network Messages
```

It was not clear which style to use so I picked a 'random' mature
project and copied their style.

* Added 'This module' in _most_ places as the start of the module
description, however I was not religious about this one.

* Fixed line length if necessary since most of our code seems to follow
short (80 char) line lengths for comments anyways.

* Added periods and fixed obvious (and sometimes not so obvious)
grammatically errors.

* Added a trailing `//!` to every block since this was almost universal
already. I don't really like this one but I'm guessing it is Andrew's
preferred style since its on the copyright notices as well.
2021-11-06 10:59:53 +11:00
Martin Habovstiak 49bd3af449 Refactor Script::bytes_to_asm_fmt to use iterator
This refactors `Script::bytes_to_asm_fmt`` function to use an iterator
instead of index. Such change makes it easier to reason about overflows
or out-of-bounds accesses. As a result this also fixes three unlikely
overflows and happens to improve formatting to not output space at the
beginning in some weird cases.

To improve robustness even better it also moves `read_uint`
implementation to internal function which returns a more specific error
type which can be exhaustively matched on to guarantee correct error
handling. Probably because of lack of this the code was previously
checking the same condition twice, the second time being unreachable and
attempting to behave differently than the first one.

Finally this uses macro to deduplicate code which differs only in single
number, ensuring the code stays in sync across all branches.
2021-09-30 14:33:10 +02:00
Andrew Poelstra 9fe840c20e
Merge pull request #644 from sanket1729/tap_opcodes
Add OP_CHECKSIGADD and OP_SUCCESSxxx
2021-09-24 22:47:48 +00:00
Andrew Poelstra 29549ccc73
Merge rust-bitcoin/rust-bitcoin#658: Check for overflow in Script::bytes_to_asm_fmt()
76cf74fa9b Added test for the overflow bug and few others (Martin Habovstiak)
a0e1d2e706 Check for overflow in Script::bytes_to_asm_fmt() (Martin Habovstiak)

Pull request description:

  This adds an overflow check in `Script::bytes_to_asm_fmt()` motivated by
  `electrs` issue. While it was not tested yet, I'm very confident that
  overflow is the cause of panic there and even if not it can cause panic
  becuase the public function takes unvalidated byte array and reads
  `data_len` from it.

  The `electrs` issue: https://github.com/romanz/electrs/issues/490

  ~~Strangely, this breaks a test case and I can't see why. I'm publishing in case someone wants to help.~~

  Edit: One damn character. :D Should be OK now.

ACKs for top commit:
  apoelstra:
    ACK 76cf74fa9b

Tree-SHA512: 4ffeca442a71b10c132f055f056128ae64e66cbdc1891662c3a4e743b82fa5d27075a44513e844be37888b33068eef3bbf6bcced5def70c17c9c5bd5b9d870cc
2021-09-20 14:22:44 +00:00
Martin Habovstiak 76cf74fa9b Added test for the overflow bug and few others
This adds a test case for script formatting which caused overflow in the
past and a few others from the same "interesting" transaction. Note that
to trigger the bug one has to run the test on 32 bit architecture.
2021-09-19 15:45:17 +02:00
Martin Habovstiak a0e1d2e706 Check for overflow in Script::bytes_to_asm_fmt()
This adds an overflow check in `Script::bytes_to_asm_fmt()` motivated by
`electrs` issue. While it was not tested yet, I'm very confident that
overflow is the cause of panic there and even if not it can cause panic
becuase the public function takes unvalidated byte array and reads
`data_len` from it.

The `electrs` issue: https://github.com/romanz/electrs/issues/490
2021-09-19 13:33:37 +02:00
Dr. Maxim Orlovsky b7f984972a
Merge pull request #655 from vss96/Limit-Script-Size 2021-09-16 10:42:08 +02:00
Andrew Poelstra b6b60fc4aa
Merge rust-bitcoin/rust-bitcoin#628: Adds Taproot BIP341 signature message and create a unified sighash cache for legacy, segwit and taproot inputs
c704ee7ffe [docs-only] Use backtick in addition to square parentheses for types references, clarify legacy, non_exhaustive comment, remove std:: (Riccardo Casatta)
f223be618f Rename access_witness to witness_mut and return Option (Riccardo Casatta)
c9bc0b928a [fmt-only] autoformatting with `rustfmt src/util/sighash.rs` (Riccardo Casatta)
07774917c2 Use get_or_insert_with in segwit_cache (Martin Habovstiak)
497dbfb7c3 Use get_or_insert_with in common_cache() (Martin Habovstiak)
ca80a5a030 Use get_or_insert_with in taproot_cache (Martin Habovstiak)
6e06a32ccc Wrap ErrorKind in Io enum variant, fix doc comment for the IO variant (Riccardo Casatta)
1a2b54ff23 introduce constant KEY_VERSION_0 (Riccardo Casatta)
417cfe31e3 Derive common traits for structs and enum, make internal struct not pub (Riccardo Casatta)
55ce3dd6ae Fix validation error if SINGLE with missing corresponding output, remove check_index and check with get().ok_or(), more details in errors (Riccardo Casatta)
2b3b22f559 impl Encodable for Annex to avoid allocation (Riccardo Casatta)
1a7afed068 Add Reserved variant to SigHashType for future use (ie SIGHASH_ANYPREVOUT) (Riccardo Casatta)
53d0e176d3 Deprecate bip143::SigHashCache in favor of sighash::SigHashCache (Riccardo Casatta)
15e3caf62d [test] Test also sighash legacy API with legacy tests (Riccardo Casatta)
24acfe3672 Implement Bip341 signature hash, create unified SigHashCache for taproot, segwit and legacy inputs (Riccardo Casatta)
683b9c14ff add [En|De]codable trait for sha256::Hash (Riccardo Casatta)

Pull request description:

  Adds https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki message signature algorithm

  The base is taken from `bip143::SigHashCache`, some code results duplicated but I think it's more clear to keep things separated

  Would mark some bullet point on https://github.com/rust-bitcoin/rust-bitcoin/issues/503

  Test vectors are taken by running d1e4c56309/test/functional/feature_taproot.py with a modified `TaprootSignatureHash` function to print intermediate values that I cannot found in the bip341 [test vector json](https://raw.githubusercontent.com/bitcoin-core/qa-assets/main/unit_test_data/script_assets_test.json)

  UPDATE: Latest version includes the suggestion from @sanket1729 to create a unified tool for signature message hash for legacy, segwit, and taproot inputs. In particular, makes sense for mixed segwit v0 and taproot v1 inputs because cached values could be shared

ACKs for top commit:
  sanket1729:
    ACK c704ee7ffe. Reviewed the diff from a37de1ade475e0c31c932121abaa7aec701b9987 which I previously ACKed
  dr-orlovsky:
    utACK c704ee7ffe by diffing it to 6e06a32ccc having my ACK before.
  apoelstra:
    ACK c704ee7ffe

Tree-SHA512: 35530995fe9d078acd0178cfca654ca980109f4502c91d578c1a0d5c6cafacab7db1ffd6216288eac99f6a763776cbc0298cfbdff00b5a83e98ec4b15aa764e8
2021-09-15 17:47:17 +00:00
Vikas S Shetty 48c732e934 Changes for checking script size and returning Error appropriately 2021-09-15 16:16:20 +05:30
Dr Maxim Orlovsky c75f3ef4a8
Handling CHECKMULTISIG(VERIFY) ops in TapScript context; refactoring classifier 2021-09-14 13:58:16 +02:00
Martin Habovstiak 95fb4e01f9 Document cargo features
This documents cargo features in two ways: explictly in text and in code
using `#[doc(cfg(...))]` attribute where possible. Notably, this is
impossible for `serde` derives. The attribute is contitional and only
activated for docs.rs or explicit local builds.

This change also adds `package.metadata.docs.rs` field to `Cargo.toml`
which instructs docs.rs to build with relevant features and with
`docsrs` config activated enabling `#[doc(cfg(...))] attributes.

I also took the opportunity to fix a few missing spaces in nearby code.
2021-09-14 12:24:57 +02:00
sanket1729 b0ad6748e4 Add tests for opcode classification 2021-09-13 07:45:15 -07:00
sanket1729 c252b36786 Add CHECKSIGADD and update classify API 2021-09-13 07:45:00 -07:00
Dr. Maxim Orlovsky 697e8f5194
Merge pull request #626 from visvirial/impl-vsize
Implement `Block.get_strippedsize()` and `Transaction.get_vsize()`
2021-09-11 00:09:22 +02:00
Dr Maxim Orlovsky ecc400826c
Updating Script::is_witness_program to use new WitnessVersion 2021-08-10 10:34:15 +02:00
Dr Maxim Orlovsky 64c1ec0b76
WitnessVersion type 2021-08-10 10:34:15 +02:00
Dr. Maxim Orlovsky 4e3c2c32fc
Merge pull request #632 from tcharding/prefixes 2021-08-09 23:26:15 +02:00
Dr. Maxim Orlovsky 33393e0bf4
Merge pull request #621 from RCasatta/verify_with_amount 2021-08-09 23:23:09 +02:00
Dr. Maxim Orlovsky 808e1708c3
Merge pull request #596 from RCasatta/script_ser 2021-08-09 23:22:06 +02:00
Tobin Harding adc1543e7c
Use consts for address prefix values
Instead of using magic numbers we can define constants for the address
prefix bytes. This makes it easier for future readers of the code to see
what these values are if they don't know them and/or see that they are
correct if they do know them.
2021-07-22 10:31:14 +10:00
Riccardo Casatta 15e3caf62d
[test] Test also sighash legacy API with legacy tests 2021-07-21 12:05:37 +02:00
Devrandom 4826d0c6cc no_std support
Based on the original work by Justin Moon.

*MSRV unchanged from 1.29.0.*

When `std` is off, `no-std` must be on, and we use the [`alloc`](https://doc.rust-lang.org/alloc/) and core2 crates. The `alloc` crate requires the user define a global allocator.

* Import from `core` and `alloc` instead of `std`
* `alloc` only used if `no-std` is on
* Create `std` feature
* Create `no-std` feature which adds a core2 dependency to polyfill `std::io` features. This is an experimental feature and should be
used with caution.
* CI runs tests `no-std`
* MSRV for `no-std` is 1.51 or so
2021-07-15 09:04:49 +02:00
Vis Virial cdf7be4765
Add extra checks for `test_segwit_transaction()`. 2021-06-29 07:59:22 +09:00
Vis Virial 2bda871628
Remove `#[inline]` from `Transaction.get_strippedsize()`. 2021-06-29 07:40:13 +09:00
Vis Virial c9dead410a
Implement `Transaction.get_strippedsize()`.
`Block.get_strippedsize()` is also simplified and optimized.
2021-06-29 07:34:37 +09:00
Vis Virial 1bf9147a6e
Optimize `Transaction.get_vsize()` (thanks @TheBlueMatt). 2021-06-29 07:14:01 +09:00
Vis Virial 2085dc32a7
Refactoring: define `Block.get_base_size()`. 2021-06-28 20:07:07 +09:00
Vis Virial 4ac9cef9e9
Implement `Block.get_strippedsize()` and `Transaction.get_vsize()`. 2021-06-28 20:03:42 +09:00
Riccardo Casatta a86bced568
use Amount type in verify 2021-06-21 14:43:09 +02:00
Sebastian e334c9deea
Merge pull request #616 from sgeisler/2021-06-dust-value-amount
Use `Amount` type for dust value calculation
2021-06-16 20:51:23 +02:00
Riccardo Casatta 4a4460b1a3
Add test for script serialize/deserialize 2021-06-16 10:17:56 +02:00
Riccardo Casatta fadd368911
use different ser/de for Script in case of non human readable format 2021-06-16 10:17:40 +02:00
Sebastian Geisler 9981da2ec8 Use `Amount` type for dust value calculation 2021-06-12 21:15:13 +02:00
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02:00
Sebastian d7eb15c6f1
Merge pull request #584 from darosior/max_tx_weight_const
Introduce some policy constants from Bitcoin Core
2021-05-29 02:49:46 +02:00
Andrew Poelstra 052aaf1d80
Merge pull request #559 from LNP-BP/fix/error-derives-2
Fix/error derives 2
2021-05-19 15:02:10 +00:00
Antoine Poinsot 2e9d62a9c7 blockdata/script: use policy's constant in dust computation
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
2021-05-18 14:52:58 +02:00
Antoine Poinsot 2b777485fa policy: introduce p2p constants from the reference implementation
This introduces some constants defined by Bitcoin Core which as a
consequence define some network rules in a new 'policy' module.

Only some were picked, which are very unlikely to change. Nonetheless a
Warning has been put in the module documentation.

Script-level constants are left into rust-miniscript where they are
already defined (src/miniscript/limits.rs).
2021-05-18 14:52:56 +02:00
Andrew Poelstra 4620c640a9
Merge pull request #579 from TheBlueMatt/master
Fix Script::dust_value()'s calculation for non-P2*PKH script_pubkeys
2021-05-06 16:01:36 +00:00
Andrew Poelstra 3fd88d317f
Merge pull request #598 from RCasatta/verify_flags
Add verify_with_flags to Script and Transaction
2021-05-05 22:33:47 +00:00
Matt Corallo fc6f23fb9b Drop not-very-useful output dust threshold constants
It doesn't really make sense to have a constant for every common
script type's dust limit, instead we should just use the
`Script::dust_value()` function to have users calculate it.
2021-05-05 14:53:26 +00:00
Dr Maxim Orlovsky c0b7defc86
More non-API breaking error derives depending on upstream PRs 2021-05-04 03:58:56 +02:00
Riccardo Casatta c96f7ec71c
use markdown autolink syntax for urls with no text 2021-05-03 13:46:10 +02:00
Riccardo Casatta ef471ccca7
Fix documentation, in particular link to code elements 2021-05-03 11:43:11 +02:00
Matt Corallo 15981c945a Fix Script::dust_value()'s calculation for non-P2*PKH script_pubkeys
The dust calculations added were only valid for P2WPKH and P2PKH
outputs, and somehow this fact was missed in review, despite the
upstream Core code being linked to and looked at by two reviewers
and the author (me).

Someday I will grow eyeballs, but that day is not today.
2021-05-01 17:47:45 +00:00
Dr. Maxim Orlovsky 68096242d3
Merge pull request #594 from RCasatta/capped
Count bytes read in encoding
2021-05-01 16:28:57 +02:00
Riccardo Casatta 69117a1f63
Use Amount for verify_with_flags 2021-05-01 10:22:35 +02:00
Riccardo Casatta d1f4c0a5c8
Remove Copy for flags parameter 2021-05-01 10:19:54 +02:00