Skip to content

Commit 5c91c1b

Browse files
committed
Improve the API of the derived fields in CommitmentTransaction
1 parent 39d86ae commit 5c91c1b

File tree

6 files changed

+183
-142
lines changed

6 files changed

+183
-142
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 12 additions & 12 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,21 @@ 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+
1151+
let tx_keys = trusted_tx.keys();
1152+
self.current_holder_commitment_number = trusted_tx.commitment_number();
11531153
HolderSignedTx {
11541154
txid,
11551155
revocation_key: tx_keys.revocation_key,
11561156
a_htlc_key: tx_keys.broadcaster_htlc_key,
11571157
b_htlc_key: tx_keys.countersignatory_htlc_key,
11581158
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
11591159
per_commitment_point: tx_keys.per_commitment_point,
1160-
feerate_per_kw: commitment_tx.feerate_per_kw(),
1160+
feerate_per_kw: trusted_tx.feerate_per_kw(),
11611161
htlc_outputs,
11621162
}
11631163
};

lightning/src/chain/keysinterface.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,16 @@ impl ChannelKeys for InMemoryChannelKeys {
463463
fn key_derivation_params(&self) -> (u64, u64) { self.key_derivation_params }
464464

465465
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();
466+
let trusted_tx = commitment_tx.trust();
467+
let keys = trusted_tx.keys();
467468

468469
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
469470
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
470471

471-
let built_tx = commitment_tx.untrusted_built_transaction();
472+
let built_tx = trusted_tx.built_transaction();
472473
let commitment_sig = built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
473474

474-
let commitment_txid = commitment_tx.untrusted_txid();
475+
let commitment_txid = trusted_tx.txid();
475476

476477
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
477478
for htlc in commitment_tx.htlcs() {
@@ -491,8 +492,8 @@ impl ChannelKeys for InMemoryChannelKeys {
491492
fn sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
492493
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
493494
let funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
494-
495-
let built_tx = commitment_tx.inner.untrusted_built_transaction();
495+
let trusted_tx = commitment_tx.trust();
496+
let built_tx = trusted_tx.built_transaction();
496497
let sig = built_tx.sign(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
497498
let htlc_sigs_o = self.sign_holder_commitment_htlc_transactions(&commitment_tx, secp_ctx)?;
498499
let htlc_sigs = htlc_sigs_o.iter().map(|o| o.unwrap()).collect();
@@ -504,15 +505,17 @@ impl ChannelKeys for InMemoryChannelKeys {
504505
fn unsafe_sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, holder_commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
505506
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
506507
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
508+
let trusted_tx = holder_commitment_tx.trust();
507509

508-
let built_tx = holder_commitment_tx.inner.untrusted_built_transaction();
510+
let built_tx = trusted_tx.built_transaction();
509511
Ok(built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
510512
}
511513

512514
fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {
513515
let channel_parameters = self.make_channel_parameters();
514516
let channel_parameters = channel_parameters.as_holder_broadcastable();
515-
commitment_tx.inner.get_htlc_sigs(&self.htlc_base_key, &channel_parameters, secp_ctx)
517+
let trusted_tx = commitment_tx.trust();
518+
trusted_tx.get_htlc_sigs(&self.htlc_base_key, &channel_parameters, secp_ctx)
516519
}
517520

518521
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)