Commit Graph

175 Commits

Author SHA1 Message Date
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
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
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 c252b36786 Add CHECKSIGADD and update classify API 2021-09-13 07:45:00 -07: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 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
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
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
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 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
Riccardo Casatta 69117a1f63
Use Amount for verify_with_flags 2021-05-01 10:22:35 +02:00
Riccardo Casatta 3aaa5d6846
Add verify with flags 2021-04-30 18:56:35 +02:00
Dr Maxim Orlovsky b17d7fc31c
Moving keys under `util::ecdsa`, re-exporting them at `util::key`
This is the first step in introducing Schnorr key support as per #588
2021-04-12 14:17:42 +02:00
Steven Roose 851a3a15c0
Make Script::fmt_asm a static method and add Script::str_asm
This makes it convenient to print/construct the script assembly on
byte slices withoout having to clone them to copy them to create a
Script struct.
2021-03-13 22:16:20 +00:00
Steven Roose 96fa4b7d9b
Implement AsRef<[u8]> for Script 2021-03-13 22:13:39 +00:00
Sebastian 5bd61967b2
Merge pull request #566 from TheBlueMatt/2021-02-dust-value
Add Script:dust_value() to get minimum output value for a spk
2021-02-07 11:45:17 +01:00
Matt Corallo 6622de4041 Add Script:dust_value() to get minimum output value for a spk 2021-02-05 15:26:56 -05:00
Steven Roose 61918dfe81
Change the signature of consensus_encode to return io::Error's
This is instead of encode::Errors because the encoders should
not be allowed to return errors that don't originate in the writer
they are writing into.

This is a part of the method definition that has been relied upon for a
while already.
2021-01-12 17:39:41 +00:00
Andrew Poelstra ffe452ac0b script: add FromHex and FromStr implementations 2021-01-05 18:45:14 +01:00
Steven Roose ab9514983b
Add explanation to witness program version assertion 2020-10-23 21:28:16 +01:00
Elichai Turkel 2d70623356
Remove deprecated Error::description impl 2020-10-08 17:11:18 +03:00
Elichai Turkel 023fae1f65
Add the dyn keyword where appropriate 2020-10-08 17:11:16 +03:00
Dr Maxim Orlovsky 1342d73734 Script hash functions with non-allocating serialization 2020-09-11 15:10:57 +02:00
Dr Maxim Orlovsky 8363c76f5c Script hash functions (normal and witness) 2020-09-11 15:10:57 +02:00
Dr Maxim Orlovsky 38a412c4d5 Fixing review comments for script generators refactoring 2020-09-10 14:44:44 +02:00
Dr Maxim Orlovsky e33cdcca14 OP_RETURN script initializer 2020-09-10 12:09:04 +02:00
Dr Maxim Orlovsky 9143fd3888 All script generation functionality moved to standalone methods in Script 2020-09-10 12:09:04 +02:00
Elichai Turkel a44ba2d878
Merge pull request #397 from stevenroose/script-iter
Improve the Instructions iterator for scripts
2020-09-09 19:06:05 +03:00
Elichai Turkel 654232a3dc
Deprecate Error::description 2020-04-13 02:15:28 +03:00
Steven Roose fea09a0a94
Improve the Instructions iterator for scripts
- Rename the `iter` method to `instructions`.
- Add `instructions_minimal` for minimal-enforced iteration.
- Iterator has `Result<Instruction, Error>` as items.
2020-04-11 23:52:26 +01:00
Elichai Turkel c19b736566
Remove the hex dependency 2020-01-20 18:50:02 +02:00
Dr Maxim Orlovsky d20ab1dbc4 Switching to XpubIdentifier 2020-01-01 13:46:44 +01:00
Dr Maxim Orlovsky 5f4f629bb1 Replaced all hash160, sha256 and sha256d with the new hash types throughout the code
Embedding Txid's in the doc exaples
2020-01-01 13:44:34 +01:00
practicalswift 0b08978af2 Fix incorrect documentation for is_p2pk(...) (#331) 2019-09-21 13:33:44 +02:00