Commit Graph

45 Commits

Author SHA1 Message Date
yu 41e8fb0863 Support signing taproot in psbt 2024-02-22 10:42:44 +08:00
Tobin C. Harding 9187bf3a65
Fix new nightly warnings/errors
The latest nightly toolchain introduced a whole bunch of new warnings
and errors, mostly to do with import statements - fix them all.
2024-02-21 14:13:49 +11:00
josibake ccbd09d5fb
Remove unnecessary m/ prefix requirement
In BIP0032, m is used as a variable for the root extended key. It is not
meant to be used as a constant prefix when serializing paths.

Update the DerivationPath parser to no longer require the m prefix.
Remove the m prefix from the unit tests and the bip32, ecdsa-psbt,
and taproot-psbt examples.

close #2449
2024-02-07 12:17:45 +01:00
Tobin C. Harding 271b45299f
Improve Signature field names
Applies to both `ecdsa::Signature` and `taproot::Signature`.

Re-name the `Signature` fields with more descriptive names. The
names used were decided upon in the issue discussion.

Impove rustdocs while we are at it.

Note, the change to `sign-tx-segwit-v0` is refactor only, the diff does
not show it but we have a local variable already called `sighash_type`
that is equal to `EcdsaSighashType::All`.

Includes a function argument rename as well, just to be uniform.

Fix: #2139
2024-01-15 10:26:40 +11:00
Tobin C. Harding 4354f37f51
Use NetworkKind in bip32 module
BIP-32 only differentiates between mainnet and some testnet when
encoding and decoding xpubs and xprivs. As such we can use the new
`NetworkKind` type instead of `Network` throughout the `bip32` module.
2023-12-15 11:50:50 +11:00
Tobin C. Harding d22f3828f6
Use NetworkKind in address module 2023-12-15 11:49:41 +11:00
Tobin C. Harding 923ce7402d
Remove Network from AddressInner
An `AddressInner` struct is created when parsing address strings however
address strings do not map 1:1 to `Network` because signet and testnet
use the same bech32 prefix "tb".

We can fix this by inlining the `Payload` variants into `AddressInner`
and adding prefix enums for legacy addresses and a `KnownHrp` for bech32
addresses.

Also enables removing the `AddressEncoding` struct as we can display the
`AddressInner` struct directly. (The `Display` impl is on `AddressInner`
and not directly on address to ignore the `NetworkValidation` wrapper,
may be able to be simplified still further.)
2023-12-05 09:27:15 +11:00
Tobin C. Harding 761de886be
Remove imports of TryFrom and TryInto
Now that MSRV is Rust 1.56.1 we no longer need to explicitly import
`TryFrom` and `TryInto`.

No clue why clippy didn't find these for us.
2023-11-24 03:52:05 +11:00
yancy b163d9b59a Replace hex_script macro with a helper function
Remove the macro hex_script and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 10:20:23 +02:00
yancy 7f26439e20 Add track_caller to test helper functions
The test helper files can panic when calling hex_psbt().  hex_psbt()
will report the calling function, instead of the offending test.  Adding
track_caller to functions that call hex_psbt will report the line number
of the failing test.
2023-10-20 09:41:50 +02:00
yancy bf08ee4499 Replace helper macro with helper function
Remove the macro hex_psbt and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 09:25:00 +02:00
junderw dac627cc09
Feature: Psbt fee checks 2023-09-28 00:11:33 -07:00
Clark Moody 72a7280d7d
Merge pull request #2006 from tcharding/08-18-tx-version
Add transaction::Version data type
2023-09-23 13:13:52 -05:00
Subhradeep Chakraborty 95b7a95fc2 test: correct psbt used for regression test 2023-09-21 11:45:18 +05:30
Tobin C. Harding c950ef4bbd
Add transaction::Version data type
BIP-68 activated a fair while ago (circa 2019) and since then only
transaction versions 1 and 2 have been considered standard.

Currently in our `Transaction` struct we use an `i32`, this means users
can construct a non-standard transaction if they do not first look up
what the value should be. We can help folk out here by abstracting over
the version number.

