From 7c565ec7a65e423756c42fc89b28a4fa7fccf80e Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 1 Aug 2021 21:03:58 +0200 Subject: [PATCH] BIP32 extended key `to_ecdsa()` and `to_schnorr()` methods --- examples/bip32.rs | 2 +- src/util/bip32.rs | 41 +++++++++++++++++++++++++++++++++++------ src/util/psbt/mod.rs | 2 +- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/examples/bip32.rs b/examples/bip32.rs index e6a232f065..0a005bb1d5 100644 --- a/examples/bip32.rs +++ b/examples/bip32.rs @@ -49,7 +49,7 @@ fn main() { let path = DerivationPath::from_str("m/84h/0h/0h").unwrap(); let child = root.derive_priv(&secp, &path).unwrap(); println!("Child at {}: {}", path, child); - let xpub = ExtendedPubKey::from_private(&secp, &child); + let xpub = ExtendedPubKey::from_priv(&secp, &child); println!("Public key at {}: {}", path, xpub); // generate first receiving address at m/0/0 diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 640e65d085..a0dc2ac3de 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -28,7 +28,7 @@ use secp256k1::{self, Secp256k1}; use network::constants::Network; use util::{base58, endian}; -use util::key; +use util::{key, ecdsa, schnorr}; use io::Write; /// A chain code @@ -507,6 +507,21 @@ impl ExtendedPrivKey { }) } + /// Constructs ECDSA compressed private key matching internal secret key representation. + pub fn to_ecdsa_priv(&self) -> ecdsa::PrivateKey { + ecdsa::PrivateKey { + compressed: true, + network: self.network, + key: self.private_key + } + } + + /// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal + /// secret key representation. + pub fn to_schnorr_keypair(&self, secp: &Secp256k1) -> schnorr::KeyPair { + schnorr::KeyPair::from_seckey_slice(secp, &self.private_key[..]).expect("BIP32 internal private key representation is broken") + } + /// Attempts to derive an extended private key from a path. /// /// The `path` argument can be both of type `DerivationPath` or `Vec`. @@ -596,7 +611,7 @@ impl ExtendedPrivKey { /// Returns the HASH160 of the public key belonging to the xpriv pub fn identifier(&self, secp: &Secp256k1) -> XpubIdentifier { - ExtendedPubKey::from_private(secp, self).identifier() + ExtendedPubKey::from_priv(secp, self).identifier() } /// Returns the first four bytes of the identifier @@ -607,7 +622,7 @@ impl ExtendedPrivKey { impl ExtendedPubKey { /// Derives a public key from a private key - pub fn from_private(secp: &Secp256k1, sk: &ExtendedPrivKey) -> ExtendedPubKey { + pub fn from_priv(secp: &Secp256k1, sk: &ExtendedPrivKey) -> ExtendedPubKey { ExtendedPubKey { network: sk.network, depth: sk.depth, @@ -618,6 +633,20 @@ impl ExtendedPubKey { } } + /// Constructs ECDSA compressed public key matching internal public key representation. + pub fn to_ecdsa_pub(&self) -> ecdsa::PublicKey { + ecdsa::PublicKey { + compressed: true, + key: self.public_key + } + } + + /// Constructs BIP340 x-only public key for BIP-340 signatures and Taproot use matching + /// the internal public key representation. + pub fn to_schnorr_pub(&self) -> schnorr::PublicKey { + schnorr::PublicKey::from(self.public_key) + } + /// Attempts to derive an extended public key from a path. /// /// The `path` argument can be both of type `DerivationPath` or `Vec`. @@ -849,7 +878,7 @@ mod tests { expected_pk: &str) { let mut sk = ExtendedPrivKey::new_master(network, seed).unwrap(); - let mut pk = ExtendedPubKey::from_private(secp, &sk); + let mut pk = ExtendedPubKey::from_priv(secp, &sk); // Check derivation convenience method for ExtendedPrivKey assert_eq!( @@ -877,7 +906,7 @@ mod tests { match num { Normal {..} => { let pk2 = pk.ckd_pub(secp, num).unwrap(); - pk = ExtendedPubKey::from_private(secp, &sk); + pk = ExtendedPubKey::from_priv(secp, &sk); assert_eq!(pk, pk2); } Hardened {..} => { @@ -885,7 +914,7 @@ mod tests { pk.ckd_pub(secp, num), Err(Error::CannotDeriveFromHardenedKey) ); - pk = ExtendedPubKey::from_private(secp, &sk); + pk = ExtendedPubKey::from_priv(secp, &sk); } } } diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index 3a9df2e2aa..012025dfe2 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -281,7 +281,7 @@ mod tests { sk = sk.derive_priv(secp, &dpath).unwrap(); - let pk: ExtendedPubKey = ExtendedPubKey::from_private(&secp, &sk); + let pk: ExtendedPubKey = ExtendedPubKey::from_priv(&secp, &sk); hd_keypaths.insert(pk.public_key, (fprint, dpath.into()));