Merge rust-bitcoin/rust-secp256k1#531: Remove `serde_keypair` module

513144c923 Remove serde_keypair module (Tobin C. Harding)

Pull request description:

  Done while unsuccessfully trying to solve https://github.com/rust-bitcoin/rust-secp256k1/issues/514#

  The `serde_keypair` module appears to be only used for testing, however it is part of the public API for the `key` module?

  serde de/serialization is already implemented on `KeyPair` by way of the normal `serde` traits, there is no obvious reason for the `serde_keypair` and the `KeyPairWrapper`.

  Remove the `KeyPairWrapper` and test `KeyPair` serde impls directly.

ACKs for top commit:
  apoelstra:
    ACK 513144c923

Tree-SHA512: 23891217f3afc7cb3bb03431946e9866ee6ae611153fca8d93fe393b5a4abbd41d4713c6aa5ab24eb2734d8c8d94a9f6aed47316284b4097aa40f49f055f36b6
This commit is contained in:
Andrew Poelstra 2022-11-24 13:48:21 +00:00
commit e4ed848fcd
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 2 additions and 61 deletions

View File

@ -1530,42 +1530,6 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
} }
} }
/// Serde implementation for the [`KeyPair`] type.
///
/// Only the secret key part of the [`KeyPair`] is serialized using the [`SecretKey`] serde
/// implementation, meaning the public key has to be regenerated on deserialization.
///
/// **Attention:** The deserialization algorithm uses the [global context] to generate the public key
/// belonging to the secret key to form a [`KeyPair`]. The typical caveats regarding use of the
/// [global context] with secret data apply.
///
/// [`SecretKey`]: crate::SecretKey
/// [global context]: crate::SECP256K1
#[cfg(all(feature = "global-context", feature = "serde"))]
pub mod serde_keypair {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::key::{KeyPair, SecretKey};
#[allow(missing_docs)]
pub fn serialize<S>(key: &KeyPair, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
SecretKey::from_keypair(key).serialize(serializer)
}
#[allow(missing_docs)]
pub fn deserialize<'de, D>(deserializer: D) -> Result<KeyPair, D::Error>
where
D: Deserializer<'de>,
{
let secret_key = SecretKey::deserialize(deserializer)?;
Ok(KeyPair::from_secret_key(crate::SECP256K1, &secret_key))
}
}
#[cfg(test)] #[cfg(test)]
#[allow(unused_imports)] #[allow(unused_imports)]
mod test { mod test {
@ -2215,31 +2179,8 @@ mod test {
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_test::{assert_tokens, Configure, Token}; use serde_test::{assert_tokens, Configure, Token};
use super::serde_keypair;
use crate::key::KeyPair; use crate::key::KeyPair;
use crate::SECP256K1;
// Normally users would derive the serde traits, but we can't easily enable the serde macros
// here, so they are implemented manually to be able to test the behaviour.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct KeyPairWrapper(KeyPair);
impl<'de> Deserialize<'de> for KeyPairWrapper {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_keypair::deserialize(deserializer).map(KeyPairWrapper)
}
}
impl Serialize for KeyPairWrapper {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_keypair::serialize(&self.0, serializer)
}
}
#[rustfmt::skip] #[rustfmt::skip]
static SK_BYTES: [u8; 32] = [ static SK_BYTES: [u8; 32] = [
@ -2250,7 +2191,7 @@ mod test {
]; ];
static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363"; static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
let sk = KeyPairWrapper(KeyPair::from_seckey_slice(&crate::SECP256K1, &SK_BYTES).unwrap()); let sk = KeyPair::from_seckey_slice(&SECP256K1, &SK_BYTES).unwrap();
#[rustfmt::skip] #[rustfmt::skip]
assert_tokens(&sk.compact(), &[ assert_tokens(&sk.compact(), &[
Token::Tuple{ len: 32 }, Token::Tuple{ len: 32 },