@@ -18,7 +18,7 @@ use bitcoin::blockdata::script::{Script, ScriptBuf, Builder};
18
18
use bitcoin:: blockdata:: opcodes;
19
19
use bitcoin:: ecdsa:: Signature as EcdsaSignature ;
20
20
use bitcoin:: network:: constants:: Network ;
21
- use bitcoin:: psbt:: PartiallySignedTransaction ;
21
+ use bitcoin:: psbt:: { PartiallySignedTransaction , raw } ;
22
22
use bitcoin:: bip32:: { ExtendedPrivKey , ExtendedPubKey , ChildNumber } ;
23
23
use bitcoin:: sighash;
24
24
use bitcoin:: sighash:: EcdsaSighashType ;
@@ -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
- use crate :: ln:: chan_utils:: { HTLCOutputInCommitment , make_funding_redeemscript, ChannelPublicKeys , HolderCommitmentTransaction , ChannelTransactionParameters , CommitmentTransaction , ClosingTransaction } ;
47
- use crate :: ln:: channel_keys:: { DelayedPaymentBasepoint , DelayedPaymentKey , HtlcKey , HtlcBasepoint , RevocationKey , RevocationBasepoint } ;
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 , PaymentBasepoint } ;
48
48
use crate :: ln:: msgs:: { UnsignedChannelAnnouncement , UnsignedGossipMessage } ;
49
49
#[ cfg( taproot) ]
50
50
use crate :: ln:: msgs:: PartialSignatureWithNonce ;
@@ -104,6 +104,7 @@ pub struct DelayedPaymentOutputDescriptor {
104
104
/// The value of the channel which this output originated from, possibly indirectly.
105
105
pub channel_value_satoshis : u64 ,
106
106
}
107
+
107
108
impl DelayedPaymentOutputDescriptor {
108
109
/// The maximum length a well-formed witness spending one of these should have.
109
110
/// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
@@ -149,6 +150,7 @@ pub struct StaticPaymentOutputDescriptor {
149
150
/// Added as optional, but always `Some` if the descriptor was produced in v0.0.117 or later.
150
151
pub channel_transaction_parameters : Option < ChannelTransactionParameters > ,
151
152
}
153
+
152
154
impl StaticPaymentOutputDescriptor {
153
155
/// Returns the `witness_script` of the spendable output.
154
156
///
@@ -304,7 +306,7 @@ impl SpendableOutputDescriptor {
304
306
///
305
307
/// This is not exported to bindings users as there is no standard serialization for an input.
306
308
/// See [`Self::create_spendable_outputs_psbt`] instead.
307
- pub fn to_psbt_input ( & self ) -> 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 {
308
310
match self {
309
311
SpendableOutputDescriptor :: StaticOutput { output, .. } => {
310
312
// Is a standard P2WPKH, no need for witness script
@@ -314,16 +316,72 @@ impl SpendableOutputDescriptor {
314
316
}
315
317
} ,
316
318
SpendableOutputDescriptor :: DelayedPaymentOutput ( descriptor) => {
317
- // TODO we could add the witness script as well
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 ( ) {
324
+ let payment_key = DelayedPaymentKey :: from_basepoint (
325
+ secp_ctx,
326
+ basepoint,
327
+ & descriptor. per_commitment_point ,
328
+ ) ;
329
+ // Required to derive signing key: privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
330
+ let add_tweak = basepoint. derive_add_tweak ( & descriptor. per_commitment_point ) ;
331
+ ( Some ( get_revokeable_redeemscript (
332
+ & descriptor. revocation_pubkey ,
333
+ descriptor. to_self_delay ,
334
+ & payment_key,
335
+ ) ) , Some ( add_tweak) )
336
+ } else {
337
+ ( None , None )
338
+ } ;
339
+
318
340
bitcoin:: psbt:: Input {
319
341
witness_utxo : Some ( descriptor. output . clone ( ) ) ,
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. to_vec( ) ,
350
+ ) ] . into_iter ( ) . collect ( ) } ) . unwrap_or_default ( ) ,
320
351
..Default :: default ( )
321
352
}
322
353
} ,
323
354
SpendableOutputDescriptor :: StaticPaymentOutput ( descriptor) => {
324
- // 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 as Payment 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
+
325
382
bitcoin:: psbt:: Input {
326
383
witness_utxo : Some ( descriptor. output . clone ( ) ) ,
384
+ witness_script,
327
385
..Default :: default ( )
328
386
}
329
387
} ,
@@ -346,7 +404,8 @@ impl SpendableOutputDescriptor {
346
404
/// does not match the one we can spend.
347
405
///
348
406
/// We do not enforce that outputs meet the dust limit or that any output scripts are standard.
349
- 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 ) , ( ) > {
407
+ 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 ) , ( ) > {
408
+ let secp_ctx = Secp256k1 :: new ( ) ;
350
409
let mut input = Vec :: with_capacity ( descriptors. len ( ) ) ;
351
410
let mut input_value = 0 ;
352
411
let mut witness_weight = 0 ;
@@ -413,7 +472,7 @@ impl SpendableOutputDescriptor {
413
472
let expected_max_weight =
414
473
transaction_utils:: maybe_add_change_output ( & mut tx, input_value, witness_weight, feerate_sat_per_1000_weight, change_destination_script) ?;
415
474
416
- let psbt_inputs = descriptors. iter ( ) . map ( |d| d. to_psbt_input ( ) ) . collect :: < Vec < _ > > ( ) ;
475
+ let psbt_inputs = descriptors. iter ( ) . map ( |d| d. to_psbt_input ( & secp_ctx , channel_public_keys ) ) . collect :: < Vec < _ > > ( ) ;
417
476
let psbt = PartiallySignedTransaction {
418
477
inputs : psbt_inputs,
419
478
outputs : vec ! [ Default :: default ( ) ; tx. output. len( ) ] ,
@@ -1615,7 +1674,8 @@ impl KeysManager {
1615
1674
/// May panic if the [`SpendableOutputDescriptor`]s were not generated by channels which used
1616
1675
/// this [`KeysManager`] or one of the [`InMemorySigner`] created by this [`KeysManager`].
1617
1676
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 , ( ) > {
1618
- let ( mut psbt, expected_max_weight) = SpendableOutputDescriptor :: create_spendable_outputs_psbt ( descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime) ?;
1677
+ // TODO: provide channel keys to construct witness script
1678
+ let ( mut psbt, expected_max_weight) = SpendableOutputDescriptor :: create_spendable_outputs_psbt ( descriptors, outputs, change_destination_script, feerate_sat_per_1000_weight, locktime, None ) ?;
1619
1679
psbt = self . sign_spendable_outputs_psbt ( descriptors, psbt, secp_ctx) ?;
1620
1680
1621
1681
let spend_tx = psbt. extract_tx ( ) ;
0 commit comments