Skip to content

Commit d5a656d

Browse files
committed
Merge #49: BIP375 (PR #45) Follow Up
b36df3a Apply feedback from PR #45 - reuse InvalidLengthError for InvalidDleqProof - remove new creator for DleqProof (macgyver13) Pull request description: This PR addresses feedback received in PR #45. - reuse `InvalidLengthError` for `InvalidDleqProof` - remove `new` creator for `DleqProof` Also replace `actual_serde` with `serde` ACKs for top commit: tcharding: ACK b36df3a Tree-SHA512: fe07a51243e7c9a37747c2739811ef46595dec79f8735f96d40cec7f0b1594ac9e93ff90d5a55e767db4d9ba2c7a31cb389ee173b235f0a1a37e162c3619f58b
2 parents 46a877e + b36df3a commit d5a656d

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

src/serialize.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use bitcoin::{
2424
use crate::error::write_err;
2525
use crate::prelude::*;
2626
use crate::sighash_type::PsbtSighashType;
27+
#[cfg(feature = "silent-payments")]
28+
use crate::v2::dleq;
2729
use crate::version;
2830

2931
/// A trait for serializing a value as raw data for insertion into PSBT
@@ -428,12 +430,8 @@ pub enum Error {
428430
expected: usize,
429431
},
430432
/// Invalid DLEQ proof for BIP-375 silent payments (expected 64 bytes).
431-
InvalidDleqProof {
432-
/// The length that was provided.
433-
got: usize,
434-
/// The expected length.
435-
expected: usize,
436-
},
433+
#[cfg(feature = "silent-payments")]
434+
InvalidDleqProof(dleq::InvalidLengthError),
437435
}
438436

439437
impl fmt::Display for Error {
@@ -467,8 +465,9 @@ impl fmt::Display for Error {
467465
InvalidEcdhShare { got, expected } => {
468466
write!(f, "invalid ECDH share: got {} bytes, expected {}", got, expected)
469467
}
470-
InvalidDleqProof { got, expected } => {
471-
write!(f, "invalid DLEQ proof: got {} bytes, expected {}", got, expected)
468+
#[cfg(feature = "silent-payments")]
469+
InvalidDleqProof(e) => {
470+
write!(f, "invalid DLEQ proof: got {} bytes, expected {}", e.got, e.expected)
472471
}
473472
}
474473
}
@@ -499,8 +498,9 @@ impl std::error::Error for Error {
499498
| TapTree(_)
500499
| PartialDataConsumption
501500
| InvalidScanKey { .. }
502-
| InvalidEcdhShare { .. }
503-
| InvalidDleqProof { .. } => None,
501+
| InvalidEcdhShare { .. } => None,
502+
#[cfg(feature = "silent-payments")]
503+
InvalidDleqProof { .. } => None,
504504
}
505505
}
506506
}

src/v2/dleq.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use crate::serialize::{Deserialize, Serialize};
1414
pub struct DleqProof(pub [u8; 64]);
1515

1616
#[cfg(feature = "serde")]
17-
impl actual_serde::Serialize for DleqProof {
17+
impl serde::Serialize for DleqProof {
1818
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1919
where
20-
S: actual_serde::Serializer,
20+
S: serde::Serializer,
2121
{
2222
if serializer.is_human_readable() {
2323
serializer.serialize_str(&bitcoin::hex::DisplayHex::to_lower_hex_string(&self.0[..]))
@@ -28,14 +28,14 @@ impl actual_serde::Serialize for DleqProof {
2828
}
2929

3030
#[cfg(feature = "serde")]
31-
impl<'de> actual_serde::Deserialize<'de> for DleqProof {
31+
impl<'de> serde::Deserialize<'de> for DleqProof {
3232
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
3333
where
34-
D: actual_serde::Deserializer<'de>,
34+
D: serde::Deserializer<'de>,
3535
{
3636
if deserializer.is_human_readable() {
3737
struct HexVisitor;
38-
impl actual_serde::de::Visitor<'_> for HexVisitor {
38+
impl serde::de::Visitor<'_> for HexVisitor {
3939
type Value = DleqProof;
4040

4141
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
@@ -44,7 +44,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {
4444

4545
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
4646
where
47-
E: actual_serde::de::Error,
47+
E: serde::de::Error,
4848
{
4949
use bitcoin::hex::FromHex;
5050
let vec = Vec::<u8>::from_hex(s).map_err(E::custom)?;
@@ -56,7 +56,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {
5656
deserializer.deserialize_str(HexVisitor)
5757
} else {
5858
struct BytesVisitor;
59-
impl actual_serde::de::Visitor<'_> for BytesVisitor {
59+
impl serde::de::Visitor<'_> for BytesVisitor {
6060
type Value = DleqProof;
6161

6262
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
@@ -65,7 +65,7 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {
6565

6666
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
6767
where
68-
E: actual_serde::de::Error,
68+
E: serde::de::Error,
6969
{
7070
DleqProof::try_from(v).map_err(|e| {
7171
E::custom(format!("expected {} bytes, got {}", e.expected, e.got))
@@ -78,9 +78,6 @@ impl<'de> actual_serde::Deserialize<'de> for DleqProof {
7878
}
7979

8080
impl DleqProof {
81-
/// Creates a new [`DleqProof`] from a 64-byte array.
82-
pub fn new(bytes: [u8; 64]) -> Self { DleqProof(bytes) }
83-
8481
/// Returns the inner 64-byte array.
8582
pub fn as_bytes(&self) -> &[u8; 64] { &self.0 }
8683
}
@@ -115,9 +112,11 @@ impl Serialize for DleqProof {
115112

116113
impl Deserialize for DleqProof {
117114
fn deserialize(bytes: &[u8]) -> Result<Self, crate::serialize::Error> {
118-
DleqProof::try_from(bytes).map_err(|e| crate::serialize::Error::InvalidDleqProof {
119-
got: e.got,
120-
expected: e.expected,
115+
DleqProof::try_from(bytes).map_err(|e| {
116+
crate::serialize::Error::InvalidDleqProof(InvalidLengthError {
117+
got: e.got,
118+
expected: e.expected,
119+
})
121120
})
122121
}
123122
}

tests/bip375-parse-invalid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn bip375_global_field_mismatch_dleq_only() {
1616
// Approach 1: Programmatic
1717
let mut psbt = Creator::new().psbt();
1818
let scan_key = CompressedPublicKey::from_slice(&[0x02u8; 33]).unwrap();
19-
let dleq_proof = DleqProof::new([0xAAu8; 64]);
19+
let dleq_proof = DleqProof::from([0xAAu8; 64]);
2020
psbt.global.sp_dleq_proofs.insert(scan_key, dleq_proof);
2121

2222
let bytes = psbt.serialize();

tests/bip375-parse-valid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn valid_psbt_with_bip375_global_fields() -> Psbt {
2121
"02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5",
2222
)
2323
.unwrap();
24-
let dleq_proof = DleqProof::new([0xAAu8; 64]);
24+
let dleq_proof = DleqProof::from([0xAAu8; 64]);
2525

2626
psbt.global.sp_ecdh_shares.insert(scan_key, ecdh_share);
2727
psbt.global.sp_dleq_proofs.insert(scan_key, dleq_proof);

0 commit comments

Comments
 (0)