Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -499,8 +498,9 @@ impl std::error::Error for Error {
| TapTree(_)
| PartialDataConsumption
| InvalidScanKey { .. }
| InvalidEcdhShare { .. }
| InvalidDleqProof { .. } => None,
| InvalidEcdhShare { .. } => None,
#[cfg(feature = "silent-payments")]
InvalidDleqProof { .. } => None,
}
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/v2/dleq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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[..]))
Expand All @@ -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<D>(deserializer: D) -> Result<Self, D::Error>
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 {
Expand All @@ -44,7 +44,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: actual_serde::de::Error,
E: serde::de::Error,
{
use bitcoin::hex::FromHex;
let vec = Vec::<u8>::from_hex(s).map_err(E::custom)?;
Expand All @@ -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 {
Expand All @@ -65,7 +65,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
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))
Expand All @@ -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 }
}
Expand Down Expand Up @@ -115,9 +112,11 @@ impl Serialize for DleqProof {

impl Deserialize for DleqProof {
fn deserialize(bytes: &[u8]) -> Result<Self, crate::serialize::Error> {
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,
})
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bip375-parse-invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/bip375-parse-valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down