Since the version number only governs standardness elect to make the
inner `i32` public (ie., not an invariant). The aim of the type is to
make life easy not restrict what versions are used.

Add transaction::Version data type that simply provides two consts `ONE`
and `TWO`.

Add a `Default` impl on `Version` that returns `Version::TWO`.

In tests that used version 0, instead use `Version::default` because the
test obviously does not care.
2023-09-21 15:02:02 +10:00
Tobin C. Harding be05f9d852
Rename xpub and xpriv types
The BIP-32 extended public key and extended private key exist in the
Bitcoin vernacular as xpub and xpriv. We can use these terms with no
loss of clarity.

Rename our current BIP-32 types

- `ExtendedPubKey` to `Xpub`
- `ExtendedPrivKey` to `Xpriv`

This patch is a mechanical search-and-replace, followed by running the
formatter, no other manual changes.
2023-08-22 13:47:35 +10:00
Tobin C. Harding 2268b44911
Depend on hex-conservative
We have just released the `hex-conservative` crate, we can now use it.

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
yancy d57ec019d5 Use Amount type for TxOut value field 2023-05-04 17:09:08 +02:00
Martin Habovstiak 8b4280d5ef Update clippy MSRV configuration
We've upgraded MSRV but didn't update clippy config, so some things that
could be improved aren't caught by clippy. This updates the config and
fixes the new issues.

I also `rg '1\.41\.1'`ed for interesting changes and found one
additional improvement.
2023-04-02 16:28:10 +02:00
Tobin C. Harding a11cf07501
Run the formatter
Various formatting issues have crept into the codebase because we do not
run the formatter in CI.

In preparation for enabling formatting checks in CI run `cargo +nightly
fmt` to fix current formatting issues. No changes other than those
create by the formatter.
2023-03-06 10:22:29 +11:00
sanket1729 289dc1e7f5 Remove serde for taprootspendinfo
Implementing this for spendinfo is really complicated because it
contains some cached data without retaining the components that are used
to compute them.

Users should serde the 1) NodeInfo and 2) internal key and reconstruct
TaprootSpendInfo from it.
2023-03-01 16:58:24 -08:00
sanket1729 9affda3012 Introduce Hidden leaves in ScriptLeaves
Cleanly separate `TapTree` and `NodeInfo`. Fix serde not respecting
invariants for several data structures

Repurpose some tests from removed taproot builder for taptree
2023-03-01 16:58:24 -08:00
Tobin C. Harding 42b07586ac
Improve the public API
We created the `crypto` crate as a container for cryptography modules
with the idea that it may be split out into a separate crate. There is
no reason for users of the lib to know about this module. Also, we have
two `taproot` modules, one in `crypto` and one at the crate root, this
makes for un-ergonomic usage of the lib.

Improve the public API by doing:

- Make the `crypto` module private (`pub(crate)`).
- Re-export `crypto::taproot::Signature` (and `Error`) from
  `crate::taproot`
2023-03-01 09:28:42 +11:00
Tobin C. Harding be7b3754a9
Rename schnorr module to taproot
"schnorr" is a dirty word; the current `schnorr` module defines a
`Signature` that includes a sighash type, this sighash type is a bitcoin
specific construct related to taproot. Therefore the `Signature` is
better named `taproot::Signature`. Note also that the usage of `schnorr`
in `secp256k1` is probably justified because the
`secp256::schnorr::Signature` is just doing the crypto.

