2020-08-31 11:26:36 +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.
|
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
//! Descriptors
|
|
|
|
//!
|
|
|
|
//! This module contains generic utilities to work with descriptors, plus some re-exported types
|
|
|
|
//! from [`miniscript`].
|
|
|
|
|
2020-08-12 12:51:50 +02:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2021-02-02 20:06:40 -05:00
|
|
|
use std::ops::Deref;
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
use bitcoin::util::bip32::{
|
|
|
|
ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint, KeySource,
|
|
|
|
};
|
2020-08-12 12:51:50 +02:00
|
|
|
use bitcoin::util::psbt;
|
2020-09-21 15:44:07 +02:00
|
|
|
use bitcoin::{Network, PublicKey, Script, TxOut};
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
use miniscript::descriptor::{DescriptorPublicKey, DescriptorType, DescriptorXKey, Wildcard};
|
|
|
|
pub use miniscript::{descriptor::KeyMap, Descriptor, Legacy, Miniscript, ScriptContext, Segwitv0};
|
|
|
|
use miniscript::{DescriptorTrait, ForEachKey, TranslatePk};
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2020-02-15 21:27:51 +01:00
|
|
|
pub mod checksum;
|
2021-02-02 20:06:40 -05:00
|
|
|
pub(crate) mod derived;
|
2020-12-16 16:10:22 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod dsl;
|
2020-01-27 22:02:55 +01:00
|
|
|
pub mod error;
|
2020-02-07 23:22:28 +01:00
|
|
|
pub mod policy;
|
2020-09-22 16:12:09 +02:00
|
|
|
pub mod template;
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2020-02-15 21:27:51 +01:00
|
|
|
pub use self::checksum::get_checksum;
|
2021-02-02 20:06:40 -05:00
|
|
|
use self::derived::AsDerived;
|
|
|
|
pub use self::derived::DerivedDescriptorKey;
|
2021-01-11 13:12:01 +01:00
|
|
|
pub use self::error::Error as DescriptorError;
|
2020-02-15 21:27:51 +01:00
|
|
|
pub use self::policy::Policy;
|
2020-12-10 11:38:42 +01:00
|
|
|
use self::template::DescriptorTemplateOut;
|
2021-02-12 23:02:13 -08:00
|
|
|
use crate::keys::{IntoDescriptorKey, KeyError};
|
2020-08-15 23:21:13 +02:00
|
|
|
use crate::wallet::signer::SignersContainer;
|
2021-02-02 20:06:40 -05:00
|
|
|
use crate::wallet::utils::SecpCtx;
|
2020-02-07 23:22:28 +01:00
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
/// Alias for a [`Descriptor`] that can contain extended keys using [`DescriptorPublicKey`]
|
2020-08-12 12:51:50 +02:00
|
|
|
pub type ExtendedDescriptor = Descriptor<DescriptorPublicKey>;
|
2020-09-04 11:44:49 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
/// Alias for a [`Descriptor`] that contains extended **derived** keys
|
|
|
|
pub type DerivedDescriptor<'s> = Descriptor<DerivedDescriptorKey<'s>>;
|
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
/// Alias for the type of maps that represent derivation paths in a [`psbt::Input`] or
|
|
|
|
/// [`psbt::Output`]
|
|
|
|
///
|
|
|
|
/// [`psbt::Input`]: bitcoin::util::psbt::Input
|
|
|
|
/// [`psbt::Output`]: bitcoin::util::psbt::Output
|
2020-12-11 11:14:30 +01:00
|
|
|
pub type HDKeyPaths = BTreeMap<PublicKey, KeySource>;
|
2020-05-10 17:42:02 +02:00
|
|
|
|
2020-09-21 15:44:07 +02:00
|
|
|
/// Trait for types which can be converted into an [`ExtendedDescriptor`] and a [`KeyMap`] usable by a wallet in a specific [`Network`]
|
2021-02-12 22:34:43 -08:00
|
|
|
pub trait IntoWalletDescriptor {
|
2020-12-13 20:36:38 -08:00
|
|
|
/// Convert to wallet descriptor
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError>;
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
impl IntoWalletDescriptor for &str {
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
|
2020-10-07 14:18:50 -07:00
|
|
|
let descriptor = if self.contains('#') {
|
|
|
|
let parts: Vec<&str> = self.splitn(2, '#').collect();
|
2020-09-21 15:44:07 +02:00
|
|
|
if !get_checksum(parts[0])
|
|
|
|
.ok()
|
|
|
|
.map(|computed| computed == parts[1])
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-01-11 13:12:01 +01:00
|
|
|
return Err(DescriptorError::InvalidDescriptorChecksum);
|
2020-09-21 15:44:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parts[0]
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
};
|
|
|
|
|
2021-02-11 11:00:48 -08:00
|
|
|
ExtendedDescriptor::parse_descriptor(secp, descriptor)?
|
|
|
|
.into_wallet_descriptor(secp, network)
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
impl IntoWalletDescriptor for &String {
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
|
2021-02-11 11:00:48 -08:00
|
|
|
self.as_str().into_wallet_descriptor(secp, network)
|
2020-09-21 15:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
impl IntoWalletDescriptor for ExtendedDescriptor {
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
|
2021-02-11 11:00:48 -08:00
|
|
|
(self, KeyMap::default()).into_wallet_descriptor(secp, network)
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
impl IntoWalletDescriptor for (ExtendedDescriptor, KeyMap) {
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
|
2020-09-21 15:44:07 +02:00
|
|
|
use crate::keys::DescriptorKey;
|
|
|
|
|
2020-10-09 12:03:47 +02:00
|
|
|
let check_key = |pk: &DescriptorPublicKey| {
|
|
|
|
let (pk, _, networks) = if self.0.is_witness() {
|
|
|
|
let desciptor_key: DescriptorKey<miniscript::Segwitv0> =
|
2021-02-11 11:00:48 -08:00
|
|
|
pk.clone().into_descriptor_key()?;
|
2020-11-16 22:07:38 +01:00
|
|
|
desciptor_key.extract(&secp)?
|
2020-10-09 12:03:47 +02:00
|
|
|
} else {
|
|
|
|
let desciptor_key: DescriptorKey<miniscript::Legacy> =
|
2021-02-11 11:00:48 -08:00
|
|
|
pk.clone().into_descriptor_key()?;
|
2020-11-16 22:07:38 +01:00
|
|
|
desciptor_key.extract(&secp)?
|
2020-10-09 12:03:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if networks.contains(&network) {
|
|
|
|
Ok(pk)
|
|
|
|
} else {
|
2021-01-11 13:12:01 +01:00
|
|
|
Err(DescriptorError::Key(KeyError::InvalidNetwork))
|
2020-10-09 12:03:47 +02:00
|
|
|
}
|
|
|
|
};
|
2020-09-21 15:44:07 +02:00
|
|
|
|
2020-10-09 12:03:47 +02:00
|
|
|
// check the network for the keys
|
|
|
|
let translated = self.0.translate_pk(check_key, check_key)?;
|
2020-09-21 15:44:07 +02:00
|
|
|
|
|
|
|
Ok((translated, self.1))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
impl IntoWalletDescriptor for DescriptorTemplateOut {
|
2021-02-11 11:00:48 -08:00
|
|
|
fn into_wallet_descriptor(
|
2020-09-21 15:44:07 +02:00
|
|
|
self,
|
2021-02-02 20:06:40 -05:00
|
|
|
_secp: &SecpCtx,
|
2020-09-21 15:44:07 +02:00
|
|
|
network: Network,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
|
2020-09-21 15:44:07 +02:00
|
|
|
let valid_networks = &self.2;
|
|
|
|
|
2020-10-09 12:03:47 +02:00
|
|
|
let fix_key = |pk: &DescriptorPublicKey| {
|
|
|
|
if valid_networks.contains(&network) {
|
|
|
|
// workaround for xpubs generated by other key types, like bip39: since when the
|
|
|
|
// conversion is made one network has to be chosen, what we generally choose
|
|
|
|
// "mainnet", but then override the set of valid networks to specify that all of
|
|
|
|
// them are valid. here we reset the network to make sure the wallet struct gets a
|
|
|
|
// descriptor with the right network everywhere.
|
|
|
|
let pk = match pk {
|
|
|
|
DescriptorPublicKey::XPub(ref xpub) => {
|
|
|
|
let mut xpub = xpub.clone();
|
|
|
|
xpub.xkey.network = network;
|
|
|
|
|
|
|
|
DescriptorPublicKey::XPub(xpub)
|
|
|
|
}
|
|
|
|
other => other.clone(),
|
|
|
|
};
|
2020-09-21 15:44:07 +02:00
|
|
|
|
2020-10-09 12:03:47 +02:00
|
|
|
Ok(pk)
|
|
|
|
} else {
|
2021-01-11 13:12:01 +01:00
|
|
|
Err(DescriptorError::Key(KeyError::InvalidNetwork))
|
2020-10-09 12:03:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// fixup the network for keys that need it
|
|
|
|
let translated = self.0.translate_pk(fix_key, fix_key)?;
|
2020-09-21 15:44:07 +02:00
|
|
|
|
|
|
|
Ok((translated, self.1))
|
2020-09-18 16:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 13:12:01 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// Used internally mainly by the `descriptor!()` and `fragment!()` macros
|
|
|
|
pub trait CheckMiniscript<Ctx: miniscript::ScriptContext> {
|
|
|
|
fn check_minsicript(&self) -> Result<(), miniscript::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Ctx: miniscript::ScriptContext, Pk: miniscript::MiniscriptKey> CheckMiniscript<Ctx>
|
|
|
|
for miniscript::Miniscript<Pk, Ctx>
|
|
|
|
{
|
|
|
|
fn check_minsicript(&self) -> Result<(), miniscript::Error> {
|
|
|
|
Ctx::check_global_validity(self)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
/// Trait implemented on [`Descriptor`]s to add a method to extract the spending [`policy`]
|
2020-08-12 12:51:50 +02:00
|
|
|
pub trait ExtractPolicy {
|
2020-12-13 20:36:38 -08:00
|
|
|
/// Extract the spending [`policy`]
|
2020-11-16 22:07:38 +01:00
|
|
|
fn extract_policy(
|
|
|
|
&self,
|
2020-11-17 12:05:32 -06:00
|
|
|
signers: &SignersContainer,
|
2020-11-16 22:07:38 +01:00
|
|
|
secp: &SecpCtx,
|
2021-01-11 13:12:01 +01:00
|
|
|
) -> Result<Option<Policy>, DescriptorError>;
|
2020-02-07 23:22:28 +01:00
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
pub(crate) trait XKeyUtils {
|
2020-08-12 12:51:50 +02:00
|
|
|
fn full_path(&self, append: &[ChildNumber]) -> DerivationPath;
|
2020-11-16 22:07:38 +01:00
|
|
|
fn root_fingerprint(&self, secp: &SecpCtx) -> Fingerprint;
|
2020-02-15 21:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
// FIXME: `InnerXKey` was made private in rust-miniscript, so we have to implement this manually on
|
|
|
|
// both `ExtendedPubKey` and `ExtendedPrivKey`.
|
|
|
|
//
|
|
|
|
// Revert back to using the trait once https://github.com/rust-bitcoin/rust-miniscript/pull/230 is
|
|
|
|
// released
|
|
|
|
impl XKeyUtils for DescriptorXKey<ExtendedPubKey> {
|
2020-08-12 12:51:50 +02:00
|
|
|
fn full_path(&self, append: &[ChildNumber]) -> DerivationPath {
|
2020-10-09 12:03:47 +02:00
|
|
|
let full_path = match self.origin {
|
2020-10-07 14:18:50 -07:00
|
|
|
Some((_, ref path)) => path
|
2020-08-12 12:51:50 +02:00
|
|
|
.into_iter()
|
|
|
|
.chain(self.derivation_path.into_iter())
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
2020-10-07 14:18:50 -07:00
|
|
|
None => self.derivation_path.clone(),
|
2020-08-12 12:51:50 +02:00
|
|
|
};
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
if self.wildcard != Wildcard::None {
|
|
|
|
full_path
|
|
|
|
.into_iter()
|
|
|
|
.chain(append.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
full_path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn root_fingerprint(&self, _: &SecpCtx) -> Fingerprint {
|
|
|
|
match self.origin {
|
|
|
|
Some((fingerprint, _)) => fingerprint,
|
|
|
|
None => self.xkey.fingerprint(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl XKeyUtils for DescriptorXKey<ExtendedPrivKey> {
|
|
|
|
fn full_path(&self, append: &[ChildNumber]) -> DerivationPath {
|
|
|
|
let full_path = match self.origin {
|
|
|
|
Some((_, ref path)) => path
|
|
|
|
.into_iter()
|
|
|
|
.chain(self.derivation_path.into_iter())
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
|
|
|
None => self.derivation_path.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.wildcard != Wildcard::None {
|
2020-08-12 12:51:50 +02:00
|
|
|
full_path
|
|
|
|
.into_iter()
|
2020-10-07 14:18:50 -07:00
|
|
|
.chain(append.iter())
|
2020-08-12 12:51:50 +02:00
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
full_path
|
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2020-11-16 22:07:38 +01:00
|
|
|
fn root_fingerprint(&self, secp: &SecpCtx) -> Fingerprint {
|
2020-10-09 12:03:47 +02:00
|
|
|
match self.origin {
|
2020-10-07 14:18:50 -07:00
|
|
|
Some((fingerprint, _)) => fingerprint,
|
2021-02-02 20:06:40 -05:00
|
|
|
None => self.xkey.fingerprint(secp),
|
2020-08-12 12:51:50 +02:00
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
pub(crate) trait DerivedDescriptorMeta {
|
|
|
|
fn get_hd_keypaths(&self, secp: &SecpCtx) -> Result<HDKeyPaths, DescriptorError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait DescriptorMeta {
|
2020-08-12 12:51:50 +02:00
|
|
|
fn is_witness(&self) -> bool;
|
2021-01-11 13:12:01 +01:00
|
|
|
fn get_extended_keys(&self) -> Result<Vec<DescriptorXKey<ExtendedPubKey>>, DescriptorError>;
|
2021-02-02 20:06:40 -05:00
|
|
|
fn derive_from_hd_keypaths<'s>(
|
|
|
|
&self,
|
|
|
|
hd_keypaths: &HDKeyPaths,
|
|
|
|
secp: &'s SecpCtx,
|
|
|
|
) -> Option<DerivedDescriptor<'s>>;
|
|
|
|
fn derive_from_psbt_input<'s>(
|
2020-11-16 22:07:38 +01:00
|
|
|
&self,
|
|
|
|
psbt_input: &psbt::Input,
|
|
|
|
utxo: Option<TxOut>,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &'s SecpCtx,
|
|
|
|
) -> Option<DerivedDescriptor<'s>>;
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:49 +02:00
|
|
|
pub(crate) trait DescriptorScripts {
|
2021-02-02 20:06:40 -05:00
|
|
|
fn psbt_redeem_script(&self) -> Option<Script>;
|
|
|
|
fn psbt_witness_script(&self) -> Option<Script>;
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
impl<'s> DescriptorScripts for DerivedDescriptor<'s> {
|
|
|
|
fn psbt_redeem_script(&self) -> Option<Script> {
|
|
|
|
match self.desc_type() {
|
|
|
|
DescriptorType::ShWpkh => Some(self.explicit_script()),
|
|
|
|
DescriptorType::ShWsh => Some(self.explicit_script().to_v0_p2wsh()),
|
|
|
|
DescriptorType::Sh => Some(self.explicit_script()),
|
|
|
|
DescriptorType::Bare => Some(self.explicit_script()),
|
|
|
|
DescriptorType::ShSortedMulti => Some(self.explicit_script()),
|
2020-01-27 22:02:55 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
fn psbt_witness_script(&self) -> Option<Script> {
|
|
|
|
match self.desc_type() {
|
|
|
|
DescriptorType::Wsh => Some(self.explicit_script()),
|
|
|
|
DescriptorType::ShWsh => Some(self.explicit_script()),
|
|
|
|
DescriptorType::WshSortedMulti | DescriptorType::ShWshSortedMulti => {
|
|
|
|
Some(self.explicit_script())
|
2020-11-16 22:07:38 +01:00
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
impl DescriptorMeta for ExtendedDescriptor {
|
2020-08-12 12:51:50 +02:00
|
|
|
fn is_witness(&self) -> bool {
|
2021-02-02 20:06:40 -05:00
|
|
|
matches!(
|
|
|
|
self.desc_type(),
|
|
|
|
DescriptorType::Wpkh
|
|
|
|
| DescriptorType::ShWpkh
|
|
|
|
| DescriptorType::Wsh
|
|
|
|
| DescriptorType::ShWsh
|
|
|
|
| DescriptorType::ShWshSortedMulti
|
|
|
|
| DescriptorType::WshSortedMulti
|
|
|
|
)
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 13:12:01 +01:00
|
|
|
fn get_extended_keys(&self) -> Result<Vec<DescriptorXKey<ExtendedPubKey>>, DescriptorError> {
|
2021-02-02 20:06:40 -05:00
|
|
|
let mut answer = Vec::new();
|
2020-11-30 15:13:33 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
self.for_each_key(|pk| {
|
|
|
|
if let DescriptorPublicKey::XPub(xpub) = pk.as_key() {
|
|
|
|
answer.push(xpub.clone());
|
2020-08-12 12:51:50 +02:00
|
|
|
}
|
2020-07-19 19:24:05 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
true
|
|
|
|
});
|
2020-08-12 12:51:50 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
Ok(answer)
|
2020-02-15 21:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
fn derive_from_hd_keypaths<'s>(
|
|
|
|
&self,
|
|
|
|
hd_keypaths: &HDKeyPaths,
|
|
|
|
secp: &'s SecpCtx,
|
|
|
|
) -> Option<DerivedDescriptor<'s>> {
|
|
|
|
let index: HashMap<_, _> = hd_keypaths.values().map(|(a, b)| (a, b)).collect();
|
|
|
|
|
|
|
|
let mut path_found = None;
|
|
|
|
self.for_each_key(|key| {
|
|
|
|
if path_found.is_some() {
|
2020-08-12 12:51:50 +02:00
|
|
|
// already found a matching path, we are done
|
2021-02-02 20:06:40 -05:00
|
|
|
return true;
|
2020-02-15 21:27:51 +01:00
|
|
|
}
|
2020-08-12 12:51:50 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
if let DescriptorPublicKey::XPub(xpub) = key.as_key().deref() {
|
2020-08-12 12:51:50 +02:00
|
|
|
// Check if the key matches one entry in our `index`. If it does, `matches()` will
|
|
|
|
// return the "prefix" that matched, so we remove that prefix from the full path
|
2020-10-09 12:03:47 +02:00
|
|
|
// found in `index` and save it in `derive_path`. We expect this to be a derivation
|
2021-02-02 20:06:40 -05:00
|
|
|
// path of length 1 if the key is `wildcard` and an empty path otherwise.
|
2020-11-16 22:07:38 +01:00
|
|
|
let root_fingerprint = xpub.root_fingerprint(secp);
|
2020-10-09 12:03:47 +02:00
|
|
|
let derivation_path: Option<Vec<ChildNumber>> = index
|
2020-08-12 12:51:50 +02:00
|
|
|
.get_key_value(&root_fingerprint)
|
2020-11-16 22:07:38 +01:00
|
|
|
.and_then(|(fingerprint, path)| {
|
2021-02-02 20:06:40 -05:00
|
|
|
xpub.matches(&(**fingerprint, (*path).clone()), secp)
|
2020-11-16 22:07:38 +01:00
|
|
|
})
|
2020-08-12 12:51:50 +02:00
|
|
|
.map(|prefix| {
|
|
|
|
index
|
2020-11-16 22:07:38 +01:00
|
|
|
.get(&xpub.root_fingerprint(secp))
|
2020-08-12 12:51:50 +02:00
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
2020-09-29 18:18:50 +02:00
|
|
|
.skip(prefix.into_iter().count())
|
2020-08-12 12:51:50 +02:00
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
2020-10-09 12:03:47 +02:00
|
|
|
|
|
|
|
match derivation_path {
|
2021-02-02 20:06:40 -05:00
|
|
|
Some(path) if xpub.wildcard != Wildcard::None && path.len() == 1 => {
|
|
|
|
// Ignore hardened wildcards
|
|
|
|
if let ChildNumber::Normal { index } = path[0] {
|
|
|
|
path_found = Some(index)
|
|
|
|
}
|
2020-10-09 12:03:47 +02:00
|
|
|
}
|
2021-02-02 20:06:40 -05:00
|
|
|
Some(path) if xpub.wildcard == Wildcard::None && path.is_empty() => {
|
|
|
|
path_found = Some(0)
|
2020-10-09 12:03:47 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-02-15 21:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
true
|
|
|
|
});
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
path_found.map(|path| self.as_derived(path, secp))
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
fn derive_from_psbt_input<'s>(
|
2020-01-27 22:02:55 +01:00
|
|
|
&self,
|
2020-08-12 12:51:50 +02:00
|
|
|
psbt_input: &psbt::Input,
|
|
|
|
utxo: Option<TxOut>,
|
2021-02-02 20:06:40 -05:00
|
|
|
secp: &'s SecpCtx,
|
|
|
|
) -> Option<DerivedDescriptor<'s>> {
|
|
|
|
if let Some(derived) = self.derive_from_hd_keypaths(&psbt_input.bip32_derivation, secp) {
|
2020-08-12 12:51:50 +02:00
|
|
|
return Some(derived);
|
2021-02-02 20:06:40 -05:00
|
|
|
}
|
|
|
|
if self.is_deriveable() {
|
|
|
|
// We can't try to bruteforce the derivation index, exit here
|
2020-08-12 12:51:50 +02:00
|
|
|
return None;
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
let descriptor = self.as_derived_fixed(secp);
|
|
|
|
match descriptor.desc_type() {
|
|
|
|
// TODO: add pk() here
|
|
|
|
DescriptorType::Pkh | DescriptorType::Wpkh | DescriptorType::ShWpkh
|
2020-08-12 12:51:50 +02:00
|
|
|
if utxo.is_some()
|
2021-02-02 20:06:40 -05:00
|
|
|
&& descriptor.script_pubkey() == utxo.as_ref().unwrap().script_pubkey =>
|
2020-11-16 22:07:38 +01:00
|
|
|
{
|
2021-02-02 20:06:40 -05:00
|
|
|
Some(descriptor)
|
2020-11-16 22:07:38 +01:00
|
|
|
}
|
2021-02-02 20:06:40 -05:00
|
|
|
DescriptorType::Bare | DescriptorType::Sh | DescriptorType::ShSortedMulti
|
2020-11-16 22:07:38 +01:00
|
|
|
if psbt_input.redeem_script.is_some()
|
2021-02-02 20:06:40 -05:00
|
|
|
&& &descriptor.explicit_script()
|
|
|
|
== psbt_input.redeem_script.as_ref().unwrap() =>
|
2020-11-16 22:07:38 +01:00
|
|
|
{
|
2021-02-02 20:06:40 -05:00
|
|
|
Some(descriptor)
|
2020-11-16 22:07:38 +01:00
|
|
|
}
|
2021-02-02 20:06:40 -05:00
|
|
|
DescriptorType::Wsh
|
|
|
|
| DescriptorType::ShWsh
|
|
|
|
| DescriptorType::ShWshSortedMulti
|
|
|
|
| DescriptorType::WshSortedMulti
|
2020-11-16 22:07:38 +01:00
|
|
|
if psbt_input.witness_script.is_some()
|
2021-02-02 20:06:40 -05:00
|
|
|
&& &descriptor.explicit_script()
|
|
|
|
== psbt_input.witness_script.as_ref().unwrap() =>
|
2020-08-12 12:51:50 +02:00
|
|
|
{
|
2021-02-02 20:06:40 -05:00
|
|
|
Some(descriptor)
|
2020-08-12 12:51:50 +02:00
|
|
|
}
|
|
|
|
_ => None,
|
2020-02-17 14:22:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-07 23:22:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
impl<'s> DerivedDescriptorMeta for DerivedDescriptor<'s> {
|
|
|
|
fn get_hd_keypaths(&self, secp: &SecpCtx) -> Result<HDKeyPaths, DescriptorError> {
|
|
|
|
let mut answer = BTreeMap::new();
|
|
|
|
self.for_each_key(|key| {
|
|
|
|
if let DescriptorPublicKey::XPub(xpub) = key.as_key().deref() {
|
|
|
|
let derived_pubkey = xpub
|
|
|
|
.xkey
|
|
|
|
.derive_pub(secp, &xpub.derivation_path)
|
|
|
|
.expect("Derivation can't fail");
|
|
|
|
|
|
|
|
answer.insert(
|
|
|
|
derived_pubkey.public_key,
|
|
|
|
(xpub.root_fingerprint(secp), xpub.full_path(&[])),
|
|
|
|
);
|
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
true
|
|
|
|
});
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
Ok(answer)
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2020-08-12 12:51:50 +02:00
|
|
|
use bitcoin::consensus::encode::deserialize;
|
2020-01-27 22:02:55 +01:00
|
|
|
use bitcoin::hashes::hex::FromHex;
|
2020-11-16 22:07:38 +01:00
|
|
|
use bitcoin::secp256k1::Secp256k1;
|
2020-09-21 15:44:07 +02:00
|
|
|
use bitcoin::util::{bip32, psbt};
|
2020-01-27 22:02:55 +01:00
|
|
|
|
2020-08-12 12:51:50 +02:00
|
|
|
use super::*;
|
|
|
|
use crate::psbt::PSBTUtils;
|
2020-01-27 22:02:55 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-08-15 21:24:13 +02:00
|
|
|
fn test_derive_from_psbt_input_wpkh_wif() {
|
2020-08-12 12:51:50 +02:00
|
|
|
let descriptor = Descriptor::<DescriptorPublicKey>::from_str(
|
|
|
|
"wpkh(02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737)",
|
2020-01-27 22:02:55 +01:00
|
|
|
)
|
2020-08-12 12:51:50 +02:00
|
|
|
.unwrap();
|
2020-08-15 21:24:13 +02:00
|
|
|
let psbt: psbt::PartiallySignedTransaction = deserialize(
|
|
|
|
&Vec::<u8>::from_hex(
|
|
|
|
"70736274ff010052010000000162307be8e431fbaff807cdf9cdc3fde44d7402\
|
|
|
|
11bc8342c31ffd6ec11fe35bcc0100000000ffffffff01328601000000000016\
|
|
|
|
001493ce48570b55c42c2af816aeaba06cfee1224fae000000000001011fa086\
|
|
|
|
01000000000016001493ce48570b55c42c2af816aeaba06cfee1224fae010304\
|
|
|
|
010000000000",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(descriptor
|
2020-11-16 22:07:38 +01:00
|
|
|
.derive_from_psbt_input(&psbt.inputs[0], psbt.get_utxo_for(0), &Secp256k1::new())
|
2020-08-15 21:24:13 +02:00
|
|
|
.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_derive_from_psbt_input_pkh_tpub() {
|
|
|
|
let descriptor = Descriptor::<DescriptorPublicKey>::from_str(
|
|
|
|
"pkh([0f056943/44h/0h/0h]tpubDDpWvmUrPZrhSPmUzCMBHffvC3HyMAPnWDSAQNBTnj1iZeJa7BZQEttFiP4DS4GCcXQHezdXhn86Hj6LHX5EDstXPWrMaSneRWM8yUf6NFd/10/*)",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let psbt: psbt::PartiallySignedTransaction = deserialize(
|
|
|
|
&Vec::<u8>::from_hex(
|
|
|
|
"70736274ff010053010000000145843b86be54a3cd8c9e38444e1162676c00df\
|
|
|
|
e7964122a70df491ea12fd67090100000000ffffffff01c19598000000000017\
|
|
|
|
a91432bb94283282f72b2e034709e348c44d5a4db0ef8700000000000100f902\
|
|
|
|
0000000001010167e99c0eb67640f3a1b6805f2d8be8238c947f8aaf49eb0a9c\
|
|
|
|
bee6a42c984200000000171600142b29a22019cca05b9c2b2d283a4c4489e1cf\
|
|
|
|
9f8ffeffffff02a01dced06100000017a914e2abf033cadbd74f0f4c74946201\
|
|
|
|
decd20d5c43c8780969800000000001976a9148b0fce5fb1264e599a65387313\
|
|
|
|
3c95478b902eb288ac02473044022015d9211576163fa5b001e84dfa3d44efd9\
|
|
|
|
86b8f3a0d3d2174369288b2b750906022048dacc0e5d73ae42512fd2b97e2071\
|
|
|
|
a8d0bce443b390b1fe0b8128fe70ec919e01210232dad1c5a67dcb0116d407e2\
|
|
|
|
52584228ab7ec00e8b9779d0c3ffe8114fc1a7d2c80600000103040100000022\
|
|
|
|
0603433b83583f8c4879b329dd08bbc7da935e4cc02f637ff746e05f0466ffb2\
|
|
|
|
a6a2180f0569432c00008000000080000000800a000000000000000000",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(descriptor
|
2020-11-16 22:07:38 +01:00
|
|
|
.derive_from_psbt_input(&psbt.inputs[0], psbt.get_utxo_for(0), &Secp256k1::new())
|
2020-08-15 21:24:13 +02:00
|
|
|
.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_derive_from_psbt_input_wsh() {
|
|
|
|
let descriptor = Descriptor::<DescriptorPublicKey>::from_str(
|
|
|
|
"wsh(and_v(v:pk(03b6633fef2397a0a9de9d7b6f23aef8368a6e362b0581f0f0af70d5ecfd254b14),older(6)))",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let psbt: psbt::PartiallySignedTransaction = deserialize(
|
|
|
|
&Vec::<u8>::from_hex(
|
|
|
|
"70736274ff01005302000000011c8116eea34408ab6529223c9a176606742207\
|
|
|
|
67a1ff1d46a6e3c4a88243ea6e01000000000600000001109698000000000017\
|
|
|
|
a914ad105f61102e0d01d7af40d06d6a5c3ae2f7fde387000000000001012b80\
|
|
|
|
969800000000002200203ca72f106a72234754890ca7640c43f65d2174e44d33\
|
|
|
|
336030f9059345091044010304010000000105252103b6633fef2397a0a9de9d\
|
|
|
|
7b6f23aef8368a6e362b0581f0f0af70d5ecfd254b14ad56b20000",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(descriptor
|
2020-11-16 22:07:38 +01:00
|
|
|
.derive_from_psbt_input(&psbt.inputs[0], psbt.get_utxo_for(0), &Secp256k1::new())
|
2020-08-15 21:24:13 +02:00
|
|
|
.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_derive_from_psbt_input_sh() {
|
|
|
|
let descriptor = Descriptor::<DescriptorPublicKey>::from_str(
|
|
|
|
"sh(and_v(v:pk(021403881a5587297818fcaf17d239cefca22fce84a45b3b1d23e836c4af671dbb),after(630000)))",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let psbt: psbt::PartiallySignedTransaction = deserialize(
|
|
|
|
&Vec::<u8>::from_hex(
|
|
|
|
"70736274ff0100530100000001bc8c13df445dfadcc42afa6dc841f85d22b01d\
|
|
|
|
a6270ebf981740f4b7b1d800390000000000feffffff01ba9598000000000017\
|
|
|
|
a91457b148ba4d3e5fa8608a8657875124e3d1c9390887f09c0900000100e002\
|
|
|
|
0000000001016ba1bbe05cc93574a0d611ec7d93ad0ab6685b28d0cd80e8a82d\
|
|
|
|
debb326643c90100000000feffffff02809698000000000017a914d9a6e8c455\
|
|
|
|
8e16c8253afe53ce37ad61cf4c38c487403504cf6100000017a9144044fb6e0b\
|
|
|
|
757dfc1b34886b6a95aef4d3db137e870247304402202a9b72d939bcde8ba2a1\
|
|
|
|
e0980597e47af4f5c152a78499143c3d0a78ac2286a602207a45b1df9e93b8c9\
|
|
|
|
6f09f5c025fe3e413ca4b905fe65ee55d32a3276439a9b8f012102dc1fcc2636\
|
|
|
|
4da1aa718f03d8d9bd6f2ff410ed2cf1245a168aa3bcc995ac18e0a806000001\
|
|
|
|
03040100000001042821021403881a5587297818fcaf17d239cefca22fce84a4\
|
|
|
|
5b3b1d23e836c4af671dbbad03f09c09b10000",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-08-12 12:51:50 +02:00
|
|
|
|
2020-08-15 21:24:13 +02:00
|
|
|
assert!(descriptor
|
2020-11-16 22:07:38 +01:00
|
|
|
.derive_from_psbt_input(&psbt.inputs[0], psbt.get_utxo_for(0), &Secp256k1::new())
|
2020-08-15 21:24:13 +02:00
|
|
|
.is_some());
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|
2020-09-21 15:44:07 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_wallet_descriptor_fixup_networks() {
|
2021-02-12 23:02:13 -08:00
|
|
|
use crate::keys::{any_network, IntoDescriptorKey};
|
2020-09-21 15:44:07 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
let secp = Secp256k1::new();
|
|
|
|
|
2020-09-21 15:44:07 +02:00
|
|
|
let xpub = bip32::ExtendedPubKey::from_str("xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL").unwrap();
|
|
|
|
let path = bip32::DerivationPath::from_str("m/0").unwrap();
|
|
|
|
|
|
|
|
// here `to_descriptor_key` will set the valid networks for the key to only mainnet, since
|
|
|
|
// we are using an "xpub"
|
2021-02-11 11:00:48 -08:00
|
|
|
let key = (xpub, path).into_descriptor_key().unwrap();
|
2020-09-21 15:44:07 +02:00
|
|
|
// override it with any. this happens in some key conversions, like bip39
|
|
|
|
let key = key.override_valid_networks(any_network());
|
|
|
|
|
|
|
|
// make a descriptor out of it
|
|
|
|
let desc = crate::descriptor!(wpkh(key)).unwrap();
|
|
|
|
// this should conver the key that supports "any_network" to the right network (testnet)
|
2021-02-11 11:00:48 -08:00
|
|
|
let (wallet_desc, _) = desc
|
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet)
|
|
|
|
.unwrap();
|
2020-09-21 15:44:07 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
assert_eq!(wallet_desc.to_string(), "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)#y8p7e8kk");
|
2020-09-21 15:44:07 +02:00
|
|
|
}
|
2020-09-25 22:21:11 -07:00
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
// test IntoWalletDescriptor trait from &str with and without checksum appended
|
2020-09-25 22:21:11 -07:00
|
|
|
#[test]
|
|
|
|
fn test_descriptor_from_str_with_checksum() {
|
2021-02-02 20:06:40 -05:00
|
|
|
let secp = Secp256k1::new();
|
|
|
|
|
2020-09-25 22:21:11 -07:00
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)#tqz0nc62"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)#67ju93jw"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)#67ju93jw"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2021-01-11 13:12:01 +01:00
|
|
|
assert!(matches!(
|
|
|
|
desc.err(),
|
|
|
|
Some(DescriptorError::InvalidDescriptorChecksum)
|
|
|
|
));
|
2020-09-25 22:21:11 -07:00
|
|
|
|
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)#67ju93jw"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2021-01-11 13:12:01 +01:00
|
|
|
assert!(matches!(
|
|
|
|
desc.err(),
|
|
|
|
Some(DescriptorError::InvalidDescriptorChecksum)
|
|
|
|
));
|
2020-09-25 22:21:11 -07:00
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
// test IntoWalletDescriptor trait from &str with keys from right and wrong network
|
2020-09-25 22:21:11 -07:00
|
|
|
#[test]
|
|
|
|
fn test_descriptor_from_str_with_keys_network() {
|
2021-02-02 20:06:40 -05:00
|
|
|
let secp = Secp256k1::new();
|
|
|
|
|
2020-09-25 22:21:11 -07:00
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Regtest);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Regtest);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "sh(wpkh(02864bb4ad00cefa806098a69e192bbda937494e69eb452b87bb3f20f6283baedb))"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "sh(wpkh(02864bb4ad00cefa806098a69e192bbda937494e69eb452b87bb3f20f6283baedb))"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Bitcoin);
|
2020-09-25 22:21:11 -07:00
|
|
|
assert!(desc.is_ok());
|
|
|
|
|
|
|
|
let desc = "wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Bitcoin);
|
2021-01-11 13:12:01 +01:00
|
|
|
assert!(matches!(
|
|
|
|
desc.err(),
|
|
|
|
Some(DescriptorError::Key(KeyError::InvalidNetwork))
|
|
|
|
));
|
2020-09-25 22:21:11 -07:00
|
|
|
|
|
|
|
let desc = "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)"
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Bitcoin);
|
2021-01-11 13:12:01 +01:00
|
|
|
assert!(matches!(
|
|
|
|
desc.err(),
|
|
|
|
Some(DescriptorError::Key(KeyError::InvalidNetwork))
|
|
|
|
));
|
2020-09-25 22:21:11 -07:00
|
|
|
}
|
|
|
|
|
2021-02-12 22:34:43 -08:00
|
|
|
// test IntoWalletDescriptor trait from the output of the descriptor!() macro
|
2020-09-25 22:21:11 -07:00
|
|
|
#[test]
|
|
|
|
fn test_descriptor_from_str_from_output_of_macro() {
|
2021-02-02 20:06:40 -05:00
|
|
|
let secp = Secp256k1::new();
|
|
|
|
|
2020-09-25 22:21:11 -07:00
|
|
|
let tpub = bip32::ExtendedPubKey::from_str("tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK").unwrap();
|
|
|
|
let path = bip32::DerivationPath::from_str("m/1/2").unwrap();
|
2021-02-11 11:00:48 -08:00
|
|
|
let key = (tpub, path).into_descriptor_key().unwrap();
|
2020-09-25 22:21:11 -07:00
|
|
|
|
|
|
|
// make a descriptor out of it
|
|
|
|
let desc = crate::descriptor!(wpkh(key)).unwrap();
|
|
|
|
|
2021-02-11 11:00:48 -08:00
|
|
|
let (wallet_desc, _) = desc
|
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet)
|
|
|
|
.unwrap();
|
2020-09-25 22:21:11 -07:00
|
|
|
let wallet_desc_str = wallet_desc.to_string();
|
2021-02-02 20:06:40 -05:00
|
|
|
assert_eq!(wallet_desc_str, "wpkh(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/2/*)#67ju93jw");
|
2020-09-25 22:21:11 -07:00
|
|
|
|
|
|
|
let (wallet_desc2, _) = wallet_desc_str
|
2021-02-11 11:00:48 -08:00
|
|
|
.into_wallet_descriptor(&secp, Network::Testnet)
|
2020-09-25 22:21:11 -07:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(wallet_desc, wallet_desc2)
|
|
|
|
}
|
2020-01-27 22:02:55 +01:00
|
|
|
}
|