@@ -37,14 +37,14 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
37
37
use bitcoin:: secp256k1:: schnorr;
38
38
use bitcoin:: { secp256k1, Sequence , Witness , Txid } ;
39
39
40
+ use crate :: ln:: channel:: ANCHOR_OUTPUT_VALUE_SATOSHI ;
40
41
use crate :: util:: transaction_utils;
41
42
use crate :: util:: crypto:: { hkdf_extract_expand_twice, sign, sign_with_aux_rand} ;
42
43
use crate :: util:: ser:: { Writeable , Writer , Readable , ReadableArgs } ;
43
44
use crate :: chain:: transaction:: OutPoint ;
44
- use crate :: ln:: channel:: ANCHOR_OUTPUT_VALUE_SATOSHI ;
45
45
use crate :: ln:: { chan_utils, PaymentPreimage } ;
46
46
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 , derive_add_tweak } ;
47
+ use crate :: ln:: channel_keys:: { DelayedPaymentBasepoint , DelayedPaymentKey , HtlcKey , HtlcBasepoint , RevocationKey , RevocationBasepoint , PaymentBasepoint } ;
48
48
use crate :: ln:: msgs:: { UnsignedChannelAnnouncement , UnsignedGossipMessage } ;
49
49
#[ cfg( taproot) ]
50
50
use crate :: ln:: msgs:: PartialSignatureWithNonce ;
@@ -103,8 +103,6 @@ pub struct DelayedPaymentOutputDescriptor {
103
103
pub channel_keys_id : [ u8 ; 32 ] ,
104
104
/// The value of the channel which this output originated from, possibly indirectly.
105
105
pub channel_value_satoshis : u64 ,
106
- /// Channel base key used to generate a witness data to spend this output.
107
- pub delayed_payment_basepoint : Option < DelayedPaymentBasepoint >
108
106
}
109
107
110
108
impl DelayedPaymentOutputDescriptor {
@@ -124,7 +122,6 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
124
122
( 8 , revocation_pubkey, required) ,
125
123
( 10 , channel_keys_id, required) ,
126
124
( 12 , channel_value_satoshis, required) ,
127
- ( 14 , delayed_payment_basepoint, option) ,
128
125
} ) ;
129
126
130
127
pub ( crate ) const P2WPKH_WITNESS_WEIGHT : u64 = 1 /* num stack items */ +
@@ -309,7 +306,7 @@ impl SpendableOutputDescriptor {
309
306
///
310
307
/// This is not exported to bindings users as there is no standard serialization for an input.
311
308
/// See [`Self::create_spendable_outputs_psbt`] instead.
312
- pub fn to_psbt_input < T : secp256k1:: Signing > ( & self , secp_ctx : & Secp256k1 < T > ) -> bitcoin:: psbt:: Input {
309
+ pub fn to_psbt_input < T : secp256k1:: Signing > ( & self , secp_ctx : & Secp256k1 < T > , channel_public_keys : Option < & ChannelPublicKeys > ) -> bitcoin:: psbt:: Input {
313
310
match self {
314
311
SpendableOutputDescriptor :: StaticOutput { output, .. } => {
315
312
// Is a standard P2WPKH, no need for witness script
@@ -319,14 +316,18 @@ impl SpendableOutputDescriptor {
319
316
}
320
317
} ,
321
318
SpendableOutputDescriptor :: DelayedPaymentOutput ( descriptor) => {
322
- let ( witness_script, add_tweak) = if let Some ( basepoint) = descriptor. delayed_payment_basepoint . as_ref ( ) {
319
+ let delayed_payment_basepoint = channel_public_keys. map ( |keys| DelayedPaymentBasepoint :: from (
320
+ keys. delayed_payment_basepoint ,
321
+ ) ) ;
322
+
323
+ let ( witness_script, add_tweak) = if let Some ( basepoint) = delayed_payment_basepoint. as_ref ( ) {
323
324
let payment_key = DelayedPaymentKey :: from_basepoint (
324
325
secp_ctx,
325
326
basepoint,
326
327
& descriptor. per_commitment_point ,
327
328
) ;
328
329
// 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
+ let add_tweak = basepoint . derive_add_tweak ( & descriptor. per_commitment_point ) ;
330
331
( Some ( get_revokeable_redeemscript (
331
332
& descriptor. revocation_pubkey ,
332
333
descriptor. to_self_delay ,
@@ -336,7 +337,6 @@ impl SpendableOutputDescriptor {
336
337
( None , None )
337
338
} ;
338
339
339
-
340
340
bitcoin:: psbt:: Input {
341
341
witness_utxo : Some ( descriptor. output . clone ( ) ) ,
342
342
witness_script,
@@ -346,15 +346,43 @@ impl SpendableOutputDescriptor {
346
346
subtype: 0 ,
347
347
key: "add_tweak" . as_bytes( ) . to_vec( ) ,
348
348
} ,
349
- add_tweak,
349
+ add_tweak. to_vec ( ) ,
350
350
) ] . into_iter ( ) . collect ( ) } ) . unwrap_or_default ( ) ,
351
351
..Default :: default ( )
352
352
}
353
353
} ,
354
354
SpendableOutputDescriptor :: StaticPaymentOutput ( descriptor) => {
355
- // TODO we could add the witness script as well
355
+ // Use simplified derivation, assuming `option_static_remotekey` or `option_anchors` is negotiated.
356
+ // `remote_payment_basepoint` is used to produce add tweak which is needed in order to produce the signing key.
357
+ let remote_payment_basepoint = channel_public_keys. map ( |keys|
358
+ PaymentBasepoint :: from ( keys. payment_point )
359
+ ) ;
360
+
361
+ let witness_script = match remote_payment_basepoint {
362
+ Some ( ref basepoint) => {
363
+ // We cannot always assume that `channel_parameters` is set, so can't just call
364
+ // `self.channel_parameters()` or anything that relies on it
365
+ let supports_anchors_zero_fee_htlc_tx = descriptor. channel_transaction_parameters . as_ref ( )
366
+ . map ( |features| features. channel_type_features . supports_anchors_zero_fee_htlc_tx ( ) )
367
+ . unwrap_or ( false ) ;
368
+
369
+ let witness_script = if supports_anchors_zero_fee_htlc_tx {
370
+ chan_utils:: get_to_countersignatory_with_anchors_redeemscript ( & basepoint. to_public_key ( ) )
371
+ } else {
372
+ ScriptBuf :: new_p2pkh ( & bitcoin:: PublicKey :: new ( basepoint. to_public_key ( ) ) . pubkey_hash ( ) )
373
+ } ;
374
+
375
+ // With simplified derivation, the private payment key is equal to private payment basepoint,
376
+ // so add tweak is not needed.
377
+ Some ( witness_script)
378
+ } ,
379
+ _ => None ,
380
+ } ;
381
+
382
+
356
383
bitcoin:: psbt:: Input {
357
384
witness_utxo : Some ( descriptor. output . clone ( ) ) ,
385
+ witness_script,
358
386
..Default :: default ( )
359
387
}
360
388
} ,
@@ -378,7 +406,7 @@ impl SpendableOutputDescriptor {
378
406
/// does not match the one we can spend.
379
407
///
380
408
/// We do not enforce that outputs meet the dust limit or that any output scripts are standard.
381
- 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 ) , ( ) > {
409
+ pub fn create_spendable_outputs_psbt ( descriptors : & [ & SpendableOutputDescriptor ] , outputs : Vec < TxOut > , change_destination_script : ScriptBuf , feerate_sat_per_1000_weight : u32 , locktime : Option < LockTime > , channel_public_keys : Option < & ChannelPublicKeys > ) -> Result < ( PartiallySignedTransaction , u64 ) , ( ) > {
382
410
let secp_ctx = Secp256k1 :: new ( ) ;
383
411
let mut input = Vec :: with_capacity ( descriptors. len ( ) ) ;
384
412
let mut input_value = 0 ;
@@ -446,7 +474,7 @@ impl SpendableOutputDescriptor {
446
474
let expected_max_weight =
447
475
transaction_utils:: maybe_add_change_output ( & mut tx, input_value, witness_weight, feerate_sat_per_1000_weight, change_destination_script) ?;
448
476
449
- let psbt_inputs = descriptors. iter ( ) . map ( |d| d. to_psbt_input ( & secp_ctx) ) . collect :: < Vec < _ > > ( ) ;
477
+ let psbt_inputs = descriptors. iter ( ) . map ( |d| d. to_psbt_input ( & secp_ctx, channel_public_keys ) ) . collect :: < Vec < _ > > ( ) ;
450
478
let psbt = PartiallySignedTransaction {
451
479
inputs : psbt_inputs,
452
480
outputs : vec ! [ Default :: default ( ) ; tx. output. len( ) ] ,
@@ -1649,7 +1677,8 @@ impl KeysManager {
1649
1677
/// May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
1650
1678
/// this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
1651
1679
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 , ( ) > {
1652
- let ( mut psbt, expected_max_weight) = SpendableOutputDescriptor :: create_spendable_outputs_psbt ( descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime) ?;
1680
+ // TODO: provide channel keys to construct witness script
1681
+ let ( mut psbt, expected_max_weight) = SpendableOutputDescriptor :: create_spendable_outputs_psbt ( descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime, None ) ?;
1653
1682
psbt = self . sign_spendable_outputs_psbt ( descriptors, psbt, secp_ctx) ?;
1654
1683
1655
1684
let spend_tx = psbt. extract_tx ( ) ;
0 commit comments