Commit Graph

116 Commits

Author SHA1 Message Date
Tobin C. Harding 022730bd8d Add a workspace to the top level directory.
Create a directory `bitcoin` and move into it the following as is with
no code changes:

- src
- Cargo.toml
- contrib
- test_data
- examples

Then do:

- Add a workspace to the repository root directory.
- Add the newly created `bitcoin` crate to the workspace.
- Exclude `fuzz` and `embedded` crates from the workspace.
- Add a contrib/test.sh script that runs contrib/test.sh in each
  sub-crate
- Fix the bitcoin/contrib/test.sh script
2022-09-13 08:44:57 +10:00
Tobin C. Harding 4c4846f82a Remove double blank line at EOF
There should only be a single blank line at end of file.
2022-09-05 10:48:47 +10:00
Tobin C. Harding 0786d92a5c Take self by value
Turns out by default clippy does not lint certain parts of the public
API. Specifically, by setting

  avoid-breaking-exported-api = false

we can get clippy to lint the public API for various things including
`wrong_self_convention`.

Clippy emits:

 warning: methods with the following characteristics: (`to_*` and `self`
 type is `Copy`) usually take `self` by value

As suggested, take `self` by value for methods named `to_*`.
2022-08-02 08:54:53 +10:00
Tobin C. Harding 21a1cc791c Use pub(crate) for macros instead of macro_use
For internal macros used only in this crate we do not need to use
`macro_use` and pollute the top level namespace now that we have edition
2018. We can add a `pub(crate) use` statement to each and then path
imports work for the macros like normal types.
2022-07-20 11:25:54 +10:00
Andrew Poelstra 0c22359618
Merge rust-bitcoin/rust-bitcoin#1001: Remove leading colons
73bc2bb058 Remove leading colons from ::core::cmp::Ordering (Tobin C. Harding)
bffe0e840d Remove _most_ leading double colons (Tobin C. Harding)

