Skip to content

Commit 075ee16

Browse files
committed
DelayedPaymentBasepoint is optional in channel monitor file for a spendable output.
Add Tweak is moved to the input level.
1 parent d018d1b commit 075ee16

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4164,7 +4164,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41644164
revocation_pubkey: broadcasted_holder_revokable_script.2,
41654165
channel_keys_id: self.channel_keys_id,
41664166
channel_value_satoshis: self.channel_value_satoshis,
4167-
delayed_payment_basepoint: self.onchain_tx_handler.signer.pubkeys().delayed_payment_basepoint,
4167+
delayed_payment_basepoint: Some(self.onchain_tx_handler.signer.pubkeys().delayed_payment_basepoint),
41684168
}));
41694169
}
41704170
}

lightning/src/ln/channel_keys.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ impl RevocationKey {
226226
key_read_write!(RevocationKey);
227227

228228

229+
/// Derives a per-commitment-transaction (eg an htlc key or delayed_payment key) private key addition tweak
230+
/// from a delayed payment basepoint and a per_commitment_point:
231+
/// `privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`
232+
/// TODO(oleg): refactor after migration to LDK v119
233+
pub fn derive_add_tweak(
234+
per_commitment_point: &PublicKey,
235+
basepoint: &DelayedPaymentBasepoint,
236+
) -> Vec<u8> {
237+
let mut sha = Sha256::engine();
238+
sha.input(&per_commitment_point.serialize());
239+
sha.input(&basepoint.to_public_key().serialize());
240+
let res = Sha256::from_engine(sha).to_byte_array();
241+
res.to_vec()
242+
}
243+
229244

230245
#[cfg(test)]
231246
mod test {

lightning/src/sign/mod.rs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::chain::transaction::OutPoint;
4444
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
4545
use crate::ln::{chan_utils, PaymentPreimage};
4646
use crate::ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction, get_revokeable_redeemscript};
47-
use crate::ln::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcKey, HtlcBasepoint, RevocationKey, RevocationBasepoint};
47+
use crate::ln::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcKey, HtlcBasepoint, RevocationKey, RevocationBasepoint, derive_add_tweak};
4848
use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
4949
#[cfg(taproot)]
5050
use crate::ln::msgs::PartialSignatureWithNonce;
@@ -104,7 +104,7 @@ pub struct DelayedPaymentOutputDescriptor {
104104
/// The value of the channel which this output originated from, possibly indirectly.
105105
pub channel_value_satoshis: u64,
106106
/// Channel base key used to generate a witness data to spend this output.
107-
pub delayed_payment_basepoint: DelayedPaymentBasepoint
107+
pub delayed_payment_basepoint: Option<DelayedPaymentBasepoint>
108108
}
109109

110110
impl DelayedPaymentOutputDescriptor {
@@ -124,7 +124,7 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
124124
(8, revocation_pubkey, required),
125125
(10, channel_keys_id, required),
126126
(12, channel_value_satoshis, required),
127-
(14, delayed_payment_basepoint, required),
127+
(14, delayed_payment_basepoint, option),
128128
});
129129

130130
pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
@@ -319,21 +319,35 @@ impl SpendableOutputDescriptor {
319319
}
320320
},
321321
SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
322-
let witness_script = {
323-
let payment_key = DelayedPaymentKey::from_basepoint(
324-
secp_ctx,
325-
&descriptor.delayed_payment_basepoint,
326-
&descriptor.per_commitment_point,
327-
);
328-
get_revokeable_redeemscript(
329-
&descriptor.revocation_pubkey,
330-
descriptor.to_self_delay,
331-
&payment_key,
332-
)
322+
let (witness_script, add_tweak) = if let Some(basepoint) = descriptor.delayed_payment_basepoint.as_ref() {
323+
let payment_key = DelayedPaymentKey::from_basepoint(
324+
secp_ctx,
325+
basepoint,
326+
&descriptor.per_commitment_point,
327+
);
328+
// Required to derive signing key: privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
329+
let add_tweak = derive_add_tweak(&descriptor.per_commitment_point, basepoint);
330+
(Some(get_revokeable_redeemscript(
331+
&descriptor.revocation_pubkey,
332+
descriptor.to_self_delay,
333+
&payment_key,
334+
)), Some(add_tweak))
335+
} else {
336+
(None, None)
333337
};
338+
339+
334340
bitcoin::psbt::Input {
335341
witness_utxo: Some(descriptor.output.clone()),
336-
witness_script: Some(witness_script),
342+
witness_script,
343+
proprietary: add_tweak.map(|add_tweak| {vec![(
344+
raw::ProprietaryKey {
345+
prefix: "LDK_spendable_output".as_bytes().to_vec(),
346+
subtype: 0,
347+
key: "add_tweak".as_bytes().to_vec(),
348+
},
349+
add_tweak,
350+
)].into_iter().collect()}).unwrap_or_default(),
337351
..Default::default()
338352
}
339353
},
@@ -370,8 +384,6 @@ impl SpendableOutputDescriptor {
370384
let mut input_value = 0;
371385
let mut witness_weight = 0;
372386
let mut output_set = HashSet::with_capacity(descriptors.len());
373-
// Required to derive signing key: privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
374-
let mut add_tweak: Option<Vec<u8>> = None;
375387
for outp in descriptors {
376388
match outp {
377389
SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
@@ -408,8 +420,6 @@ impl SpendableOutputDescriptor {
408420
#[cfg(feature = "grind_signatures")]
409421
{ witness_weight -= 1; } // Guarantees a low R signature
410422
input_value += descriptor.output.value;
411-
412-
add_tweak = Some(derive_add_tweak(&descriptor.per_commitment_point, &descriptor.delayed_payment_basepoint));
413423
},
414424
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
415425
if !output_set.insert(*outpoint) { return Err(()); }
@@ -443,35 +453,13 @@ impl SpendableOutputDescriptor {
443453
unsigned_tx: tx,
444454
xpub: Default::default(),
445455
version: 0,
446-
proprietary: add_tweak.map(|add_tweak| {vec![(
447-
raw::ProprietaryKey {
448-
prefix: "spendable_output".as_bytes().to_vec(),
449-
subtype: 0,
450-
key: "add_tweak".as_bytes().to_vec(),
451-
},
452-
add_tweak,
453-
)].into_iter().collect()}).unwrap_or_default(),
456+
proprietary: Default::default(),
454457
unknown: Default::default(),
455458
};
456459
Ok((psbt, expected_max_weight))
457460
}
458461
}
459462

460-
/// Derives a per-commitment-transaction (eg an htlc key or delayed_payment key) private key addition tweak
461-
/// from a delayed payment basepoint and a per_commitment_point:
462-
/// `privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`
463-
/// TODO(oleg): refactor after migration to LDK v119
464-
pub fn derive_add_tweak(
465-
per_commitment_point: &PublicKey,
466-
basepoint: &DelayedPaymentBasepoint,
467-
) -> Vec<u8> {
468-
let mut sha = Sha256::engine();
469-
sha.input(&per_commitment_point.serialize());
470-
sha.input(&basepoint.to_public_key().serialize());
471-
let res = Sha256::from_engine(sha).to_byte_array();
472-
res.to_vec()
473-
}
474-
475463

476464
/// The parameters required to derive a channel signer via [`SignerProvider`].
477465
#[derive(Clone, Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)