Commit Graph

129 Commits

Author SHA1 Message Date
Tobin C. Harding dca0d67771 Fix in preparation for next edition
Use cargo to upgrade from edition 2015 to edition 2018.

 cargo fix --edition

No manual changes made. The result of the command above is just to fix
all the use statements (add `crate::`) and fix the fully qualified path
formats i.e., `::Foo` -> `crate::Foo`.
2022-05-11 10:16:17 +10:00
Andrew Poelstra 0d5565e131
Merge rust-bitcoin/rust-bitcoin#839: feat: Add Address.is_related_to_*_key()
51fef76129 feat: Add Address.is_related_to_pubkey() (Andrew Ahlers)

Pull request description:

  ## Motivation

  This is addressing the second half of this comment: https://github.com/rust-bitcoin/rust-bitcoin/pull/684#issuecomment-1012136845

  > but would accept a PR (or two PRs) that returns Result<bool, UnsupportedAddress> and a method to check if a PublicKey is associated with an address.

  (The first half was addressed [here](https://github.com/rust-bitcoin/rust-bitcoin/pull/819))

  These changes will help build out and improve message signature verification. We don't necessarily need to add it to this crate but it allows for easy verification with something such as:
  1. recovering a pubkey
  2. checking if that pubkey relates to the given address

  ## Possible Improvements

  - There is likely a better name than `is_related_to_secp256k1_key()`
  - This could drop the `secp256k1` part of the name and take in a Pubkey enum that also supports Schnorr pubkeys and then this could be used for taproot addresses as well. This felt like a much larger change that will likely get turned down. Verifying taproot is simple enough and if absolutely desired, similar functions can be added for schnorr keys (tweaked and untweaked)

ACKs for top commit:
  Kixunil:
    ACK 51fef76129 for merging after TR
  apoelstra:
    ACK 51fef76129

Tree-SHA512: c9ab8c0f101fb4c647713e7f500656617025d8741676e8eb8a3132009dde9937d50cf9ac3d8055feb14452324a292397e46639cbaca71cac77af4b06dc42d09d
2022-03-28 17:29:48 +00: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 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
Andrew Ahlers 51fef76129 feat: Add Address.is_related_to_pubkey() 2022-03-06 20:30:22 +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
Dr Maxim Orlovsky b9170162d5 Re-org keys and ecdsa mods - pt.1
This commit tries to achieve separation of signature- and key-related types, previously mixed in a single ECDSA module.

Rationale: bitcoin key types are not specific for signature algorithm.

This is achieved through
- Remove key mod with its content moved to ecdsa mod
- Re-export keys under key module in util mod - to make git generate diff for the rename of ecdsa mod in the next commit correctly.
2022-01-14 01:35:48 +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
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
Marko Bencun 506e03fa4d
util/address: use hash functions of PublicKey/Script
Simpler code, less duplication.
2021-12-12 13:11:15 +01:00
Marko Bencun f826316c25
util/address: avoid .expect/panic 2021-12-12 13:11:15 +01:00
Marko Bencun ad83f6ae00
util/address: make address encoding more modular
This allow library clients to plug their own encoding parameters in a
backwards compatible manner.
2021-12-12 13:10:48 +01:00
Tobin Harding a6d3514f2b Return parity when doing tap_tweak
Currently we calculate the parity during `tap_tweak` but do not return
it, this means others must re-do work done inside `tap_tweak` in order
to calculate the parity. We can just return the parity along with the
tweaked key.
2021-12-10 11:45:58 +11:00
Noah 5b21a9cb1f Use TapTweakHash method for computing tweak 2021-11-25 09:45:27 +11:00
Dr Maxim Orlovsky d614b6c759
Merge rust-bitcoin/rust-bitcoin#704: util/address: Improve docs
822c99222d Improve constructor rustdocs for Address (Tobin Harding)
804a38cb67 Improve documentation of `WitnessVersion` (Tobin Harding)
eb8278fd2e util/address: Improve docs (Tobin Harding)

Pull request description:

  Improve documentation of the `address` module by doing:

  - Add full stops to all sentences
  - Use code ticks even inside links e.g., [`WitnessVersion`]
  - Use 100 character line length
  - Do grammar fixes
  - Use comment sections (e.g. `# Returns`)
  - Use 3rd person for function comments e.g. 'Converts foo to bar' instead of 'Convert foo to bar'
  - Use ticks for scriptPubkey

  This patch does a single file because a bunch of these changes pick an
  arbitrary stlye, if we can bikeshed on this PR then future PRs should be
  able to progress more quickly. I'll take lack of comment on any of the
  above as approval and I'll attempt to be uniform when doing the rest of
  the codebase. I plan on just chipping away at this, I can only do so
  much docs work in a day without getting bored of it :)

  Notes:

  - I didn't touch 'segwit' vs 'SegWit', seems both are widely used.
  - Using ticks inside links may be an overkill but seems more correct?
  - I'm not totally sure where the line is in the Rust ecosystem between
    readability in an editor and rendering as HTML, open to input on this.

ACKs for top commit:
  Kixunil:
    ACK 822c99222d
  dr-orlovsky:
    ACK 822c99222d

Tree-SHA512: bfbaeec74803dd0704ed3e39b9a4966db34dbb3d7ea850ed6230abf220b877687ac1479f4940b7bf39d7e8172cd62c36b232bfaa8186a92cc58b3d7e642674f6
2021-11-23 18:00:55 +01:00
sanket1729 e4774e74eb fixups to taptweaking code 2021-11-21 11:53:55 -08:00
Tobin Harding 822c99222d Improve constructor rustdocs for Address
Improve the rustdocs for the various `Address` constructors by putting
the brief description on a separate line with further description in its
own paragraph. This is the layout best practice for function documentation
using rustdocs.

Also, favour 'creates' over 'constructs' because it is more common in
the docs of this struct.
2021-11-19 09:42:19 +11:00
Tobin Harding 804a38cb67 Improve documentation of `WitnessVersion`
Attempt to improve the rustdocs for `WitnessVersion` in line with
review comments from a previous patch.
2021-11-19 09:35:14 +11:00
Tobin Harding eb8278fd2e util/address: Improve docs
Improve documentation of the `address` module by doing:

- Add full stops to all sentences
- Use code ticks even inside links e.g., [`WitnessVersion`]
- Use 100 character line length
- Do grammar fixes
- Use comment sections (e.g. `# Returns`)
- Use 3rd person for function comments e.g. 'Converts foo to bar' instead of 'Convert foo to bar'
- Use ticks for scriptPubkey

This patch does a single file because a bunch of these changes pick an
arbitrary stlye, if we can bikeshed on this PR then future PRs should be
able to progress more quickly. I'll take lack of comment on any of the
above as approval and I'll attempt to be uniform when doing the rest of
the codebase. I plan on just chipping away at this, I can only do so
much docs work in a day without getting bored of it :)

