Use miniscript::DescriptorPublicKey

This allows us to remove all our custom "ExtendedDescriptor" implementation since that is
now built directly in miniscript.
This commit is contained in:
Alekos Filini
2020-08-12 12:51:50 +02:00
parent ddc2bded99
commit 5777431135
19 changed files with 935 additions and 1763 deletions

View File

@@ -35,7 +35,9 @@ impl WalletExport {
label: &str,
include_blockheight: bool,
) -> Result<Self, &'static str> {
let descriptor = wallet.descriptor.as_ref().to_string();
let descriptor = wallet
.descriptor
.to_string_with_secret(&wallet.signers.as_key_map());
Self::is_compatible_with_core(&descriptor)?;
let blockheight = match wallet.database.borrow().iter_txs(false) {
@@ -62,7 +64,7 @@ impl WalletExport {
!= wallet
.change_descriptor
.as_ref()
.map(|d| d.as_ref().to_string())
.map(|d| d.to_string_with_secret(&wallet.change_signers.as_key_map()))
{
return Err("Incompatible change descriptor");
}
@@ -193,14 +195,14 @@ mod test {
#[test]
fn test_export_multi() {
let descriptor = "wsh(multi(2,\
[73756c7f/48h/0h/0h/2h]tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/0/*,\
[f9f62194/48h/0h/0h/2h]tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4/0/*,\
[c98b1535/48h/0h/0h/2h]tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a/0/*\
[73756c7f/48'/0'/0'/2']tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/0/*,\
[f9f62194/48'/0'/0'/2']tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4/0/*,\
[c98b1535/48'/0'/0'/2']tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a/0/*\
))";
let change_descriptor = "wsh(multi(2,\
[73756c7f/48h/0h/0h/2h]tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/1/*,\
[f9f62194/48h/0h/0h/2h]tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4/1/*,\
[c98b1535/48h/0h/0h/2h]tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a/1/*\
[73756c7f/48'/0'/0'/2']tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/1/*,\
[f9f62194/48'/0'/0'/2']tpubDDp3ZSH1yCwusRppH7zgSxq2t1VEUyXSeEp8E5aFS8m43MknUjiF1bSLo3CGWAxbDyhF1XowA5ukPzyJZjznYk3kYi6oe7QxtX2euvKWsk4/1/*,\
[c98b1535/48'/0'/0'/2']tpubDCDi5W4sP6zSnzJeowy8rQDVhBdRARaPhK1axABi8V1661wEPeanpEXj4ZLAUEoikVtoWcyK26TKKJSecSfeKxwHCcRrge9k1ybuiL71z4a/1/*\
))";
let wallet: OfflineWallet<_> = Wallet::new_offline(

View File

@@ -2,17 +2,14 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::{BTreeMap, HashSet};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::sync::Arc;
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::script::Builder;
use bitcoin::consensus::encode::serialize;
use bitcoin::util::bip32::ChildNumber;
use bitcoin::util::psbt::PartiallySignedTransaction as PSBT;
use bitcoin::{
Address, Network, OutPoint, PublicKey, Script, SigHashType, Transaction, TxOut, Txid,
};
use bitcoin::{Address, Network, OutPoint, Script, SigHashType, Transaction, TxOut, Txid};
use miniscript::BitcoinSig;
use miniscript::descriptor::DescriptorPublicKey;
#[allow(unused_imports)]
use log::{debug, error, info, trace};
@@ -20,19 +17,23 @@ use log::{debug, error, info, trace};
pub mod coin_selection;
pub mod export;
mod rbf;
pub mod signer;
pub mod time;
pub mod tx_builder;
pub mod utils;
use signer::{Signer, SignersContainer};
use tx_builder::TxBuilder;
use utils::{FeeRate, IsDust};
use utils::{After, FeeRate, IsDust, Older};
use crate::blockchain::{Blockchain, OfflineBlockchain, OnlineBlockchain, Progress};
use crate::database::{BatchDatabase, BatchOperations, DatabaseUtils};
use crate::descriptor::{get_checksum, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, Policy};
use crate::descriptor::{
get_checksum, DescriptorMeta, DescriptorScripts, ExtendedDescriptor, ExtractPolicy, Policy,
};
use crate::error::Error;
use crate::psbt::{utils::PSBTUtils, PSBTSatisfier, PSBTSigner};
use crate::signer::Signer;
use crate::psbt::PSBTUtils;
// use crate::psbt::{utils::PSBTUtils, PSBTSatisfier, PSBTSigner};
use crate::types::*;
const CACHE_ADDR_BATCH_SIZE: u32 = 100;
@@ -42,6 +43,10 @@ pub type OfflineWallet<D> = Wallet<OfflineBlockchain, D>;
pub struct Wallet<B: Blockchain, D: BatchDatabase> {
descriptor: ExtendedDescriptor,
change_descriptor: Option<ExtendedDescriptor>,
signers: Arc<SignersContainer<DescriptorPublicKey>>,
change_signers: Arc<SignersContainer<DescriptorPublicKey>>,
network: Network,
current_height: Option<u32>,
@@ -66,27 +71,32 @@ where
ScriptType::External,
get_checksum(descriptor)?.as_bytes(),
)?;
let descriptor = ExtendedDescriptor::from_str(descriptor)?;
let change_descriptor = match change_descriptor {
let (descriptor, keymap) = ExtendedDescriptor::parse_secret(descriptor)?;
let signers = Arc::new(SignersContainer::from(keymap));
let (change_descriptor, change_signers) = match change_descriptor {
Some(desc) => {
database.check_descriptor_checksum(
ScriptType::Internal,
get_checksum(desc)?.as_bytes(),
)?;
let parsed = ExtendedDescriptor::from_str(desc)?;
if !parsed.same_structure(descriptor.as_ref()) {
return Err(Error::DifferentDescriptorStructure);
}
let (change_descriptor, change_keymap) = ExtendedDescriptor::parse_secret(desc)?;
let change_signers = Arc::new(SignersContainer::from(change_keymap));
// if !parsed.same_structure(descriptor.as_ref()) {
// return Err(Error::DifferentDescriptorStructure);
// }
Some(parsed)
(Some(change_descriptor), change_signers)
}
None => None,
None => (None, Arc::new(SignersContainer::new())),
};
Ok(Wallet {
descriptor,
change_descriptor,
signers,
change_signers,
network,
current_height: None,
@@ -100,7 +110,7 @@ where
let index = self.fetch_and_increment_index(ScriptType::External)?;
self.descriptor
.derive(index)?
.derive(&[ChildNumber::from_normal_idx(index).unwrap()])
.address(self.network)
.ok_or(Error::ScriptDoesntHaveAddressForm)
}
@@ -133,7 +143,10 @@ where
}
// TODO: fetch both internal and external policies
let policy = self.descriptor.extract_policy()?.unwrap();
let policy = self
.descriptor
.extract_policy(Arc::clone(&self.signers))?
.unwrap();
if policy.requires_path() && builder.policy_path.is_none() {
return Err(Error::SpendingPolicyRequired);
}
@@ -220,10 +233,10 @@ where
// TODO: use the right weight instead of the maximum, and only fall-back to it if the
// script is unknown in the database
let input_witness_weight = std::cmp::max(
self.get_descriptor_for(ScriptType::Internal)
self.get_descriptor_for_script_type(ScriptType::Internal)
.0
.max_satisfaction_weight(),
self.get_descriptor_for(ScriptType::External)
self.get_descriptor_for_script_type(ScriptType::External)
.0
.max_satisfaction_weight(),
);
@@ -369,7 +382,7 @@ where
// `get_deget_descriptor_for` to find what's the ScriptType for `Internal`
// addresses really is, because if there's no change_descriptor it's actually equal
// to "External"
let (_, change_type) = self.get_descriptor_for(ScriptType::Internal);
let (_, change_type) = self.get_descriptor_for_script_type(ScriptType::Internal);
match self
.database
.borrow()
@@ -435,10 +448,10 @@ where
// TODO: use the right weight instead of the maximum, and only fall-back to it if the
// script is unknown in the database
let input_witness_weight = std::cmp::max(
self.get_descriptor_for(ScriptType::Internal)
self.get_descriptor_for_script_type(ScriptType::Internal)
.0
.max_satisfaction_weight(),
self.get_descriptor_for(ScriptType::External)
self.get_descriptor_for_script_type(ScriptType::External)
.0
.max_satisfaction_weight(),
);
@@ -545,139 +558,11 @@ where
// this helps us doing our job later
self.add_input_hd_keypaths(&mut psbt)?;
let tx = &psbt.global.unsigned_tx;
let mut signer = PSBTSigner::from_descriptor(&psbt.global.unsigned_tx, &self.descriptor)?;
if let Some(desc) = &self.change_descriptor {
let change_signer = PSBTSigner::from_descriptor(&psbt.global.unsigned_tx, desc)?;
signer.extend(change_signer)?;
}
// sign everything we can. TODO: ideally we should only sign with the keys in the policy
// path selected, if present
for (i, input) in psbt.inputs.iter_mut().enumerate() {
let sighash = input.sighash_type.unwrap_or(SigHashType::All);
let prevout = tx.input[i].previous_output;
let mut partial_sigs = BTreeMap::new();
{
let mut push_sig = |pubkey: &PublicKey, opt_sig: Option<BitcoinSig>| {
if let Some((signature, sighash)) = opt_sig {
let mut concat_sig = Vec::new();
concat_sig.extend_from_slice(&signature.serialize_der());
concat_sig.extend_from_slice(&[sighash as u8]);
//input.partial_sigs.insert(*pubkey, concat_sig);
partial_sigs.insert(*pubkey, concat_sig);
}
};
if let Some(non_wit_utxo) = &input.non_witness_utxo {
if non_wit_utxo.txid() != prevout.txid {
return Err(Error::InputTxidMismatch((non_wit_utxo.txid(), prevout)));
}
let prev_script = &non_wit_utxo.output
[psbt.global.unsigned_tx.input[i].previous_output.vout as usize]
.script_pubkey;
// return (signature, sighash) from here
let sign_script = if let Some(redeem_script) = &input.redeem_script {
if &redeem_script.to_p2sh() != prev_script {
return Err(Error::InputRedeemScriptMismatch((
prev_script.clone(),
redeem_script.clone(),
)));
}
redeem_script
} else {
prev_script
};
for (pubkey, (fing, path)) in &input.hd_keypaths {
push_sig(
pubkey,
signer.sig_legacy_from_fingerprint(
i,
sighash,
fing,
path,
sign_script,
)?,
);
}
// TODO: this sucks, we sign with every key
for pubkey in signer.all_public_keys() {
push_sig(
pubkey,
signer.sig_legacy_from_pubkey(i, sighash, pubkey, sign_script)?,
);
}
} else if let Some(witness_utxo) = &input.witness_utxo {
let value = witness_utxo.value;
let script = match &input.redeem_script {
Some(script) if script.to_p2sh() != witness_utxo.script_pubkey => {
return Err(Error::InputRedeemScriptMismatch((
witness_utxo.script_pubkey.clone(),
script.clone(),
)))
}
Some(script) => script,
None => &witness_utxo.script_pubkey,
};
let sign_script = if script.is_v0_p2wpkh() {
self.to_p2pkh(&script.as_bytes()[2..])
} else if script.is_v0_p2wsh() {
match &input.witness_script {
None => Err(Error::InputMissingWitnessScript(i)),
Some(witness_script) if script != &witness_script.to_v0_p2wsh() => {
Err(Error::InputRedeemScriptMismatch((
script.clone(),
witness_script.clone(),
)))
}
Some(witness_script) => Ok(witness_script),
}?
.clone()
} else {
return Err(Error::InputUnknownSegwitScript(script.clone()));
};
for (pubkey, (fing, path)) in &input.hd_keypaths {
push_sig(
pubkey,
signer.sig_segwit_from_fingerprint(
i,
sighash,
fing,
path,
&sign_script,
value,
)?,
);
}
// TODO: this sucks, we sign with every key
for pubkey in signer.all_public_keys() {
push_sig(
pubkey,
signer.sig_segwit_from_pubkey(
i,
sighash,
pubkey,
&sign_script,
value,
)?,
);
}
} else {
return Err(Error::MissingUTXO);
}
for index in 0..psbt.inputs.len() {
self.signers.sign(&mut psbt, index)?;
if self.change_descriptor.is_some() {
self.change_signers.sign(&mut psbt, index)?;
}
// push all the signatures into the psbt
input.partial_sigs.append(&mut partial_sigs);
}
// attempt to finalize
@@ -688,9 +573,13 @@ where
pub fn policies(&self, script_type: ScriptType) -> Result<Option<Policy>, Error> {
match (script_type, self.change_descriptor.as_ref()) {
(ScriptType::External, _) => Ok(self.descriptor.extract_policy()?),
(ScriptType::External, _) => {
Ok(self.descriptor.extract_policy(Arc::clone(&self.signers))?)
}
(ScriptType::Internal, None) => Ok(None),
(ScriptType::Internal, Some(desc)) => Ok(desc.extract_policy()?),
(ScriptType::Internal, Some(desc)) => {
Ok(desc.extract_policy(Arc::clone(&self.change_signers))?)
}
}
}
@@ -699,9 +588,9 @@ where
script_type: ScriptType,
) -> Result<Option<ExtendedDescriptor>, Error> {
match (script_type, self.change_descriptor.as_ref()) {
(ScriptType::External, _) => Ok(Some(self.descriptor.as_public_version()?)),
(ScriptType::External, _) => Ok(Some(self.descriptor.clone())),
(ScriptType::Internal, None) => Ok(None),
(ScriptType::Internal, Some(desc)) => Ok(Some(desc.as_public_version()?)),
(ScriptType::Internal, Some(desc)) => Ok(Some(desc.clone())),
}
}
@@ -712,18 +601,7 @@ where
) -> Result<bool, Error> {
let mut tx = psbt.global.unsigned_tx.clone();
for (n, input) in tx.input.iter_mut().enumerate() {
// safe to run only on the descriptor because we assume the change descriptor also has
// the same structure
let desc = self.descriptor.derive_from_psbt_input(psbt, n);
debug!("{:?}", psbt.inputs[n].hd_keypaths);
debug!("reconstructed descriptor is {:?}", desc);
let desc = match desc {
Err(_) => return Ok(false),
Ok(desc) => desc,
};
for (n, (input, psbt_input)) in tx.input.iter_mut().zip(psbt.inputs.iter()).enumerate() {
// if the height is None in the database it means it's still unconfirmed, so consider
// that as a very high value
let create_height = self
@@ -738,10 +616,43 @@ where
n, input.previous_output, create_height, current_height
);
let satisfier =
PSBTSatisfier::new(&psbt.inputs[n], false, create_height, current_height);
// - Try to derive the descriptor by looking at the txout. If it's in our database, we
// know exactly which `script_type` to use, and which derivation index it is
// - If that fails, try to derive it by looking at the psbt input: the complete logic
// is in `src/descriptor/mod.rs`, but it will basically look at `hd_keypaths`,
// `redeem_script` and `witness_script` to determine the right derivation
// - If that also fails, it will try it on the internal descriptor, if present
let desc = if let Some(desc) = psbt
.get_utxo_for(n)
.map(|txout| self.get_descriptor_for_txout(&txout))
.transpose()?
.flatten()
{
desc
} else if let Some(desc) = self
.descriptor
.derive_from_psbt_input(psbt_input, psbt.get_utxo_for(n))
{
desc
} else if let Some(desc) = self
.change_descriptor
.as_ref()
.and_then(|desc| desc.derive_from_psbt_input(psbt_input, psbt.get_utxo_for(n)))
{
desc
} else {
debug!("Couldn't find the right derived descriptor for input {}", n);
return Ok(false);
};
match desc.satisfy(input, satisfier) {
match desc.satisfy(
input,
(
psbt_input.clone(),
After::new(current_height, false),
Older::new(current_height, create_height, false),
),
) {
Ok(_) => continue,
Err(e) => {
debug!("satisfy error {:?} for input {}", e, n);
@@ -761,7 +672,10 @@ where
// Internals
fn get_descriptor_for(&self, script_type: ScriptType) -> (&ExtendedDescriptor, ScriptType) {
fn get_descriptor_for_script_type(
&self,
script_type: ScriptType,
) -> (&ExtendedDescriptor, ScriptType) {
let desc = match script_type {
ScriptType::Internal if self.change_descriptor.is_some() => (
self.change_descriptor.as_ref().unwrap(),
@@ -773,25 +687,26 @@ where
desc
}
fn to_p2pkh(&self, pubkey_hash: &[u8]) -> Script {
Builder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(pubkey_hash)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script()
fn get_descriptor_for_txout(&self, txout: &TxOut) -> Result<Option<ExtendedDescriptor>, Error> {
Ok(self
.database
.borrow()
.get_path_from_script_pubkey(&txout.script_pubkey)?
.map(|(script_type, child)| (self.get_descriptor_for_script_type(script_type).0, child))
.map(|(desc, child)| desc.derive(&[ChildNumber::from_normal_idx(child).unwrap()])))
}
fn get_change_address(&self) -> Result<Script, Error> {
let (desc, script_type) = self.get_descriptor_for(ScriptType::Internal);
let (desc, script_type) = self.get_descriptor_for_script_type(ScriptType::Internal);
let index = self.fetch_and_increment_index(script_type)?;
Ok(desc.derive(index)?.script_pubkey())
Ok(desc
.derive(&[ChildNumber::from_normal_idx(index).unwrap()])
.script_pubkey())
}
fn fetch_and_increment_index(&self, script_type: ScriptType) -> Result<u32, Error> {
let (descriptor, script_type) = self.get_descriptor_for(script_type);
let (descriptor, script_type) = self.get_descriptor_for_script_type(script_type);
let index = match descriptor.is_fixed() {
true => 0,
false => self
@@ -818,7 +733,7 @@ where
from: u32,
mut count: u32,
) -> Result<(), Error> {
let (descriptor, script_type) = self.get_descriptor_for(script_type);
let (descriptor, script_type) = self.get_descriptor_for_script_type(script_type);
if descriptor.is_fixed() {
if from > 0 {
return Ok(());
@@ -832,7 +747,9 @@ where
let start_time = time::Instant::new();
for i in from..(from + count) {
address_batch.set_script_pubkey(
&descriptor.derive(i)?.script_pubkey(),
&descriptor
.derive(&[ChildNumber::from_normal_idx(i).unwrap()])
.script_pubkey(),
script_type,
i,
)?;
@@ -924,9 +841,9 @@ where
None => continue,
};
let (desc, _) = self.get_descriptor_for(script_type);
let (desc, _) = self.get_descriptor_for_script_type(script_type);
psbt_input.hd_keypaths = desc.get_hd_keypaths(child)?;
let derived_descriptor = desc.derive(child)?;
let derived_descriptor = desc.derive(&[ChildNumber::from_normal_idx(child).unwrap()]);
psbt_input.redeem_script = derived_descriptor.psbt_redeem_script();
psbt_input.witness_script = derived_descriptor.psbt_witness_script();
@@ -957,7 +874,7 @@ where
.borrow()
.get_path_from_script_pubkey(&tx_output.script_pubkey)?
{
let (desc, _) = self.get_descriptor_for(script_type);
let (desc, _) = self.get_descriptor_for_script_type(script_type);
psbt_output.hd_keypaths = desc.get_hd_keypaths(child)?;
}
}
@@ -982,7 +899,7 @@ where
debug!("Found descriptor {:?}/{}", script_type, child);
// merge hd_keypaths
let (desc, _) = self.get_descriptor_for(script_type);
let (desc, _) = self.get_descriptor_for_script_type(script_type);
let mut hd_keypaths = desc.get_hd_keypaths(child)?;
psbt_input.hd_keypaths.append(&mut hd_keypaths);
}
@@ -1086,13 +1003,12 @@ where
#[cfg(test)]
mod test {
use bitcoin::Network;
use std::str::FromStr;
use miniscript::Descriptor;
use bitcoin::Network;
use crate::database::memory::MemoryDatabase;
use crate::database::Database;
use crate::descriptor::ExtendedDescriptor;
use crate::types::ScriptType;
use super::*;
@@ -1205,12 +1121,12 @@ mod test {
descriptor: &str,
) -> (
OfflineWallet<MemoryDatabase>,
(ExtendedDescriptor, Option<ExtendedDescriptor>),
(String, Option<String>),
bitcoin::Txid,
) {
let descriptors = testutils!(@descriptors (descriptor));
let wallet: OfflineWallet<_> = Wallet::new_offline(
&descriptors.0.to_string(),
&descriptors.0,
None,
Network::Regtest,
MemoryDatabase::new(),

327
src/wallet/signer.rs Normal file
View File

@@ -0,0 +1,327 @@
use std::any::Any;
use std::collections::HashMap;
use std::fmt;
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::script::Builder as ScriptBuilder;
use bitcoin::hashes::{hash160, Hash};
use bitcoin::secp256k1::{Message, Secp256k1};
use bitcoin::util::bip32::{ExtendedPrivKey, Fingerprint};
use bitcoin::util::{bip143, psbt};
use bitcoin::{PrivateKey, SigHash, SigHashType};
use miniscript::descriptor::{DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, KeyMap};
use miniscript::{Legacy, MiniscriptKey, Segwitv0};
use crate::descriptor::XKeyUtils;
/// Identifier of a signer in the `SignersContainers`. Used as a key to find the right signer among
/// many of them
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum SignerId<Pk: MiniscriptKey> {
PkHash(<Pk as MiniscriptKey>::Hash),
Fingerprint(Fingerprint),
}
impl From<hash160::Hash> for SignerId<DescriptorPublicKey> {
fn from(hash: hash160::Hash) -> SignerId<DescriptorPublicKey> {
SignerId::PkHash(hash)
}
}
impl From<Fingerprint> for SignerId<DescriptorPublicKey> {
fn from(fing: Fingerprint) -> SignerId<DescriptorPublicKey> {
SignerId::Fingerprint(fing)
}
}
/// Signing error
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum SignerError {
/// The private key is missing for the required public key
MissingKey,
/// The user canceled the operation
UserCanceled,
/// The sighash is missing in the PSBT input
MissingSighash,
/// Input index is out of range
InputIndexOutOfRange,
/// The `non_witness_utxo` field of the transaction is required to sign this input
MissingNonWitnessUtxo,
/// The `non_witness_utxo` specified is invalid
InvalidNonWitnessUtxo,
/// The `witness_utxo` field of the transaction is required to sign this input
MissingWitnessUtxo,
/// The `witness_script` field of the transaction is requied to sign this input
MissingWitnessScript,
/// The fingerprint and derivation path are missing from the psbt input
MissingHDKeypath,
}
/// Trait for signers
pub trait Signer: fmt::Debug {
fn sign(
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(), SignerError>;
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
None
}
}
impl Signer for DescriptorXKey<ExtendedPrivKey> {
fn sign(
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(), SignerError> {
if input_index >= psbt.inputs.len() {
return Err(SignerError::InputIndexOutOfRange);
}
let deriv_path = match psbt.inputs[input_index]
.hd_keypaths
.iter()
.filter_map(|(_, &(fingerprint, ref path))| self.matches(fingerprint.clone(), &path))
.next()
{
Some(deriv_path) => deriv_path,
None => return Ok(()), // TODO: should report an error maybe?
};
let ctx = Secp256k1::signing_only();
let derived_key = self.xkey.derive_priv(&ctx, &deriv_path).unwrap();
derived_key.private_key.sign(psbt, input_index)
}
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
Some(DescriptorSecretKey::XPrv(self.clone()))
}
}
impl Signer for PrivateKey {
fn sign(
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(), SignerError> {
if input_index >= psbt.inputs.len() {
return Err(SignerError::InputIndexOutOfRange);
}
let ctx = Secp256k1::signing_only();
let pubkey = self.public_key(&ctx);
if psbt.inputs[input_index].partial_sigs.contains_key(&pubkey) {
return Ok(());
}
// FIXME: use the presence of `witness_utxo` as an indication that we should make a bip143
// sig. Does this make sense? Should we add an extra argument to explicitly swith between
// these? The original idea was to declare sign() as sign<Ctx: ScriptContex>() and use Ctx,
// but that violates the rules for trait-objects, so we can't do it.
let (hash, sighash) = match psbt.inputs[input_index].witness_utxo {
Some(_) => Segwitv0::sighash(psbt, input_index)?,
None => Legacy::sighash(psbt, input_index)?,
};
let signature = ctx.sign(
&Message::from_slice(&hash.into_inner()[..]).unwrap(),
&self.key,
);
let mut final_signature = Vec::with_capacity(75);
final_signature.extend_from_slice(&signature.serialize_der());
final_signature.push(sighash.as_u32() as u8);
psbt.inputs[input_index]
.partial_sigs
.insert(pubkey, final_signature);
Ok(())
}
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
Some(DescriptorSecretKey::PrivKey(self.clone()))
}
}
/// Container for multiple signers
#[derive(Debug, Default)]
pub struct SignersContainer<Pk: MiniscriptKey>(HashMap<SignerId<Pk>, Box<dyn Signer>>);
impl SignersContainer<DescriptorPublicKey> {
pub fn as_key_map(&self) -> KeyMap {
self.0
.values()
.filter_map(|signer| signer.descriptor_secret_key())
.filter_map(|secret| secret.as_public().ok().map(|public| (public, secret)))
.collect()
}
}
impl<Pk: MiniscriptKey + Any> Signer for SignersContainer<Pk> {
fn sign(
&self,
psbt: &mut psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(), SignerError> {
for signer in self.0.values() {
signer.sign(psbt, input_index)?;
}
Ok(())
}
}
impl From<KeyMap> for SignersContainer<DescriptorPublicKey> {
fn from(keymap: KeyMap) -> SignersContainer<DescriptorPublicKey> {
let mut container = SignersContainer::new();
for (_, secret) in keymap {
match secret {
DescriptorSecretKey::PrivKey(private_key) => container.add_external(
SignerId::from(
private_key
.public_key(&Secp256k1::signing_only())
.to_pubkeyhash(),
),
Box::new(private_key),
),
DescriptorSecretKey::XPrv(xprv) => {
container.add_external(SignerId::from(xprv.root_fingerprint()), Box::new(xprv))
}
};
}
container
}
}
impl<Pk: MiniscriptKey> SignersContainer<Pk> {
/// Default constructor
pub fn new() -> Self {
SignersContainer(HashMap::new())
}
/// Adds an external signer to the container for the specified id. Optionally returns the
/// signer that was previosuly in the container, if any
pub fn add_external(
&mut self,
id: SignerId<Pk>,
signer: Box<dyn Signer>,
) -> Option<Box<dyn Signer>> {
self.0.insert(id, signer)
}
/// Removes a signer from the container and returns it
pub fn remove(&mut self, id: SignerId<Pk>) -> Option<Box<dyn Signer>> {
self.0.remove(&id)
}
/// Returns the list of identifiers of all the signers in the container
pub fn ids(&self) -> Vec<&SignerId<Pk>> {
self.0.keys().collect()
}
/// Finds the signer with a given id in the container
pub fn find(&self, id: SignerId<Pk>) -> Option<&Box<dyn Signer>> {
self.0.get(&id)
}
}
pub trait ComputeSighash {
fn sighash(
psbt: &psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(SigHash, SigHashType), SignerError>;
}
impl ComputeSighash for Legacy {
fn sighash(
psbt: &psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(SigHash, SigHashType), SignerError> {
if input_index >= psbt.inputs.len() {
return Err(SignerError::InputIndexOutOfRange);
}
let psbt_input = &psbt.inputs[input_index];
let tx_input = &psbt.global.unsigned_tx.input[input_index];
let sighash = psbt_input.sighash_type.ok_or(SignerError::MissingSighash)?;
let script = match &psbt_input.redeem_script {
&Some(ref redeem_script) => redeem_script.clone(),
&None => {
let non_witness_utxo = psbt_input
.non_witness_utxo
.as_ref()
.ok_or(SignerError::MissingNonWitnessUtxo)?;
let prev_out = non_witness_utxo
.output
.get(tx_input.previous_output.vout as usize)
.ok_or(SignerError::InvalidNonWitnessUtxo)?;
prev_out.script_pubkey.clone()
}
};
Ok((
psbt.global
.unsigned_tx
.signature_hash(input_index, &script, sighash.as_u32()),
sighash,
))
}
}
impl ComputeSighash for Segwitv0 {
fn sighash(
psbt: &psbt::PartiallySignedTransaction,
input_index: usize,
) -> Result<(SigHash, SigHashType), SignerError> {
if input_index >= psbt.inputs.len() {
return Err(SignerError::InputIndexOutOfRange);
}
let psbt_input = &psbt.inputs[input_index];
let sighash = psbt_input.sighash_type.ok_or(SignerError::MissingSighash)?;
let witness_utxo = psbt_input
.witness_utxo
.as_ref()
.ok_or(SignerError::MissingNonWitnessUtxo)?;
let value = witness_utxo.value;
let script = match &psbt_input.witness_script {
&Some(ref witness_script) => witness_script.clone(),
&None => {
if witness_utxo.script_pubkey.is_v0_p2wpkh() {
ScriptBuilder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&witness_utxo.script_pubkey[2..])
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script()
} else {
return Err(SignerError::MissingWitnessScript);
}
}
};
Ok((
bip143::SigHashCache::new(&psbt.global.unsigned_tx).signature_hash(
input_index,
&script,
value,
sighash,
),
sighash,
))
}
}

View File

@@ -1,3 +1,5 @@
use miniscript::{MiniscriptKey, Satisfier};
// De-facto standard "dust limit" (even though it should change based on the output type)
const DUST_LIMIT_SATOSHI: u64 = 546;
@@ -42,6 +44,61 @@ impl std::default::Default for FeeRate {
}
}
pub struct After {
pub current_height: Option<u32>,
pub assume_height_reached: bool,
}
impl After {
pub(crate) fn new(current_height: Option<u32>, assume_height_reached: bool) -> After {
After {
current_height,
assume_height_reached,
}
}
}
impl<Pk: MiniscriptKey> Satisfier<Pk> for After {
fn check_after(&self, n: u32) -> bool {
if let Some(current_height) = self.current_height {
current_height >= n
} else {
self.assume_height_reached
}
}
}
pub struct Older {
pub current_height: Option<u32>,
pub create_height: Option<u32>,
pub assume_height_reached: bool,
}
impl Older {
pub(crate) fn new(
current_height: Option<u32>,
create_height: Option<u32>,
assume_height_reached: bool,
) -> Older {
Older {
current_height,
create_height,
assume_height_reached,
}
}
}
impl<Pk: MiniscriptKey> Satisfier<Pk> for Older {
fn check_older(&self, n: u32) -> bool {
if let Some(current_height) = self.current_height {
// TODO: test >= / >
current_height as u64 >= self.create_height.unwrap_or(0) as u64 + n as u64
} else {
self.assume_height_reached
}
}
}
pub struct ChunksIterator<I: Iterator> {
iter: I,
size: usize,