While we are at it, update docs and error messages to use "taproot"
instead of "schnorr". Also change function names and identifiers that
use "schnorr".
2023-02-20 12:58:09 +11:00
Tobin C. Harding 9f39e872bc
Rename SchnorrSighashType to TapSighashType
As we did for `SchnorrSighash`, rename the `SchnorrSighashType` to
`TapSighashType`.
2023-02-20 12:58:09 +11:00
Tobin C. Harding 7e4da3c0ab
Move taproot keys to the keys module
We have a keys module, taproot keys should live in there.
2023-02-20 12:58:05 +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
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
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
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
DanGould 81ca10701a
Use as_ref() instead of costly clone() 2023-01-09 23:17:04 -05:00
DanGould c4363e5ba1
Deserialize Psbt fields, don't consensus_encode 2022-12-21 12:24:06 -05:00
DanGould c1dd6ad8a2
Serialize Psbt fields, don't consensus_encode them 2022-12-21 12:19:38 -05:00
DanGould 1b7b08aa5d
De/serialize Psbt without consensus traits 2022-12-21 12:01:20 -05:00
Martin Habovstiak 8e428562cb Implemented unsized `Script`
This renames `Script` to `ScriptBuf` and adds unsized `Script` modeled
after `PathBuf`/`Path`. The change cleans up the API a bit, especially
all functions that previously accepted `&Script` now accept truly
borrowed version. Some functions that perviously accepted `&[u8]` can
now accept `&Script` because constructing it is no loger costly.
2022-12-14 23:21:27 +01:00
Andrew Poelstra 5b7d801ee6
remove PackedLockTime type
This can be replicated by deleting the `type PackedLockTime = LockTime'
line, and then running
    find . -type f | xargs sed -i 's/PackedLockTime/LockTime/g
at the root of the repo.
2022-12-11 19:08:14 +00:00
Andrew Poelstra 74ff4946e4
locktime: unify serde impls 2022-12-11 18:48:56 +00:00
sanket1729 3a923980e1 Hotfix: Fix broken serde
My local scripts did not test serde feature on merge commit. While
merging 734, I accidently broke the serde feature on latest master. This
PR fixes it.
2022-12-01 15:57:37 -08:00
sanket1729 df57a195c1
Merge rust-bitcoin/rust-bitcoin#724: Serde regression tests
962abcc963 Add serde regression tests (Tobin Harding)

Pull request description:

  Attempts to add regression tests for _all_ types defined in `rust-bitcoin` that implement `Serialize`/`Deserialize`.

  - Add a `tests` directory and implement regression tests in there
  - Use files for input hex and output bincode to reduce source file clutter
  - Copy test block and `include_bytes!` usage from RCasatta's [PR](https://github.com/rust-bitcoin/rust-bitcoin/pull/750)
  - Uses Kixunil's macro suggested below
  - Adds a single regression test to `util/taproot.rs` for private types

  ## Note to reviewers
  - Uses JSON for opcodes in a separate file (`tests/regression_opcodes.rs`), for all other tests uses bincode.
  - Bypasses the order issue for maps by only serializing maps with a single element - is this correct?

  Fixes #723

ACKs for top commit:
  apoelstra:
    ACK 962abcc963
  sanket1729:
    ACK 962abcc963. This has been open for a long time. Merging this in the interest of progress.

Tree-SHA512: e34e48e1c56fab5898bc74e7fb867435ed387d828dd3daf0c7d6df8f305e1da6883e91487115ac428618eb7d95bd16aa2cd209ca219684959bc95587ef0b4083
2022-12-01 15:29:12 -08:00
Tobin C. Harding 30888f74c5 Move psbt module to crate root module
Move the `psbt` module out of `util` and into the crate root module.
Done as part of an effort to clean up `util`.
2022-11-16 10:43:35 +11:00
Martin Habovstiak d6ca7e4b9f Add a test parsing transaction with a huge witness
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
2022-11-01 14:27:38 +01:00
Tobin Harding 962abcc963 Add serde regression tests
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.
2022-10-29 10:47:16 +11:00
DanGould b8bd31d5a8
Promote rust-miniscript finalizer 2022-10-27 10:40:45 -04:00
DanGould 16bf6f68dd
Test PSBT integration
Implement Test Vectors from BIP 174

Co-authored-by: Tobin Harding <me@tobin.cc>
Co-authored-by: Armin Sabouri <armins88@gmail.com>
2022-10-27 10:40:45 -04:00
Tobin Harding 72935a0f6e
Move test_data/* tests/data
In preparation for adding integration tests in the standard Rust
`tests/` directroy; move the contents of `test_data` to `tests/data`.
2022-10-27 10:40:44 -04:00