Merge rust-bitcoin/rust-bitcoin#2214: Derive Debug for PrivateKey for no-std builds

3d17031725 Derive Debug for PrivateKey for no-std builds (Tobin C. Harding)

Pull request description:

  Currently we derive `impl Debug for PrivateKey` for "std" builds and manually implement an obfuscated version for "no-std" builds. Since we enable the `hashes` feature of `rust-secp` this is unnecessary because secp takes care of obfuscating the secret for us.

ACKs for top commit:
  apoelstra:
    ACK 3d17031725
  Kixunil:
    ACK 3d17031725

Tree-SHA512: 0ce394c6517c51e8964290a980cddd20186d19bcc6cbb8c71aa09b7485d6a0df373960798418184971e1c6e5a6b8f725dd44ebfa7184e31b63faf105dea69725
This commit is contained in:
Andrew Poelstra 2023-12-10 14:28:20 +00:00
commit e235a80c59
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 22 additions and 7 deletions

View File

@ -264,8 +264,7 @@ impl From<&PublicKey> for PubkeyHash {
} }
/// A Bitcoin ECDSA private key /// A Bitcoin ECDSA private key
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct PrivateKey { pub struct PrivateKey {
/// Whether this private key should be serialized as compressed /// Whether this private key should be serialized as compressed
pub compressed: bool, pub compressed: bool,
@ -368,11 +367,6 @@ impl fmt::Display for PrivateKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_wif(f) } fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_wif(f) }
} }
#[cfg(not(feature = "std"))]
impl fmt::Debug for PrivateKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[private key data]") }
}
impl FromStr for PrivateKey { impl FromStr for PrivateKey {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<PrivateKey, Error> { PrivateKey::from_wif(s) } fn from_str(s: &str) -> Result<PrivateKey, Error> { PrivateKey::from_wif(s) }
@ -1092,4 +1086,25 @@ mod tests {
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err(), Error::InvalidHexLength(8)); assert_eq!(res.unwrap_err(), Error::InvalidHexLength(8));
} }
#[test]
#[cfg(feature = "std")]
fn private_key_debug_is_obfuscated() {
let sk =
PrivateKey::from_str("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
let want = "PrivateKey { compressed: true, network: Testnet, inner: SecretKey(#32014e414fdce702) }";
let got = format!("{:?}", sk);
assert_eq!(got, want)
}
#[test]
#[cfg(all(not(feature = "std"), feature = "no-std"))]
fn private_key_debug_is_obfuscated() {
let sk =
PrivateKey::from_str("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
// Why is this not shortened? In rust-secp256k1/src/secret it is printed with "#{:016x}"?
let want = "PrivateKey { compressed: true, network: Testnet, inner: SecretKey(#7217ac58fbad8880a91032107b82cb6c5422544b426c350ee005cf509f3dbf7b) }";
let got = format!("{:?}", sk);
assert_eq!(got, want)
}
} }