@@ -69,7 +69,7 @@ pub enum SpendableOutputDescriptor {
69
69
///
70
70
/// To derive the remote_revocation_pubkey provided here (which is used in the witness
71
71
/// script generation), you must pass the remote revocation_basepoint (which appears in the
72
- /// call to ChannelKeys::set_remote_channel_pubkeys ) and the provided per_commitment point
72
+ /// call to ChannelKeys::on_accept ) and the provided per_commitment point
73
73
/// to chan_utils::derive_public_revocation_key.
74
74
///
75
75
/// The witness script which is hashed and included in the output script_pubkey may be
@@ -223,7 +223,7 @@ pub trait ChannelKeys : Send+Clone {
223
223
// TODO: Document the things someone using this interface should enforce before signing.
224
224
// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
225
225
// making the callee generate it via some util function we expose)!
226
- fn sign_remote_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , feerate_per_kw : u32 , commitment_tx : & Transaction , keys : & TxCreationKeys , htlcs : & [ & HTLCOutputInCommitment ] , to_self_delay : u16 , secp_ctx : & Secp256k1 < T > ) -> Result < ( Signature , Vec < Signature > ) , ( ) > ;
226
+ fn sign_remote_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , feerate_per_kw : u32 , commitment_tx : & Transaction , keys : & TxCreationKeys , htlcs : & [ & HTLCOutputInCommitment ] , secp_ctx : & Secp256k1 < T > ) -> Result < ( Signature , Vec < Signature > ) , ( ) > ;
227
227
228
228
/// Create a signature for a local commitment transaction. This will only ever be called with
229
229
/// the same local_commitment_tx (or a copy thereof), though there are currently no guarantees
@@ -254,7 +254,7 @@ pub trait ChannelKeys : Send+Clone {
254
254
/// (implying they were considered dust at the time the commitment transaction was negotiated),
255
255
/// a corresponding None should be included in the return value. All other positions in the
256
256
/// return value must contain a signature.
257
- fn sign_local_commitment_htlc_transactions < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , local_csv : u16 , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > ;
257
+ fn sign_local_commitment_htlc_transactions < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > ;
258
258
259
259
/// Create a signature for the given input in a transaction spending an HTLC or commitment
260
260
/// transaction output when our counterparty broadcasts an old state.
@@ -313,11 +313,13 @@ pub trait ChannelKeys : Send+Clone {
313
313
/// protocol.
314
314
fn sign_channel_announcement < T : secp256k1:: Signing > ( & self , msg : & msgs:: UnsignedChannelAnnouncement , secp_ctx : & Secp256k1 < T > ) -> Result < Signature , ( ) > ;
315
315
316
- /// Set the remote channel basepoints. This is done immediately on incoming channels
317
- /// and as soon as the channel is accepted on outgoing channels.
316
+ /// Set the remote channel basepoints and remote/local to_self_delay.
317
+ /// This is done immediately on incoming channels and as soon as the channel is accepted on outgoing channels.
318
+ ///
319
+ /// We bind local_to_safe_delay late here for API convenience.
318
320
///
319
321
/// Will be called before any signatures are applied.
320
- fn set_remote_channel_pubkeys ( & mut self , channel_points : & ChannelPublicKeys ) ;
322
+ fn on_accept ( & mut self , channel_points : & ChannelPublicKeys , remote_to_self_delay : u16 , local_to_self_delay : u16 ) ;
321
323
}
322
324
323
325
/// A trait to describe an object which can get user secrets and key material.
@@ -342,6 +344,18 @@ pub trait KeysInterface: Send + Sync {
342
344
fn get_channel_id ( & self ) -> [ u8 ; 32 ] ;
343
345
}
344
346
347
+ #[ derive( Clone ) ]
348
+ /// Holds late-bound channel data
349
+ /// This data is available after the remote accepted the channel.
350
+ pub struct AcceptedChannelData {
351
+ /// Remote public keys and base points
352
+ pub ( crate ) remote_channel_pubkeys : ChannelPublicKeys ,
353
+ /// Remote to_self_delay
354
+ pub ( crate ) remote_to_self_delay : u16 ,
355
+ /// Local to_self_delay
356
+ pub ( crate ) local_to_self_delay : u16 ,
357
+ }
358
+
345
359
#[ derive( Clone ) ]
346
360
/// A simple implementation of ChannelKeys that just keeps the private keys in memory.
347
361
pub struct InMemoryChannelKeys {
@@ -359,8 +373,8 @@ pub struct InMemoryChannelKeys {
359
373
pub commitment_seed : [ u8 ; 32 ] ,
360
374
/// Local public keys and basepoints
361
375
pub ( crate ) local_channel_pubkeys : ChannelPublicKeys ,
362
- /// Remote public keys and base points
363
- pub ( crate ) remote_channel_pubkeys : Option < ChannelPublicKeys > ,
376
+ /// Remote public keys and remote/local to_self_delay, populated on channel acceptance
377
+ pub ( crate ) accepted_channel_data : Option < AcceptedChannelData > ,
364
378
/// The total value of this channel
365
379
channel_value_satoshis : u64 ,
366
380
/// Key derivation parameters
@@ -392,7 +406,7 @@ impl InMemoryChannelKeys {
392
406
commitment_seed,
393
407
channel_value_satoshis,
394
408
local_channel_pubkeys,
395
- remote_channel_pubkeys : None ,
409
+ accepted_channel_data : None ,
396
410
key_derivation_params,
397
411
}
398
412
}
@@ -413,7 +427,17 @@ impl InMemoryChannelKeys {
413
427
}
414
428
}
415
429
416
- fn remote_pubkeys < ' a > ( & ' a self ) -> & ' a ChannelPublicKeys { self . remote_channel_pubkeys . as_ref ( ) . unwrap ( ) }
430
+ /// Remote pubkeys
431
+ /// Will panic if the remote pubkeys were not set with on_accept
432
+ pub fn remote_pubkeys ( & self ) -> & ChannelPublicKeys { & self . accepted_channel_data . as_ref ( ) . unwrap ( ) . remote_channel_pubkeys }
433
+
434
+ /// Remote to-self delay, constraining local commitment txs
435
+ /// Will panic if the remote pubkeys were not set with on_accept
436
+ pub fn remote_to_self_delay ( & self ) -> u16 { self . accepted_channel_data . as_ref ( ) . unwrap ( ) . remote_to_self_delay }
437
+
438
+ /// Local to-self delay, constraining remote commitment txs
439
+ /// Will panic if the remote pubkeys were not set with on_accept
440
+ pub fn local_to_self_delay ( & self ) -> u16 { self . accepted_channel_data . as_ref ( ) . unwrap ( ) . local_to_self_delay }
417
441
}
418
442
419
443
impl ChannelKeys for InMemoryChannelKeys {
@@ -429,12 +453,12 @@ impl ChannelKeys for InMemoryChannelKeys {
429
453
fn pubkeys ( & self ) -> & ChannelPublicKeys { & self . local_channel_pubkeys }
430
454
fn key_derivation_params ( & self ) -> ( u64 , u64 ) { self . key_derivation_params }
431
455
432
- fn sign_remote_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , feerate_per_kw : u32 , commitment_tx : & Transaction , keys : & TxCreationKeys , htlcs : & [ & HTLCOutputInCommitment ] , to_self_delay : u16 , secp_ctx : & Secp256k1 < T > ) -> Result < ( Signature , Vec < Signature > ) , ( ) > {
456
+ fn sign_remote_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , feerate_per_kw : u32 , commitment_tx : & Transaction , keys : & TxCreationKeys , htlcs : & [ & HTLCOutputInCommitment ] , secp_ctx : & Secp256k1 < T > ) -> Result < ( Signature , Vec < Signature > ) , ( ) > {
433
457
if commitment_tx. input . len ( ) != 1 { return Err ( ( ) ) ; }
434
458
435
459
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
436
- let remote_channel_pubkeys = self . remote_channel_pubkeys . as_ref ( ) . expect ( "must set remote channel pubkeys before signing" ) ;
437
- let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_pubkeys. funding_pubkey ) ;
460
+ let accepted_data = self . accepted_channel_data . as_ref ( ) . expect ( "must accept before signing" ) ;
461
+ let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & accepted_data . remote_channel_pubkeys . funding_pubkey ) ;
438
462
439
463
let commitment_sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & commitment_tx) . sighash_all( & commitment_tx. input[ 0 ] , & channel_funding_redeemscript, self . channel_value_satoshis) [ ..] ) ;
440
464
let commitment_sig = secp_ctx. sign ( & commitment_sighash, & self . funding_key ) ;
@@ -444,7 +468,7 @@ impl ChannelKeys for InMemoryChannelKeys {
444
468
let mut htlc_sigs = Vec :: with_capacity ( htlcs. len ( ) ) ;
445
469
for ref htlc in htlcs {
446
470
if let Some ( _) = htlc. transaction_output_index {
447
- let htlc_tx = chan_utils:: build_htlc_transaction ( & commitment_txid, feerate_per_kw, to_self_delay , htlc, & keys. a_delayed_payment_key , & keys. revocation_key ) ;
471
+ let htlc_tx = chan_utils:: build_htlc_transaction ( & commitment_txid, feerate_per_kw, accepted_data . local_to_self_delay , htlc, & keys. a_delayed_payment_key , & keys. revocation_key ) ;
448
472
let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & keys) ;
449
473
let htlc_sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & htlc_tx) . sighash_all( & htlc_tx. input[ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) ;
450
474
let our_htlc_key = match chan_utils:: derive_private_key ( & secp_ctx, & keys. per_commitment_point , & self . htlc_base_key ) {
@@ -460,22 +484,23 @@ impl ChannelKeys for InMemoryChannelKeys {
460
484
461
485
fn sign_local_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , secp_ctx : & Secp256k1 < T > ) -> Result < Signature , ( ) > {
462
486
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
463
- let remote_channel_pubkeys = self . remote_channel_pubkeys . as_ref ( ) . expect ( "must set remote channel pubkeys before signing" ) ;
464
- let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_pubkeys. funding_pubkey ) ;
487
+ let remote_channel_data = self . accepted_channel_data . as_ref ( ) . expect ( "must accept before signing" ) ;
488
+ let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_data . remote_channel_pubkeys . funding_pubkey ) ;
465
489
466
490
Ok ( local_commitment_tx. get_local_sig ( & self . funding_key , & channel_funding_redeemscript, self . channel_value_satoshis , secp_ctx) )
467
491
}
468
492
469
493
#[ cfg( test) ]
470
494
fn unsafe_sign_local_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , secp_ctx : & Secp256k1 < T > ) -> Result < Signature , ( ) > {
471
495
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
472
- let remote_channel_pubkeys = self . remote_channel_pubkeys . as_ref ( ) . expect ( "must set remote channel pubkeys before signing" ) ;
496
+ let remote_channel_pubkeys = & self . accepted_channel_data . as_ref ( ) . expect ( "must accept before signing" ) . remote_channel_pubkeys ;
473
497
let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_pubkeys. funding_pubkey ) ;
474
498
475
499
Ok ( local_commitment_tx. get_local_sig ( & self . funding_key , & channel_funding_redeemscript, self . channel_value_satoshis , secp_ctx) )
476
500
}
477
501
478
- fn sign_local_commitment_htlc_transactions < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , local_csv : u16 , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > {
502
+ fn sign_local_commitment_htlc_transactions < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & LocalCommitmentTransaction , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > {
503
+ let local_csv = self . accepted_channel_data . as_ref ( ) . unwrap ( ) . remote_to_self_delay ;
479
504
local_commitment_tx. get_htlc_sigs ( & self . htlc_base_key , local_csv, secp_ctx)
480
505
}
481
506
@@ -532,9 +557,9 @@ impl ChannelKeys for InMemoryChannelKeys {
532
557
if closing_tx. input [ 0 ] . witness . len ( ) != 0 { return Err ( ( ) ) ; }
533
558
if closing_tx. output . len ( ) > 2 { return Err ( ( ) ) ; }
534
559
535
- let remote_channel_pubkeys = self . remote_channel_pubkeys . as_ref ( ) . expect ( "must set remote channel pubkeys before signing" ) ;
536
560
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
537
- let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_pubkeys. funding_pubkey ) ;
561
+ let remote_channel_data = self . accepted_channel_data . as_ref ( ) . expect ( "must accept before signing" ) ;
562
+ let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & remote_channel_data. remote_channel_pubkeys . funding_pubkey ) ;
538
563
539
564
let sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( closing_tx)
540
565
. sighash_all( & closing_tx. input[ 0 ] , & channel_funding_redeemscript, self . channel_value_satoshis) [ ..] ) ;
@@ -546,12 +571,19 @@ impl ChannelKeys for InMemoryChannelKeys {
546
571
Ok ( secp_ctx. sign ( & msghash, & self . funding_key ) )
547
572
}
548
573
549
- fn set_remote_channel_pubkeys ( & mut self , channel_pubkeys : & ChannelPublicKeys ) {
550
- assert ! ( self . remote_channel_pubkeys. is_none( ) , "Already set remote channel pubkeys" ) ;
551
- self . remote_channel_pubkeys = Some ( channel_pubkeys. clone ( ) ) ;
574
+ fn on_accept ( & mut self , channel_pubkeys : & ChannelPublicKeys , remote_to_self_delay : u16 , local_to_self_delay : u16 ) {
575
+ assert ! ( self . accepted_channel_data. is_none( ) , "Already accepted" ) ;
576
+ self . accepted_channel_data = Some ( AcceptedChannelData {
577
+ remote_channel_pubkeys : channel_pubkeys. clone ( ) ,
578
+ remote_to_self_delay,
579
+ local_to_self_delay,
580
+ } ) ;
552
581
}
553
582
}
554
583
584
+ impl_writeable ! ( AcceptedChannelData , 0 ,
585
+ { remote_channel_pubkeys, remote_to_self_delay, local_to_self_delay } ) ;
586
+
555
587
impl Writeable for InMemoryChannelKeys {
556
588
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , Error > {
557
589
self . funding_key . write ( writer) ?;
@@ -560,7 +592,7 @@ impl Writeable for InMemoryChannelKeys {
560
592
self . delayed_payment_base_key . write ( writer) ?;
561
593
self . htlc_base_key . write ( writer) ?;
562
594
self . commitment_seed . write ( writer) ?;
563
- self . remote_channel_pubkeys . write ( writer) ?;
595
+ self . accepted_channel_data . write ( writer) ?;
564
596
self . channel_value_satoshis . write ( writer) ?;
565
597
self . key_derivation_params . 0 . write ( writer) ?;
566
598
self . key_derivation_params . 1 . write ( writer) ?;
@@ -577,7 +609,7 @@ impl Readable for InMemoryChannelKeys {
577
609
let delayed_payment_base_key = Readable :: read ( reader) ?;
578
610
let htlc_base_key = Readable :: read ( reader) ?;
579
611
let commitment_seed = Readable :: read ( reader) ?;
580
- let remote_channel_pubkeys = Readable :: read ( reader) ?;
612
+ let remote_channel_data = Readable :: read ( reader) ?;
581
613
let channel_value_satoshis = Readable :: read ( reader) ?;
582
614
let secp_ctx = Secp256k1 :: signing_only ( ) ;
583
615
let local_channel_pubkeys =
@@ -596,7 +628,7 @@ impl Readable for InMemoryChannelKeys {
596
628
commitment_seed,
597
629
channel_value_satoshis,
598
630
local_channel_pubkeys,
599
- remote_channel_pubkeys ,
631
+ accepted_channel_data : remote_channel_data ,
600
632
key_derivation_params : ( params_1, params_2) ,
601
633
} )
602
634
}
0 commit comments