2020-09-18 16:31:03 +02:00
|
|
|
// Magical Bitcoin Library
|
|
|
|
// Written in 2020 by
|
|
|
|
// Alekos Filini <alekos.filini@gmail.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2020 Magical Bitcoin
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
//! Key formats
|
|
|
|
|
|
|
|
use bitcoin::util::bip32;
|
|
|
|
use bitcoin::{PrivateKey, PublicKey};
|
|
|
|
|
|
|
|
use miniscript::descriptor::{DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, KeyMap};
|
|
|
|
|
|
|
|
use crate::Error;
|
|
|
|
|
2020-09-18 17:26:58 +02:00
|
|
|
#[cfg(feature = "keys-bip39")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "keys-bip39")))]
|
|
|
|
pub mod bip39;
|
|
|
|
|
|
|
|
/// Container for public or secret keys
|
2020-09-18 16:31:03 +02:00
|
|
|
pub enum DescriptorKey {
|
|
|
|
Public(DescriptorPublicKey),
|
|
|
|
Secret(DescriptorSecretKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DescriptorKey {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn into_key_and_secret(self) -> Result<(DescriptorPublicKey, KeyMap), Error> {
|
|
|
|
match self {
|
|
|
|
DescriptorKey::Public(public) => Ok((public, KeyMap::default())),
|
|
|
|
DescriptorKey::Secret(secret) => {
|
|
|
|
let mut key_map = KeyMap::with_capacity(1);
|
|
|
|
|
|
|
|
let public = secret
|
|
|
|
.as_public()
|
|
|
|
.map_err(|e| miniscript::Error::Unexpected(e.to_string()))?;
|
|
|
|
key_map.insert(public.clone(), secret);
|
|
|
|
|
|
|
|
Ok((public, key_map))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:26:58 +02:00
|
|
|
/// Trait for objects that can be turned into a public or secret [`DescriptorKey`]
|
2020-09-18 16:31:03 +02:00
|
|
|
pub trait ToDescriptorKey {
|
2020-09-18 17:26:58 +02:00
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identity conversion. This is used internally by [`bdk::fragment`]
|
|
|
|
impl ToDescriptorKey for DescriptorKey {
|
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(self)
|
|
|
|
}
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDescriptorKey for DescriptorPublicKey {
|
2020-09-18 17:26:58 +02:00
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Public(self))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDescriptorKey for PublicKey {
|
2020-09-18 17:26:58 +02:00
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Public(DescriptorPublicKey::PubKey(self)))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:26:58 +02:00
|
|
|
impl ToDescriptorKey for (bip32::ExtendedPubKey, bip32::DerivationPath) {
|
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Public(DescriptorPublicKey::XPub(
|
|
|
|
DescriptorXKey {
|
|
|
|
source: None,
|
|
|
|
xkey: self.0,
|
|
|
|
derivation_path: self.1,
|
|
|
|
is_wildcard: true,
|
|
|
|
},
|
|
|
|
)))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDescriptorKey for DescriptorSecretKey {
|
2020-09-18 17:26:58 +02:00
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Secret(self))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDescriptorKey for PrivateKey {
|
2020-09-18 17:26:58 +02:00
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Secret(DescriptorSecretKey::PrivKey(self)))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:26:58 +02:00
|
|
|
impl ToDescriptorKey for (bip32::ExtendedPrivKey, bip32::DerivationPath) {
|
|
|
|
fn to_descriptor_key(self) -> Result<DescriptorKey, Error> {
|
|
|
|
Ok(DescriptorKey::Secret(DescriptorSecretKey::XPrv(
|
|
|
|
DescriptorXKey {
|
|
|
|
source: None,
|
|
|
|
xkey: self.0,
|
|
|
|
derivation_path: self.1,
|
|
|
|
is_wildcard: true,
|
|
|
|
},
|
|
|
|
)))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|