Skip to content

Commit a3e622d

Browse files
committed
ChannelSignerDescriptor
1 parent df9bd93 commit a3e622d

File tree

4 files changed

+64
-55
lines changed

4 files changed

+64
-55
lines changed

lightning/src/chain/channelmonitor.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::chain;
4242
use crate::chain::{BestBlock, WatchedOutput};
4343
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoundedFeeEstimator};
4444
use crate::chain::transaction::{OutPoint, TransactionData};
45-
use crate::sign::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
45+
use crate::sign::{ChannelSignerDescriptor, SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
4646
#[cfg(anchors)]
4747
use crate::chain::onchaintx::ClaimEvent;
4848
use crate::chain::onchaintx::OnchainTxHandler;
@@ -2521,8 +2521,11 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25212521
commitment_tx,
25222522
commitment_tx_fee_satoshis,
25232523
anchor_descriptor: AnchorDescriptor {
2524-
channel_keys_id: self.channel_keys_id,
2525-
channel_value_satoshis: self.channel_value_satoshis,
2524+
signer_descriptor: ChannelSignerDescriptor {
2525+
channel_keys_id: self.channel_keys_id,
2526+
channel_value_satoshis: self.channel_value_satoshis,
2527+
channel_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
2528+
},
25262529
outpoint: BitcoinOutPoint {
25272530
txid: commitment_txid,
25282531
vout: anchor_output_idx,
@@ -2537,9 +2540,11 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25372540
let mut htlc_descriptors = Vec::with_capacity(htlcs.len());
25382541
for htlc in htlcs {
25392542
htlc_descriptors.push(HTLCDescriptor {
2540-
channel_keys_id: self.channel_keys_id,
2541-
channel_value_satoshis: self.channel_value_satoshis,
2542-
channel_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
2543+
signer_descriptor: ChannelSignerDescriptor {
2544+
channel_keys_id: self.channel_keys_id,
2545+
channel_value_satoshis: self.channel_value_satoshis,
2546+
channel_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
2547+
},
25432548
commitment_txid: htlc.commitment_txid,
25442549
per_commitment_number: htlc.per_commitment_number,
25452550
htlc: htlc.htlc,

lightning/src/events/bump_transaction.rs

+22-41
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use core::ops::Deref;
1414

1515
use crate::chain::chaininterface::BroadcasterInterface;
1616
use crate::chain::ClaimId;
17-
use crate::sign::{ChannelSigner, EcdsaChannelSigner, SignerProvider};
17+
use crate::sign::{ChannelSigner, ChannelSignerDescriptor, EcdsaChannelSigner, SignerProvider};
1818
use crate::io_extras::sink;
1919
use crate::ln::PaymentPreimage;
2020
use crate::ln::chan_utils;
2121
use crate::ln::chan_utils::{
2222
ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
23-
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, ChannelTransactionParameters, HTLCOutputInCommitment
23+
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, HTLCOutputInCommitment
2424
};
2525
use crate::events::Event;
2626
use crate::prelude::HashMap;
@@ -51,17 +51,8 @@ const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
5151
/// A descriptor used to sign for a commitment transaction's anchor output.
5252
#[derive(Clone, Debug, PartialEq, Eq)]
5353
pub struct AnchorDescriptor {
54-
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
55-
/// [`InMemorySigner`] required to sign `input`.
56-
///
57-
/// [`InMemorySigner`]: crate::sign::InMemorySigner
58-
pub channel_keys_id: [u8; 32],
59-
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
60-
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
61-
/// `input`.
62-
///
63-
/// [`InMemorySigner`]: crate::sign::InMemorySigner
64-
pub channel_value_satoshis: u64,
54+
/// The parameters needed to re-derive the [`InMemorySigner`] required to sign `input`.
55+
pub signer_descriptor: ChannelSignerDescriptor,
6556
/// The transaction input's outpoint corresponding to the commitment transaction's anchor
6657
/// output.
6758
pub outpoint: OutPoint,
@@ -70,23 +61,8 @@ pub struct AnchorDescriptor {
7061
/// A descriptor used to sign for a commitment transaction's HTLC output.
7162
#[derive(Clone, Debug, PartialEq, Eq)]
7263
pub struct HTLCDescriptor {
73-
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
74-
/// [`InMemorySigner`] required to sign `input`.
75-
///
76-
/// [`InMemorySigner`]: crate::sign::InMemorySigner
77-
pub channel_keys_id: [u8; 32],
78-
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
79-
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
80-
/// `input`.
81-
///
82-
/// [`InMemorySigner`]: crate::sign::InMemorySigner
83-
pub channel_value_satoshis: u64,
84-
/// The necessary channel parameters that need to be provided to the re-derived
85-
/// [`InMemorySigner`] through [`ChannelSigner::provide_channel_parameters`].
86-
///
87-
/// [`InMemorySigner`]: crate::sign::InMemorySigner
88-
/// [`ChannelSigner::provide_channel_parameters`]: crate::sign::ChannelSigner::provide_channel_parameters
89-
pub channel_parameters: ChannelTransactionParameters,
64+
/// The parameters needed to re-derive the [`InMemorySigner`] required to sign `input`.
65+
pub signer_descriptor: ChannelSignerDescriptor,
9066
/// The txid of the commitment transaction in which the HTLC output lives.
9167
pub commitment_txid: Txid,
9268
/// The number of the commitment transaction in which the HTLC output lives.
@@ -112,7 +88,7 @@ impl HTLCDescriptor {
11288
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
11389
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
11490
) -> TxOut {
115-
let channel_params = self.channel_parameters.as_holder_broadcastable();
91+
let channel_params = self.signer_descriptor.channel_parameters.as_holder_broadcastable();
11692
let broadcaster_keys = channel_params.broadcaster_pubkeys();
11793
let counterparty_keys = channel_params.countersignatory_pubkeys();
11894
let broadcaster_delayed_key = chan_utils::derive_public_key(
@@ -131,7 +107,7 @@ impl HTLCDescriptor {
131107
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
132108
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
133109
) -> Script {
134-
let channel_params = self.channel_parameters.as_holder_broadcastable();
110+
let channel_params = self.signer_descriptor.channel_parameters.as_holder_broadcastable();
135111
let broadcaster_keys = channel_params.broadcaster_pubkeys();
136112
let counterparty_keys = channel_params.countersignatory_pubkeys();
137113
let broadcaster_htlc_key = chan_utils::derive_public_key(
@@ -675,9 +651,11 @@ where
675651

676652
debug_assert_eq!(anchor_tx.output.len(), 1);
677653
self.utxo_source.sign_tx(&mut anchor_tx)?;
678-
let signer = self.signer_provider.derive_channel_signer(
679-
anchor_descriptor.channel_value_satoshis, anchor_descriptor.channel_keys_id,
654+
let mut signer = self.signer_provider.derive_channel_signer(
655+
anchor_descriptor.signer_descriptor.channel_value_satoshis,
656+
anchor_descriptor.signer_descriptor.channel_keys_id,
680657
);
658+
signer.provide_channel_parameters(&anchor_descriptor.signer_descriptor.channel_parameters);
681659
let anchor_sig = signer.sign_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
682660
anchor_tx.input[0].witness =
683661
chan_utils::build_anchor_input_witness(&signer.pubkeys().funding_pubkey, &anchor_sig);
@@ -703,12 +681,15 @@ where
703681
let mut signers = HashMap::new();
704682
let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
705683
for htlc_descriptor in htlc_descriptors {
706-
let signer = signers.entry(htlc_descriptor.channel_keys_id)
707-
.or_insert_with(||
708-
self.signer_provider.derive_channel_signer(
709-
htlc_descriptor.channel_value_satoshis, htlc_descriptor.channel_keys_id,
710-
)
711-
);
684+
let signer = signers.entry(htlc_descriptor.signer_descriptor.channel_keys_id)
685+
.or_insert_with(|| {
686+
let mut signer = self.signer_provider.derive_channel_signer(
687+
htlc_descriptor.signer_descriptor.channel_value_satoshis,
688+
htlc_descriptor.signer_descriptor.channel_keys_id,
689+
);
690+
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
691+
signer
692+
});
712693
let per_commitment_point = signer.get_per_commitment_point(
713694
htlc_descriptor.per_commitment_number, &self.secp
714695
);
@@ -746,7 +727,7 @@ where
746727

747728
self.utxo_source.sign_tx(&mut htlc_tx)?;
748729
for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
749-
let signer = signers.get(&htlc_descriptor.channel_keys_id).unwrap();
730+
let signer = signers.get(&htlc_descriptor.signer_descriptor.channel_keys_id).unwrap();
750731
let htlc_sig = signer.sign_holder_htlc_transaction(
751732
&htlc_tx, idx, htlc_descriptor, &self.secp
752733
)?;

lightning/src/ln/monitor_tests.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1800,9 +1800,11 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
18001800
// feerate for the test, we just want to make sure the feerates we receive from
18011801
// the events never decrease.
18021802
tx.input.push(descriptor.unsigned_tx_input());
1803-
let signer = nodes[0].keys_manager.derive_channel_keys(
1804-
descriptor.channel_value_satoshis, &descriptor.channel_keys_id,
1803+
let mut signer = nodes[0].keys_manager.derive_channel_keys(
1804+
descriptor.signer_descriptor.channel_value_satoshis,
1805+
&descriptor.signer_descriptor.channel_keys_id,
18051806
);
1807+
signer.provide_channel_parameters(&descriptor.signer_descriptor.channel_parameters);
18061808
let per_commitment_point = signer.get_per_commitment_point(
18071809
descriptor.per_commitment_number, &secp
18081810
);
@@ -2127,9 +2129,11 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
21272129
previous_output: anchor_descriptor.outpoint,
21282130
..Default::default()
21292131
});
2130-
let signer = nodes[1].keys_manager.derive_channel_keys(
2131-
anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
2132+
let mut signer = nodes[1].keys_manager.derive_channel_keys(
2133+
anchor_descriptor.signer_descriptor.channel_value_satoshis,
2134+
&anchor_descriptor.signer_descriptor.channel_keys_id,
21322135
);
2136+
signer.provide_channel_parameters(&anchor_descriptor.signer_descriptor.channel_parameters);
21332137
signers.push(signer);
21342138
},
21352139
_ => panic!("Unexpected event"),
@@ -2232,9 +2236,11 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
22322236
assert_eq!(htlc_descriptors.len(), 2);
22332237
for htlc_descriptor in &htlc_descriptors {
22342238
assert!(!htlc_descriptor.htlc.offered);
2235-
let signer = nodes[1].keys_manager.derive_channel_keys(
2236-
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2239+
let mut signer = nodes[1].keys_manager.derive_channel_keys(
2240+
htlc_descriptor.signer_descriptor.channel_value_satoshis,
2241+
&htlc_descriptor.signer_descriptor.channel_keys_id
22372242
);
2243+
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
22382244
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
22392245
htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
22402246
htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
@@ -2247,9 +2253,11 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
22472253
}
22482254
for (idx, htlc_descriptor) in descriptors.into_iter().enumerate() {
22492255
let htlc_input_idx = idx + 1;
2250-
let signer = nodes[1].keys_manager.derive_channel_keys(
2251-
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2256+
let mut signer = nodes[1].keys_manager.derive_channel_keys(
2257+
htlc_descriptor.signer_descriptor.channel_value_satoshis,
2258+
&htlc_descriptor.signer_descriptor.channel_keys_id
22522259
);
2260+
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
22532261
let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
22542262
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
22552263
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);

lightning/src/sign/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ use crate::util::invoice::construct_invoice_preimage;
6161
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
6262
pub struct KeyMaterial(pub [u8; 32]);
6363

64+
/// Information needed to re-derive a [`ChannelSigner`] via
65+
/// [`SignerProvider::derive_channel_signer`].
66+
#[derive(Clone, Debug, PartialEq, Eq)]
67+
pub struct ChannelSignerDescriptor {
68+
/// Arbitrary identification information returned by a call to
69+
/// [`ChannelSigner::channel_keys_id`]. This may be useful in re-deriving keys used in the
70+
/// channel.
71+
pub channel_keys_id: [u8; 32],
72+
/// The value of the channel in satoshis.
73+
pub channel_value_satoshis: u64,
74+
/// The necessary channel parameters that need to be provided to the re-derived
75+
/// [`InMemorySigner`] through [`ChannelSigner::provide_channel_parameters`].
76+
pub channel_parameters: ChannelTransactionParameters,
77+
}
78+
6479
/// Information about a spendable output to a P2WSH script.
6580
///
6681
/// See [`SpendableOutputDescriptor::DelayedPaymentOutput`] for more details on how to spend this.

0 commit comments

Comments
 (0)