Commit Graph

193 Commits

Author SHA1 Message Date
Dr Maxim Orlovsky 992857ad0a
PsbtSighashType unit tests 2022-03-28 17:03:44 +02:00
Dr Maxim Orlovsky 5be1cdb8c7
PsbtSigHashType Display and FromStr implementation 2022-03-28 17:03:34 +02:00
Andrew Poelstra 388897bf93
Merge rust-bitcoin/rust-bitcoin#901: TapTree iterator
e27f8ff594 TapTree iterator implementation (Dr Maxim Orlovsky)

Pull request description:

  Implemented after @sanket1729 suggestion in https://github.com/rust-bitcoin/rust-bitcoin/issues/895#issuecomment-1074366108

  Iterates all scripts present in TapTree in DFS order returning `(depth, script)` pairs.

  I propose to have it as an RC fix since this functionality is really lacking and may be required for many wallets working with Taproot PSBT even outside of the scope where I originally needed it (OP_RETURN tweaks for TapTree described in #895)

ACKs for top commit:
  sanket1729:
    utACK e27f8ff594.
  apoelstra:
    ACK e27f8ff594

Tree-SHA512: b398e468a10534561297f22dba47e340391069734a41999edd85d726890752035053690a22014402879ea40b948160f00310f78771443d382c0bbaf0201dfbe5
2022-03-28 13:45:34 +00:00
Tobin Harding 8e2422f92b Add unit test for deserialize non-standard sighash
It is possible, although not immediately obvious, that it is possible to
create a `PsbtSigHashType` with a non-standard value.

Add a unit test to show this and also catch any regressions if we
accidental change this logic.
2022-03-28 10:43:37 +11:00
Tobin Harding e05776f176 Improve PsbtSigHashType conversion methods
Improve the `PsbtSigHashType` conversion methods by doing:

- Re-name `inner` -> `to_u32` as per Rust convention
- Add `from_u32` method

Note, we explicitly do _not_ use suffix 'consensus' because these
conversion methods make no guarantees about the validity of the
underlying `u32`.
2022-03-28 10:43:37 +11:00
Tobin Harding ac462897b1 Remove hungarian-ish notation
The functions `from_u32_standard` and `from_u32_consensus` smell a bit
like hungarian notation. We can look at the method definition to see
that the methods accept `u32` arguments without mentioning that in the
method names.

Remove `_u32_` from the method names. This brings the `from_*` methods
in line  with the `to_standard` method also.
2022-03-28 10:43:37 +11:00
Dr Maxim Orlovsky e27f8ff594
TapTree iterator implementation 2022-03-24 00:03:54 +01:00
Tobin Harding 1629348c24 Use conventional spacing for default type parameters
The exact code formatting we use is not as important as uniformity.
Since we do not use tooling to control the formatting we have to be
vigilant ourselves. Recently I (Tobin) changed the way default type
parameters were formatted (arbitrarily but uniformly). Turns out I
picked the wrong way, there is already a convention as shown in the rust
documentation online (e.g. [1]).

Use 'conventional' spacing for default type parameters. Make the change
across the whole repository, found using

    git grep '\<.* = .*\>'

[1] - https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
2022-03-18 10:40:51 +11:00
Tobin Harding 71cf00a314 Use less vertical lines
In this library we specifically do not use rustfmt and tend to favour
terse statements that do not use extra lines unnecessarily. In order to
help new devs understand the style modify code that seems to use an
unnecessary number of lines.

None of these changes should reduce the readability of the code.
2022-03-14 13:52:13 +11:00
Tobin Harding 702e8bf82d Refactor consensus_encode
The implementations of `consensus_encode` use an unnecessary number of
lines. Favour more terse code with no loss of clarity.
2022-03-14 13:52:13 +11:00
Tobin Harding bf4f5638e0 Refactor whitespace
Do various whitespace refactorings, of note:

- Use space around equals e.g., 'since = "blah"'
- Put return/break/continue on separate line

Whitespace only, no logic changes.
2022-03-14 13:51:50 +11:00
Tobin Harding 7638d59fa6 Improve rusntdocs for *_hash_ty methods
Improve the docs by doing:
- Use markdown heading for `Errors` section
- Use 100 character lines
2022-03-08 09:14:20 +11:00
sanket1729 1ec9e87255
Merge rust-bitcoin/rust-bitcoin#842: Separate out merge method into public trait
5e2449922d Separate merge logic out of Map trait (Tobin Harding)

Pull request description:

  Recently we (*cough* Tobin) made the `Map` trait private and neglected
  to add a public API for merging together two PSBTs. Doing so broke the
  `psbt` module.

  Add a public trait `Merge` and implement it for
  `PartiallySignedTransaction` using the code currently in the `merge`
  method of the now private `Map` trait.

  Motivated by https://github.com/rust-bitcoin/rust-bitcoin/pull/841

ACKs for top commit:
  JeremyRubin:
    > ACK 5e24499
  apoelstra:
    ACK 5e2449922d
  sanket1729:
    ACK 5e2449922d. Also verified that the vectors are same of that of BIP174

Tree-SHA512: 79eefe93e870b61231b388aa28a95ee5c8ac06b68910f4ff324569512a79eafe5b86239fd45f54ca7a868cf59dc6301e45d1f046c039a64b2493a8ffcea659fd
2022-02-28 08:30:31 -08:00
Rishabh Singhal fb04cabe1d
Add a method to psbt to compute find sighash type
Fixes #838: Add a utility method to psbt to compute find sighash
type of a given input.
2022-02-25 18:38:19 +05:30
Tobin Harding 5e2449922d
Separate merge logic out of Map trait
Recently we (*cough* Tobin) made the `Map` trait private and neglected
to add a public API for combining together two PSBTs. Doing so broke the
`psbt` module.

Pull the merge logic out of the `Map` trait and put it in methods on
each individual type (`Input`, `Output`, `PartiallySignedTransaction`).
Doing so allows for simplification of return types since combining
inputs/outputs never errors.

Use the term 'combine' instead of 'merge' since that is the term used in
BIP 174.
2022-02-23 09:03:16 +00:00
sanket1729 4e19973d4e Add a breaking test
This commit can be re-ordered before the fix to see that the test fail
during psbt decoding
2022-02-17 02:48:29 -08:00
sanket1729 69c6eb6173 Bug: Change type of pbst partial sig from secp key to bitcoin key
This changes the type of secp signature from secp256k1::Signature to
bitcoin::PublicKey. Psbt allows storing signatures for both compressed
as well as uncompressed keys. This bug was introduced in #591 while
trying to change the type of BIP32 keys from bitcoin::PublicKey to
secp256k1::PublicKey.
2022-02-16 23:45:35 -08:00
Riccardo Casatta 22aeaef52b
Use write_all instead of write
write() could write only a part of the given buffer, the caller should
check the numbers of byte written (which is what write_all does)
2022-01-25 15:09:21 +01:00
sanket1729 325e0ccf51
Merge rust-bitcoin/rust-bitcoin#800: Use fn name to_ instead of into_
151173821b Use fn name to_ instead of into_ (Tobin Harding)

Pull request description:

  Rust convention is to use `to_` for conversion methods that convert from
  an owned type to an owned `Copy` type. `into_` is for owned to owned
  non-`Copy` types.

  Re-name conversion methods that use `into_` for `Copy` types to use
  `to_`, no need to deprecate these ones because they are unreleased.

  **Note to maintainers**

  This is similar in concept to #798 but only touches new code introduced in this release. Has been labelled 'RC fix' for that reason. Please feel free to remove the label if you disagree.

  From the docs: https://rust-lang.github.io/api-guidelines/naming.html

  <h2><a class="header" href="https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv" id="ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv">Ad-hoc conversions follow <code>as_</code>, <code>to_</code>, <code>into_</code> conventions (C-CONV)</a></h2>
  <p>Conversions should be provided as methods, with names prefixed as follows:</p>

  Prefix | Cost | Ownership
  -- | -- | --
  as_ | Free | borrowed -> borrowed
  to_ | Expensive | borrowed -> borrowed
  | | | borrowed -> owned (non-Copy types)
  | | | owned -> owned (Copy types)
  into_ | Variable | owned -> owned (non-Copy types)

ACKs for top commit:
  Kixunil:
    ACK 151173821b
  apoelstra:
    ACK 151173821b
  sanket1729:
    ACK 151173821b

Tree-SHA512: 4bb97e4fb78beda0fd1ec9482d24ef0f4ade6d3689f5c1bcf2208fa2df3195962522fa5d5ac700e6d4d5ff2096a20b2a0ad51784909a3c12405762aa08d1ced2
2022-01-21 08:00:31 +05:30
Riccardo Casatta 1f0810ad6e
Merge rust-bitcoin/rust-bitcoin#790: Re-export psbt module from root level
b138428df7 Re-export public map types from root level (Tobin Harding)

Pull request description:

  We currently have the `map` module private but containing a bunch of types that are needed in the public API (specifically in a `PartiallySignedTransaction`).

  To give access to them re-export the `util::psbt` module at the root level.

  Found while testing `master` with `rust-miniscript`.

ACKs for top commit:
  sanket1729:
    utACK b138428df7
  Kixunil:
    ACK b138428df7
  RCasatta:
    ACK b138428df7
  dr-orlovsky:
    ACK b138428df7

Tree-SHA512: 36fc8595164c4975abdadb6c8149ef27686a2d681a1815379f91b1bd36f8a56ceaa7faed5979ba6869823684790721a16a0c41e662c6227a09cd0ba576a0a181
2022-01-19 12:18:14 +01:00
Tobin Harding 151173821b Use fn name to_ instead of into_
Rust convention is to use `to_` for conversion methods that convert from
an owned type to an owned `Copy` type. `into_` is for owned to owned
non-`Copy` types.

Re-name conversion methods that use `into_` for `Copy` types to use
`to_`, no need to deprecate these ones because they are unreleased.
2022-01-19 14:59:18 +11:00
Andrew Poelstra 64451a2144
Merge rust-bitcoin/rust-bitcoin#794: Refactor use map_err
9f848472e4 Refactor use map_err (wim-web)

Pull request description:

  issue: https://github.com/rust-bitcoin/rust-bitcoin/issues/793

  change to using map_err

ACKs for top commit:
  Kixunil:
    ACK 9f848472e4
  apoelstra:
    ACK 9f848472e4

Tree-SHA512: 93dac16463bf84825f764f3ef81833c27722a52f56737d30f14160d070959ad13bbfdf5f3c4871b961ce05fa9f75ed36acbacaa40ff6ba3bbf449b9c9173c0c7
2022-01-18 20:55:31 +00:00
wim-web 9f848472e4 Refactor use map_err 2022-01-18 13:20:53 +09:00
Tobin Harding b138428df7 Re-export public map types from root level
We currently have the `map` module private but containing a bunch of
types that are needed in the public API (specifically in a
`PartiallySignedTransaction`).

Re-export the publicly required types to the `psbt` module and then
again at the root level of `rust-bitcoin` as we do for other types.
2022-01-18 12:56:47 +11:00
Andrew Poelstra 8acdb1ab64
Merge rust-bitcoin/rust-bitcoin#786: Fix unused arg in PSBT impl_psbt_get_pair macro
1b77e3609c Fix unused arg in PSBT impl_psbt_get_pair macro (Dr Maxim Orlovsky)

Pull request description:

  Closes #754

ACKs for top commit:
  apoelstra:
    ACK 1b77e3609c
  RCasatta:
    utACK 1b77e3609c
  Kixunil:
    ACK 1b77e3609c

Tree-SHA512: 339aae0a9b6adef34bc6bca27ea19eb89205c4b4694e3a95721590696b7eefbfded9294fd9101a66110b4bdfb69da7a7ef97a8e8321d9e05b3777c3fa9afff7c
2022-01-17 19:54:29 +00:00
Dr. Maxim Orlovsky d5686ee01d
Merge rust-bitcoin/rust-bitcoin#776: Change EcdsaSig hash type deser in psbt
abe52f681b Cleanup/Dedup psbt (De)Serialization code (sanket1729)
fbd86dcf63 Update documentation of EcdsaSig::from_slice (sanket1729)
85009a7b50 Update documentation of from_u32_consensus (sanket1729)
0fed04e2d5 Change EcdsaSig hash type deser (sanket1729)

Pull request description:

  Changes the parsing behavior in PSBT on non-standard sighash types to give an explicit error, rather than silently mangling the parsed value

ACKs for top commit:
  dr-orlovsky:
    ACK abe52f681b
  apoelstra:
    ACK abe52f681b
  Kixunil:
    ACK abe52f681b

Tree-SHA512: 1d5dbe3aa5885ca16649cf8ea05a7476e8dd977dd870b79358d97a3ce383bee93754d2b88163e7db3792cdc4b9cb867356409c8eea4e110877577ad196ba0786
2022-01-17 12:08:18 +02:00
Dr Maxim Orlovsky 1b77e3609c Fix unused arg in PSBT impl_psbt_get_pair macro
Closes #754
2022-01-16 11:59:29 +01:00
sanket1729 abe52f681b Cleanup/Dedup psbt (De)Serialization code 2022-01-15 06:15:54 +05:30
sanket1729 0fed04e2d5 Change EcdsaSig hash type deser 2022-01-15 06:15:10 +05:30
Tobin Harding dfd8924398 Remove insert_pair from Map trait
The method implementation of `insert_pair` is currently not used for
`PartiallySignedTransaction`. Having an implementation available is
deceiving.

Delete the unused `insert_pair` code from
`PartiallySignedTransaction` (dead code). Make the `insert_pair` methods
from `Input` and `Output` be standalone functions.
2022-01-15 10:04:15 +11:00
Tobin Harding ad75d5181f Make Map trait private to psbt module
The `Map` trait has been deemed confusing and not that useful to users
of the library, we still use it internally within the `psbt` module
though so make it visible only in `psbt` and `psbt::map`.
2022-01-15 10:03:47 +11:00
Tobin Harding 53225c0a6e Improve docs in map module
Improve the function rustdocs in the `psbt::map` module by:

- using third person tense as is idiomatic in the Rust ecosystem
- using rustdoc `///` not code comments `//` for methods
- Use `# Return` section for documenting return values

Done for this module only as part of a PR fixing code within this
module.
2022-01-15 10:03:43 +11:00
Tobin Harding 92059c2841 Add full stops to rustdocs
Mildly improve the docs by adding full stops to every rustdoc comment.
2022-01-15 10:03:38 +11:00
Tobin Harding 11c046b707 Refactor match arms
Refactor the match arms to make the code around the key used for map look
up easier read.

Refactor only, no logic changes.
2022-01-15 10:02:41 +11:00
Tobin Harding e6af569490 Move imports to top of file
These imports are unusually placed, from the code comment it seems the
reason is stale.

Move imports to top of file as is typical.
2022-01-15 10:02:38 +11:00
sanket1729 ebdeed086e Cleanup imports
We do not want to imports from within the lib and external of lib in the
same line
2022-01-14 05:39:17 +05:30
sanket1729 382c8f9e4f Introduce PsbtSigHashType 2022-01-14 05:39:17 +05:30
sanket1729 9a8ab3f3ff Change type of final script witness to Witness from Vec<Vec<u8>> 2022-01-11 21:11:18 +05:30
Dr Maxim Orlovsky a6e8f581db PSBT BIP32 keys moved to Secp256k1 from bitcoin ECDSA
Fourth step in implementation of Schnorr key support after #588.

While PSBT BIP174 does not specify whether uncompressed keys are supported in BIP32-related fields, from BIP32 it follows that it is impossible to use uncompressed keys within the extended keys.  This PR fixes this situation and is a companion to BIP174 PR clarifying key serialization: https://github.com/bitcoin/bips/pull/1100
2022-01-10 10:16:57 +01:00
sanket1729 476eed7f2f
Merge rust-bitcoin/rust-bitcoin#590: Taproot: BIP32 extended keys using Scep256k1 keys instead of bitcoin ECDSA
cf0c48cc86 Improve Debug for PrivateKey (Dr Maxim Orlovsky)
b65a6ae49b Test for extended private key keypair generation  f5875a (Dr Maxim Orlovsky)
e6a3d603c9 BIP32 extended key `to_ecdsa()` and `to_schnorr()` methods (Dr Maxim Orlovsky)
b72f56c4ae BIP32 extended keys are using Scep256k1 keys instead of bitcoin ECDSA (Dr Maxim Orlovsky)

Pull request description:

  This is third step required to introduce Schnorr key support according to #588. This PR starts API-breaking changes and is follow-up to non-API breaking #589, which is already merged.

  PR rationale: BIP32 does not support uncompressed keys and using type with compression flag was a mistake

ACKs for top commit:
  apoelstra:
    ACK cf0c48cc86
  sanket1729:
    ACK cf0c48cc86. #757 might need rework after this

Tree-SHA512: 6356a65004e7517256bacbf9aaeb69a22fd8536b341e567c5c4e819288e1105d083fe12ac0641404c407c97acf039bdc525f8e02b1b594a6cdda90106f3b1bdc
2022-01-10 03:46:05 +05:30
Andrew Poelstra 8e9f99b620
Merge rust-bitcoin/rust-bitcoin#718: Converting LeafVersion into an enum
ef8a3a839e Introduce FutureLeafVersion (Dr Maxim Orlovsky)
b028385a72 Improve docs in LeafVersion (Dr Maxim Orlovsky)
839c022f29 Make serde for LeafVersion to have byte representation (Dr Maxim Orlovsky)
67b8db05a8 Converting LeafVersion into an enum (Dr Maxim Orlovsky)
2405417432 Use TAPROOT_ANNEX_PREFIX in sighash module (Dr Maxim Orlovsky)

Pull request description:

  The original `LeafVersion` implementation was just a newtype around `u8`. I think that having enum explicitly listing consensus script implementation rules may be more beneficial in terms of both code readibility and future use of multiple script types, where `LeafVersion` may operate as a context object provided to `Script` to specify interpretation rules for particular op codes.

ACKs for top commit:
  Kixunil:
    ACK ef8a3a839e
  sanket1729:
    crACK ef8a3a839e. Waiting a day to let others complete review before merging.
  apoelstra:
    ACK ef8a3a839e

Tree-SHA512: 3356d2b9b00cf904edfece26d26ffbc646ba74446cc23ec4b2b4026ed50861285802f077226e30ba8fed466f68f8e8556c729ce48cb38581b1d95a02a6fde9cf
2022-01-09 15:26:05 +00:00
Dr Maxim Orlovsky e6a3d603c9 BIP32 extended key `to_ecdsa()` and `to_schnorr()` methods 2022-01-09 07:17:02 +01:00
Dr Maxim Orlovsky b72f56c4ae BIP32 extended keys are using Scep256k1 keys instead of bitcoin ECDSA
According to #588, BIP32 does not support uncompressed keys and using type with compression flag is a mistake
2022-01-09 07:16:49 +01:00
Dr Maxim Orlovsky 14ace92666 Fix SchnorrSig type references in PSBT serialization macros 2022-01-08 16:29:45 +01:00
Dr Maxim Orlovsky 2b530000d3 Use EcdsaSig in PSBT partial signatures instead of Vec<u8> 2022-01-07 21:57:42 +01:00
Dr Maxim Orlovsky c92057d98f PSBT serialize/deserialize impl for EcdsaSig type 2022-01-07 21:57:42 +01:00
Dr Maxim Orlovsky 67b8db05a8 Converting LeafVersion into an enum 2022-01-07 20:28:36 +01:00
sanket1729 2178c7367c Update to secp256k1 0.21.2 2022-01-07 04:45:40 +05:30
Tobin Harding 533120899e Put rustdocs above attributes
Rust idiomatic style is to put the rustdoc _above_ any attributes on
types, functions, etc.

Audit the codebase and move comments/attributes to the correct place.
Add a trailing full stop at times to neaten things up a little extra.
2022-01-06 13:04:47 +11:00
Dr. Maxim Orlovsky 670e808c17
Merge rust-bitcoin/rust-bitcoin#681: Add support for taproot psbt fields BIP 371
7d982fa9a2 Add all tests from BIP 371 (sanket1729)
d22e0149ad Taproot psbt impl BIP 371 (sanket1729)
108fc3d4db Impl encodable traits for TapLeafhash (sanket1729)
c7478d8fd0 Derive serde for taproot stuctures (sanket1729)

Pull request description:

  Built on top of #677 . Will rebase and mark ready for review after #677 is merged.

ACKs for top commit:
  apoelstra:
    ACK 7d982fa9a2
  dr-orlovsky:
    re-tACK 7d982fa9a2 basing on `git range-diff`. The original PR before last re-base was tested commit-by-commit.

Tree-SHA512: feb30e4b38d13110a9c0fabf6466d8f0fb7df09a82f4e01d70b8371b34ab0187004a6c63f9796c6585ee30841e8ee765ae9becae139d2e1e3d839553d64c3d1e
2021-12-30 02:12:03 +02:00
Dr. Maxim Orlovsky 86055d9df5
Merge rust-bitcoin/rust-bitcoin#672: New Witness struct to improve ser/de perfomance
106acdc3ac Add fuzzing for Witness struct (Riccardo Casatta)
2fd0125bfa Introduce Witness struct mainly to improve ser/de performance while keeping most usability. (Riccardo Casatta)

Pull request description:

  At the moment the Witness struct is  `Vec<Vec<u8>>`, the vec inside a vec cause a lot of allocations, specifically:

  - empty witness -> 1 allocation, while an empty vec doesn't allocate, the outer vec is not empty
  - witness with n elements -> n+1 allocations

  The proposed Witness struct contains the serialized format of the witness. This reduces the allocations to:

  - empty witness -> 0 allocations
  - witness with n elements -> 1 allocation for most common cases (you don't know how many bytes is long the entire witness beforehand, thus you need to estimate a good value, not too big to avoid wasting space and not too low to avoid vector reallocation, I used 128 since it covers about 80% of cases on mainnet)

  The inconvenience is having slightly less comfortable access to the witness, but the iterator is efficient (no allocations) and you can always collect the iteration to have a Vec of slices. If you collect the iteration you end up doing allocation anyway, but the rationale is that it is an operation you need to do rarely while ser/de is done much more often.

  I had to add a bigger block to better see the improvement (ae860247e191e2136d7c87382f78c96e0908d700), these are the results of the benches on my machine:

  ```
  RCasatta/master_with_block
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   5,496,821 ns/iter (+/- 298,859)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     437,389 ns/iter (+/- 31,576)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:     108,759 ns/iter (+/- 5,807)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         670 ns/iter (+/- 49)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          51 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          13 ns/iter (+/- 0)

  branch witness_with_block (this one)
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   4,302,788 ns/iter (+/- 424,806)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     366,493 ns/iter (+/- 42,216)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:      84,646 ns/iter (+/- 7,366)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         648 ns/iter (+/- 77)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          50 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          14 ns/iter (+/- 0)
  ```

  With an increased performance to deserialize a block of about 21% and to serialize a block of about 16% (seems even higher than expected, need to do more tests to confirm, I'll appreciate tests results from reviewers)

ACKs for top commit:
  apoelstra:
    ACK 106acdc3ac
  sanket1729:
    ACK 106acdc3ac
  dr-orlovsky:
    utACK 106acdc3ac

Tree-SHA512: e4f23bdd55075c7ea788bc55846fd9e30f9cb76d5847cb259bddbf72523857715b0d4dbac505be3dfb9d4b1bcae289384ab39885b4887e188f8f1c06caf4049a
2021-12-30 01:55:44 +02:00
sanket1729 7d982fa9a2 Add all tests from BIP 371 2021-12-28 20:40:58 +05:30
sanket1729 d22e0149ad Taproot psbt impl BIP 371 2021-12-28 20:40:58 +05:30
Riccardo Casatta 2fd0125bfa
Introduce Witness struct mainly to improve ser/de performance while keeping most usability.
Witness struct is in place of the Vec<Vec<u8>> we have before this commit.

from_vec() and to_vec() methods are provided to switch between this type and Vec<Vec<u8>>

Moreover, implementation of Default, Iterator and others allows to have similar behaviour but
using a single Vec prevent many allocations during deserialization which in turns results in
better performance, even 20% better perfomance on recent block.

last() and second_to_last() allows to access respective element without going through costly Vec
transformation
2021-12-28 09:56:38 +01:00
Martin Habovstiak 779d4110c6 Fixed a bunch of clippy lints, added clippy.toml
This is the initial step towards using and maybe enforcing clippy.
It does not fix all lints as some are not applicable. They may be
explicitly ignored later.
2021-12-21 22:50:13 +01:00
sanket1729 94cfe79170 Rename existing SigHashType to EcdsaSigHashType 2021-12-15 20:00:52 +05:30
Martin Habovštiak ab97d2db1a
Merge pull request #689 from tcharding/module-rustdocs
Clean up module level rustdocs
2021-11-16 13:21:20 +01:00
sanket1729 abc242dfe1
Merge rust-bitcoin/rust-bitcoin#654: Making globals part of PSBT struct. Closes #652
55c627715f Moving globals into PSBT struct (Dr Maxim Orlovsky)

Pull request description:

  I took the most non-invasive approach to reduce diff size. Many parts of the code can be improved in style or further refactored (like some functions are not necessary and can be just moved to be part of other functions), but I'd prefer to do that as a separate PR once this will be merged.

  My approach with this PR:
  1. Remove `Global` struct by moving its fields right into `PartiallySignedTransaction` - but keep the `util/psbt/map/global.rs` file with all its logic
  2. Keep existing `Map for Global` implementation in the same file, but just change it to `Map for PartiallySignedTransaction`
  3. With serialization, convert `Global` deserialization into crate-private function and use it from `PartiallySignedTransaction` deserialization
  4. Refactor the tests and imports as required to get the thing compile and pass tests

  The refactoring will be followed by PR(s) adding support for Taproot

ACKs for top commit:
  apoelstra:
    ACK 55c627715f
  sanket1729:
    ACK 55c627715f . Reviewed range diff with ac0c908 that I previously ACKed

Tree-SHA512: 79b329b6e4e60af905e4e00507d6abc558261d921bcf8f5d4ee34dd685322d7a529b18015423da50a388ba6732b7b662a92bc95ad078228cc809254ad010d467
2021-11-12 11:50:51 -08: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
Dr Maxim Orlovsky 55c627715f
Moving globals into PSBT struct 2021-09-16 12:34:04 +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
Dr Maxim Orlovsky 994079b099
Refactoring error variants: removing unused; better names & inner types 2021-08-01 21:08:39 +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
Sebastian b0ae2a6842
Merge pull request #521 from RCasatta/errors_enum
Errors enum improvements
2021-06-15 14:01:56 +02:00
Riccardo Casatta 0eaf45ca81
Use boxed sliced instead of Vec in InvalidPreimageHashPair variant 2021-06-13 22:33:52 +02:00
Riccardo Casatta 781b0e014a
Box big Transaction type in psbt error 2021-06-13 22:33:48 +02:00
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02: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
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 f692c4a938
Limit bytes read with Take 2021-04-28 09:33:37 +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 7df0d14f1a
Merge pull request #577 from RCasatta/proprietary_key
Include proprietary key in deserialized PSBT
2021-03-15 12:16:30 +00:00
Riccardo Casatta 7e25c133d8
Include proprietary keys in deserialized fields 2021-03-13 15:41:07 +01:00
Riccardo Casatta fcd7200efe
Proprietary key test failing rtt 2021-03-13 15:19:21 +01:00
Dr Maxim Orlovsky 7b7b73f6d2
Improving PSBT FromStr error type 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 79f2729b20
Improving PSBT error tests 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 94102fa597
PSBT Base64 serialization test cases 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 072e1d1b86
PSBT Display & FromStr using Base64 serialization 2021-02-22 12:46:22 +01:00
Antoine Poinsot e36f3a38e4
transaction: deprecate SigHashType::from_u32 in favor of from_u32_consensus
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
2021-02-19 11:36:44 +01: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
Steven Roose 5fd92d1c09 psbt: Serialize maps with byte values using hex 2020-12-30 16:32:52 +01:00
Steven Roose a05f8feec6 serde_util: Rename the btreemap module to btreemap_as_seq
So that we can later distinguish other modules over maps.
2020-12-30 16:32:52 +01:00
Steven Roose a9c13272a0 psbt: Fix serde for maps with non-string keys and binary values 2020-12-30 16:32:52 +01:00
Steven Roose e5f3bca2b4 Add serde_utils module to fix JSON serialization 2020-12-30 16:32:52 +01:00
Steven Roose cdedb0a9d5 psbt::raw: Use hex::format_hex to format the raw key bytes 2020-12-30 16:32:52 +01:00
Steven Roose 94b7371424 Replace serde_struct_impl with derive-based impls 2020-12-30 16:32:52 +01:00
Dr Maxim Orlovsky 7400bccb60
PSBT: proprietary keys type system according to BIP 174 2020-12-21 12:22:35 +01:00
Dr Maxim Orlovsky c3024c3ebb
PSBT: basic support for proprietary keys 2020-12-21 12:17:07 +01:00
Dr Maxim Orlovsky 7f5c2795d6
PSBT global xpub merging algorithm reworked 2020-12-20 11:41:24 +01:00
Dr Maxim Orlovsky b84faa7f5e
PSBT: Improved global keys version and xpub handling 2020-12-16 16:46:28 +01:00
Dr Maxim Orlovsky 21c11e3315
BSPT: Improving global xpub merging algorithm 2020-12-16 16:39:46 +01:00
Dr Maxim Orlovsky 8b1666295c
Nits in new PSBT global types reviews 2020-12-15 16:04:02 +01:00
Dr Maxim Orlovsky 0235abfac2
Improving PSBT merge with dedicated 1.29 rustc borrow scope 2020-12-15 15:50:18 +01:00
Dr Maxim Orlovsky df8635c5fe
PSBT: Key pair serialization for new global keys
Conflicts:
	src/util/psbt/map/global.rs
2020-12-15 15:50:18 +01:00
Dr Maxim Orlovsky 2f838218a8
PSBT: merging new global keys
Plus necessary changes to BIP 32 implementations and error type
2020-12-15 15:47:37 +01:00
Dr Maxim Orlovsky af61d7e7bc
PSBT: adding global types (version, xpub) 2020-12-15 15:47:37 +01:00
Dr Maxim Orlovsky 881f4d75dd
PSBT: Making hash preimage constant names match BIP 174 2020-12-07 16:35:28 +01:00
Dr Maxim Orlovsky 214e10baef
PSBT types are now constants 2020-12-07 15:37:16 +01:00
Dr Maxim Orlovsky 27037e2c72
PSBT: Making RIPEMD160 hash match standard name 2020-12-05 15:46:30 +01:00