Skip to content

Commit b06a652

Browse files
committed
Move HTLCDescriptor to sign module
Now that `HTLCDescriptor` is no longer specific to anchors, it doesn't make sense for it to live in the `bump_transaction` module anymore.
1 parent aae4e7c commit b06a652

File tree

6 files changed

+158
-163
lines changed

6 files changed

+158
-163
lines changed

lightning/src/chain/channelmonitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ 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::{ChannelDerivationParameters, HTLCDescriptor, SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
4646
use crate::chain::onchaintx::{ClaimEvent, OnchainTxHandler};
4747
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
4848
use crate::chain::Filter;
4949
use crate::util::logger::Logger;
5050
use crate::util::ser::{Readable, ReadableArgs, RequiredWrapper, MaybeReadable, UpgradableRequired, Writer, Writeable, U48};
5151
use crate::util::byte_utils;
5252
use crate::events::{Event, EventHandler};
53-
use crate::events::bump_transaction::{ChannelDerivationParameters, AnchorDescriptor, HTLCDescriptor, BumpTransactionEvent};
53+
use crate::events::bump_transaction::{AnchorDescriptor, BumpTransactionEvent};
5454

5555
use crate::prelude::*;
5656
use core::{cmp, mem};

lightning/src/chain/onchaintx.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
2323
use bitcoin::secp256k1;
2424

2525
use crate::chain::chaininterface::compute_feerate_sat_per_1000_weight;
26-
use crate::events::bump_transaction::{ChannelDerivationParameters, HTLCDescriptor};
27-
use crate::sign::{ChannelSigner, EntropySource, SignerProvider};
26+
use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, ChannelSigner, EntropySource, SignerProvider, WriteableEcdsaChannelSigner};
2827
use crate::ln::msgs::DecodeError;
2928
use crate::ln::PaymentPreimage;
3029
use crate::ln::chan_utils::{self, ChannelTransactionParameters, HTLCOutputInCommitment, HolderCommitmentTransaction};
3130
use crate::chain::ClaimId;
3231
use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
3332
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
34-
use crate::sign::WriteableEcdsaChannelSigner;
3533
use crate::chain::package::{PackageSolvingData, PackageTemplate};
3634
use crate::util::logger::Logger;
3735
use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, UpgradableRequired, Writer, Writeable, VecWriter};

lightning/src/events/bump_transaction.rs

+7-153
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
2121
use crate::ln::chan_utils;
2222
use crate::ln::chan_utils::{
2323
ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
24-
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, ChannelTransactionParameters, HTLCOutputInCommitment
24+
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, HTLCOutputInCommitment
2525
};
26-
use crate::ln::PaymentPreimage;
2726
use crate::prelude::*;
28-
use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT};
27+
use crate::sign::{
28+
ChannelDerivationParameters, HTLCDescriptor, EcdsaChannelSigner, SignerProvider,
29+
WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT
30+
};
2931
use crate::sync::Mutex;
3032
use crate::util::logger::Logger;
3133

32-
use bitcoin::{OutPoint, PackedLockTime, PubkeyHash, Sequence, Script, Transaction, Txid, TxIn, TxOut, Witness, WPubkeyHash};
34+
use bitcoin::{OutPoint, PackedLockTime, PubkeyHash, Sequence, Script, Transaction, TxIn, TxOut, Witness, WPubkeyHash};
3335
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
3436
use bitcoin::consensus::Encodable;
3537
use bitcoin::secp256k1;
36-
use bitcoin::secp256k1::{PublicKey, Secp256k1};
38+
use bitcoin::secp256k1::Secp256k1;
3739
use bitcoin::secp256k1::ecdsa::Signature;
3840

3941
const EMPTY_SCRIPT_SIG_WEIGHT: u64 = 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64;
@@ -42,26 +44,6 @@ const BASE_INPUT_SIZE: u64 = 32 /* txid */ + 4 /* vout */ + 4 /* sequence */;
4244

4345
const BASE_INPUT_WEIGHT: u64 = BASE_INPUT_SIZE * WITNESS_SCALE_FACTOR as u64;
4446

