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: ACK3d17031725
Kixunil: ACK3d17031725
Tree-SHA512: 0ce394c6517c51e8964290a980cddd20186d19bcc6cbb8c71aa09b7485d6a0df373960798418184971e1c6e5a6b8f725dd44ebfa7184e31b63faf105dea69725
This commit is contained in:
commit
e235a80c59
|
@ -264,8 +264,7 @@ impl From<&PublicKey> for PubkeyHash {
|
|||
}
|
||||
|
||||
/// A Bitcoin ECDSA private key
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct PrivateKey {
|
||||
/// Whether this private key should be serialized as compressed
|
||||
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) }
|
||||
}
|
||||
|
||||
#[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 {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<PrivateKey, Error> { PrivateKey::from_wif(s) }
|
||||
|
@ -1092,4 +1086,25 @@ mod tests {
|
|||
assert!(res.is_err());
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue