Tweak transaction bumping `sign_tx` types for bindings
authorMatt Corallo <git@bluematt.me>
Tue, 18 Jul 2023 19:13:19 +0000 (19:13 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 20 Jul 2023 19:49:22 +0000 (19:49 +0000)
In bindings we can't practically pass a mutable transaction, and
instead need to pass an owned transaction and have the sign method
return a signed copy. We do this here for all build modes as the
API is roughly equivalent also to Rust users.

lightning/src/events/bump_transaction.rs
lightning/src/util/test_utils.rs

index e56035b562be2444cd1601e4da43b4ef36be702a..a9133f50ed8448cdd855856398581241ca41b6a6 100644 (file)
@@ -469,7 +469,7 @@ pub trait CoinSelectionSource {
        ) -> Result<CoinSelection, ()>;
        /// Signs and provides the full witness for all inputs within the transaction known to the
        /// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
-       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
+       fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
 }
 
 /// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -483,7 +483,7 @@ pub trait WalletSource {
        /// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
        /// the transaction known to the wallet (i.e., any provided via
        /// [`WalletSource::list_confirmed_utxos`]).
-       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
+       fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
 }
 
 /// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
@@ -629,7 +629,7 @@ where
                        .or_else(|_| do_coin_selection(true, true))
        }
 
-       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
+       fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()> {
                self.source.sign_tx(tx)
        }
 }
@@ -748,7 +748,8 @@ where
                let unsigned_tx_weight = anchor_tx.weight() as u64 - (anchor_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
 
                log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
-               self.utxo_source.sign_tx(&mut anchor_tx)?;
+               anchor_tx = self.utxo_source.sign_tx(anchor_tx)?;
+
                let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
                let anchor_sig = signer.sign_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
                anchor_tx.input[0].witness = anchor_descriptor.tx_input_witness(&anchor_sig);
@@ -812,7 +813,8 @@ where
                let unsigned_tx_weight = htlc_tx.weight() as u64 - (htlc_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
 
                log_debug!(self.logger, "Signing HTLC transaction {}", htlc_tx.txid());
-               self.utxo_source.sign_tx(&mut htlc_tx)?;
+               htlc_tx = self.utxo_source.sign_tx(htlc_tx)?;
+
                let mut signers = BTreeMap::new();
                for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
                        let signer = signers.entry(htlc_descriptor.channel_derivation_parameters.keys_id)
index bf5aa02bb41e310f92d06253487ca39d6c2ed8df..3cc08e046283c0480b81952ec98464565b550605 100644 (file)
@@ -1105,20 +1105,20 @@ impl TestWalletSource {
 }
 
 impl WalletSource for TestWalletSource {
-    fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
+       fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
                Ok(self.utxos.borrow().clone())
-    }
+       }
 
-    fn get_change_script(&self) -> Result<Script, ()> {
+       fn get_change_script(&self) -> Result<Script, ()> {
                let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
                Ok(Script::new_p2pkh(&public_key.pubkey_hash()))
-    }
+       }
 
-    fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
+       fn sign_tx(&self, mut tx: Transaction) -> Result<Transaction, ()> {
                let utxos = self.utxos.borrow();
                for i in 0..tx.input.len() {
                        if let Some(utxo) = utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output) {
-                               let sighash = SighashCache::new(&*tx)
+                               let sighash = SighashCache::new(&tx)
                                        .legacy_signature_hash(i, &utxo.output.script_pubkey, EcdsaSighashType::All as u32)
                                        .map_err(|_| ())?;
                                let sig = self.secp.sign_ecdsa(&sighash.as_hash().into(), &self.secret_key);
@@ -1129,6 +1129,6 @@ impl WalletSource for TestWalletSource {
                                        .into_script();
                        }
                }
-               Ok(())
-    }
+               Ok(tx)
+       }
 }