Commit Graph

1109 Commits

Author SHA1 Message Date
Martin Habovstiak 4f1200d629 Added `amount::Display` - configurable formatting
This significatnly refactors the amount formatting code to make
formatting more configurable. The main addition is the
`amount::Display` type which is a builder that can configure
denomination or other things (possibly more in the future).

Further, this makes all representations of numbers minimal by default,
so should be documented as a possibly-breaking change.

Because of the effort to support all other `fmt::Formatter` options this
required practically complete rewrite of `fmt_satoshi_in`. As a
byproduct I took the opportunity of removing one allocation from there.

Closes #709
2022-03-14 19:03:03 +01:00
Andrew Poelstra cb35766979
Merge rust-bitcoin/rust-bitcoin#808: Refactor logical operators
df7bb03a67 Simplify read_scriptbool (Tobin Harding)
4b6e86658d Refactor is_provably_unspendable (Tobin Harding)
e54a2d653b Put && operator at front of line (Tobin Harding)
f5512c4931 Refactor is_p2pkh (Tobin Harding)
373ea89a9a Simplify read_scriptbool (Tobin Harding)
654b2772b8 Add passing unit tests for read_scriptbool (Tobin Harding)

Pull request description:

  In an effort to make the code clearer and more explicit, do various refactorings around logical operators. Each done as a separate patch to ease review and limit scope of discussion.

  Based on review of https://github.com/rust-bitcoin/rust-bitcoin/pull/806

ACKs for top commit:
  Kixunil:
    ACK df7bb03a67
  apoelstra:
    ACK df7bb03a67

Tree-SHA512: 06460979d492eb38cefc147397338b7fd95320c66ce8e8b4f8e2b454bb35721ce308413690a0618bd19d695df56175646d4d0c619388c0268f7fd35d5a7b6a3d
2022-01-26 13:14:16 +00:00
Tobin Harding df7bb03a67 Simplify read_scriptbool
Simplify `read_scriptbool` by doing:

- Use `split_last` to get at the last element
- Mask the last byte against ^0x80 instead of using two equality
  statements
2022-01-26 16:52:41 +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
Tobin Harding 4b6e86658d Refactor is_provably_unspendable
Refactor with the aim of making the code easier to read. Code path is
covered by current unit tests.

Refactor only, no logic changes.
2022-01-25 10:21:06 +11:00
Tobin Harding e54a2d653b Put && operator at front of line
In an effort to make code containing multi-line logical AND clearer to
read put the operator at the start of the line.
2022-01-25 10:21:06 +11:00
Tobin Harding f5512c4931 Refactor is_p2pkh
Refactor with the aim of simplifying `is_p2kh`. This function is covered
sufficiently by current unit tests.

Refactor only, no logic changes.
2022-01-25 10:16:56 +11:00
Tobin Harding 373ea89a9a Simplify read_scriptbool
Refactor and simplify the logical operators in `read_scriptbool`.

Refactor only, no logic changes.
2022-01-25 10:16:56 +11:00
Tobin Harding 654b2772b8 Add passing unit tests for read_scriptbool
In preparation for refactoring `read_scriptbool` add passing unit
tests.
2022-01-25 10:16:54 +11: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
Casey Rodarmor 94dd57de12 Add subsidy halving interval constant 2022-01-20 08:52:46 -08:00
Andrew Poelstra b327386cc8
Merge rust-bitcoin/rust-bitcoin#799: Use 4 character indentation
8d780e8172 Use 4 character indentation (Tobin Harding)

Pull request description:

  Bizarrely the impl block for `EcdsaSigHashType` uses 5 character indentation.

  Use 4 character indentation as is typical.

ACKs for top commit:
  Kixunil:
    ACK 8d780e8172
  apoelstra:
    ACK 8d780e8172

Tree-SHA512: 4b0789b4ef6f3e817fcbe002ca0d9ee4fca4a3dc70bbeaebae745a2f0497eb072f3c3876805d97ff58eb8a9464ea4443f23cea0c95bfd052e382caa9f76b515d
2022-01-19 18:57:48 +00:00
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
Tobin Harding 8d780e8172 Use 4 character indentation
Bizarrely the impl block for `EcdsaSigHashType` uses 5 character
indentation.

