Skip to content

Use Key instead of MiniscriptKey #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/sign_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() {
assert_eq!(descriptor.max_satisfaction_weight().unwrap(), 258);

// Sometimes it is necessary to have additional information to get the
// `bitcoin::PublicKey` from the `MiniscriptKey` which can be supplied by
// `bitcoin::PublicKey` from the `Key` which can be supplied by
// the `to_pk_ctx` parameter. For example, when calculating the script
// pubkey of a descriptor with xpubs, the secp context and child information
// maybe required.
Expand Down
39 changes: 19 additions & 20 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ use crate::policy::{semantic, Liftable};
use crate::prelude::*;
use crate::util::{varint_len, witness_to_scriptsig};
use crate::{
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
Translator,
BareCtx, Error, ForEachKey, Key, Miniscript, Satisfier, ToPublicKey, TranslatePk, Translator,
};

/// Create a Bare Descriptor. That is descriptor that is
/// not wrapped in sh or wsh. This covers the Pk descriptor
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Bare<Pk: MiniscriptKey> {
pub struct Bare<Pk: Key> {
/// underlying miniscript
ms: Miniscript<Pk, BareCtx>,
}

impl<Pk: MiniscriptKey> Bare<Pk> {
impl<Pk: Key> Bare<Pk> {
/// Create a new raw descriptor
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
// do the top-level checks
Expand Down Expand Up @@ -81,7 +80,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
}
}

impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
impl<Pk: Key + ToPublicKey> Bare<Pk> {
/// Obtains the corresponding script pubkey for this descriptor.
pub fn script_pubkey(&self) -> Script {
self.ms.encode()
Expand Down Expand Up @@ -124,21 +123,21 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
impl<Pk: Key> fmt::Debug for Bare<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.ms)
}
}

impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
impl<Pk: Key> fmt::Display for Bare<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let desc = format!("{}", self.ms);
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
write!(f, "{}#{}", &desc, &checksum)
}
}

impl<Pk: MiniscriptKey> Liftable<Pk> for Bare<Pk> {
impl<Pk: Key> Liftable<Pk> for Bare<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
self.ms.lift()
}
Expand All @@ -163,7 +162,7 @@ impl_from_str!(
}
);

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
impl<Pk: Key> ForEachKey<Pk> for Bare<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Expand All @@ -175,8 +174,8 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {

impl<P, Q> TranslatePk<P, Q> for Bare<P>
where
P: MiniscriptKey,
Q: MiniscriptKey,
P: Key,
Q: Key,
{
type Output = Bare<Q>;

Expand All @@ -190,12 +189,12 @@ where

/// A bare PkH descriptor at top level
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Pkh<Pk: MiniscriptKey> {
pub struct Pkh<Pk: Key> {
/// underlying publickey
pk: Pk,
}

impl<Pk: MiniscriptKey> Pkh<Pk> {
impl<Pk: Key> Pkh<Pk> {
/// Create a new Pkh descriptor
pub fn new(pk: Pk) -> Self {
// do the top-level checks
Expand Down Expand Up @@ -223,7 +222,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
}
}

impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
impl<Pk: Key + ToPublicKey> Pkh<Pk> {
/// Obtains the corresponding script pubkey for this descriptor.
pub fn script_pubkey(&self) -> Script {
// Fine to hard code the `Network` here because we immediately call
Expand Down Expand Up @@ -278,21 +277,21 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
impl<Pk: Key> fmt::Debug for Pkh<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "pkh({:?})", self.pk)
}
}

impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
impl<Pk: Key> fmt::Display for Pkh<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let desc = format!("pkh({})", self.pk);
let checksum = desc_checksum(&desc).map_err(|_| fmt::Error)?;
write!(f, "{}#{}", &desc, &checksum)
}
}

impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
impl<Pk: Key> Liftable<Pk> for Pkh<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
}
Expand Down Expand Up @@ -325,7 +324,7 @@ impl_from_str!(
}
);

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
impl<Pk: Key> ForEachKey<Pk> for Pkh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Expand All @@ -337,8 +336,8 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {

impl<P, Q> TranslatePk<P, Q> for Pkh<P>
where
P: MiniscriptKey,
Q: MiniscriptKey,
P: Key,
Q: Key,
{
type Output = Pkh<Q>;

Expand Down
6 changes: 3 additions & 3 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bitcoin::util::bip32;
use bitcoin::{self, XOnlyPublicKey, XpubIdentifier};

use crate::prelude::*;
use crate::{hash256, MiniscriptKey, ToPublicKey};
use crate::{hash256, Key, ToPublicKey};

/// The descriptor pubkey, either a single pubkey or an xpub.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -729,7 +729,7 @@ impl<K: InnerXKey> DescriptorXKey<K> {
}
}

