From: Matt Corallo Date: Tue, 18 Jul 2023 19:13:19 +0000 (+0000) Subject: Tweak transaction bumping `sign_tx` types for bindings X-Git-Tag: v0.0.116~4^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=be08b4f6b8ff3248710852f17760647f5b63ee24;p=rust-lightning Tweak transaction bumping `sign_tx` types for bindings 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. --- diff --git a/lightning/src/events/bump_transaction.rs b/lightning/src/events/bump_transaction.rs index e56035b56..a9133f50e 100644 --- a/lightning/src/events/bump_transaction.rs +++ b/lightning/src/events/bump_transaction.rs @@ -469,7 +469,7 @@ pub trait CoinSelectionSource { ) -> Result; /// 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; } /// 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; } /// 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 { 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) diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index bf5aa02bb..3cc08e046 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -1105,20 +1105,20 @@ impl TestWalletSource { } impl WalletSource for TestWalletSource { - fn list_confirmed_utxos(&self) -> Result, ()> { + fn list_confirmed_utxos(&self) -> Result, ()> { Ok(self.utxos.borrow().clone()) - } + } - fn get_change_script(&self) -> Result { + fn get_change_script(&self) -> Result { 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 { 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) + } }