45-
/// The parameters required to derive a channel signer via [`SignerProvider`].
46-
#[derive(Clone, Debug, PartialEq, Eq)]
47-
pub struct ChannelDerivationParameters {
48-
/// The value in satoshis of the channel we're attempting to spend the anchor output of.
49-
pub value_satoshis: u64,
50-
/// The unique identifier to re-derive the signer for the associated channel.
51-
pub keys_id: [u8; 32],
52-
/// The necessary channel parameters that need to be provided to the re-derived signer through
53-
/// [`ChannelSigner::provide_channel_parameters`].
54-
///
55-
/// [`ChannelSigner::provide_channel_parameters`]: crate::sign::ChannelSigner::provide_channel_parameters
56-
pub transaction_parameters: ChannelTransactionParameters,
57-
}
58-
59-
impl_writeable_tlv_based!(ChannelDerivationParameters, {
60-
(0, value_satoshis, required),
61-
(2, keys_id, required),
62-
(4, transaction_parameters, required),
63-
});
64-
6547
/// A descriptor used to sign for a commitment transaction's anchor output.
6648
#[derive(Clone, Debug, PartialEq, Eq)]
6749
pub struct AnchorDescriptor {
@@ -120,133 +102,6 @@ impl AnchorDescriptor {
120102
}
121103
}
122104

