Create a PSBT signer from an ExtendedDescriptor

This commit is contained in:
Alekos Filini
2020-02-04 11:05:54 +01:00
parent fa9a62fbee
commit 2c179dd1b8
4 changed files with 70 additions and 34 deletions

View File

@@ -94,11 +94,7 @@ where
trait Key: std::fmt::Debug {
fn fingerprint(&self, secp: &Secp256k1<All>) -> Option<Fingerprint>;
fn as_public_key(&self, secp: &Secp256k1<All>, index: Option<u32>) -> Result<PublicKey, Error>;
fn as_secret_key(
&self,
secp: &Secp256k1<All>,
index: Option<u32>,
) -> Result<Option<PrivateKey>, Error>;
fn as_secret_key(&self) -> Option<PrivateKey>;
fn xprv(&self) -> Option<ExtendedPrivKey>;
fn full_path(&self, index: u32) -> Option<DerivationPath>;
fn is_fixed(&self) -> bool;
@@ -117,12 +113,8 @@ impl Key for PublicKey {
Ok(PublicKey::clone(self))
}
fn as_secret_key(
&self,
_secp: &Secp256k1<All>,
_index: Option<u32>,
) -> Result<Option<PrivateKey>, Error> {
Ok(None)
fn as_secret_key(&self) -> Option<PrivateKey> {
None
}
fn xprv(&self) -> Option<ExtendedPrivKey> {
@@ -151,12 +143,8 @@ impl Key for PrivateKey {
Ok(self.public_key(secp))
}
fn as_secret_key(
&self,
_secp: &Secp256k1<All>,
_index: Option<u32>,
) -> Result<Option<PrivateKey>, Error> {
Ok(Some(PrivateKey::clone(self)))
fn as_secret_key(&self) -> Option<PrivateKey> {
Some(PrivateKey::clone(self))
}
fn xprv(&self) -> Option<ExtendedPrivKey> {
@@ -181,22 +169,8 @@ impl Key for DescriptorExtendedKey {
Ok(self.derive_xpub(secp, index.unwrap_or(0))?.public_key)
}
fn as_secret_key(
&self,
secp: &Secp256k1<All>,
index: Option<u32>,
) -> Result<Option<PrivateKey>, Error> {
if self.secret.is_none() {
return Ok(None);
}
let derivation_path = self.full_path(index.unwrap_or(0));
Ok(Some(
self.secret
.unwrap()
.derive_priv(secp, &derivation_path)?
.private_key,
))
fn as_secret_key(&self) -> Option<PrivateKey> {
None
}
fn xprv(&self) -> Option<ExtendedPrivKey> {
@@ -295,6 +269,14 @@ impl ExtendedDescriptor {
.collect()
}
pub fn get_secret_keys(&self) -> Vec<PrivateKey> {
self.keys
.iter()
.filter(|(_, v)| v.as_secret_key().is_some())
.map(|(_, v)| v.as_secret_key().unwrap())
.collect()
}
pub fn get_hd_keypaths(
&self,
index: u32,

View File

@@ -4,5 +4,8 @@ pub extern crate miniscript;
extern crate serde;
extern crate serde_json;
pub mod descriptor;
#[macro_use]
pub mod error;
pub mod descriptor;
pub mod psbt;
pub mod signer;