Skip to content

Commit b991ef2

Browse files
committed
refactor: Parse byte array instead of byte slice
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.
1 parent 0ecd2a2 commit b991ef2

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/key.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
403403
} else {
404404
let visitor = super::serde_util::Tuple32Visitor::new(
405405
"raw 32 bytes SecretKey",
406-
SecretKey::from_slice,
406+
|bytes| SecretKey::from_byte_array(&bytes)
407407
);
408408
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
409409
}
@@ -792,7 +792,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
792792
} else {
793793
let visitor = super::serde_util::Tuple33Visitor::new(
794794
"33 bytes compressed public key",
795-
PublicKey::from_slice,
795+
|bytes| PublicKey::from_byte_array_compressed(&bytes),
796796
);
797797
d.deserialize_tuple(constants::PUBLIC_KEY_SIZE, visitor)
798798
}
@@ -1117,7 +1117,7 @@ impl<'de> serde::Deserialize<'de> for Keypair {
11171117
let ctx = Secp256k1::signing_only();
11181118

11191119
#[allow(clippy::needless_borrow)]
1120-
Keypair::from_seckey_slice(&ctx, data)
1120+
Keypair::from_seckey_slice(&ctx, &data)
11211121
});
11221122
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
11231123
}
@@ -1597,7 +1597,7 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
15971597
} else {
15981598
let visitor = super::serde_util::Tuple32Visitor::new(
15991599
"raw 32 bytes schnorr public key",
1600-
XOnlyPublicKey::from_slice,
1600+
|bytes| XOnlyPublicKey::from_byte_array(&bytes),
16011601
);
16021602
d.deserialize_tuple(constants::SCHNORR_PUBLIC_KEY_SIZE, visitor)
16031603
}

src/serde_util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! impl_tuple_visitor {
7474

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

8585
impl<'de, F, T, E> de::Visitor<'de> for $thing<F>
8686
where
87-
F: FnOnce(&[u8]) -> Result<T, E>,
87+
F: FnOnce([u8; $len]) -> Result<T, E>,
8888
E: fmt::Display,
8989
{
9090
type Value = T;
@@ -106,7 +106,7 @@ macro_rules! impl_tuple_visitor {
106106
return Err(de::Error::invalid_length(i, &self));
107107
}
108108
}
109-
(self.parse_fn)(&bytes).map_err(de::Error::custom)
109+
(self.parse_fn)(bytes).map_err(de::Error::custom)
110110
}
111111
}
112112
};

0 commit comments

Comments
 (0)