Skip to content

Commit ded0873

Browse files
committed
fixup! improve the API of the derived fields in CommitmentTransaction
1 parent fb39cdd commit ded0873

File tree

6 files changed

+218
-174
lines changed

6 files changed

+218
-174
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -969,23 +969,23 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
969969

970970
let secp_ctx = Secp256k1::new();
971971

972-
let txid = initial_holder_commitment_tx.untrusted_txid();
973-
974972
// block for Rust 1.34 compat
975973
let (holder_commitment_tx, current_holder_commitment_number) = {
976-
let commitment_tx = &initial_holder_commitment_tx.inner;
977-
let tx_keys = commitment_tx.untrusted_key_derivation();
974+
let trusted_tx = initial_holder_commitment_tx.trust();
975+
let txid = trusted_tx.txid();
976+
977+
let tx_keys = trusted_tx.keys();
978978
let holder_commitment_tx = HolderSignedTx {
979979
txid,
980980
revocation_key: tx_keys.revocation_key,
981981
a_htlc_key: tx_keys.broadcaster_htlc_key,
982982
b_htlc_key: tx_keys.countersignatory_htlc_key,
983983
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
984984
per_commitment_point: tx_keys.per_commitment_point,
985-
feerate_per_kw: commitment_tx.feerate_per_kw(),
985+
feerate_per_kw: trusted_tx.feerate_per_kw(),
986986
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
987987
};
988-
(holder_commitment_tx, commitment_tx.commitment_number())
988+
(holder_commitment_tx, trusted_tx.commitment_number())
989989
};
990990
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_tx);
991991

@@ -1143,21 +1143,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11431143
/// up-to-date as our holder commitment transaction is updated.
11441144
/// Panics if set_on_holder_tx_csv has never been called.
11451145
fn provide_latest_holder_commitment_tx(&mut self, holder_commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1146-
let txid = holder_commitment_tx.untrusted_txid();
1147-
11481146
// block for Rust 1.34 compat
11491147
let mut new_holder_commitment_tx = {
1150-
let commitment_tx = &holder_commitment_tx.inner;
1151-
let tx_keys = &commitment_tx.untrusted_key_derivation();
1152-
self.current_holder_commitment_number = commitment_tx.commitment_number();
1148+
let trusted_tx = holder_commitment_tx.trust();
1149+
let txid = trusted_tx.txid();
1150+
let tx_keys = trusted_tx.keys();
1151+
self.current_holder_commitment_number = trusted_tx.commitment_number();
11531152
HolderSignedTx {
11541153
txid,
11551154
revocation_key: tx_keys.revocation_key,
11561155
a_htlc_key: tx_keys.broadcaster_htlc_key,
11571156
b_htlc_key: tx_keys.countersignatory_htlc_key,
11581157
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
11591158
per_commitment_point: tx_keys.per_commitment_point,
1160-
feerate_per_kw: commitment_tx.feerate_per_kw(),
1159+
feerate_per_kw: trusted_tx.feerate_per_kw(),
11611160
htlc_outputs,
11621161
}
11631162
};
@@ -1249,11 +1248,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12491248
}
12501249
for update in updates.updates.iter() {
12511250
match update {
1252-
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx: commitment_info, htlc_outputs } => {
1251+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
12531252
log_trace!(logger, "Updating ChannelMonitor with latest holder commitment transaction info");
12541253
if self.lockdown_from_offchain { panic!(); }
1255-
self.provide_latest_holder_commitment_tx(commitment_info.clone(), htlc_outputs.clone())?
1256-
},
1254+
self.provide_latest_holder_commitment_tx(commitment_tx.clone(), htlc_outputs.clone())?
1255+
}
12571256
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_revocation_point } => {
12581257
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
12591258
self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)

