From c0a92bd0843f906501e2be0a13cf719a85986ccf Mon Sep 17 00:00:00 2001 From: Alekos Filini Date: Fri, 11 Dec 2020 11:14:30 +0100 Subject: [PATCH] [keys] Replace `(Fingerprint, DerivationPath)` with `KeySource` --- src/descriptor/mod.rs | 4 ++-- src/keys/bip39.rs | 6 +++--- src/keys/mod.rs | 16 ++++++---------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index 38cc8d8e..1fad0bae 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -31,7 +31,7 @@ use std::collections::{BTreeMap, HashMap}; use std::fmt; use bitcoin::secp256k1::Secp256k1; -use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPubKey, Fingerprint}; +use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPubKey, Fingerprint, KeySource}; use bitcoin::util::psbt; use bitcoin::{Network, PublicKey, Script, TxOut}; @@ -65,7 +65,7 @@ pub type ExtendedDescriptor = Descriptor; /// /// [`psbt::Input`]: bitcoin::util::psbt::Input /// [`psbt::Output`]: bitcoin::util::psbt::Output -pub type HDKeyPaths = BTreeMap; +pub type HDKeyPaths = BTreeMap; #[allow(missing_docs)] // TODO add missing docs and remove this allow /// Trait for types which can be converted into an [`ExtendedDescriptor`] and a [`KeyMap`] usable by a wallet in a specific [`Network`] diff --git a/src/keys/bip39.rs b/src/keys/bip39.rs index b7f7765e..b3223740 100644 --- a/src/keys/bip39.rs +++ b/src/keys/bip39.rs @@ -43,7 +43,7 @@ pub type MnemonicWithPassphrase = (Mnemonic, Option); impl DerivableKey for Seed { fn add_metadata( self, - source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + source: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { let xprv = bip32::ExtendedPrivKey::new_master(Network::Bitcoin, &self.as_bytes())?; @@ -60,7 +60,7 @@ impl DerivableKey for Seed { impl DerivableKey for MnemonicWithPassphrase { fn add_metadata( self, - source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + source: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { let (mnemonic, passphrase) = self; @@ -73,7 +73,7 @@ impl DerivableKey for MnemonicWithPassphrase { impl DerivableKey for Mnemonic { fn add_metadata( self, - source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + source: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { (self, None).add_metadata(source, derivation_path) diff --git a/src/keys/mod.rs b/src/keys/mod.rs index f8e36aad..807bbe64 100644 --- a/src/keys/mod.rs +++ b/src/keys/mod.rs @@ -296,7 +296,7 @@ pub trait ToDescriptorKey: Sized { /// When extra metadata are provided, a [`DerivableKey`] can be transofrmed into a /// [`DescriptorKey`]: the trait [`ToDescriptorKey`] is automatically implemented /// for `(DerivableKey, DerivationPath)` and -/// `(DerivableKey, (Fingerprint, DerivationPath), DerivationPath)` tuples. +/// `(DerivableKey, KeySource, DerivationPath)` tuples. /// /// For key types that don't encode any indication about the path to use (like bip39), it's /// generally recommended to implemented this trait instead of [`ToDescriptorKey`]. The same @@ -307,7 +307,7 @@ pub trait DerivableKey { /// Add a extra metadata, consume `self` and turn it into a [`DescriptorKey`] fn add_metadata( self, - origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + origin: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError>; } @@ -315,7 +315,7 @@ pub trait DerivableKey { impl DerivableKey for bip32::ExtendedPubKey { fn add_metadata( self, - origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + origin: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { DescriptorPublicKey::XPub(DescriptorXKey { @@ -331,7 +331,7 @@ impl DerivableKey for bip32::ExtendedPubKey { impl DerivableKey for bip32::ExtendedPrivKey { fn add_metadata( self, - origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + origin: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { DescriptorSecretKey::XPrv(DescriptorXKey { @@ -383,7 +383,7 @@ where { fn add_metadata( self, - origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, + origin: Option, derivation_path: bip32::DerivationPath, ) -> Result, KeyError> { let descriptor_key = self.key.add_metadata(origin, derivation_path)?; @@ -528,11 +528,7 @@ impl> ToDescriptorKey for (T, bip3 } impl> ToDescriptorKey - for ( - T, - (bip32::Fingerprint, bip32::DerivationPath), - bip32::DerivationPath, - ) + for (T, bip32::KeySource, bip32::DerivationPath) { fn to_descriptor_key(self) -> Result, KeyError> { self.0.add_metadata(Some(self.1), self.2)