simplify proof serde

This commit is contained in:
wiesche
2026-09-12 17:17:51 +02:00
parent d2c1070f8f
commit f645f46a69
4 changed files with 16 additions and 14 deletions
+6 -1
View File
@@ -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<PaymentMemo>,
@@ -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::<ProofType>(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::<ProofType>(invalid.clone()).is_err());
json["proof_type"] = invalid;
assert!(serde_json::from_value::<ProofArgs>(json.clone()).is_err());
}
-1
View File
@@ -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")]
+10 -11
View File
@@ -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<u8> for PaymentProofType {
}
}
pub(crate) mod payment_proof_type_serde {
use super::PaymentProofType;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(proof_type: &PaymentProofType, serializer: S) -> Result<S::Ok, S::Error>
impl serde::Serialize for PaymentProofType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<PaymentProofType, D::Error>
impl<'de> serde::Deserialize<'de> for PaymentProofType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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)
}
}
-1
View File
@@ -196,7 +196,6 @@ fn default_part_sig_none() -> Option<Signature> {
#[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")]