Pull request description:

  Leading double colons are a relic of edition 2015. Attempt to remove _all_ leading double colons (assuming I didn't miss any).

  - Patch 1 is done mechanically so it can be repeated by reviewers, just search-and-replace ' ::' with '::' (note the leading space).
  - Patch 2 does a single other instance of leading `::`

ACKs for top commit:
  apoelstra:
    ACK 73bc2bb058
  sanket1729:
    utACK 73bc2bb058.

Tree-SHA512: 8f7aafdda1aed5b69dcc83f544e65085dfec6590765839f0a6f259b99872805d1f00602fd9deac05c80a5cac3bc222d454dde0117dff4484e5cd34ea930fdfa1
2022-07-19 01:39:04 +00:00
Andrew Poelstra 4965495354
Merge rust-bitcoin/rust-bitcoin#1066: Upgrade to secp256k1 v0.23.0
36f29d4357 Upgrade to secp256k1 v0.23.0 (Tobin C. Harding)

Pull request description:

  We recently released a new version of `rust-secp256k1`, upgrade to use it.

ACKs for top commit:
  apoelstra:
    ACK 36f29d4357
  Kixunil:
    ACK 36f29d4357

Tree-SHA512: 46a909dec8bc59daa78acdb76824d93f4f1da0e9736cf6ca443d3bbadfa43867e720293bb7c4919cb0658e75ec59daeffea080611f0e7eed4df439ddac0305de
2022-07-12 14:03:49 +00:00
Tobin C. Harding 36f29d4357 Upgrade to secp256k1 v0.23.0
We recently released a new version of `rust-secp256k1`, upgrade to use
it.
2022-07-12 09:22:55 +10:00
Tobin C. Harding bffe0e840d Remove _most_ leading double colons
Leading double colons are a relic of edition 2015. Remove all leading
double colons that follow a space, done like this so that reviewers can
do the same and verify the diff. Done with

search-and-replace ' ::' '::'

And, for the record:

```bash
function search-and-replace() {
    if (($# != 2))
    then
        echo "Usage: $0 <this> <that>"
        return
    fi

    local this="$1"
    local that="$2"

    for file in $(git grep -l "$this")
    do
        perl -pi -e "s/$this/$that/g" "$file"
    done
}
```
2022-07-11 15:20:05 +10:00
Tobin C. Harding 91ff2f628c Introduce SPDX license identifiers
When `rust-bitcoin` was started in 2014 the SPDX license list and short
identifiers where not a thing. Now that we have short identifiers and
they are gaining popularity in other projects we can consider using
them.

- Add links to the SPDX website in the readme
- Shorten the author section to a single line
- Remove all the licence information in each file and replace it with an
SPDX ID (see https://spdx.dev/ids/#how)

Of note:

- If the author of a file is explicitly listed, maintain this
information
- If the 'author' is listed as the generic 'Rust Bitcoin developers'
just remove the attribution, this is implicit. This does loose the date
info but that can be seen at any time from the git index using

  `git log --follow --format=%ad --date default <FILE> | tail -1`
2022-06-29 14:12:02 +10:00
sanket1729 165cae959a
Merge rust-bitcoin/rust-bitcoin#1003: Improve error `Display` implementations
57dd6739c3 Do not print error when displaying for std builds (Tobin C. Harding)
b80cfeed85 Bind to error_kind instead of e (Tobin C. Harding)
241ec72497 Bind to b instead of e (Tobin C. Harding)
01f481bf5c Bind to s instead of e (Tobin C. Harding)
5c6d369289 network: Remove unused error variants (Tobin C. Harding)
e67e97bb37 Put From impl below std::error::Error impl (Tobin C. Harding)
6ca98e5275 Remove error TODO (Tobin C. Harding)

Pull request description:

  As part of the ongoing error improvement work and as a direct result of [this comment](https://github.com/rust-bitcoin/rust-bitcoin/pull/987#issuecomment-1135563287) improve the `Display` implementations of all our error types so as to not repeat the source error when printing.

  The first 5 patches are trivial clean ups around the errors. Patch 6 is the real work.

  EDIT: ~CC @Kixunil, have I got the right idea here bro?~ Patch 6 now includes a macro as suggested.

ACKs for top commit:
  Kixunil:
    ACK 57dd6739c3
  apoelstra:
    ACK 57dd6739c3
  sanket1729:
    ACK 57dd6739c3. Did not check if we covered all cases. We need to remember to use `write_err!` instead of `write!` in future.

Tree-SHA512: 1ed26b0cc5f9a0f71684c431cbb9f94404c116c9136be696434c56a2f56fd93cb5406b0955edbd0dc6f8612e77345c93fa70a70650118968cc58e680333a41de
2022-06-01 15:17:24 -07:00
Andrew Poelstra 50489c8d5e
Merge rust-bitcoin/rust-bitcoin#1026: Add non_exhaustive to all error enums
99f565f932 Add non_exhaustive to all error enums (Tobin C. Harding)

Pull request description:

  Adding an error variant to a public enum is an API breaking change, this means making, what could be, small refactorings or improvements harder. If we use `non_exhaustive` for error types then we mitigate this cost.

  There is a tradeoff however, downstream users who explicitly match on our public error types must include a wildcard pattern.

ACKs for top commit:
  apoelstra:
    ACK 99f565f932
  Kixunil:
    ACK 99f565f932

Tree-SHA512: ff329f87d52b3fbe24654f32e4062ddae73173cba5a13d511591158e68ee278e9bdc0a70a3e0b42d6606b369255923f9c46d8b3d1b2ff75f8461a82567df80cd
2022-06-01 16:22:44 +00:00
Tobin C. Harding 99f565f932 Add non_exhaustive to all error enums
Adding an error variant to a public enum is an API breaking change, this
means making what could be small refactorings or improvements harder. If
we use `non_exhaustive` for error types then we mitigate this cost.
There is a tradeoff however, downstream users who explicitly match on
our public error types must include a wildcard pattern.
2022-05-31 14:29:50 +10:00
Tobin C. Harding 57dd6739c3 Do not print error when displaying for std builds
We implement `source` for all our error types. This means that we should
not display the source error explicitly because users can call `source`
to get the source error.

However, `std::Error::source()` is only available for "std" builds, so
that we do not loose the error source information in "no-std" builds add
a macro that conditionally adds the source onto the error message.
2022-05-27 08:38:52 +10:00
Tobin C. Harding 1940b00132 Implement From instead of Into
Implementing `From` gives us an implementation of `Into` for free so is
therefore superior.

Found by Clippy.
2022-05-25 13:19:17 +10:00
Tobin C. Harding 97a5bb1439 Implement std::error::source codebase wide
Audit ever error type we have and implement `source` for each.
2022-05-19 16:35:11 +10:00
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
mcroad 7854bd7918
Fix `no_std` MSRV
Fixes #690, #947
2022-04-25 11:14:41 -05:00
sanket1729 ea80e6568a
Merge rust-bitcoin/rust-bitcoin#805: Remove impl_index_newtype macro
63e36fe6b4 Remove impl_index_newtype macro (Tobin Harding)

Pull request description:

  This macro is no longer needed since we bumped MSRV to 1.29.

  ~We can implement `SliceIndex` to get the `Index` implementations.~
  We can implement `core::ops::Index` directly since all the inner types implement `Index` already.

  Original ~Idea shamelessly stolen from @elichai [in this comment](https://github.com/rust-bitcoin/rust-bitcoin/issues/352#issuecomment-560331856).~

  New idea proposed by @Kixunil during review below. Thanks.

ACKs for top commit:
  apoelstra:
    ACK 63e36fe6b4
  dr-orlovsky:
    utACK 63e36fe6b4
  sanket1729:
    ACK 63e36fe6b4

Tree-SHA512: f7b4555c7fd9a2d458dcd53ec8caece0d12f3af77a10e850f35201bd7a580ba8fd7cb1d47a7f78ba6582e777dffa13416916ecacac6e0e874bdbb1c866132dc2
2022-03-23 19:01:56 -07: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 63e36fe6b4 Remove impl_index_newtype macro
This macro is no longer needed since we bumped MSRV to 1.29.

We can implement `core::ops::Index` directly since all the inner types
implement `Index` already.
2022-03-17 08:12:09 +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 b021415a88 Use block stlye function call
This function uses neither "Block" nor "Visual" style (as defined by
`rustfmt`). This is unusual, code that is regular is less jarring to
read. We tent to use "Block" style for functions so elect to do that
here.
2022-03-14 13:52:13 +11:00
Tobin Harding 39ec59620d Fix unusual indentation
We have a few instances of strange indentation:

- Incorrect number of characters
- Usage of neither "Block" style or "View" style (elect to use "Block")
2022-03-14 13:52:13 +11:00
Noah Lanson c0d36efb8b Don't allow uncompressed public keys without prefix 0x04 2022-02-17 08:46:20 +11: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
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
Dr Maxim Orlovsky eb09019720 Rename inner key field in PrivateKey and PublicKey
Closes #532
2022-01-11 08:39:52 +01:00
Dr Maxim Orlovsky b65a6ae49b Test for extended private key keypair generation f5875a 2022-01-09 07:17:06 +01: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
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
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 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 c26b3b9679
remove unused RngError, saving 24 bytes 2021-06-13 22:40:55 +02:00
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02:00
Sebastian Geisler 091ac89440 Fix semver breaking Display change of ChildNumber
Fixes #608. In #567 the Display impl for ChildNumber was
consciously changed, assuming the semver break would not
affect any correctly implemented downstream projects. We
were wrong.
2021-06-08 15:33:02 +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 d0fb626baf
Merge pull request #592 from LNP-BP/feat/ecdsa-key-creation
Constructors for compressed and uncompressed ECDSA keys
2021-05-06 15:50:06 +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
Dr Maxim Orlovsky 187eae8a13
Streamlining private key construction API in BIP32 2021-05-01 13:40:21 +02:00
Dr Maxim Orlovsky 18b6bd0d15
Adopting new ECDSA key constructors throughout the library 2021-05-01 13:34:31 +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
Dr Maxim Orlovsky 017cd71ca7
Improving bip32 ChildNumber display implementation 2021-02-11 23:06:24 +01:00
Steven Roose d527a2c2cf Remove the impl_array_newtype_show macro
Instead, implement Debug in impl_bytes_newtype.
2020-12-30 16:32:52 +01:00
Dr Maxim Orlovsky 44ffddab8c
Impl IntoDerivationPath for string types 2020-12-20 23:39:01 +01:00