Skip to content

Commit

Permalink
refactor: Parse byte array instead of byte slice
Browse files Browse the repository at this point in the history
Update TupleVisitor{32,33} to pass its owned byte array to the parsing
function instead of a mere byte slice. This gives us more flexibility
inside the parsing function.
  • Loading branch information
uncomputable committed Feb 15, 2025
1 parent 0ecd2a2 commit e723d80
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
"a hex string representing 32 byte SecretKey",
))
} else {
let visitor = super::serde_util::Tuple32Visitor::new(
"raw 32 bytes SecretKey",
SecretKey::from_slice,
);
let visitor =
super::serde_util::Tuple32Visitor::new("raw 32 bytes SecretKey", |bytes| {
SecretKey::from_byte_array(&bytes)
});
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
}
}
Expand Down Expand Up @@ -790,10 +790,10 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
"an ASCII hex string representing a public key",
))
} else {
let visitor = super::serde_util::Tuple33Visitor::new(
"33 bytes compressed public key",
PublicKey::from_slice,
);
let visitor =
super::serde_util::Tuple33Visitor::new("33 bytes compressed public key", |bytes| {
PublicKey::from_byte_array_compressed(&bytes)
});
d.deserialize_tuple(constants::PUBLIC_KEY_SIZE, visitor)
}
}
Expand Down Expand Up @@ -1117,7 +1117,7 @@ impl<'de> serde::Deserialize<'de> for Keypair {
let ctx = Secp256k1::signing_only();

#[allow(clippy::needless_borrow)]
Keypair::from_seckey_slice(&ctx, data)
Keypair::from_seckey_slice(&ctx, &data)
});
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
}
Expand Down Expand Up @@ -1597,7 +1597,7 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
} else {
let visitor = super::serde_util::Tuple32Visitor::new(
"raw 32 bytes schnorr public key",
XOnlyPublicKey::from_slice,
|bytes| XOnlyPublicKey::from_byte_array(&bytes),
);
d.deserialize_tuple(constants::SCHNORR_PUBLIC_KEY_SIZE, visitor)
}
Expand Down
6 changes: 3 additions & 3 deletions src/serde_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ macro_rules! impl_tuple_visitor {

impl<F, T, E> $thing<F>
where
F: FnOnce(&[u8]) -> Result<T, E>,
F: FnOnce([u8; $len]) -> Result<T, E>,
E: fmt::Display,
{
pub fn new(expectation: &'static str, parse_fn: F) -> Self {
Expand All @@ -84,7 +84,7 @@ macro_rules! impl_tuple_visitor {

impl<'de, F, T, E> de::Visitor<'de> for $thing<F>
where
F: FnOnce(&[u8]) -> Result<T, E>,
F: FnOnce([u8; $len]) -> Result<T, E>,
E: fmt::Display,
{
type Value = T;
Expand All @@ -106,7 +106,7 @@ macro_rules! impl_tuple_visitor {
return Err(de::Error::invalid_length(i, &self));
}
}
(self.parse_fn)(&bytes).map_err(de::Error::custom)
(self.parse_fn)(bytes).map_err(de::Error::custom)
}
}
};
Expand Down

0 comments on commit e723d80

Please sign in to comment.