Skip to content

Commit 82e7887

Browse files
author
Antoine Riard
committed
Dryup SecretKey from ChannelMonitor::OnchainDetection
Key access is provided through ChanSigner.
1 parent 16fba9f commit 82e7887

File tree

1 file changed

+18
-48
lines changed

1 file changed

+18
-48
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,6 @@ pub(crate) const ANTI_REORG_DELAY: u32 = 6;
393393

394394
struct OnchainDetection<ChanSigner: ChannelKeys> {
395395
keys: ChanSigner,
396-
funding_key: SecretKey,
397-
revocation_base_key: SecretKey,
398-
htlc_base_key: SecretKey,
399-
delayed_payment_base_key: SecretKey,
400-
payment_base_key: SecretKey,
401396
funding_info: Option<(OutPoint, Script)>,
402397
current_remote_commitment_txid: Option<Sha256dHash>,
403398
prev_remote_commitment_txid: Option<Sha256dHash>,
@@ -875,11 +870,6 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
875870
self.shutdown_script.write(writer)?;
876871

877872
self.onchain_detection.keys.write(writer)?;
878-
writer.write_all(&self.onchain_detection.funding_key[..])?;
879-
writer.write_all(&self.onchain_detection.revocation_base_key[..])?;
880-
writer.write_all(&self.onchain_detection.htlc_base_key[..])?;
881-
writer.write_all(&self.onchain_detection.delayed_payment_base_key[..])?;
882-
writer.write_all(&self.onchain_detection.payment_base_key[..])?;
883873
match self.onchain_detection.funding_info {
884874
Some((ref outpoint, ref script)) => {
885875
writer.write_all(&outpoint.txid[..])?;
@@ -1086,21 +1076,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10861076
logger: Arc<Logger>) -> ChannelMonitor<ChanSigner> {
10871077

10881078
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
1089-
let funding_key = keys.funding_key().clone();
1090-
let revocation_base_key = keys.revocation_base_key().clone();
1091-
let htlc_base_key = keys.htlc_base_key().clone();
1092-
let delayed_payment_base_key = keys.delayed_payment_base_key().clone();
1093-
let payment_base_key = keys.payment_base_key().clone();
10941079
let our_channel_close_key_hash = Hash160::hash(&shutdown_pubkey.serialize());
10951080
let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
10961081

10971082
let onchain_detection = OnchainDetection {
10981083
keys: keys.clone(),
1099-
funding_key,
1100-
revocation_base_key,
1101-
htlc_base_key,
1102-
delayed_payment_base_key,
1103-
payment_base_key,
11041084
funding_info: Some(funding_info.clone()),
11051085
current_remote_commitment_txid: None,
11061086
prev_remote_commitment_txid: None,
@@ -1246,7 +1226,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12461226
let to_remote_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
12471227
.push_slice(&Hash160::hash(&payment_key.serialize())[..])
12481228
.into_script();
1249-
if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &self.onchain_detection.payment_base_key) {
1229+
if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &self.onchain_detection.keys.payment_base_key()) {
12501230
self.broadcasted_remote_payment_script = Some((to_remote_script, to_remote_key));
12511231
}
12521232
}
@@ -1449,9 +1429,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14491429
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
14501430
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
14511431
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
1452-
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.revocation_base_key));
1432+
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.keys.revocation_base_key()));
14531433
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().htlc_basepoint));
1454-
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.payment_base_key));
1434+
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.payment_base_key()));
14551435
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key.unwrap()));
14561436
let a_htlc_key = match self.their_htlc_base_key {
14571437
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
@@ -1606,12 +1586,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16061586
if let Some(revocation_point) = revocation_point_option {
16071587
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
16081588
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().htlc_basepoint));
1609-
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.htlc_base_key));
1589+
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.htlc_base_key()));
16101590
let a_htlc_key = match self.their_htlc_base_key {
16111591
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
16121592
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
16131593
};
1614-
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.payment_base_key));
1594+
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.payment_base_key()));
16151595

16161596
self.broadcasted_remote_payment_script = {
16171597
// Note that the Network here is ignored as we immediately drop the address for the
@@ -1663,7 +1643,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16631643
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
16641644
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
16651645
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
1666-
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.revocation_base_key));
1646+
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.keys.revocation_base_key()));
16671647
let delayed_key = match self.their_delayed_payment_base_key {
16681648
None => return (Vec::new(), None),
16691649
Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
@@ -1676,12 +1656,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16761656
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
16771657
}
16781658

1679-
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, delayed_payment_base_key: &SecretKey) -> (Vec<Transaction>, Vec<TxOut>, Option<(Script, SecretKey, Script)>) {
1659+
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx) -> (Vec<Transaction>, Vec<TxOut>, Option<(Script, SecretKey, Script)>) {
16801660
let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
16811661
let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
16821662

16831663
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
1684-
let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, delayed_payment_base_key) {
1664+
let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, self.onchain_detection.keys.delayed_payment_base_key()) {
16851665
Some((redeemscript.to_v0_p2wsh(), local_delayedkey, redeemscript))
16861666
} else { None };
16871667