impl MiniscriptKey for DescriptorPublicKey {
impl Key for DescriptorPublicKey {
// This allows us to be able to derive public keys even for PkH s
type RawPkHash = Self;
type Sha256 = sha256::Hash;
Expand Down Expand Up @@ -817,7 +817,7 @@ impl fmt::Display for DefiniteDescriptorKey {
}
}

impl MiniscriptKey for DefiniteDescriptorKey {
impl Key for DefiniteDescriptorKey {
// This allows us to be able to derive public keys even for PkH s
type RawPkHash = Self;
type Sha256 = sha256::Hash;
Expand Down
32 changes: 16 additions & 16 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use self::checksum::verify_checksum;
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
use crate::prelude::*;
use crate::{
expression, hash256, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator,
Satisfier, ToPublicKey, TranslatePk, Translator,
expression, hash256, miniscript, BareCtx, Error, ForEachKey, Key, PkTranslator, Satisfier,
ToPublicKey, TranslatePk, Translator,
};

mod bare;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;

/// Script descriptor
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Descriptor<Pk: MiniscriptKey> {
pub enum Descriptor<Pk: Key> {
/// A raw scriptpubkey (including pay-to-pubkey) under Legacy context
Bare(Bare<Pk>),
/// Pay-to-PubKey-Hash
Expand All @@ -87,42 +87,42 @@ pub enum Descriptor<Pk: MiniscriptKey> {
Tr(Tr<Pk>),
}

impl<Pk: MiniscriptKey> From<Bare<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Bare<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Bare<Pk>) -> Self {
Descriptor::Bare(inner)
}
}

impl<Pk: MiniscriptKey> From<Pkh<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Pkh<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Pkh<Pk>) -> Self {
Descriptor::Pkh(inner)
}
}

impl<Pk: MiniscriptKey> From<Wpkh<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Wpkh<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Wpkh<Pk>) -> Self {
Descriptor::Wpkh(inner)
}
}

impl<Pk: MiniscriptKey> From<Sh<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Sh<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Sh<Pk>) -> Self {
Descriptor::Sh(inner)
}
}

impl<Pk: MiniscriptKey> From<Wsh<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Wsh<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Wsh<Pk>) -> Self {
Descriptor::Wsh(inner)
}
}

impl<Pk: MiniscriptKey> From<Tr<Pk>> for Descriptor<Pk> {
impl<Pk: Key> From<Tr<Pk>> for Descriptor<Pk> {
#[inline]
fn from(inner: Tr<Pk>) -> Self {
Descriptor::Tr(inner)
Expand Down Expand Up @@ -172,7 +172,7 @@ impl DescriptorType {
}
}

impl<Pk: MiniscriptKey> Descriptor<Pk> {
impl<Pk: Key> Descriptor<Pk> {
// Keys

/// Create a new pk descriptor
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
}
}

impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
impl<Pk: Key + ToPublicKey> Descriptor<Pk> {
/// Computes the Bitcoin address of the descriptor, if one exists
///
/// Some descriptors like pk() don't have an address.
Expand Down Expand Up @@ -472,8 +472,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {

impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
where
P: MiniscriptKey,
Q: MiniscriptKey,
P: Key,
Q: Key,
{
type Output = Descriptor<Q>;

Expand All @@ -494,7 +494,7 @@ where
}
}

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
impl<Pk: Key> ForEachKey<Pk> for Descriptor<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Expand Down Expand Up @@ -821,7 +821,7 @@ impl_from_str!(
}
);

impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
impl<Pk: Key> fmt::Debug for Descriptor<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Descriptor::Bare(ref sub) => write!(f, "{:?}", sub),
Expand All @@ -834,7 +834,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
}
}

impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
impl<Pk: Key> fmt::Display for Descriptor<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Descriptor::Bare(ref sub) => write!(f, "{}", sub),
Expand Down
Loading