Skip to content
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

Make common generics more terse #573

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use bitcoin::secp256k1;
use internals::hex::exts::DisplayHex;
use miniscript::descriptor::{SinglePub, SinglePubKey};
use miniscript::{
bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext,
TranslatePk, Translator,
bitcoin, hash256, Context, Descriptor, DescriptorPublicKey, Error, Miniscript, TranslatePk,
Translator,
};
use rand::RngCore;
use secp256k1::XOnlyPublicKey;
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn random_pk(mut seed: u8) -> bitcoin::PublicKey {
#[allow(dead_code)]
// https://github.com/rust-lang/rust/issues/46379. The code is pub fn and integration test, but still shows warnings
/// Parse an insane miniscript into a miniscript with the format described above at file header
pub fn parse_insane_ms<Ctx: ScriptContext>(
pub fn parse_insane_ms<Ctx: Context>(
ms: &str,
pubdata: &PubData,
) -> Miniscript<DescriptorPublicKey, Ctx> {
Expand Down
4 changes: 2 additions & 2 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use bitcoin::{
use bitcoind::bitcoincore_rpc::{json, Client, RpcApi};
use miniscript::bitcoin::{self, ecdsa, taproot, ScriptBuf};
use miniscript::psbt::{PsbtExt, PsbtInputExt};
use miniscript::{Descriptor, Miniscript, ScriptContext, ToPublicKey};
use miniscript::{Context, Descriptor, Miniscript, ToPublicKey};
mod setup;

use rand::RngCore;
Expand Down Expand Up @@ -318,7 +318,7 @@ pub fn test_desc_satisfy(
}

// Find all secret corresponding to the known public keys in ms
fn find_sks_ms<Ctx: ScriptContext>(
fn find_sks_ms<Ctx: Context>(
ms: &Miniscript<bitcoin::PublicKey, Ctx>,
testdata: &TestData,
) -> Vec<secp256k1::SecretKey> {
Expand Down
2 changes: 1 addition & 1 deletion examples/sign_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
assert_eq!(descriptor.max_weight_to_satisfy().unwrap(), 253);

// 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
48 changes: 20 additions & 28 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ use bitcoin::{Address, Network, ScriptBuf};

use super::checksum::{self, verify_checksum};
use crate::expression::{self, FromTree};
use crate::miniscript::context::{ScriptContext, ScriptContextError};
use crate::miniscript::context::{Context, ContextError};
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, TranslateErr,
TranslatePk, Translator,
BareCtx, Error, ForEachKey, Key, Miniscript, Satisfier, ToPublicKey, TranslateErr, 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 @@ -91,7 +91,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) -> ScriptBuf {
self.ms.encode()
Expand Down Expand Up @@ -134,13 +134,13 @@ 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 {
use fmt::Write;
let mut wrapped_f = checksum::Formatter::new(f);
Expand All @@ -149,7 +149,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Bare<Pk> {
}
}

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 @@ -174,17 +174,13 @@ 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 {
self.ms.for_each_key(pred)
}
}

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

fn translate_pk<T, E>(&self, t: &mut T) -> Result<Bare<Q>, TranslateErr<E>>
Expand All @@ -197,14 +193,14 @@ 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) -> Result<Self, ScriptContextError> {
pub fn new(pk: Pk) -> Result<Self, ContextError> {
// do the top-level checks
match BareCtx::check_pk(&pk) {
Ok(()) => Ok(Pkh { pk }),
Expand Down Expand Up @@ -254,7 +250,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) -> ScriptBuf {
// Fine to hard code the `Network` here because we immediately call
Expand Down Expand Up @@ -311,13 +307,13 @@ 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 {
use fmt::Write;
let mut wrapped_f = checksum::Formatter::new(f);
Expand All @@ -326,7 +322,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
}
}

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::Key(self.pk.clone()))
}
Expand Down Expand Up @@ -359,17 +355,13 @@ 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 {
pred(&self.pk)
}
}

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

fn translate_pk<T, E>(&self, t: &mut T) -> Result<Self::Output, TranslateErr<E>>
Expand Down
8 changes: 4 additions & 4 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bitcoin::secp256k1::{Secp256k1, Signing, Verification};
use crate::prelude::*;
#[cfg(feature = "serde")]
use crate::serde::{Deserialize, Deserializer, Serialize, Serializer};
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 @@ -982,7 +982,7 @@ impl<K: InnerXKey> DescriptorXKey<K> {
}
}

impl MiniscriptKey for DescriptorPublicKey {
impl Key for DescriptorPublicKey {
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
Expand Down Expand Up @@ -1103,7 +1103,7 @@ impl fmt::Display for DefiniteDescriptorKey {
}
}

impl MiniscriptKey for DefiniteDescriptorKey {
impl Key for DefiniteDescriptorKey {
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
Expand Down Expand Up @@ -1188,7 +1188,7 @@ mod test {

use super::{
DescriptorKeyParseError, DescriptorMultiXKey, DescriptorPublicKey, DescriptorSecretKey,
MiniscriptKey, Wildcard,
Key, Wildcard,
};
use crate::prelude::*;

Expand Down
34 changes: 15 additions & 19 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use crate::miniscript::decode::Terminal;
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
use crate::prelude::*;
use crate::{
expression, hash256, BareCtx, Error, ForEachKey, MiniscriptKey, Satisfier, ToPublicKey,
TranslateErr, TranslatePk, Translator,
expression, hash256, BareCtx, Error, ForEachKey, Key, Satisfier, ToPublicKey, TranslateErr,
TranslatePk, Translator,
};

mod bare;
Expand Down Expand Up @@ -61,7 +61,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 @@ -76,42 +76,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 @@ -161,7 +161,7 @@ impl DescriptorType {
}
}

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

/// Create a new pk descriptor
Expand Down Expand Up @@ -378,7 +378,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 @@ -509,11 +509,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
}
}

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

/// Converts a descriptor using abstract keys to one using specific keys.
Expand All @@ -533,7 +529,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 {
match *self {
Descriptor::Bare(ref bare) => bare.for_each_key(pred),
Expand Down Expand Up @@ -919,7 +915,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) => fmt::Debug::fmt(sub, f),
Expand All @@ -932,7 +928,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) => fmt::Display::fmt(sub, f),
Expand Down
Loading