Merge rust-bitcoin/rust-bitcoin#3672: units: Remove serde from amounts

77085a1fa1 units: Remove serde from amounts (Tobin C. Harding)

Pull request description:

  The `Amount` and `SignedAmount` were not supposed to implement `serde` traits by design because doing so implicitly uses sats. We provide two modules `as_sat` and `as_btc` to allow users to explicitly serialize in their preferred format.

  In commit: `d57ec019d5 Use Amount type for TxOut value field` derives were added for `serde` and we did not notice it during review.

ACKs for top commit:
  apoelstra:
    ACK 77085a1fa1f39e7358202e49189282e48a965747; successfully ran local tests

Tree-SHA512: ff4c1f34191e12d955e66737493af4b2dad761fa6f95eb334b86715906e70d2180e7f15d8b4af302d53fb0b1c8b5fb50b4924e9fcdee8824840ba555e73c1c17
This commit is contained in:
merge-script 2024-11-28 16:32:23 +00:00
commit 1f731e9162
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 38 additions and 9 deletions

View File

@ -1862,6 +1862,7 @@ mod tests {
#[serde(rename = "scriptPubKey")]
script_pubkey: ScriptBuf,
#[serde(rename = "amountSats")]
#[serde(with = "crate::amount::serde::as_sat")]
value: Amount,
}

View File

@ -343,6 +343,7 @@ impl TxIn {
#[cfg(feature = "alloc")]
pub struct TxOut {
/// The value of the output, in satoshis.
#[cfg_attr(feature = "serde", serde(with = "crate::amount::serde::as_sat"))]
pub value: Amount,
/// The script which must be satisfied for the output to be spent.
pub script_pubkey: ScriptBuf,

View File

@ -17,9 +17,9 @@ use super::{
/// A signed amount.
///
/// The [`SignedAmount`] type can be used to express Bitcoin amounts that support
/// arithmetic and conversion to various denominations.
///
/// The [`SignedAmount`] type can be used to express Bitcoin amounts that support arithmetic and
/// conversion to various denominations. The `Amount` type does not implement `serde` traits but we
/// do provide modules for serializing as satoshis or bitcoin.
///
/// Warning!
///
@ -29,6 +29,21 @@ use super::{
/// start with `checked_`. The operations from [`core::ops`] that [`Amount`]
/// implements will panic when overflow or underflow occurs.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "serde")] {
/// use serde::{Serialize, Deserialize};
/// use bitcoin_units::SignedAmount;
///
/// #[derive(Serialize, Deserialize)]
/// struct Foo {
/// // If you are using `rust-bitcoin` then `bitcoin::amount::serde::as_sat` also works.
/// #[serde(with = "bitcoin_units::amount::serde::as_sat")] // Also `serde::as_btc`.
/// amount: SignedAmount,
/// }
/// # }
/// ```
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SignedAmount(i64);

View File

@ -7,8 +7,6 @@ use alloc::string::{String, ToString};
use core::str::FromStr;
use core::{default, fmt, ops};
#[cfg(feature = "serde")]
use ::serde::{Deserialize, Serialize};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
@ -21,9 +19,9 @@ use crate::{FeeRate, Weight};
/// An amount.
///
/// The [`Amount`] type can be used to express Bitcoin amounts that support
/// arithmetic and conversion to various denominations.
///
/// The [`Amount`] type can be used to express Bitcoin amounts that support arithmetic and
/// conversion to various denominations. The `Amount` type does not implement `serde` traits but we
/// do provide modules for serializing as satoshis or bitcoin.
///
/// Warning!
///
@ -36,8 +34,22 @@ use crate::{FeeRate, Weight};
/// zero is considered an underflow and will cause a panic if you're not using
/// the checked arithmetic methods.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "serde")] {
/// use serde::{Serialize, Deserialize};
/// use bitcoin_units::Amount;
///
/// #[derive(Serialize, Deserialize)]
/// struct Foo {
/// // If you are using `rust-bitcoin` then `bitcoin::amount::serde::as_sat` also works.
/// #[serde(with = "bitcoin_units::amount::serde::as_sat")] // Also `serde::as_btc`.
/// amount: Amount,
/// }
/// # }
/// ```
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Amount(u64);
impl Amount {