]> Untitled Git - bdk/commitdiff
[wallet] Support signing the whole tx instead of individual inputs
authorAlekos Filini <alekos.filini@gmail.com>
Mon, 17 Aug 2020 21:50:50 +0000 (23:50 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Sun, 30 Aug 2020 18:38:22 +0000 (20:38 +0200)
src/wallet/mod.rs
src/wallet/signer.rs

index 57d822add158f16d8eaf080c80b3951228e6d2b9..003b5ac243f3311899ccba2850b8771f589ce11f 100644 (file)
@@ -586,8 +586,12 @@ where
             .iter()
             .chain(self.change_signers.signers().iter())
         {
-            for index in 0..psbt.inputs.len() {
-                signer.sign(&mut psbt, index)?;
+            if signer.sign_whole_tx() {
+                signer.sign(&mut psbt, None)?;
+            } else {
+                for index in 0..psbt.inputs.len() {
+                    signer.sign(&mut psbt, Some(index))?;
+                }
             }
         }
 
index d94589c1d39e78f950716b76874d048cbaad06f5..9744a6286571fc77fd99585490629ee01f64e85d 100644 (file)
@@ -1,4 +1,3 @@
-use std::any::Any;
 use std::cmp::Ordering;
 use std::collections::BTreeMap;
 use std::fmt;
@@ -66,9 +65,13 @@ pub trait Signer: fmt::Debug {
     fn sign(
         &self,
         psbt: &mut psbt::PartiallySignedTransaction,
-        input_index: usize,
+        input_index: Option<usize>,
     ) -> Result<(), SignerError>;
 
+    fn sign_whole_tx(&self) -> bool {
+        false
+    }
+
     fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
         None
     }
@@ -78,8 +81,9 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
     fn sign(
         &self,
         psbt: &mut psbt::PartiallySignedTransaction,
-        input_index: usize,
+        input_index: Option<usize>,
     ) -> Result<(), SignerError> {
+        let input_index = input_index.unwrap();
         if input_index >= psbt.inputs.len() {
             return Err(SignerError::InputIndexOutOfRange);
         }
@@ -97,7 +101,7 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
         let ctx = Secp256k1::signing_only();
 
         let derived_key = self.xkey.derive_priv(&ctx, &deriv_path).unwrap();
-        derived_key.private_key.sign(psbt, input_index)
+        derived_key.private_key.sign(psbt, Some(input_index))
     }
 
     fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
@@ -109,8 +113,9 @@ impl Signer for PrivateKey {
     fn sign(
         &self,
         psbt: &mut psbt::PartiallySignedTransaction,
-        input_index: usize,
+        input_index: Option<usize>,
     ) -> Result<(), SignerError> {
+        let input_index = input_index.unwrap();
         if input_index >= psbt.inputs.len() {
             return Err(SignerError::InputIndexOutOfRange);
         }
@@ -192,20 +197,6 @@ impl SignersContainer<DescriptorPublicKey> {
     }
 }
 
-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();