lightning/src/chain/keysinterface.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ pub trait ChannelKeys : Send+Clone {
231231
/// Note that if signing fails or is rejected, the channel will be force-closed.
232232
//
233233
// TODO: Document the things someone using this interface should enforce before signing.
234-
// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
235-
// making the callee generate it via some util function we expose)!
236234
fn sign_counterparty_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()>;
237235

238236
/// Create a signature for a holder's commitment transaction. This will only ever be called with
@@ -241,7 +239,6 @@ pub trait ChannelKeys : Send+Clone {
241239
/// An external signer implementation should check that the commitment has not been revoked.
242240
//
243241
// TODO: Document the things someone using this interface should enforce before signing.
244-
// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
245242
fn sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()>;
246243

247244
/// Same as sign_holder_commitment, but exists only for tests to get access to holder commitment
@@ -260,10 +257,7 @@ pub trait ChannelKeys : Send+Clone {
260257
/// ChannelMonitor decided to broadcast before it had been updated to the latest.
261258
///
262259
/// Either an Err should be returned, or a Vec with one entry for each HTLC which exists in
263-
/// holder_commitment_tx. For those HTLCs which have transaction_output_index set to None
264-
/// (implying they were considered dust at the time the commitment transaction was negotiated),
265-
/// a corresponding None should be included in the return value. All other positions in the
266-
/// return value must contain a signature.
260+
/// holder_commitment_tx.
267261
fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()>;
268262

269263
/// Create a signature for the given input in a transaction spending an HTLC or commitment
@@ -430,8 +424,7 @@ impl InMemoryChannelKeys {
430424

431425
/// The contest_delay value specified by our counterparty and applied on holder-broadcastable
432426
/// transactions, ie the amount of time that we have to wait to recover our funds if we
433-
/// broadcast a transaction. You'll likely want to pass this to the
434-
/// ln::chan_utils::build*_transaction functions when signing holder's transactions.
427+
/// broadcast a transaction.
435428
/// Will panic if ready_channel wasn't called.
436429
pub fn counterparty_selected_contest_delay(&self) -> u16 { self.channel_parameters.as_ref().unwrap().counterparty_parameters.as_ref().unwrap().selected_contest_delay }
437430

@@ -463,15 +456,15 @@ impl ChannelKeys for InMemoryChannelKeys {
463456
fn key_derivation_params(&self) -> (u64, u64) { self.key_derivation_params }
464457

465458
fn sign_counterparty_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
466-
let keys = commitment_tx.untrusted_key_derivation();
459+
let trusted_tx = commitment_tx.trust();
460+
let keys = trusted_tx.keys();
467461

468462
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
469463
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
470464

471-
let built_tx = commitment_tx.untrusted_built_transaction();
465+
let built_tx = trusted_tx.built_transaction();
472466
let commitment_sig = built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
473-
474-
let commitment_txid = commitment_tx.untrusted_txid();
467+
let commitment_txid = built_tx.txid;
475468

476469
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
477470
for htlc in commitment_tx.htlcs() {
@@ -491,28 +484,24 @@ impl ChannelKeys for InMemoryChannelKeys {
491484
fn sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
492485
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
493486
let funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
494-
495-
let built_tx = commitment_tx.inner.untrusted_built_transaction();
496-
let sig = built_tx.sign(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
487+
let sig = commitment_tx.trust().built_transaction().sign(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
497488
let htlc_sigs_o = self.sign_holder_commitment_htlc_transactions(&commitment_tx, secp_ctx)?;
498489
let htlc_sigs = htlc_sigs_o.iter().map(|o| o.unwrap()).collect();
499490

500491
Ok((sig, htlc_sigs))
501492
}
502493

503494
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
504-
fn unsafe_sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, holder_commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
495+
fn unsafe_sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
505496
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
506497
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
507-
508-
let built_tx = holder_commitment_tx.inner.untrusted_built_transaction();
509-
Ok(built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
498+
Ok(commitment_tx.trust().built_transaction().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
510499
}
511500

512501
fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {
513502
let channel_parameters = self.make_channel_parameters();
514-
let channel_parameters = channel_parameters.as_holder_broadcastable();
515-
commitment_tx.inner.get_htlc_sigs(&self.htlc_base_key, &channel_parameters, secp_ctx)
503+
let trusted_tx = commitment_tx.trust();
504+
trusted_tx.get_htlc_sigs(&self.htlc_base_key, &channel_parameters.as_holder_broadcastable(), secp_ctx)
516505
}
517506

518507
fn sign_justice_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &Option<HTLCOutputInCommitment>, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {

0 commit comments

Comments
 (0)