@@ -1692,7 +1672,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16921672
log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
16931673
let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
16941674
let (our_sig, htlc_script) = match
1695-
chan_utils::sign_htlc_transaction(&mut htlc_timeout_tx, their_sig, &None, htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.htlc_base_key, &self.secp_ctx) {
1675+
chan_utils::sign_htlc_transaction(&mut htlc_timeout_tx, their_sig, &None, htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.keys.htlc_base_key(), &self.secp_ctx) {
16961676
Ok(res) => res,
16971677
Err(_) => continue,
16981678
};
@@ -1707,7 +1687,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17071687
log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
17081688
let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
17091689
let (our_sig, htlc_script) = match
1710-
chan_utils::sign_htlc_transaction(&mut htlc_success_tx, their_sig, &Some(*payment_preimage), htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.htlc_base_key, &self.secp_ctx) {
1690+
chan_utils::sign_htlc_transaction(&mut htlc_success_tx, their_sig, &Some(*payment_preimage), htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.keys.htlc_base_key(), &self.secp_ctx) {
17111691
Ok(res) => res,
17121692
Err(_) => continue,
17131693
};
@@ -1771,29 +1751,29 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17711751

17721752
if let &mut Some(ref mut local_tx) = &mut self.current_local_signed_commitment_tx {
17731753
if local_tx.txid == commitment_txid {
1774-
local_tx.tx.add_local_sig(&self.onchain_detection.funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
1754+
local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
17751755
}
17761756
}
17771757
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
17781758
if local_tx.txid == commitment_txid {
17791759
is_local_tx = true;
17801760
log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
17811761
assert!(local_tx.tx.has_local_sig());
1782-
let mut res = self.broadcast_by_local_state(local_tx, &self.onchain_detection.delayed_payment_base_key);
1762+
let mut res = self.broadcast_by_local_state(local_tx);
17831763
append_onchain_update!(res);
17841764
}
17851765
}
17861766
if let &mut Some(ref mut local_tx) = &mut self.prev_local_signed_commitment_tx {
17871767
if local_tx.txid == commitment_txid {
1788-
local_tx.tx.add_local_sig(&self.onchain_detection.funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
1768+
local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
17891769
}
17901770
}
17911771
if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
17921772
if local_tx.txid == commitment_txid {
17931773
is_local_tx = true;
17941774
log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
17951775
assert!(local_tx.tx.has_local_sig());
1796-
let mut res = self.broadcast_by_local_state(local_tx, &self.onchain_detection.delayed_payment_base_key);
1776+
let mut res = self.broadcast_by_local_state(local_tx);
17971777
append_onchain_update!(res);
17981778
}
17991779
}
@@ -1837,11 +1817,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18371817
// tracking state and panic!()ing if we get an update after force-closure/local-tx signing.
18381818
log_trace!(self, "Getting signed latest local commitment transaction!");
18391819
if let &mut Some(ref mut local_tx) = &mut self.current_local_signed_commitment_tx {
1840-
local_tx.tx.add_local_sig(&self.onchain_detection.funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
1820+
local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
18411821
}
18421822
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
18431823
let mut res = vec![local_tx.tx.with_valid_witness().clone()];
1844-
res.append(&mut self.broadcast_by_local_state(local_tx, &self.onchain_detection.delayed_payment_base_key).0);
1824+
res.append(&mut self.broadcast_by_local_state(local_tx).0);
18451825
// We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
18461826
// The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
18471827
res
@@ -1919,14 +1899,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19191899
} else { false };
19201900
if let Some(ref mut cur_local_tx) = self.current_local_signed_commitment_tx {
19211901
if should_broadcast {
1922-
cur_local_tx.tx.add_local_sig(&self.onchain_detection.funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
1902+
cur_local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
19231903
}
19241904
}
19251905
if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
19261906
if should_broadcast {
19271907
log_trace!(self, "Broadcast onchain {}", log_tx!(cur_local_tx.tx.with_valid_witness()));
19281908
broadcaster.broadcast_transaction(&cur_local_tx.tx.with_valid_witness());
1929-
let (txs, new_outputs, _) = self.broadcast_by_local_state(&cur_local_tx, &self.onchain_detection.delayed_payment_base_key);
1909+
let (txs, new_outputs, _) = self.broadcast_by_local_state(&cur_local_tx);
19301910
if !new_outputs.is_empty() {
19311911
watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
19321912
}
@@ -2283,11 +2263,6 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
22832263

22842264
let onchain_detection = {
22852265
let keys = Readable::read(reader)?;
2286-
let funding_key = Readable::read(reader)?;
2287-
let revocation_base_key = Readable::read(reader)?;
2288-
let htlc_base_key = Readable::read(reader)?;
2289-
let delayed_payment_base_key = Readable::read(reader)?;
2290-
let payment_base_key = Readable::read(reader)?;
22912266
// Technically this can fail and serialize fail a round-trip, but only for serialization of
22922267
// barely-init'd ChannelMonitors that we can't do anything with.
22932268
let outpoint = OutPoint {
@@ -2299,11 +2274,6 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
22992274
let prev_remote_commitment_txid = Readable::read(reader)?;
23002275
OnchainDetection {
23012276
keys,
2302-
funding_key,
2303-
revocation_base_key,
2304-
htlc_base_key,
2305-
delayed_payment_base_key,
2306-
payment_base_key,
23072277
funding_info,
23082278
current_remote_commitment_txid,
23092279
prev_remote_commitment_txid,

0 commit comments

Comments
 (0)