Notes:

- I didn't touch 'segwit' vs 'SegWit', seems both are widely used.
- Using ticks inside links may be an overkill but seems more correct?
- I'm not totally sure where the line is in the Rust ecosystem between
  readability in an editor and rendering as HTML, open to input on this.
2021-11-18 09:43:08 +11:00
sanket1729 e66a94fa40
Merge rust-bitcoin/rust-bitcoin#683: tests: improve coverage for P2tr and AddressType
0d463ec19e tests: improve coverage for P2tr and AddressType (Leonardo Comandini)

Pull request description:

  The new AddressType test shows addresses that are valid but have
  no type. If in the future some of those get a type or become
  invalid (either voluntarily or due to a regression), this will
  highlight it.

ACKs for top commit:
  dr-orlovsky:
    utACK 0d463ec19e
  sanket1729:
    ACK 0d463ec19e

Tree-SHA512: 9e062a1807173638cb62a61a2e8ea5be8324449a8944c356073e8bd9f53941dea369c65a35dfa0019bd8323eaa5dd26a9907c1823522fef9a524e919728973a6
2021-11-16 08:49:13 -08:00
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
Leonardo Comandini 0d463ec19e
tests: improve coverage for P2tr and AddressType
The new AddressType test shows addresses that are valid but have
no type. If in the future some of those get a type or become
invalid (either voluntarily or due to a regression), this will
highlight it.
2021-11-15 21:36:13 +01:00
Marko Bencun 8b1dbf5c9f
util/address: remove unused generic type 2021-11-14 13:10:40 +01:00
Noah Lanson 803b5fed8a P2TR address from untweaked public key
Ambiguous TweakedPublicKey and UntweakedPublicKey type aliases and methods to convert

Use structs for Untweaked and Tweaked key type

swap dangerous api to work on tweaked keys

remove unecessary allocations and rename methods

Use type alias for UntweakedPublicKey

TweakedPublicKey::new(...) method added

minor naming and doc changes
2021-11-11 14:36:56 +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
Dr Maxim Orlovsky c1991d748f
Improving error information for address parser 2021-09-21 12:45:17 +02:00
Clark Moody eeeb722155
Bump bech32 to 0.8.0 and use BIP-0350 Bech32m checksum
Replace BIP-0173 test vectors with those in BIP-0350.
2021-09-21 12:45:17 +02:00
Dr Maxim Orlovsky 5573a546ca
Taproot P2TR address 2021-09-21 12:45:17 +02:00
Vikas S Shetty 48c732e934 Changes for checking script size and returning Error appropriately 2021-09-15 16:16:20 +05:30
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 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 8ae030b951
Merge pull request #618 from elsirion/possible_networks 2021-08-09 23:25: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
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
elsirion 94229ae964 Implement is_valid_for_network function for Address 2021-06-26 19:34:33 +02:00
Andrew Poelstra abff973e83
Merge pull request #601 from LNP-BP/feat/bech32m-1
Bech32m adoption
2021-06-18 21:14:12 +00:00
Dr Maxim Orlovsky aa5c36df12
Covering all BIP-173 and BIP-350 test vectors 2021-06-16 08:43:18 +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 9613181601
Split invalid version for address and extended key, saving also 16 bytes on the stack 2021-06-13 22:39:32 +02:00
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02:00
Dr Maxim Orlovsky c1fae03686
Non-API breaking Bech32m adoption 2021-06-08 08:43:39 +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
Andrew Poelstra e5f37b817d
Merge pull request #581 from RCasatta/qr_string
Address to optimized QR string
2021-05-06 17:12:17 +00:00
Dr Maxim Orlovsky c0b7defc86
More non-API breaking error derives depending on upstream PRs 2021-05-04 03:58:56 +02:00
Dr Maxim Orlovsky 18b6bd0d15
Adopting new ECDSA key constructors throughout the library 2021-05-01 13:34:31 +02:00
Riccardo Casatta 0a91496570
rename to_qr_string into to_qr_uri returning also the schema 2021-04-15 10:40:57 +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
Riccardo Casatta 3158cedea0
document alternate formatting 2021-04-11 18:14:28 +02:00