Skip to content

Commit 19fd700

Browse files
committed
Add delayed payment basepoint to the DelayedPaymentOutput descriptor so it could be used to generate the script for witness.
1 parent 5cdbf5e commit 19fd700

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lightning/src/chain/channelmonitor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40954095
revocation_pubkey: broadcasted_holder_revokable_script.2,
40964096
channel_keys_id: self.channel_keys_id,
40974097
channel_value_satoshis: self.channel_value_satoshis,
4098+
delayed_payment_basepoint: self.onchain_tx_handler.signer.pubkeys().delayed_payment_basepoint,
40984099
}));
40994100
}
41004101
}

lightning/src/sign/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ pub struct DelayedPaymentOutputDescriptor {
103103
pub channel_keys_id: [u8; 32],
104104
/// The value of the channel which this output originated from, possibly indirectly.
105105
pub channel_value_satoshis: u64,
106+
/// Channel base key used to generate a witness data to spend this output.
107+
pub delayed_payment_basepoint: DelayedPaymentBasepoint
106108
}
109+
107110
impl DelayedPaymentOutputDescriptor {
108111
/// The maximum length a well-formed witness spending one of these should have.
109112
/// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
@@ -121,6 +124,7 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
121124
(8, revocation_pubkey, required),
122125
(10, channel_keys_id, required),
123126
(12, channel_value_satoshis, required),
127+
(14, delayed_payment_basepoint, required),
124128
});
125129

126130
pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
@@ -305,7 +309,7 @@ impl SpendableOutputDescriptor {
305309
///
306310
/// This is not exported to bindings users as there is no standard serialization for an input.
307311
/// See [`Self::create_spendable_outputs_psbt`] instead.
308-
pub fn to_psbt_input<T: secp256k1::Signing>(&self, secp_ctx: &Secp256k1<T>, delayed_payment_basepoint: Option<&DelayedPaymentBasepoint>) -> bitcoin::psbt::Input {
312+
pub fn to_psbt_input<T: secp256k1::Signing>(&self, secp_ctx: &Secp256k1<T>) -> bitcoin::psbt::Input {
309313
match self {
310314
SpendableOutputDescriptor::StaticOutput { output, .. } => {
311315
// Is a standard P2WPKH, no need for witness script
@@ -315,21 +319,21 @@ impl SpendableOutputDescriptor {
315319
}
316320
},
317321
SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
318-
let witness_script = delayed_payment_basepoint.map(|basepoint| {
322+
let witness_script = {
319323
let payment_key = DelayedPaymentKey::from_basepoint(
320324
secp_ctx,
321-
basepoint,
325+
&descriptor.delayed_payment_basepoint,
322326
&descriptor.per_commitment_point,
323327
);
324328
get_revokeable_redeemscript(
325329
&descriptor.revocation_pubkey,
326330
descriptor.to_self_delay,
327331
&payment_key,
328332
)
329-
});
333+
};
330334
bitcoin::psbt::Input {
331335
witness_utxo: Some(descriptor.output.clone()),
332-
witness_script: witness_script,
336+
witness_script: Some(witness_script),
333337
..Default::default()
334338
}
335339
},
@@ -359,7 +363,7 @@ impl SpendableOutputDescriptor {
359363
/// does not match the one we can spend.
360364
///
361365
/// We do not enforce that outputs meet the dust limit or that any output scripts are standard.
362-
pub fn create_spendable_outputs_psbt(descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>, change_destination_script: ScriptBuf, feerate_sat_per_1000_weight: u32, locktime: Option<LockTime>, delayed_payment_basepoint: Option<&DelayedPaymentBasepoint>) -> Result<(PartiallySignedTransaction, u64), ()> {
366+
pub fn create_spendable_outputs_psbt(descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>, change_destination_script: ScriptBuf, feerate_sat_per_1000_weight: u32, locktime: Option<LockTime>) -> Result<(PartiallySignedTransaction, u64), ()> {
363367
let secp_ctx = Secp256k1::new();
364368
let mut input = Vec::with_capacity(descriptors.len());
365369
let mut input_value = 0;
@@ -404,7 +408,7 @@ impl SpendableOutputDescriptor {
404408
{ witness_weight -= 1; } // Guarantees a low R signature
405409
input_value += descriptor.output.value;
406410

407-
add_tweak = delayed_payment_basepoint.and_then(|basepoint| Some(derive_add_tweak(&descriptor.per_commitment_point, &basepoint)));
411+
add_tweak = Some(derive_add_tweak(&descriptor.per_commitment_point, &descriptor.delayed_payment_basepoint));
408412
},
409413
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
410414
if !output_set.insert(*outpoint) { return Err(()); }
@@ -431,7 +435,7 @@ impl SpendableOutputDescriptor {
431435
let expected_max_weight =
432436
transaction_utils::maybe_add_change_output(&mut tx, input_value, witness_weight, feerate_sat_per_1000_weight, change_destination_script)?;
433437

434-
let psbt_inputs = descriptors.iter().map(|d| d.to_psbt_input(&secp_ctx, delayed_payment_basepoint)).collect::<Vec<_>>();
438+
let psbt_inputs = descriptors.iter().map(|d| d.to_psbt_input(&secp_ctx)).collect::<Vec<_>>();
435439
let psbt = PartiallySignedTransaction {
436440
inputs: psbt_inputs,
437441
outputs: vec![Default::default(); tx.output.len()],
@@ -1656,7 +1660,7 @@ impl KeysManager {
16561660
/// May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
16571661
/// this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
16581662
pub fn spend_spendable_outputs<C: Signing>(&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>, change_destination_script: ScriptBuf, feerate_sat_per_1000_weight: u32, locktime: Option<LockTime>, secp_ctx: &Secp256k1<C>) -> Result<Transaction, ()> {
1659-
let (mut psbt, expected_max_weight) = SpendableOutputDescriptor::create_spendable_outputs_psbt(descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime, None)?;
1663+
let (mut psbt, expected_max_weight) = SpendableOutputDescriptor::create_spendable_outputs_psbt(descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime)?;
16601664
psbt = self.sign_spendable_outputs_psbt(descriptors, psbt, secp_ctx)?;
16611665

16621666
let spend_tx = psbt.extract_tx();

0 commit comments

Comments
 (0)