From eb09019720fa3dcd0514b7d7d7d60a85fe0e7fd7 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 9 Jan 2022 23:09:02 +0100 Subject: [PATCH] Rename inner key field in PrivateKey and PublicKey Closes #532 --- src/blockdata/script.rs | 8 +++---- src/util/bip32.rs | 4 ++-- src/util/contracthash.rs | 8 +++---- src/util/ecdsa.rs | 46 ++++++++++++++++++++-------------------- src/util/misc.rs | 4 ++-- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 4a946747..616597df 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -815,9 +815,9 @@ impl Builder { /// Pushes a public key pub fn push_key(self, key: &PublicKey) -> Builder { if key.compressed { - self.push_slice(&key.key.serialize()[..]) + self.push_slice(&key.inner.serialize()[..]) } else { - self.push_slice(&key.key.serialize_uncompressed()[..]) + self.push_slice(&key.inner.serialize_uncompressed()[..]) } } @@ -1038,10 +1038,10 @@ mod test { let pubkey = PublicKey::from_str("0234e6a79c5359c613762d537e0e19d86c77c1666d8c9ab050f23acd198e97f93e").unwrap(); assert!(Script::new_p2pk(&pubkey).is_p2pk()); - let pubkey_hash = PubkeyHash::hash(&pubkey.key.serialize()); + let pubkey_hash = PubkeyHash::hash(&pubkey.inner.serialize()); assert!(Script::new_p2pkh(&pubkey_hash).is_p2pkh()); - let wpubkey_hash = WPubkeyHash::hash(&pubkey.key.serialize()); + let wpubkey_hash = WPubkeyHash::hash(&pubkey.inner.serialize()); assert!(Script::new_v0_wpkh(&wpubkey_hash).is_v0_p2wpkh()); let script = Builder::new().push_opcode(opcodes::all::OP_NUMEQUAL) diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 96ea64a5..71658726 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -532,7 +532,7 @@ impl ExtendedPrivKey { ecdsa::PrivateKey { compressed: true, network: self.network, - key: self.private_key + inner: self.private_key } } @@ -663,7 +663,7 @@ impl ExtendedPubKey { pub fn to_pub(&self) -> ecdsa::PublicKey { ecdsa::PublicKey { compressed: true, - key: self.public_key + inner: self.public_key } } diff --git a/src/util/contracthash.rs b/src/util/contracthash.rs index c08e9b57..919254f0 100644 --- a/src/util/contracthash.rs +++ b/src/util/contracthash.rs @@ -160,7 +160,7 @@ impl<'a> From<&'a [u8]> for Template { /// Tweak a single key using some arbitrary data pub fn tweak_key(secp: &Secp256k1, mut key: PublicKey, contract: &[u8]) -> PublicKey { let hmac_result = compute_tweak(&key, contract); - key.key.add_exp_assign(secp, &hmac_result[..]).expect("HMAC cannot produce invalid tweak"); + key.inner.add_exp_assign(secp, &hmac_result[..]).expect("HMAC cannot produce invalid tweak"); key } @@ -172,9 +172,9 @@ pub fn tweak_keys(secp: &Secp256k1, keys: &[Publi /// Compute a tweak from some given data for the given public key pub fn compute_tweak(pk: &PublicKey, contract: &[u8]) -> Hmac { let mut hmac_engine: HmacEngine = if pk.compressed { - HmacEngine::new(&pk.key.serialize()) + HmacEngine::new(&pk.inner.serialize()) } else { - HmacEngine::new(&pk.key.serialize_uncompressed()) + HmacEngine::new(&pk.inner.serialize_uncompressed()) }; hmac_engine.input(contract); Hmac::from_engine(hmac_engine) @@ -188,7 +188,7 @@ pub fn tweak_secret_key(secp: &Secp256k1, key: &Privat let hmac_sk = compute_tweak(&pk, contract); // Execute the tweak let mut key = *key; - key.key.add_assign(&hmac_sk[..]).map_err(Error::Secp)?; + key.inner.add_assign(&hmac_sk[..]).map_err(Error::Secp)?; // Return Ok(key) } diff --git a/src/util/ecdsa.rs b/src/util/ecdsa.rs index 9d0a8732..c939d419 100644 --- a/src/util/ecdsa.rs +++ b/src/util/ecdsa.rs @@ -39,7 +39,7 @@ pub struct PublicKey { /// Whether this public key should be serialized as compressed pub compressed: bool, /// The actual ECDSA key - pub key: secp256k1::PublicKey, + pub inner: secp256k1::PublicKey, } impl PublicKey { @@ -47,7 +47,7 @@ impl PublicKey { pub fn new(key: secp256k1::PublicKey) -> PublicKey { PublicKey { compressed: true, - key, + inner: key, } } @@ -56,16 +56,16 @@ impl PublicKey { pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey { PublicKey { compressed: false, - key, + inner: key, } } /// Returns bitcoin 160-bit hash of the public key pub fn pubkey_hash(&self) -> PubkeyHash { if self.compressed { - PubkeyHash::hash(&self.key.serialize()) + PubkeyHash::hash(&self.inner.serialize()) } else { - PubkeyHash::hash(&self.key.serialize_uncompressed()) + PubkeyHash::hash(&self.inner.serialize_uncompressed()) } } @@ -73,7 +73,7 @@ impl PublicKey { pub fn wpubkey_hash(&self) -> Option { if self.compressed { Some(WPubkeyHash::from_inner( - hash160::Hash::hash(&self.key.serialize()).into_inner() + hash160::Hash::hash(&self.inner.serialize()).into_inner() )) } else { // We can't create witness pubkey hashes for an uncompressed @@ -85,9 +85,9 @@ impl PublicKey { /// Write the public key into a writer pub fn write_into(&self, mut writer: W) -> Result<(), io::Error> { if self.compressed { - writer.write_all(&self.key.serialize()) + writer.write_all(&self.inner.serialize()) } else { - writer.write_all(&self.key.serialize_uncompressed()) + writer.write_all(&self.inner.serialize_uncompressed()) } } @@ -136,7 +136,7 @@ impl PublicKey { Ok(PublicKey { compressed, - key: secp256k1::PublicKey::from_slice(data)?, + inner: secp256k1::PublicKey::from_slice(data)?, }) } @@ -149,11 +149,11 @@ impl PublicKey { impl fmt::Display for PublicKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.compressed { - for ch in &self.key.serialize()[..] { + for ch in &self.inner.serialize()[..] { write!(f, "{:02x}", ch)?; } } else { - for ch in &self.key.serialize_uncompressed()[..] { + for ch in &self.inner.serialize_uncompressed()[..] { write!(f, "{:02x}", ch)?; } } @@ -166,7 +166,7 @@ impl FromStr for PublicKey { fn from_str(s: &str) -> Result { let key = secp256k1::PublicKey::from_str(s)?; Ok(PublicKey { - key, + inner: key, compressed: s.len() == 66 }) } @@ -181,7 +181,7 @@ pub struct PrivateKey { /// The network on which this key should be used pub network: Network, /// The actual ECDSA key - pub key: secp256k1::SecretKey, + pub inner: secp256k1::SecretKey, } impl PrivateKey { @@ -191,7 +191,7 @@ impl PrivateKey { PrivateKey { compressed: true, network, - key, + inner: key, } } @@ -201,7 +201,7 @@ impl PrivateKey { PrivateKey { compressed: false, network, - key, + inner: key, } } @@ -209,13 +209,13 @@ impl PrivateKey { pub fn public_key(&self, secp: &Secp256k1) -> PublicKey { PublicKey { compressed: self.compressed, - key: secp256k1::PublicKey::from_secret_key(secp, &self.key) + inner: secp256k1::PublicKey::from_secret_key(secp, &self.inner) } } /// Serialize the private key to bytes pub fn to_bytes(&self) -> Vec { - self.key[..].to_vec() + self.inner[..].to_vec() } /// Deserialize a private key from a slice @@ -233,7 +233,7 @@ impl PrivateKey { Network::Bitcoin => 128, Network::Testnet | Network::Signet | Network::Regtest => 239, }; - ret[1..33].copy_from_slice(&self.key[..]); + ret[1..33].copy_from_slice(&self.inner[..]); let privkey = if self.compressed { ret[33] = 1; base58::check_encode_slice(&ret[..]) @@ -270,7 +270,7 @@ impl PrivateKey { Ok(PrivateKey { compressed, network, - key: secp256k1::SecretKey::from_slice(&data[1..33])?, + inner: secp256k1::SecretKey::from_slice(&data[1..33])?, }) } } @@ -298,7 +298,7 @@ impl FromStr for PrivateKey { impl ops::Index for PrivateKey { type Output = [u8]; fn index(&self, _: ops::RangeFull) -> &[u8] { - &self.key[..] + &self.inner[..] } } @@ -354,9 +354,9 @@ impl ::serde::Serialize for PublicKey { s.collect_str(self) } else { if self.compressed { - s.serialize_bytes(&self.key.serialize()[..]) + s.serialize_bytes(&self.inner.serialize()[..]) } else { - s.serialize_bytes(&self.key.serialize_uncompressed()[..]) + s.serialize_bytes(&self.inner.serialize_uncompressed()[..]) } } } @@ -626,7 +626,7 @@ mod tests { let sk = PrivateKey::from_str(&KEY_WIF).unwrap(); let pk = PublicKey::from_private_key(&s, &sk); let pk_u = PublicKey { - key: pk.key, + inner: pk.inner, compressed: false, }; diff --git a/src/util/misc.rs b/src/util/misc.rs index 9cf39561..5bcfb3fa 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -148,7 +148,7 @@ mod message_signing { let msg = secp256k1::Message::from_slice(&msg_hash[..])?; let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?; Ok(PublicKey { - key: pubkey, + inner: pubkey, compressed: self.compressed, }) } @@ -330,7 +330,7 @@ mod tests { let signature2 = super::MessageSignature::from_str(&signature.to_string()).unwrap(); let pubkey = signature2.recover_pubkey(&secp, msg_hash).unwrap(); assert_eq!(pubkey.compressed, true); - assert_eq!(pubkey.key, secp256k1::PublicKey::from_secret_key(&secp, &privkey)); + assert_eq!(pubkey.inner, secp256k1::PublicKey::from_secret_key(&secp, &privkey)); let p2pkh = ::Address::p2pkh(&pubkey, ::Network::Bitcoin); assert_eq!(signature2.is_signed_by_address(&secp, &p2pkh, msg_hash), Ok(true));