[keys] Replace (Fingerprint, DerivationPath) with KeySource

This commit is contained in:
Alekos Filini 2020-12-11 11:14:30 +01:00
parent 1a90832f3a
commit c0a92bd084
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F
3 changed files with 11 additions and 15 deletions

View File

@ -31,7 +31,7 @@ use std::collections::{BTreeMap, HashMap};
use std::fmt; use std::fmt;
use bitcoin::secp256k1::Secp256k1; 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::util::psbt;
use bitcoin::{Network, PublicKey, Script, TxOut}; use bitcoin::{Network, PublicKey, Script, TxOut};
@ -65,7 +65,7 @@ pub type ExtendedDescriptor = Descriptor<DescriptorPublicKey>;
/// ///
/// [`psbt::Input`]: bitcoin::util::psbt::Input /// [`psbt::Input`]: bitcoin::util::psbt::Input
/// [`psbt::Output`]: bitcoin::util::psbt::Output /// [`psbt::Output`]: bitcoin::util::psbt::Output
pub type HDKeyPaths = BTreeMap<PublicKey, (Fingerprint, DerivationPath)>; pub type HDKeyPaths = BTreeMap<PublicKey, KeySource>;
#[allow(missing_docs)] // TODO add missing docs and remove this allow #[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`] /// Trait for types which can be converted into an [`ExtendedDescriptor`] and a [`KeyMap`] usable by a wallet in a specific [`Network`]

View File

@ -43,7 +43,7 @@ pub type MnemonicWithPassphrase = (Mnemonic, Option<String>);
impl<Ctx: ScriptContext> DerivableKey<Ctx> for Seed { impl<Ctx: ScriptContext> DerivableKey<Ctx> for Seed {
fn add_metadata( fn add_metadata(
self, self,
source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, source: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
let xprv = bip32::ExtendedPrivKey::new_master(Network::Bitcoin, &self.as_bytes())?; let xprv = bip32::ExtendedPrivKey::new_master(Network::Bitcoin, &self.as_bytes())?;
@ -60,7 +60,7 @@ impl<Ctx: ScriptContext> DerivableKey<Ctx> for Seed {
impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase { impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase {
fn add_metadata( fn add_metadata(
self, self,
source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, source: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
let (mnemonic, passphrase) = self; let (mnemonic, passphrase) = self;
@ -73,7 +73,7 @@ impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase {
impl<Ctx: ScriptContext> DerivableKey<Ctx> for Mnemonic { impl<Ctx: ScriptContext> DerivableKey<Ctx> for Mnemonic {
fn add_metadata( fn add_metadata(
self, self,
source: Option<(bip32::Fingerprint, bip32::DerivationPath)>, source: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
(self, None).add_metadata(source, derivation_path) (self, None).add_metadata(source, derivation_path)

View File

@ -296,7 +296,7 @@ pub trait ToDescriptorKey<Ctx: ScriptContext>: Sized {
/// When extra metadata are provided, a [`DerivableKey`] can be transofrmed into a /// When extra metadata are provided, a [`DerivableKey`] can be transofrmed into a
/// [`DescriptorKey`]: the trait [`ToDescriptorKey`] is automatically implemented /// [`DescriptorKey`]: the trait [`ToDescriptorKey`] is automatically implemented
/// for `(DerivableKey, DerivationPath)` and /// 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 /// 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 /// generally recommended to implemented this trait instead of [`ToDescriptorKey`]. The same
@ -307,7 +307,7 @@ pub trait DerivableKey<Ctx: ScriptContext> {
/// Add a extra metadata, consume `self` and turn it into a [`DescriptorKey`] /// Add a extra metadata, consume `self` and turn it into a [`DescriptorKey`]
fn add_metadata( fn add_metadata(
self, self,
origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, origin: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError>; ) -> Result<DescriptorKey<Ctx>, KeyError>;
} }
@ -315,7 +315,7 @@ pub trait DerivableKey<Ctx: ScriptContext> {
impl<Ctx: ScriptContext> DerivableKey<Ctx> for bip32::ExtendedPubKey { impl<Ctx: ScriptContext> DerivableKey<Ctx> for bip32::ExtendedPubKey {
fn add_metadata( fn add_metadata(
self, self,
origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, origin: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
DescriptorPublicKey::XPub(DescriptorXKey { DescriptorPublicKey::XPub(DescriptorXKey {
@ -331,7 +331,7 @@ impl<Ctx: ScriptContext> DerivableKey<Ctx> for bip32::ExtendedPubKey {
impl<Ctx: ScriptContext> DerivableKey<Ctx> for bip32::ExtendedPrivKey { impl<Ctx: ScriptContext> DerivableKey<Ctx> for bip32::ExtendedPrivKey {
fn add_metadata( fn add_metadata(
self, self,
origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, origin: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
DescriptorSecretKey::XPrv(DescriptorXKey { DescriptorSecretKey::XPrv(DescriptorXKey {
@ -383,7 +383,7 @@ where
{ {
fn add_metadata( fn add_metadata(
self, self,
origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, origin: Option<bip32::KeySource>,
derivation_path: bip32::DerivationPath, derivation_path: bip32::DerivationPath,
) -> Result<DescriptorKey<Ctx>, KeyError> { ) -> Result<DescriptorKey<Ctx>, KeyError> {
let descriptor_key = self.key.add_metadata(origin, derivation_path)?; let descriptor_key = self.key.add_metadata(origin, derivation_path)?;
@ -528,11 +528,7 @@ impl<Ctx: ScriptContext, T: DerivableKey<Ctx>> ToDescriptorKey<Ctx> for (T, bip3
} }
impl<Ctx: ScriptContext, T: DerivableKey<Ctx>> ToDescriptorKey<Ctx> impl<Ctx: ScriptContext, T: DerivableKey<Ctx>> ToDescriptorKey<Ctx>
for ( for (T, bip32::KeySource, bip32::DerivationPath)
T,
(bip32::Fingerprint, bip32::DerivationPath),
bip32::DerivationPath,
)
{ {
fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError> { fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError> {
self.0.add_metadata(Some(self.1), self.2) self.0.add_metadata(Some(self.1), self.2)