Skip to content

Commit fac2c9f

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 only for the `#[cfg(c_bindings)]` build mode.
1 parent 1e119a2 commit fac2c9f

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,14 @@ pub trait CoinSelectionSource {
467467
&self, claim_id: ClaimId, must_spend: &[Input], must_pay_to: &[TxOut],
468468
target_feerate_sat_per_1000_weight: u32,
469469
) -> Result<CoinSelection, ()>;
470+
#[cfg(not(c_bindings))]
470471
/// Signs and provides the full witness for all inputs within the transaction known to the
471472
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
472473
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
474+
#[cfg(c_bindings)]
475+
/// Signs and provides the full witness for all inputs within the transaction known to the
476+
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
477+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
473478
}
474479

475480
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -480,10 +485,16 @@ pub trait WalletSource {
480485
/// Returns a script to use for change above dust resulting from a successful coin selection
481486
/// attempt.
482487
fn get_change_script(&self) -> Result<Script, ()>;
488+
#[cfg(not(c_bindings))]
483489
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
484490
/// the transaction known to the wallet (i.e., any provided via
485491
/// [`WalletSource::list_confirmed_utxos`]).
486492
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
493+
#[cfg(c_bindings)]
494+
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
495+
/// the transaction known to the wallet (i.e., any provided via
496+
/// [`WalletSource::list_confirmed_utxos`]).
497+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()>;
487498
}
488499

489500
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
@@ -629,9 +640,15 @@ where
629640
.or_else(|_| do_coin_selection(true, true))
630641
}
631642

643+
#[cfg(not(c_bindings))]
632644
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
633645
self.source.sign_tx(tx)
634646
}
647+
648+
#[cfg(c_bindings)]
649+
fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()> {
650+
self.source.sign_tx(tx)
651+
}
635652
}
636653

637654
/// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
@@ -748,7 +765,13 @@ where
748765
let unsigned_tx_weight = anchor_tx.weight() as u64 - (anchor_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
749766

750767
log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
751-
self.utxo_source.sign_tx(&mut anchor_tx)?;
768+
#[cfg(not(c_bindings))] {
769+
self.utxo_source.sign_tx(&mut anchor_tx)?;
770+
}
771+
#[cfg(c_bindings)] {
772+
anchor_tx = self.utxo_source.sign_tx(anchor_tx)?;
773+
}
774+
752775
let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
753776
let anchor_sig = signer.sign_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
754777
anchor_tx.input[0].witness = anchor_descriptor.tx_input_witness(&anchor_sig);
@@ -812,7 +835,13 @@ where
812835
let unsigned_tx_weight = htlc_tx.weight() as u64 - (htlc_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
813836

814837
log_debug!(self.logger, "Signing HTLC transaction {}", htlc_tx.txid());
815-
self.utxo_source.sign_tx(&mut htlc_tx)?;
838+
#[cfg(not(c_bindings))] {
839+
self.utxo_source.sign_tx(&mut htlc_tx)?;
840+
}
841+
#[cfg(c_bindings)] {
842+
htlc_tx = self.utxo_source.sign_tx(htlc_tx)?;
843+
}
844+
816845
let mut signers = BTreeMap::new();
817846
for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
818847
let signer = signers.entry(htlc_descriptor.channel_derivation_parameters.keys_id)

lightning/src/util/test_utils.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,19 +1102,8 @@ impl TestWalletSource {
11021102
pub fn remove_utxo(&self, outpoint: bitcoin::OutPoint) {
11031103
self.utxos.borrow_mut().retain(|utxo| utxo.outpoint != outpoint);
11041104
}
1105-
}
1106-
1107-
impl WalletSource for TestWalletSource {
1108-
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
1109-
Ok(self.utxos.borrow().clone())
1110-
}
1111-
1112-
fn get_change_script(&self) -> Result<Script, ()> {
1113-
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
1114-
Ok(Script::new_p2pkh(&public_key.pubkey_hash()))
1115-
}
11161105

1117-
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
1106+
fn do_sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
11181107
let utxos = self.utxos.borrow();
11191108
for i in 0..tx.input.len() {
11201109
if let Some(utxo) = utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output) {
@@ -1130,5 +1119,26 @@ impl WalletSource for TestWalletSource {
11301119
}
11311120
}
11321121
Ok(())
1133-
}
1122+
}
1123+
}
1124+
1125+
impl WalletSource for TestWalletSource {
1126+
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
1127+
Ok(self.utxos.borrow().clone())
1128+
}
1129+
1130+
fn get_change_script(&self) -> Result<Script, ()> {
1131+
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
1132+
Ok(Script::new_p2pkh(&public_key.pubkey_hash()))
1133+
}
1134+
1135+
#[cfg(not(c_bindings))]
1136+
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
1137+
self.do_sign_tx(tx)
1138+
}
1139+
#[cfg(c_bindings)]
1140+
fn sign_tx(&self, mut tx: Transaction) -> Result<Transaction, ()> {
1141+
self.do_sign_tx(&mut tx)?;
1142+
Ok(tx)
1143+
}
11341144
}

0 commit comments

Comments
 (0)