8000 Rename inner key field in PrivateKey and PublicKey by dr-orlovsky · Pull Request #762 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rename inner key field in PrivateKey and PublicKey #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()[..])
}
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/util/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl ExtendedPrivKey {
ecdsa::PrivateKey {
compressed: true,
network: self.network,
key: self.private_key
inner: self.private_key
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/util/contracthash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<'a> From<&'a [u8]> for Template {
/// Tweak a single key using some arbitrary data
pub fn tweak_key<C: secp256k1::Verification>(secp: &Secp256k1<C>, 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
}

Expand All @@ -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
pub fn compute_tweak(pk: &PublicKey, contract: &[u8]) -> Hmac<sha256::Hash> {
let mut hmac_engine: HmacEngine<sha256::Hash> = 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)
Expand All @@ -188,7 +188,7 @@ pub fn tweak_secret_key<C: secp256k1::Signing>(secp: &Secp256k1<C>, 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)
}
Expand Down
46 changes: 23 additions & 23 deletions src/util/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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 {
/// Constructs compressed ECDSA public key from the provided generic Secp256k1 public key
pub fn new(key: secp256k1::PublicKey) -> PublicKey {
PublicKey {
compressed: true,
key,
inner: key,
}
}

Expand All @@ -56,24 +56,24 @@ 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())
}
}

/// Returns bitcoin 160-bit hash of the public key for witness program
pub fn wpubkey_hash(&self) -> Option<WPubkeyHash> {
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
Expand All @@ -85,9 +85,9 @@ impl PublicKey {
/// Write the public key into a writer
pub fn write_into<W: io::Write>(&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())
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ impl PublicKey {

Ok(PublicKey {
compressed,
key: secp256k1::PublicKey::from_slice(data)?,
inner: secp256k1::PublicKey::from_slice(data)?,
})
}

Expand All @@ -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)?;
}
}
Expand All @@ -166,7 +166,7 @@ impl FromStr for PublicKey {
fn from_str(s: &str) -> Result<PublicKey, Error> {
let key = secp256k1::PublicKey::from_str(s)?;
Ok(PublicKey {
key,
inner: key,
compressed: s.len() == 66
})
}
Expand All @@ -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 {
Expand All @@ -191,7 +191,7 @@ impl PrivateKey {
PrivateKey {
compressed: true,
network,
key,
inner: key,
}
}

Expand All @@ -201,21 +201,21 @@ impl PrivateKey {
PrivateKey {
compressed: false,
network,
key,
inner: key,
}
}

/// Creates a public key from this private key
pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> 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<u8> {
self.key[..].to_vec()
self.inner[..].to_vec()
}

/// Deserialize a private key from a slice
Expand All @@ -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[..])
Expand Down Expand Up @@ -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])?,
})
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ impl FromStr for PrivateKey {
impl ops::Index<ops::RangeFull> for PrivateKey {
type Output = [u8];
fn index(&self, _: ops::RangeFull) -> &[u8] {
&self.key[..]
&self.inner[..]
}
}

Expand Down Expand Up @@ -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()[..])
}
}
}
Expand Down Expand Up @@ -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,
};

Expand Down
4 changes: 2 additions & 2 deletions src/util/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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));
Expand Down
0