Switch to "mainline" rust-miniscript

This commit is contained in:
Alekos Filini
2020-11-16 22:07:38 +01:00
parent 35579cb216
commit 7a42c5e095
12 changed files with 332 additions and 178 deletions

View File

@@ -30,6 +30,7 @@
//! ```
//! # use std::sync::Arc;
//! # use std::str::FromStr;
//! # use bitcoin::secp256k1::{Secp256k1, All};
//! # use bitcoin::*;
//! # use bitcoin::util::psbt;
//! # use bitcoin::util::bip32::Fingerprint;
@@ -62,6 +63,7 @@
//! &self,
//! psbt: &mut psbt::PartiallySignedTransaction,
//! input_index: Option<usize>,
//! _secp: &Secp256k1<All>,
//! ) -> Result<(), SignerError> {
//! let input_index = input_index.ok_or(SignerError::InputIndexOutOfRange)?;
//! self.device.sign_input(psbt, input_index)?;
@@ -105,6 +107,7 @@ use bitcoin::{PrivateKey, Script, SigHash, SigHashType};
use miniscript::descriptor::{DescriptorSecretKey, DescriptorSinglePriv, DescriptorXKey, KeyMap};
use miniscript::{Legacy, MiniscriptKey, Segwitv0};
use super::utils::SecpCtx;
use crate::descriptor::XKeyUtils;
/// Identifier of a signer in the `SignersContainers`. Used as a key to find the right signer among
@@ -172,6 +175,7 @@ pub trait Signer: fmt::Debug + Send + Sync {
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: Option<usize>,
secp: &SecpCtx,
) -> Result<(), SignerError>;
/// Return whether or not the signer signs the whole transaction in one go instead of every
@@ -193,6 +197,7 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: Option<usize>,
secp: &SecpCtx,
) -> Result<(), SignerError> {
let input_index = input_index.unwrap();
if input_index >= psbt.inputs.len() {
@@ -203,7 +208,7 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
.hd_keypaths
.iter()
.filter_map(|(pk, &(fingerprint, ref path))| {
if self.matches(fingerprint, &path).is_some() {
if self.matches(&(fingerprint, path.clone()), &secp).is_some() {
Some((pk, path))
} else {
None
@@ -215,13 +220,11 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
None => return Ok(()),
};
let ctx = Secp256k1::signing_only();
let derived_key = self.xkey.derive_priv(&ctx, &deriv_path).unwrap();
if &derived_key.private_key.public_key(&ctx) != public_key {
let derived_key = self.xkey.derive_priv(&secp, &deriv_path).unwrap();
if &derived_key.private_key.public_key(&secp) != public_key {
Err(SignerError::InvalidKey)
} else {
derived_key.private_key.sign(psbt, Some(input_index))
derived_key.private_key.sign(psbt, Some(input_index), secp)
}
}
@@ -239,15 +242,14 @@ impl Signer for PrivateKey {
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: Option<usize>,
secp: &SecpCtx,
) -> Result<(), SignerError> {
let input_index = input_index.unwrap();
if input_index >= psbt.inputs.len() {
return Err(SignerError::InputIndexOutOfRange);
}
let ctx = Secp256k1::signing_only();
let pubkey = self.public_key(&ctx);
let pubkey = self.public_key(&secp);
if psbt.inputs[input_index].partial_sigs.contains_key(&pubkey) {
return Ok(());
}
@@ -261,7 +263,7 @@ impl Signer for PrivateKey {
None => Legacy::sighash(psbt, input_index)?,
};
let signature = ctx.sign(
let signature = secp.sign(
&Message::from_slice(&hash.into_inner()[..]).unwrap(),
&self.key,
);
@@ -323,17 +325,18 @@ impl From<(SignerId, SignerOrdering)> for SignersContainerKey {
pub struct SignersContainer(BTreeMap<SignersContainerKey, Arc<dyn Signer>>);
impl SignersContainer {
pub fn as_key_map(&self) -> KeyMap {
pub fn as_key_map(&self, secp: &SecpCtx) -> KeyMap {
self.0
.values()
.filter_map(|signer| signer.descriptor_secret_key())
.filter_map(|secret| secret.as_public().ok().map(|public| (public, secret)))
.filter_map(|secret| secret.as_public(secp).ok().map(|public| (public, secret)))
.collect()
}
}
impl From<KeyMap> for SignersContainer {
fn from(keymap: KeyMap) -> SignersContainer {
let secp = Secp256k1::new();
let mut container = SignersContainer::new();
for (_, secret) in keymap {
@@ -349,7 +352,7 @@ impl From<KeyMap> for SignersContainer {
Arc::new(private_key.key),
),
DescriptorSecretKey::XPrv(xprv) => container.add_external(
SignerId::from(xprv.root_fingerprint()),
SignerId::from(xprv.root_fingerprint(&secp)),
SignerOrdering::default(),
Arc::new(xprv),
),