Skip to content

Commit

Permalink
BIP32 extended key to_ecdsa() and to_schnorr() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Apr 12, 2021
1 parent 1f73cb3 commit d155115
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/util/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ use std::default::Default;
use std::{error, fmt};
use std::str::FromStr;
#[cfg(feature = "serde")] use serde;
use std::io::Write;

use hash_types::XpubIdentifier;
use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
use secp256k1::{self, Secp256k1};

use network::constants::Network;
use util::{base58, endian};
use util::key;
use std::io::Write;
use util::{key, ecdsa, schnorr};

/// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -508,6 +508,21 @@ impl ExtendedPrivKey {
})
}

/// Constructs ECDSA compressed private key matching internal secret key representation.
pub fn to_ecdsa(&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<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> 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<ChildNumber>`.
Expand Down Expand Up @@ -619,6 +634,20 @@ impl ExtendedPubKey {
}
}

/// Constructs ECDSA compressed private key matching internal public key representation.
pub fn to_ecdsa(&self) -> ecdsa::PublicKey {
ecdsa::PublicKey {
compressed: true,
key: self.public_key
}
}

/// Constructs BIP340 xcoord-only public key for Schnorr signatures and Taproot use matching
/// the internal public key representation.
pub fn to_schnorr(&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<ChildNumber>`.
Expand Down

0 comments on commit d155115

Please sign in to comment.