Use 4 character indentation as is typical.
2022-01-19 14:30:36 +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 6cca59296e
Merge rust-bitcoin/rust-bitcoin#791: Remove trailing spaces from 'address.rs'
4284c2be3b Remove trailing spaces from 'address.rs' (Roman Zeyde)

Pull request description:

ACKs for top commit:
  dr-orlovsky:
    ACK 4284c2be3b
  Kixunil:
    ACK 4284c2be3b

Tree-SHA512: 62fb1ea934ed0e089766db84d1a9a218a922272555d89081f5e47ac3a9d236474c8218e1873251961a9562405817e16f2f1ea84731f330813b21898015c1cabd
2022-01-17 12:15:48 +02: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
Roman Zeyde 4284c2be3b Remove trailing spaces from 'address.rs' 2022-01-17 10:03:17 +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 093f8b612d
Merge rust-bitcoin/rust-bitcoin#752: Make Map trait private
dfd8924398 Remove insert_pair from Map trait (Tobin Harding)
ad75d5181f Make Map trait private to psbt module (Tobin Harding)
53225c0a6e Improve docs in map module (Tobin Harding)
92059c2841 Add full stops to rustdocs (Tobin Harding)
11c046b707 Refactor match arms (Tobin Harding)
e6af569490 Move imports to top of file (Tobin Harding)

Pull request description:

  The `Map` method `insert_pair` is never called for `PartiallySignedTransaction`. Separate the method into its own trait (`Insert`) and delete dead code. The dead code contains the alleged bug in #576.

  - Patch 1: Preparatory cleanup
  - Patch 2: Preparatory refactor
  - Patch 3 and 4: Improve docs in the module that this PR touches
  - Patch 5: Make `Map` trait private to the `psbt` module
  - ~Patch 6: Make `concensus_decode_global` method into a function~
  - Patch ~7~ 6: Pull `insert_pair` method out of `Map` trait into newly create `Insert` trait

  Resolves: https://github.com/rust-bitcoin/rust-bitcoin/issues/576

  (Title of PR is `Make Map trait private` because that is the API break.)

ACKs for top commit:
  dr-orlovsky:
    ACK dfd8924398
  apoelstra:
    ACK dfd8924398

Tree-SHA512: 1a78294bc8a455552d93caf64db697f886345ba979f574abad55820415958fee1c2dd16945f4eafdbe542fa202cb7e08618aa137ec7ee22b3c9dac5df0328157
2022-01-16 08:49:27 +05:30
sanket1729 abe52f681b Cleanup/Dedup psbt (De)Serialization code 2022-01-15 06:15:54 +05:30
sanket1729 fbd86dcf63 Update documentation of EcdsaSig::from_slice 2022-01-15 06:15:14 +05:30
sanket1729 85009a7b50 Update documentation of from_u32_consensus 2022-01-15 06:15:14 +05:30
sanket1729 0fed04e2d5 Change EcdsaSig hash type deser 2022-01-15 06:15:10 +05:30
sanket1729 d1f051c95a
Merge rust-bitcoin/rust-bitcoin#757: Minimally-invasive separation of bitcoin keys from ECDSA signature types
8a993e8a58 Properly deprecate util::ecdsa key re-exports (Dr Maxim Orlovsky)
bcb8932ccf Re-org keys and ecdsa mods - pt.3 (Dr Maxim Orlovsky)
d1c2213d3b Re-org keys and ecdsa mods - pt.2 (Dr Maxim Orlovsky)
b9170162d5 Re-org keys and ecdsa mods - pt.1 (Dr Maxim Orlovsky)
2d9de78725 Re-export all key types under `util::key`. Deprecate other exports. (Dr Maxim Orlovsky)

Pull request description:

  This PR tries to do a minimally-invazive separation of signature- and key-related types, previously mixed in a single `util::ecdsa` module.

  Rationale: bitcoin key types are not specific for signature algorithm. See discussion at #588.

  This PR became possible after we moved on new `secp256k1` version exposing `XonlyPublicKey` type, since now all key types may co-exist in a single module under different names

  The PR goal is achieved through
  - Renaming ecdsa mod into private ec module such that the code is not copied and diff size is small;
  - Introducing dummy ecdsa mod back in the next commit and re-exporiting only signature types from internal `ec` mod in it;
  - Re-exporting all key types under `key` module, removing previous depreciation message for bitcoin keys.

