diff --git a/src/serialize.rs b/src/serialize.rs index 63a7620..5ee5e67 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -24,6 +24,8 @@ use bitcoin::{ use crate::error::write_err; use crate::prelude::*; use crate::sighash_type::PsbtSighashType; +#[cfg(feature = "silent-payments")] +use crate::v2::dleq; use crate::version; /// A trait for serializing a value as raw data for insertion into PSBT @@ -428,12 +430,8 @@ pub enum Error { expected: usize, }, /// Invalid DLEQ proof for BIP-375 silent payments (expected 64 bytes). - InvalidDleqProof { - /// The length that was provided. - got: usize, - /// The expected length. - expected: usize, - }, + #[cfg(feature = "silent-payments")] + InvalidDleqProof(dleq::InvalidLengthError), } impl fmt::Display for Error { @@ -467,8 +465,9 @@ impl fmt::Display for Error { InvalidEcdhShare { got, expected } => { write!(f, "invalid ECDH share: got {} bytes, expected {}", got, expected) } - InvalidDleqProof { got, expected } => { - write!(f, "invalid DLEQ proof: got {} bytes, expected {}", got, expected) + #[cfg(feature = "silent-payments")] + InvalidDleqProof(e) => { + write!(f, "invalid DLEQ proof: got {} bytes, expected {}", e.got, e.expected) } } } @@ -499,8 +498,9 @@ impl std::error::Error for Error { | TapTree(_) | PartialDataConsumption | InvalidScanKey { .. } - | InvalidEcdhShare { .. } - | InvalidDleqProof { .. } => None, + | InvalidEcdhShare { .. } => None, + #[cfg(feature = "silent-payments")] + InvalidDleqProof { .. } => None, } } } diff --git a/src/v2/dleq.rs b/src/v2/dleq.rs index 36b03e1..0834f59 100644 --- a/src/v2/dleq.rs +++ b/src/v2/dleq.rs @@ -14,10 +14,10 @@ use crate::serialize::{Deserialize, Serialize}; pub struct DleqProof(pub [u8; 64]); #[cfg(feature = "serde")] -impl actual_serde::Serialize for DleqProof { +impl serde::Serialize for DleqProof { fn serialize(&self, serializer: S) -> Result where - S: actual_serde::Serializer, + S: serde::Serializer, { if serializer.is_human_readable() { serializer.serialize_str(&bitcoin::hex::DisplayHex::to_lower_hex_string(&self.0[..])) @@ -28,14 +28,14 @@ impl actual_serde::Serialize for DleqProof { } #[cfg(feature = "serde")] -impl<'de> actual_serde::Deserialize<'de> for DleqProof { +impl<'de> serde::Deserialize<'de> for DleqProof { fn deserialize(deserializer: D) -> Result where - D: actual_serde::Deserializer<'de>, + D: serde::Deserializer<'de>, { if deserializer.is_human_readable() { struct HexVisitor; - impl actual_serde::de::Visitor<'_> for HexVisitor { + impl serde::de::Visitor<'_> for HexVisitor { type Value = DleqProof; fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { @@ -44,7 +44,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof { fn visit_str(self, s: &str) -> Result where - E: actual_serde::de::Error, + E: serde::de::Error, { use bitcoin::hex::FromHex; let vec = Vec::::from_hex(s).map_err(E::custom)?; @@ -56,7 +56,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof { deserializer.deserialize_str(HexVisitor) } else { struct BytesVisitor; - impl actual_serde::de::Visitor<'_> for BytesVisitor { + impl serde::de::Visitor<'_> for BytesVisitor { type Value = DleqProof; fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { @@ -65,7 +65,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof { fn visit_bytes(self, v: &[u8]) -> Result where - E: actual_serde::de::Error, + E: serde::de::Error, { DleqProof::try_from(v).map_err(|e| { E::custom(format!("expected {} bytes, got {}", e.expected, e.got)) @@ -78,9 +78,6 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof { } impl DleqProof { - /// Creates a new [`DleqProof`] from a 64-byte array. - pub fn new(bytes: [u8; 64]) -> Self { DleqProof(bytes) } - /// Returns the inner 64-byte array. pub fn as_bytes(&self) -> &[u8; 64] { &self.0 } } @@ -115,9 +112,11 @@ impl Serialize for DleqProof { impl Deserialize for DleqProof { fn deserialize(bytes: &[u8]) -> Result { - DleqProof::try_from(bytes).map_err(|e| crate::serialize::Error::InvalidDleqProof { - got: e.got, - expected: e.expected, + DleqProof::try_from(bytes).map_err(|e| { + crate::serialize::Error::InvalidDleqProof(InvalidLengthError { + got: e.got, + expected: e.expected, + }) }) } } diff --git a/tests/bip375-parse-invalid.rs b/tests/bip375-parse-invalid.rs index 553a39e..3e743d5 100644 --- a/tests/bip375-parse-invalid.rs +++ b/tests/bip375-parse-invalid.rs @@ -16,7 +16,7 @@ fn bip375_global_field_mismatch_dleq_only() { // Approach 1: Programmatic let mut psbt = Creator::new().psbt(); let scan_key = CompressedPublicKey::from_slice(&[0x02u8; 33]).unwrap(); - let dleq_proof = DleqProof::new([0xAAu8; 64]); + let dleq_proof = DleqProof::from([0xAAu8; 64]); psbt.global.sp_dleq_proofs.insert(scan_key, dleq_proof); let bytes = psbt.serialize(); diff --git a/tests/bip375-parse-valid.rs b/tests/bip375-parse-valid.rs index c6a27e4..fc10d8a 100644 --- a/tests/bip375-parse-valid.rs +++ b/tests/bip375-parse-valid.rs @@ -21,7 +21,7 @@ fn valid_psbt_with_bip375_global_fields() -> Psbt { "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", ) .unwrap(); - let dleq_proof = DleqProof::new([0xAAu8; 64]); + let dleq_proof = DleqProof::from([0xAAu8; 64]); psbt.global.sp_ecdh_shares.insert(scan_key, ecdh_share); psbt.global.sp_dleq_proofs.insert(scan_key, dleq_proof);