Rename inner key field in PrivateKey and PublicKey

Closes #532
This commit is contained in:
Dr Maxim Orlovsky 2022-01-09 23:09:02 +01:00
parent e4d5039a86
commit eb09019720
5 changed files with 35 additions and 35 deletions

View File

@ -815,9 +815,9 @@ impl Builder {
/// Pushes a public key /// Pushes a public key
pub fn push_key(self, key: &PublicKey) -> Builder { pub fn push_key(self, key: &PublicKey) -> Builder {
if key.compressed { if key.compressed {
self.push_slice(&key.key.serialize()[..]) self.push_slice(&key.inner.serialize()[..])
} else { } 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(); let pubkey = PublicKey::from_str("0234e6a79c5359c613762d537e0e19d86c77c1666d8c9ab050f23acd198e97f93e").unwrap();
assert!(Script::new_p2pk(&pubkey).is_p2pk()); 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()); 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()); assert!(Script::new_v0_wpkh(&wpubkey_hash).is_v0_p2wpkh());
let script = Builder::new().push_opcode(opcodes::all::OP_NUMEQUAL) let script = Builder::new().push_opcode(opcodes::all::OP_NUMEQUAL)

View File

@ -532,7 +532,7 @@ impl ExtendedPrivKey {
ecdsa::PrivateKey { ecdsa::PrivateKey {
compressed: true, compressed: true,
network: self.network, network: self.network,
key: self.private_key inner: self.private_key
} }
} }
@ -663,7 +663,7 @@ impl ExtendedPubKey {
pub fn to_pub(&self) -> ecdsa::PublicKey { pub fn to_pub(&self) -> ecdsa::PublicKey {
ecdsa::PublicKey { ecdsa::PublicKey {
compressed: true, compressed: true,
key: self.public_key inner: self.public_key
} }
} }

View File

@ -160,7 +160,7 @@ impl<'a> From<&'a [u8]> for Template {
/// Tweak a single key using some arbitrary data /// Tweak a single key using some arbitrary data
pub fn tweak_key<C: secp256k1::Verification>(secp: &Secp256k1<C>, mut key: PublicKey, contract: &[u8]) -> PublicKey { pub fn tweak_key<C: secp256k1::Verification>(secp: &Secp256k1<C>, mut key: PublicKey, contract: &[u8]) -> PublicKey {
let hmac_result = compute_tweak(&key, contract); 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 key
} }
@ -172,9 +172,9 @@ pub fn tweak_keys<C: secp256k1::Verification>(secp: &Secp256k1<C>, keys: &[Publi
/// Compute a tweak from some given data for the given public key /// Compute a tweak from some given data for the given public key
pub fn compute_tweak(pk: &PublicKey, contract: &[u8]) -> Hmac<sha256::Hash> { pub fn compute_tweak(pk: &PublicKey, contract: &[u8]) -> Hmac<sha256::Hash> {
let mut hmac_engine: HmacEngine<sha256::Hash> = if pk.compressed { let mut hmac_engine: HmacEngine<sha256::Hash> = if pk.compressed {
HmacEngine::new(&pk.key.serialize()) HmacEngine::new(&pk.inner.serialize())
} else { } else {
HmacEngine::new(&pk.key.serialize_uncompressed()) HmacEngine::new(&pk.inner.serialize_uncompressed())
}; };
hmac_engine.input(contract); hmac_engine.input(contract);
Hmac::from_engine(hmac_engine) Hmac::from_engine(hmac_engine)
@ -188,7 +188,7 @@ pub fn tweak_secret_key<C: secp256k1::Signing>(secp: &Secp256k1<C>, key: &Privat
let hmac_sk = compute_tweak(&pk, contract); let hmac_sk = compute_tweak(&pk, contract);
// Execute the tweak // Execute the tweak
let mut key = *key; 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 // Return
Ok(key) Ok(key)
} }

View File

@ -39,7 +39,7 @@ pub struct PublicKey {
/// Whether this public key should be serialized as compressed /// Whether this public key should be serialized as compressed
pub compressed: bool, pub compressed: bool,
/// The actual ECDSA key /// The actual ECDSA key
pub key: secp256k1::PublicKey, pub inner: secp256k1::PublicKey,
} }
impl PublicKey { impl PublicKey {
@ -47,7 +47,7 @@ impl PublicKey {
pub fn new(key: secp256k1::PublicKey) -> PublicKey { pub fn new(key: secp256k1::PublicKey) -> PublicKey {
PublicKey { PublicKey {
compressed: true, compressed: true,
key, inner: key,
} }
} }
@ -56,16 +56,16 @@ impl PublicKey {
pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey { pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey {
PublicKey { PublicKey {
compressed: false, compressed: false,
key, inner: key,
} }
} }
/// Returns bitcoin 160-bit hash of the public key /// Returns bitcoin 160-bit hash of the public key
pub fn pubkey_hash(&self) -> PubkeyHash { pub fn pubkey_hash(&self) -> PubkeyHash {
if self.compressed { if self.compressed {
PubkeyHash::hash(&self.key.serialize()) PubkeyHash::hash(&self.inner.serialize())
} else { } 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<WPubkeyHash> { pub fn wpubkey_hash(&self) -> Option<WPubkeyHash> {
if self.compressed { if self.compressed {
Some(WPubkeyHash::from_inner( Some(WPubkeyHash::from_inner(
hash160::Hash::hash(&self.key.serialize()).into_inner() hash160::Hash::hash(&self.inner.serialize()).into_inner()
)) ))
} else { } else {
// We can't create witness pubkey hashes for an uncompressed // We can't create witness pubkey hashes for an uncompressed
@ -85,9 +85,9 @@ impl PublicKey {
/// Write the public key into a writer /// Write the public key into a writer
pub fn write_into<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { pub fn write_into<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
if self.compressed { if self.compressed {
writer.write_all(&self.key.serialize()) writer.write_all(&self.inner.serialize())
} else { } else {
writer.write_all(&self.key.serialize_uncompressed()) writer.write_all(&self.inner.serialize_uncompressed())
} }
} }
@ -136,7 +136,7 @@ impl PublicKey {
Ok(PublicKey { Ok(PublicKey {
compressed, compressed,
key: secp256k1::PublicKey::from_slice(data)?, inner: secp256k1::PublicKey::from_slice(data)?,
}) })
} }
@ -149,11 +149,11 @@ impl PublicKey {
impl fmt::Display for PublicKey { impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.compressed { if self.compressed {
for ch in &self.key.serialize()[..] { for ch in &self.inner.serialize()[..] {
write!(f, "{:02x}", ch)?; write!(f, "{:02x}", ch)?;
} }
} else { } else {
for ch in &self.key.serialize_uncompressed()[..] { for ch in &self.inner.serialize_uncompressed()[..] {
write!(f, "{:02x}", ch)?; write!(f, "{:02x}", ch)?;
} }
} }
@ -166,7 +166,7 @@ impl FromStr for PublicKey {
fn from_str(s: &str) -> Result<PublicKey, Error> { fn from_str(s: &str) -> Result<PublicKey, Error> {
let key = secp256k1::PublicKey::from_str(s)?; let key = secp256k1::PublicKey::from_str(s)?;
Ok(PublicKey { Ok(PublicKey {
key, inner: key,
compressed: s.len() == 66 compressed: s.len() == 66
}) })
} }
@ -181,7 +181,7 @@ pub struct PrivateKey {
/// The network on which this key should be used /// The network on which this key should be used
pub network: Network, pub network: Network,
/// The actual ECDSA key /// The actual ECDSA key
pub key: secp256k1::SecretKey, pub inner: secp256k1::SecretKey,
} }
impl PrivateKey { impl PrivateKey {
@ -191,7 +191,7 @@ impl PrivateKey {
PrivateKey { PrivateKey {
compressed: true, compressed: true,
network, network,
key, inner: key,
} }
} }
@ -201,7 +201,7 @@ impl PrivateKey {
PrivateKey { PrivateKey {
compressed: false, compressed: false,
network, network,
key, inner: key,
} }
} }
@ -209,13 +209,13 @@ impl PrivateKey {
pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> PublicKey { pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> PublicKey {
PublicKey { PublicKey {
compressed: self.compressed, 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 /// Serialize the private key to bytes
pub fn to_bytes(&self) -> Vec<u8> { pub fn to_bytes(&self) -> Vec<u8> {
self.key[..].to_vec() self.inner[..].to_vec()
} }
/// Deserialize a private key from a slice /// Deserialize a private key from a slice
@ -233,7 +233,7 @@ impl PrivateKey {
Network::Bitcoin => 128, Network::Bitcoin => 128,
Network::Testnet | Network::Signet | Network::Regtest => 239, 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 { let privkey = if self.compressed {
ret[33] = 1; ret[33] = 1;
base58::check_encode_slice(&ret[..]) base58::check_encode_slice(&ret[..])
@ -270,7 +270,7 @@ impl PrivateKey {
Ok(PrivateKey { Ok(PrivateKey {
compressed, compressed,
network, 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<ops::RangeFull> for PrivateKey { impl ops::Index<ops::RangeFull> for PrivateKey {
type Output = [u8]; type Output = [u8];
fn index(&self, _: ops::RangeFull) -> &[u8] { fn index(&self, _: ops::RangeFull) -> &[u8] {
&self.key[..] &self.inner[..]
} }
} }
@ -354,9 +354,9 @@ impl ::serde::Serialize for PublicKey {
s.collect_str(self) s.collect_str(self)
} else { } else {
if self.compressed { if self.compressed {
s.serialize_bytes(&self.key.serialize()[..]) s.serialize_bytes(&self.inner.serialize()[..])
} else { } 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 sk = PrivateKey::from_str(&KEY_WIF).unwrap();
let pk = PublicKey::from_private_key(&s, &sk); let pk = PublicKey::from_private_key(&s, &sk);
let pk_u = PublicKey { let pk_u = PublicKey {
key: pk.key, inner: pk.inner,
compressed: false, compressed: false,
}; };

View File

@ -148,7 +148,7 @@ mod message_signing {
let msg = secp256k1::Message::from_slice(&msg_hash[..])?; let msg = secp256k1::Message::from_slice(&msg_hash[..])?;
let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?; let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
Ok(PublicKey { Ok(PublicKey {
key: pubkey, inner: pubkey,
compressed: self.compressed, compressed: self.compressed,
}) })
} }
@ -330,7 +330,7 @@ mod tests {
let signature2 = super::MessageSignature::from_str(&signature.to_string()).unwrap(); let signature2 = super::MessageSignature::from_str(&signature.to_string()).unwrap();
let pubkey = signature2.recover_pubkey(&secp, msg_hash).unwrap(); let pubkey = signature2.recover_pubkey(&secp, msg_hash).unwrap();
assert_eq!(pubkey.compressed, true); 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); let p2pkh = ::Address::p2pkh(&pubkey, ::Network::Bitcoin);
assert_eq!(signature2.is_signed_by_address(&secp, &p2pkh, msg_hash), Ok(true)); assert_eq!(signature2.is_signed_by_address(&secp, &p2pkh, msg_hash), Ok(true));