Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create keys from owned array values #781

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/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