Skip to content

Commit 1d2d393

Browse files
authored
Merge pull request #662 from lightning-signer/compat5
Export various fields and structures
2 parents 22a0dd5 + fd2db40 commit 1d2d393

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

lightning/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ max_level_error = []
1818
max_level_warn = []
1919
max_level_info = []
2020
max_level_debug = []
21+
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
22+
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
23+
unsafe_revoked_tx_signing = []
2124

2225
[dependencies]
2326
bitcoin = "0.23"

lightning/src/chain/keysinterface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub trait ChannelKeys : Send+Clone {
247247
/// transactions which will be broadcasted later, after the channel has moved on to a newer
248248
/// state. Thus, needs its own method as sign_local_commitment may enforce that we only ever
249249
/// get called once.
250-
#[cfg(test)]
250+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
251251
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
252252

253253
/// Create a signature for each HTLC transaction spending a local commitment transaction.
@@ -508,7 +508,7 @@ impl ChannelKeys for InMemoryChannelKeys {
508508
Ok(local_commitment_tx.get_local_sig(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
509509
}
510510

511-
#[cfg(test)]
511+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
512512
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
513513
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
514514
let remote_channel_pubkeys = &self.accepted_channel_data.as_ref().expect("must accept before signing").remote_channel_pubkeys;

lightning/src/ln/chan_utils.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,26 @@ impl LocalCommitmentTransaction {
599599
}
600600

601601
/// Generate a new LocalCommitmentTransaction based on a raw commitment transaction,
602-
/// remote signature and both parties keys
603-
pub(crate) fn new_missing_local_sig(unsigned_tx: Transaction, their_sig: Signature, our_funding_key: &PublicKey, their_funding_key: &PublicKey, local_keys: TxCreationKeys, feerate_per_kw: u32, htlc_data: Vec<(HTLCOutputInCommitment, Option<Signature>)>) -> LocalCommitmentTransaction {
602+
/// remote signature and both parties keys.
603+
///
604+
/// The unsigned transaction outputs must be consistent with htlc_data. This function
605+
/// only checks that the shape and amounts are consistent, but does not check the scriptPubkey.
606+
pub fn new_missing_local_sig(unsigned_tx: Transaction, their_sig: Signature, our_funding_key: &PublicKey, their_funding_key: &PublicKey, local_keys: TxCreationKeys, feerate_per_kw: u32, htlc_data: Vec<(HTLCOutputInCommitment, Option<Signature>)>) -> LocalCommitmentTransaction {
604607
if unsigned_tx.input.len() != 1 { panic!("Tried to store a commitment transaction that had input count != 1!"); }
605608
if unsigned_tx.input[0].witness.len() != 0 { panic!("Tried to store a signed commitment transaction?"); }
606609

610+
for htlc in &htlc_data {
611+
if let Some(index) = htlc.0.transaction_output_index {
612+
let out = &unsigned_tx.output[index as usize];
613+
if out.value != htlc.0.amount_msat / 1000 {
614+
panic!("HTLC at index {} has incorrect amount", index);
615+
}
616+
if !out.script_pubkey.is_v0_p2wsh() {
617+
panic!("HTLC at index {} doesn't have p2wsh scriptPubkey", index);
618+
}
619+
}
620+
}
621+
607622
Self {
608623
unsigned_tx,
609624
their_sig,

lightning/src/ln/channelmonitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: D
175175
L::Target: Logger,
176176
C::Target: ChainWatchInterface,
177177
{
178-
#[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
178+
/// The monitors
179179
pub monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
180-
#[cfg(not(test))]
181-
monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
182180
chain_monitor: C,
183181
broadcaster: T,
184182
logger: L,
@@ -1849,7 +1847,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18491847
/// Unsafe test-only version of get_latest_local_commitment_txn used by our test framework
18501848
/// to bypass LocalCommitmentTransaction state update lockdown after signature and generate
18511849
/// revoked commitment transaction.
1852-
#[cfg(test)]
1850+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
18531851
pub fn unsafe_get_latest_local_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
18541852
log_trace!(logger, "Getting signed copy of latest local commitment transaction!");
18551853
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_copy_local_tx(&self.funding_redeemscript) {

lightning/src/ln/onchaintx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
939939
}
940940
}
941941

942-
#[cfg(test)]
942+
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
943943
pub(super) fn get_fully_signed_copy_local_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
944944
if let Some(ref mut local_commitment) = self.local_commitment {
945945
let local_commitment = local_commitment.clone();
@@ -979,7 +979,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
979979
htlc_tx
980980
}
981981

982-
#[cfg(test)]
982+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
983983
pub(super) fn unsafe_get_fully_signed_htlc_tx(&mut self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>) -> Option<Transaction> {
984984
let latest_had_sigs = self.local_htlc_sigs.is_some();
985985
let prev_had_sigs = self.prev_local_htlc_sigs.is_some();

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl ChannelKeys for EnforcingChannelKeys {
9393
Ok(self.inner.sign_local_commitment(local_commitment_tx, secp_ctx).unwrap())
9494
}
9595

96-
#[cfg(test)]
96+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
9797
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
9898
Ok(self.inner.unsafe_sign_local_commitment(local_commitment_tx, secp_ctx).unwrap())
9999
}

0 commit comments

Comments
 (0)