Skip to content

Commit be08b4f

Browse files
committed
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.
1 parent 1e119a2 commit be08b4f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pub trait CoinSelectionSource {
469469
) -> Result<CoinSelection, ()>;
470470
/// Signs and provides the full witness for all inputs within the transaction known to the
471471
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
472-
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
472+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
473473
}
474474

475475
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -483,7 +483,7 @@ pub trait WalletSource {
483483
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
484484
/// the transaction known to the wallet (i.e., any provided via
485485
/// [`WalletSource::list_confirmed_utxos`]).
486-
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
486+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
487487
}
488488

489489
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
@@ -629,7 +629,7 @@ where
629629
.or_else(|_| do_coin_selection(true, true))
630630
}
631631

632-
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
632+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()> {
633633
self.source.sign_tx(tx)
634634
}
635635
}
@@ -748,7 +748,8 @@ where
748748
let unsigned_tx_weight = anchor_tx.weight() as u64 - (anchor_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
749749

750750
log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
751-
self.utxo_source.sign_tx(&mut anchor_tx)?;
751+
anchor_tx = self.utxo_source.sign_tx(anchor_tx)?;
752+
752753
let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
753754
let anchor_sig = signer.sign_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
754755
anchor_tx.input[0].witness = anchor_descriptor.tx_input_witness(&anchor_sig);
@@ -812,7 +813,8 @@ where
812813
let unsigned_tx_weight = htlc_tx.weight() as u64 - (htlc_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
813814

814815
log_debug!(self.logger, "Signing HTLC transaction {}", htlc_tx.txid());
815-
self.utxo_source.sign_tx(&mut htlc_tx)?;
816+
htlc_tx = self.utxo_source.sign_tx(htlc_tx)?;
817+
816818
let mut signers = BTreeMap::new();
817819
for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
818820
let signer = signers.entry(htlc_descriptor.channel_derivation_parameters.keys_id)

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,20 +1105,20 @@ impl TestWalletSource {
11051105
}
11061106

11071107
impl WalletSource for TestWalletSource {
1108-
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
1108+
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
11091109
Ok(self.utxos.borrow().clone())
1110-
}
1110+
}
11111111

1112-
fn get_change_script(&self) -> Result<Script, ()> {
1112+
fn get_change_script(&self) -> Result<Script, ()> {
11131113
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
11141114
Ok(Script::new_p2pkh(&public_key.pubkey_hash()))
1115-
}
1115+
}
11161116

1117-
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
1117+
fn sign_tx(&self, mut tx: Transaction) -> Result<Transaction, ()> {
11181118
let utxos = self.utxos.borrow();
11191119
for i in 0..tx.input.len() {
11201120
if let Some(utxo) = utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output) {
1121-
let sighash = SighashCache::new(&*tx)
1121+
let sighash = SighashCache::new(&tx)
11221122
.legacy_signature_hash(i, &utxo.output.script_pubkey, EcdsaSighashType::All as u32)
11231123
.map_err(|_| ())?;
11241124
let sig = self.secp.sign_ecdsa(&sighash.as_hash().into(), &self.secret_key);
@@ -1129,6 +1129,6 @@ impl WalletSource for TestWalletSource {
11291129
.into_script();
11301130
}
11311131
}
1132-
Ok(())
1133-
}
1132+
Ok(tx)
1133+
}
11341134
}

0 commit comments

Comments
 (0)