123-
/// A descriptor used to sign for a commitment transaction's HTLC output.
124-
#[derive(Clone, Debug, PartialEq, Eq)]
125-
pub struct HTLCDescriptor {
126-
/// The parameters required to derive the signer for the HTLC input.
127-
pub channel_derivation_parameters: ChannelDerivationParameters,
128-
/// The txid of the commitment transaction in which the HTLC output lives.
129-
pub commitment_txid: Txid,
130-
/// The number of the commitment transaction in which the HTLC output lives.
131-
pub per_commitment_number: u64,
132-
/// The key tweak corresponding to the number of the commitment transaction in which the HTLC
133-
/// output lives. This tweak is applied to all the basepoints for both parties in the channel to
134-
/// arrive at unique keys per commitment.
135-
///
136-
/// See <https://github.com/lightning/bolts/blob/master/03-transactions.md#keys> for more info.
137-
pub per_commitment_point: PublicKey,
138-
/// The feerate to use on the HTLC claiming transaction. This is always `0` for HTLCs
139-
/// originating from a channel supporting anchor outputs, otherwise it is the channel's
140-
/// negotiated feerate at the time the commitment transaction was built.
141-
pub feerate_per_kw: u32,
142-
/// The details of the HTLC as it appears in the commitment transaction.
143-
pub htlc: HTLCOutputInCommitment,
144-
/// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
145-
/// taken.
146-
pub preimage: Option<PaymentPreimage>,
147-
/// The counterparty's signature required to spend the HTLC output.
148-
pub counterparty_sig: Signature
149-
}
150-
151-
impl_writeable_tlv_based!(HTLCDescriptor, {
152-
(0, channel_derivation_parameters, required),
153-
(1, feerate_per_kw, (default_value, 0)),
154-
(2, commitment_txid, required),
155-
(4, per_commitment_number, required),
156-
(6, per_commitment_point, required),
157-
(8, htlc, required),
158-
(10, preimage, option),
159-
(12, counterparty_sig, required),
160-
});
161-
162-
impl HTLCDescriptor {
163-
/// Returns the outpoint of the HTLC output in the commitment transaction. This is the outpoint
164-
/// being spent by the HTLC input in the HTLC transaction.
165-
pub fn outpoint(&self) -> OutPoint {
166-
OutPoint {
167-
txid: self.commitment_txid,
168-
vout: self.htlc.transaction_output_index.unwrap(),
169-
}
170-
}
171-
172-
/// Returns the UTXO to be spent by the HTLC input, which can be obtained via
173-
/// [`Self::unsigned_tx_input`].
174-
pub fn previous_utxo<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> TxOut {
175-
TxOut {
176-
script_pubkey: self.witness_script(secp).to_v0_p2wsh(),
177-
value: self.htlc.amount_msat / 1000,
178-
}
179-
}
180-
181-
/// Returns the unsigned transaction input spending the HTLC output in the commitment
182-
/// transaction.
183-
pub fn unsigned_tx_input(&self) -> TxIn {
184-
chan_utils::build_htlc_input(
185-
&self.commitment_txid, &self.htlc, &self.channel_derivation_parameters.transaction_parameters.channel_type_features
186-
)
187-
}
188-
189-
/// Returns the delayed output created as a result of spending the HTLC output in the commitment
190-
/// transaction.
191-
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> TxOut {
192-
let channel_params = self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
193-
let broadcaster_keys = channel_params.broadcaster_pubkeys();
194-
let counterparty_keys = channel_params.countersignatory_pubkeys();
195-
let broadcaster_delayed_key = chan_utils::derive_public_key(
196-
secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
197-
);
198-
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
199-
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
200-
);
201-
chan_utils::build_htlc_output(
202-
self.feerate_per_kw, channel_params.contest_delay(), &self.htlc,
203-
channel_params.channel_type_features(), &broadcaster_delayed_key, &counterparty_revocation_key
204-
)
205-
}
206-
207-
/// Returns the witness script of the HTLC output in the commitment transaction.
208-
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> Script {
209-
let channel_params = self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
210-
let broadcaster_keys = channel_params.broadcaster_pubkeys();
211-
let counterparty_keys = channel_params.countersignatory_pubkeys();
212-
let broadcaster_htlc_key = chan_utils::derive_public_key(
213-
secp, &self.per_commitment_point, &broadcaster_keys.htlc_basepoint
214-
);
215-
let counterparty_htlc_key = chan_utils::derive_public_key(
216-
secp, &self.per_commitment_point, &counterparty_keys.htlc_basepoint
217-
);
218-
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
219-
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
220-
);
221-
chan_utils::get_htlc_redeemscript_with_explicit_keys(
222-
&self.htlc, channel_params.channel_type_features(), &broadcaster_htlc_key, &counterparty_htlc_key,
223-
&counterparty_revocation_key,
224-
)
225-
}
226-
227-
/// Returns the fully signed witness required to spend the HTLC output in the commitment
228-
/// transaction.
229-
pub fn tx_input_witness(&self, signature: &Signature, witness_script: &Script) -> Witness {
230-
chan_utils::build_htlc_input_witness(
231-
signature, &self.counterparty_sig, &self.preimage, witness_script,
232-
&self.channel_derivation_parameters.transaction_parameters.channel_type_features
233-
)
234-
}
235-
236-
/// Derives the channel signer required to sign the HTLC input.
237-
pub fn derive_channel_signer<S: WriteableEcdsaChannelSigner, SP: Deref>(&self, signer_provider: &SP) -> S
238-
where
239-
SP::Target: SignerProvider<Signer = S>
240-
{
241-
let mut signer = signer_provider.derive_channel_signer(
242-
self.channel_derivation_parameters.value_satoshis,
243-
self.channel_derivation_parameters.keys_id,
244-
);
245-
signer.provide_channel_parameters(&self.channel_derivation_parameters.transaction_parameters);
246-
signer
247-
}
248-
}
249-
250105
/// Represents the different types of transactions, originating from LDK, to be bumped.
251106
#[derive(Clone, Debug, PartialEq, Eq)]
252107
pub enum BumpTransactionEvent {
@@ -342,7 +197,6 @@ pub enum BumpTransactionEvent {
342197
///
343198
/// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
344199
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::EcdsaChannelSigner::sign_holder_htlc_transaction
345-
/// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
346200
HTLCResolution {
347201
/// The unique identifier for the claim of the HTLCs in the confirmed commitment
348202
/// transaction.

lightning/src/ln/channel.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8184,8 +8184,7 @@ mod tests {
81848184
use bitcoin::hashes::hex::FromHex;
81858185
use bitcoin::hash_types::Txid;
81868186
use bitcoin::secp256k1::Message;
8187-
use crate::events::bump_transaction::{ChannelDerivationParameters, HTLCDescriptor};
8188-
use crate::sign::EcdsaChannelSigner;
8187+
use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, EcdsaChannelSigner};
81898188
use crate::ln::PaymentPreimage;
81908189
use crate::ln::channel::{HTLCOutputInCommitment ,TxCreationKeys};
81918190
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};

0 commit comments

Comments
 (0)