From f645f46a69a76a0dc5a6311864dc7a9194fcc364 Mon Sep 17 00:00:00 2001 From: wiesche Date: Sat, 12 Sep 2026 17:17:51 +0200 Subject: [PATCH] simplify proof serde --- libwallet/src/contract/types.rs | 7 ++++++- libwallet/src/payment_proof.rs | 1 - libwallet/src/slate.rs | 21 ++++++++++----------- libwallet/src/slate_versions/v5.rs | 1 - 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/libwallet/src/contract/types.rs b/libwallet/src/contract/types.rs index fda442e..f2a39cb 100644 --- a/libwallet/src/contract/types.rs +++ b/libwallet/src/contract/types.rs @@ -95,7 +95,6 @@ pub struct ProofArgs { /// If net change is positive during this step, whether to suppress the creation of payment proof pub suppress_proof: bool, /// Requested early payment proof type - #[serde(with = "crate::slate::payment_proof_type_serde")] pub proof_type: ProofType, /// Memo used when this step creates the payment proof pub memo: Option, @@ -257,6 +256,11 @@ mod tests { (ProofType::Invoice, 1), (ProofType::SenderNonce, 2), ] { + assert_eq!(serde_json::to_value(proof_type).unwrap(), value); + assert_eq!( + serde_json::from_value::(serde_json::json!(value)).unwrap(), + proof_type + ); let args = ProofArgs { proof_type, ..Default::default() @@ -268,6 +272,7 @@ mod tests { } let mut json = serde_json::to_value(ProofArgs::default()).unwrap(); for invalid in [serde_json::json!(3), serde_json::json!("Invoice")] { + assert!(serde_json::from_value::(invalid.clone()).is_err()); json["proof_type"] = invalid; assert!(serde_json::from_value::(json.clone()).is_err()); } diff --git a/libwallet/src/payment_proof.rs b/libwallet/src/payment_proof.rs index 342ca90..ef6e26d 100644 --- a/libwallet/src/payment_proof.rs +++ b/libwallet/src/payment_proof.rs @@ -127,7 +127,6 @@ pub struct ProofWitness { #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct EarlyPaymentProof { /// Proof type - #[serde(with = "crate::slate::payment_proof_type_serde")] pub proof_type: PaymentProofType, /// amount #[serde(with = "secp_ser::string_or_u64")] diff --git a/libwallet/src/slate.rs b/libwallet/src/slate.rs index 7cbc929..ffcba0c 100644 --- a/libwallet/src/slate.rs +++ b/libwallet/src/slate.rs @@ -51,7 +51,7 @@ use crate::Context; /// Payment proof type from https://github.com/mimblewimble/grin-rfcs/pull/70 #[repr(u8)] -#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum PaymentProofType { /// Legacy payment proof Legacy = 0, @@ -95,23 +95,22 @@ impl TryFrom for PaymentProofType { } } -pub(crate) mod payment_proof_type_serde { - use super::PaymentProofType; - use serde::{Deserialize, Deserializer, Serializer}; - - pub fn serialize(proof_type: &PaymentProofType, serializer: S) -> Result +impl serde::Serialize for PaymentProofType { + fn serialize(&self, serializer: S) -> Result where - S: Serializer, + S: serde::Serializer, { - serializer.serialize_u8(proof_type.as_u8()) + serializer.serialize_u8(self.as_u8()) } +} - pub fn deserialize<'de, D>(deserializer: D) -> Result +impl<'de> serde::Deserialize<'de> for PaymentProofType { + fn deserialize(deserializer: D) -> Result where - D: Deserializer<'de>, + D: serde::Deserializer<'de>, { let value = u8::deserialize(deserializer)?; - PaymentProofType::try_from(value).map_err(serde::de::Error::custom) + Self::try_from(value).map_err(serde::de::Error::custom) } } diff --git a/libwallet/src/slate_versions/v5.rs b/libwallet/src/slate_versions/v5.rs index d30aba9..b621e03 100644 --- a/libwallet/src/slate_versions/v5.rs +++ b/libwallet/src/slate_versions/v5.rs @@ -196,7 +196,6 @@ fn default_part_sig_none() -> Option { #[serde_as] #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] pub struct PaymentInfoV5 { - #[serde(with = "crate::slate::payment_proof_type_serde")] pub ptype: PaymentProofType, #[serde(default)] #[serde(with = "ser::option_dalek_pubkey_serde")]