ACKs for top commit:
  apoelstra:
    ACK 8a993e8a58
  sanket1729:
    utACK 8a993e8a58

Tree-SHA512: 9f71edaa2cf4cdab4b239cb1d57576e2ba0fc3c2ec0ea19ae232005967b9400da6ded992b33d10b190ca617a66dca9b99be430bc5058a064f0be1489723c4a3a
2022-01-15 05:52:53 +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
KaFai Choi 8fef869c15
repalce unncessary extra closure with function pointer in starts_with_uppercase closure inside Denomination from_str 2022-01-14 21:07:43 +07:00
Dr. Maxim Orlovsky b165b8da05
Merge rust-bitcoin/rust-bitcoin#768: add nano and pico BTC to Denomination enum
40f38b3edc enforce strict SI(treat capital of m, u, n, p as invalid) in parsing amount denomiation. add disallow_unknown_denomination test (KaFai Choi)
e80de8b1ee add nano and pico BTC to Donomination enum (KaFai Choi)

Pull request description:

  Close [741](https://github.com/rust-bitcoin/rust-bitcoin/issues/741)

ACKs for top commit:
  Kixunil:
    ACK 40f38b3edc
  apoelstra:
    ACK 40f38b3edc
  dr-orlovsky:
    Changing review to ACK 40f38b3edc since it was my misunderstanding and not a bug

Tree-SHA512: 4cc380b8e7403e37e7993e25848b25d74c610d4e9fe274526c613d4b3e2a9f6677c7df52310fc1cab6f1d629d9529ff9f5a2efa41d9e07eab62d0989780ae3a4
2022-01-14 11:22:57 +02:00
Dr Maxim Orlovsky 8a993e8a58 Properly deprecate util::ecdsa key re-exports 2022-01-14 09:45:22 +01:00
Dr Maxim Orlovsky bcb8932ccf Re-org keys and ecdsa mods - pt.3 2022-01-14 01:35:48 +01:00
Dr Maxim Orlovsky d1c2213d3b Re-org keys and ecdsa mods - pt.2 2022-01-14 01:35:48 +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 2d9de78725 Re-export all key types under `util::key`. Deprecate other exports. 2022-01-14 01:35:48 +01: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
Dr Maxim Orlovsky 62a27a51e2 Document that serde impl of LeafVersion uses u8 in consensus encoding
Closes #764
2022-01-13 17:53:50 +01:00
Dr Maxim Orlovsky 73e6ce4e53 Re-export Witness at crate level. Closes #770 2022-01-13 17:51:01 +01:00
Dr Maxim Orlovsky 6364ebd927 Code style fixups to taproot key functions 2022-01-13 17:48:13 +01:00
Dr Maxim Orlovsky 7514f2ca18 Tweaked -> untweaked keys conversions 2022-01-13 17:40:27 +01:00
Andrew Poelstra 907b3a7a6a
Merge rust-bitcoin/rust-bitcoin#775: Issue #394 - Refactor Block::merkle_root()
cd2435c807 Change deprecated version to 0.28. Remove redundant Block::merkle_root() logic. (Nils Loewen)
05788285f5 Issue #394 - Refactor Block::merkle_root() to Block::compute_merkle_root() and deprecate Block::merkle_root(). (Nils Loewen)

Pull request description:

  Refactor `Block::merkle_root()` to `Block::compute_merkle_root()` and deprecate `Block::merkle_root()`.

ACKs for top commit:
  apoelstra:
    ACK cd2435c807
  Kixunil:
    ACK cd2435c807

Tree-SHA512: 820d85d5a25b7316046d5df8e3ab1e8cd57f72c7fa63f0d6826b965c5da3ef1bfacd4704180810aa8c0a7a224dcd62f398a2cf93fc344e2b0d52d9d7024c6c27
2022-01-13 14:32:30 +00:00
KaFai Choi 40f38b3edc
enforce strict SI(treat capital of m, u, n, p as invalid) in parsing amount denomiation. add disallow_unknown_denomination test 2022-01-13 20:27:41 +07:00