@@ -33,7 +33,7 @@ use bitcoin::{PackedLockTime, secp256k1, Sequence, Witness};
33
33
34
34
use crate :: util:: transaction_utils;
35
35
use crate :: util:: crypto:: { hkdf_extract_expand_twice, sign} ;
36
- use crate :: util:: ser:: { Writeable , Writer , Readable } ;
36
+ use crate :: util:: ser:: { Writeable , Writer , Readable , ReadableArgs } ;
37
37
use crate :: chain:: transaction:: OutPoint ;
38
38
#[ cfg( anchors) ]
39
39
use crate :: events:: bump_transaction:: HTLCDescriptor ;
@@ -45,6 +45,7 @@ use crate::ln::script::ShutdownScript;
45
45
46
46
use crate :: prelude:: * ;
47
47
use core:: convert:: TryInto ;
48
+ use core:: ops:: Deref ;
48
49
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
49
50
use crate :: io:: { self , Error } ;
50
51
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -553,7 +554,6 @@ pub trait SignerProvider {
553
554
fn get_shutdown_scriptpubkey ( & self ) -> ShutdownScript ;
554
555
}
555
556
556
- #[ derive( Clone ) ]
557
557
/// A simple implementation of [`WriteableEcdsaChannelSigner`] that just keeps the private keys in memory.
558
558
///
559
559
/// This implementation performs no policy checks and is insufficient by itself as
@@ -580,6 +580,30 @@ pub struct InMemorySigner {
580
580
channel_value_satoshis : u64 ,
581
581
/// Key derivation parameters.
582
582
channel_keys_id : [ u8 ; 32 ] ,
583
+ /// Seed from which all randomness produced is derived from.
584
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
585
+ /// Tracks the number of times we've produced randomness to ensure we don't return the same
586
+ /// bytes twice.
587
+ rand_bytes_index : AtomicCounter ,
588
+ }
589
+
590
+ impl Clone for InMemorySigner {
591
+ fn clone ( & self ) -> Self {
592
+ Self {
593
+ funding_key : self . funding_key . clone ( ) ,
594
+ revocation_base_key : self . revocation_base_key . clone ( ) ,
595
+ payment_key : self . payment_key . clone ( ) ,
596
+ delayed_payment_base_key : self . delayed_payment_base_key . clone ( ) ,
597
+ htlc_base_key : self . htlc_base_key . clone ( ) ,
598
+ commitment_seed : self . commitment_seed . clone ( ) ,
599
+ holder_channel_pubkeys : self . holder_channel_pubkeys . clone ( ) ,
600
+ channel_parameters : self . channel_parameters . clone ( ) ,
601
+ channel_value_satoshis : self . channel_value_satoshis ,
602
+ channel_keys_id : self . channel_keys_id ,
603
+ rand_bytes_unique_start : self . get_secure_random_bytes ( ) ,
604
+ rand_bytes_index : AtomicCounter :: new ( ) ,
605
+ }
606
+ }
583
607
}
584
608
585
609
impl InMemorySigner {
@@ -594,6 +618,7 @@ impl InMemorySigner {
594
618
commitment_seed : [ u8 ; 32 ] ,
595
619
channel_value_satoshis : u64 ,
596
620
channel_keys_id : [ u8 ; 32 ] ,
621
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
597
622
) -> InMemorySigner {
598
623
let holder_channel_pubkeys =
599
624
InMemorySigner :: make_holder_keys ( secp_ctx, & funding_key, & revocation_base_key,
@@ -610,6 +635,8 @@ impl InMemorySigner {
610
635
holder_channel_pubkeys,
611
636
channel_parameters : None ,
612
637
channel_keys_id,
638
+ rand_bytes_unique_start,
639
+ rand_bytes_index : AtomicCounter :: new ( ) ,
613
640
}
614
641
}
615
642
@@ -736,6 +763,15 @@ impl InMemorySigner {
736
763
}
737
764
}
738
765
766
+ impl EntropySource for InMemorySigner {
767
+ fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
768
+ let index = self . rand_bytes_index . get_increment ( ) ;
769
+ let mut nonce = [ 0u8 ; 16 ] ;
770
+ nonce[ ..8 ] . copy_from_slice ( & index. to_be_bytes ( ) ) ;
771
+ ChaCha20 :: get_single_block ( & self . rand_bytes_unique_start , & nonce)
772
+ }
773
+ }
774
+
739
775
impl ChannelSigner for InMemorySigner {
740
776
fn get_per_commitment_point ( & self , idx : u64 , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> PublicKey {
741
777
let commitment_secret = SecretKey :: from_slice ( & chan_utils:: build_commitment_secret ( & self . commitment_seed , idx) ) . unwrap ( ) ;
@@ -922,8 +958,8 @@ impl Writeable for InMemorySigner {
922
958
}
923
959
}
924
960
925
- impl Readable for InMemorySigner {
926
- fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
961
+ impl < ES : Deref > ReadableArgs < ES > for InMemorySigner where ES :: Target : EntropySource {
962
+ fn read < R : io:: Read > ( reader : & mut R , entropy_source : ES ) -> Result < Self , DecodeError > {
927
963
let _ver = read_ver_prefix ! ( reader, SERIALIZATION_VERSION ) ;
928
964
929
965
let funding_key = Readable :: read ( reader) ?;
@@ -953,6 +989,8 @@ impl Readable for InMemorySigner {
953
989
holder_channel_pubkeys,
954
990
channel_parameters : counterparty_channel_data,
955
991
channel_keys_id : keys_id,
992
+ rand_bytes_unique_start : entropy_source. get_secure_random_bytes ( ) ,
993
+ rand_bytes_index : AtomicCounter :: new ( ) ,
956
994
} )
957
995
}
958
996
}
@@ -1107,6 +1145,7 @@ impl KeysManager {
1107
1145
let payment_key = key_step ! ( b"payment key" , revocation_base_key) ;
1108
1146
let delayed_payment_base_key = key_step ! ( b"delayed payment base key" , payment_key) ;
1109
1147
let htlc_base_key = key_step ! ( b"HTLC base key" , delayed_payment_base_key) ;
1148
+ let prng_seed = self . get_secure_random_bytes ( ) ;
1110
1149
1111
1150
InMemorySigner :: new (
1112
1151
& self . secp_ctx ,
@@ -1118,6 +1157,7 @@ impl KeysManager {
1118
1157
commitment_seed,
1119
1158
channel_value_satoshis,
1120
1159
params. clone ( ) ,
1160
+ prng_seed,
1121
1161
)
1122
1162
}
1123
1163
@@ -1323,7 +1363,7 @@ impl SignerProvider for KeysManager {
1323
1363
}
1324
1364
1325
1365
fn read_chan_signer ( & self , reader : & [ u8 ] ) -> Result < Self :: Signer , DecodeError > {
1326
- InMemorySigner :: read ( & mut io:: Cursor :: new ( reader) )
1366
+ InMemorySigner :: read ( & mut io:: Cursor :: new ( reader) , self )
1327
1367
}
1328
1368
1329
1369
fn get_destination_script ( & self ) -> Script {
0 commit comments