@@ -32,8 +32,8 @@ use bitcoin::secp256k1::ecdsa::RecoverableSignature;
32
32
use bitcoin:: { PackedLockTime , secp256k1, Sequence , Witness } ;
33
33
34
34
use crate :: util:: transaction_utils;
35
- use crate :: util:: crypto:: { hkdf_extract_expand_twice, sign} ;
36
- use crate :: util:: ser:: { Writeable , Writer , Readable } ;
35
+ use crate :: util:: crypto:: { hkdf_extract_expand_twice, sign, sign_with_aux_rand } ;
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
@@ -686,7 +713,7 @@ impl InMemorySigner {
686
713
let remotepubkey = self . pubkeys ( ) . payment_point ;
687
714
let witness_script = bitcoin:: Address :: p2pkh ( & :: bitcoin:: PublicKey { compressed : true , inner : remotepubkey} , Network :: Testnet ) . script_pubkey ( ) ;
688
715
let sighash = hash_to_message ! ( & sighash:: SighashCache :: new( spend_tx) . segwit_signature_hash( input_idx, & witness_script, descriptor. output. value, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
689
- let remotesig = sign ( secp_ctx, & sighash, & self . payment_key ) ;
716
+ let remotesig = sign_with_aux_rand ( secp_ctx, & sighash, & self . payment_key , & self ) ;
690
717
let payment_script = bitcoin:: Address :: p2wpkh ( & :: bitcoin:: PublicKey { compressed : true , inner : remotepubkey} , Network :: Bitcoin ) . unwrap ( ) . script_pubkey ( ) ;
691
718
692
719
if payment_script != descriptor. output . script_pubkey { return Err ( ( ) ) ; }
@@ -722,7 +749,7 @@ impl InMemorySigner {
722
749
let delayed_payment_pubkey = PublicKey :: from_secret_key ( & secp_ctx, & delayed_payment_key) ;
723
750
let witness_script = chan_utils:: get_revokeable_redeemscript ( & descriptor. revocation_pubkey , descriptor. to_self_delay , & delayed_payment_pubkey) ;
724
751
let sighash = hash_to_message ! ( & sighash:: SighashCache :: new( spend_tx) . segwit_signature_hash( input_idx, & witness_script, descriptor. output. value, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
725
- let local_delayedsig = sign ( secp_ctx, & sighash, & delayed_payment_key) ;
752
+ let local_delayedsig = sign_with_aux_rand ( secp_ctx, & sighash, & delayed_payment_key, & self ) ;
726
753
let payment_script = bitcoin:: Address :: p2wsh ( & witness_script, Network :: Bitcoin ) . script_pubkey ( ) ;
727
754
728
755
if descriptor. output . script_pubkey != payment_script { return Err ( ( ) ) ; }
@@ -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 ( ) ;
@@ -774,7 +810,7 @@ impl EcdsaChannelSigner for InMemorySigner {
774
810
let channel_funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & self . counterparty_pubkeys ( ) . funding_pubkey ) ;
775
811
776
812
let built_tx = trusted_tx. built_transaction ( ) ;
777
- let commitment_sig = built_tx. sign ( & self . funding_key , & channel_funding_redeemscript, self . channel_value_satoshis , secp_ctx) ;
813
+ let commitment_sig = built_tx. sign_counterparty_commitment ( & self . funding_key , & channel_funding_redeemscript, self . channel_value_satoshis , secp_ctx) ;
778
814
let commitment_txid = built_tx. txid ;
779
815
780
816
let mut htlc_sigs = Vec :: with_capacity ( commitment_tx. htlcs ( ) . len ( ) ) ;
@@ -799,9 +835,9 @@ impl EcdsaChannelSigner for InMemorySigner {
799
835
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
800
836
let funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & self . counterparty_pubkeys ( ) . funding_pubkey ) ;
801
837
let trusted_tx = commitment_tx. trust ( ) ;
802
- let sig = trusted_tx. built_transaction ( ) . sign ( & self . funding_key , & funding_redeemscript, self . channel_value_satoshis , secp_ctx) ;
838
+ let sig = trusted_tx. built_transaction ( ) . sign_holder_commitment ( & self . funding_key , & funding_redeemscript, self . channel_value_satoshis , & self , secp_ctx) ;
803
839
let channel_parameters = self . get_channel_parameters ( ) ;
804
- let htlc_sigs = trusted_tx. get_htlc_sigs ( & self . htlc_base_key , & channel_parameters. as_holder_broadcastable ( ) , secp_ctx) ?;
840
+ let htlc_sigs = trusted_tx. get_htlc_sigs ( & self . htlc_base_key , & channel_parameters. as_holder_broadcastable ( ) , & self , secp_ctx) ?;
805
841
Ok ( ( sig, htlc_sigs) )
806
842
}
807
843
@@ -810,9 +846,9 @@ impl EcdsaChannelSigner for InMemorySigner {
810
846
let funding_pubkey = PublicKey :: from_secret_key ( secp_ctx, & self . funding_key ) ;
811
847
let funding_redeemscript = make_funding_redeemscript ( & funding_pubkey, & self . counterparty_pubkeys ( ) . funding_pubkey ) ;
812
848
let trusted_tx = commitment_tx. trust ( ) ;
813
- let sig = trusted_tx. built_transaction ( ) . sign ( & self . funding_key , & funding_redeemscript, self . channel_value_satoshis , secp_ctx) ;
849
+ let sig = trusted_tx. built_transaction ( ) . sign_holder_commitment ( & self . funding_key , & funding_redeemscript, self . channel_value_satoshis , & self , secp_ctx) ;
814
850
let channel_parameters = self . get_channel_parameters ( ) ;
815
- let htlc_sigs = trusted_tx. get_htlc_sigs ( & self . htlc_base_key , & channel_parameters. as_holder_broadcastable ( ) , secp_ctx) ?;
851
+ let htlc_sigs = trusted_tx. get_htlc_sigs ( & self . htlc_base_key , & channel_parameters. as_holder_broadcastable ( ) , & self , secp_ctx) ?;
816
852
Ok ( ( sig, htlc_sigs) )
817
853
}
818
854
@@ -826,7 +862,7 @@ impl EcdsaChannelSigner for InMemorySigner {
826
862
} ;
827
863
let mut sighash_parts = sighash:: SighashCache :: new ( justice_tx) ;
828
864
let sighash = hash_to_message ! ( & sighash_parts. segwit_signature_hash( input, & witness_script, amount, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
829
- return Ok ( sign ( secp_ctx, & sighash, & revocation_key) )
865
+ return Ok ( sign_with_aux_rand ( secp_ctx, & sighash, & revocation_key, & self ) )
830
866
}
831
867
832
868
fn sign_justice_revoked_htlc ( & self , justice_tx : & Transaction , input : usize , amount : u64 , per_commitment_key : & SecretKey , htlc : & HTLCOutputInCommitment , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
@@ -840,7 +876,7 @@ impl EcdsaChannelSigner for InMemorySigner {
840
876
} ;
841
877
let mut sighash_parts = sighash:: SighashCache :: new ( justice_tx) ;
842
878
let sighash = hash_to_message ! ( & sighash_parts. segwit_signature_hash( input, & witness_script, amount, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
843
- return Ok ( sign ( secp_ctx, & sighash, & revocation_key) )
879
+ return Ok ( sign_with_aux_rand ( secp_ctx, & sighash, & revocation_key, & self ) )
844
880
}
845
881
846
882
#[ cfg( anchors) ]
@@ -858,7 +894,7 @@ impl EcdsaChannelSigner for InMemorySigner {
858
894
let our_htlc_private_key = chan_utils:: derive_private_key (
859
895
& secp_ctx, & per_commitment_point, & self . htlc_base_key
860
896
) ;
861
- Ok ( sign ( & secp_ctx, & hash_to_message ! ( sighash) , & our_htlc_private_key) )
897
+ Ok ( sign_with_aux_rand ( & secp_ctx, & hash_to_message ! ( sighash) , & our_htlc_private_key, & self ) )
862
898
}
863
899
864
900
fn sign_counterparty_htlc_transaction ( & self , htlc_tx : & Transaction , input : usize , amount : u64 , per_commitment_point : & PublicKey , htlc : & HTLCOutputInCommitment , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
@@ -869,7 +905,7 @@ impl EcdsaChannelSigner for InMemorySigner {
869
905
let witness_script = chan_utils:: get_htlc_redeemscript_with_explicit_keys ( & htlc, self . opt_anchors ( ) , & counterparty_htlcpubkey, & htlcpubkey, & revocation_pubkey) ;
870
906
let mut sighash_parts = sighash:: SighashCache :: new ( htlc_tx) ;
871
907
let sighash = hash_to_message ! ( & sighash_parts. segwit_signature_hash( input, & witness_script, amount, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
872
- Ok ( sign ( secp_ctx, & sighash, & htlc_key) )
908
+ Ok ( sign_with_aux_rand ( secp_ctx, & sighash, & htlc_key, & self ) )
873
909
}
874
910
875
911
fn sign_closing_transaction ( & self , closing_tx : & ClosingTransaction , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
@@ -885,14 +921,14 @@ impl EcdsaChannelSigner for InMemorySigner {
885
921
let sighash = sighash:: SighashCache :: new ( & * anchor_tx) . segwit_signature_hash (
886
922
input, & witness_script, ANCHOR_OUTPUT_VALUE_SATOSHI , EcdsaSighashType :: All ,
887
923
) . unwrap ( ) ;
888
- Ok ( sign ( secp_ctx, & hash_to_message ! ( & sighash[ ..] ) , & self . funding_key ) )
924
+ Ok ( sign_with_aux_rand ( secp_ctx, & hash_to_message ! ( & sighash[ ..] ) , & self . funding_key , & self ) )
889
925
}
890
926
891
927
fn sign_channel_announcement_with_funding_key (
892
928
& self , msg : & UnsignedChannelAnnouncement , secp_ctx : & Secp256k1 < secp256k1:: All >
893
929
) -> Result < Signature , ( ) > {
894
930
let msghash = hash_to_message ! ( & Sha256dHash :: hash( & msg. encode( ) [ ..] ) [ ..] ) ;
895
- Ok ( sign ( secp_ctx, & msghash, & self . funding_key ) )
931
+ Ok ( secp_ctx. sign_ecdsa ( & msghash, & self . funding_key ) )
896
932
}
897
933
}
898
934
@@ -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
@@ -1233,7 +1273,7 @@ impl KeysManager {
1233
1273
if payment_script != output. script_pubkey { return Err ( ( ) ) ; } ;
1234
1274
1235
1275
let sighash = hash_to_message ! ( & sighash:: SighashCache :: new( & spend_tx) . segwit_signature_hash( input_idx, & witness_script, output. value, EcdsaSighashType :: All ) . unwrap( ) [ ..] ) ;
1236
- let sig = sign ( secp_ctx, & sighash, & secret. private_key ) ;
1276
+ let sig = sign_with_aux_rand ( secp_ctx, & sighash, & secret. private_key , & self ) ;
1237
1277
let mut sig_ser = sig. serialize_der ( ) . to_vec ( ) ;
1238
1278
sig_ser. push ( EcdsaSighashType :: All as u8 ) ;
1239
1279
spend_tx. input [ input_idx] . witness . push ( sig_ser) ;
@@ -1295,7 +1335,7 @@ impl NodeSigner for KeysManager {
1295
1335
1296
1336
fn sign_gossip_message ( & self , msg : UnsignedGossipMessage ) -> Result < Signature , ( ) > {
1297
1337
let msg_hash = hash_to_message ! ( & Sha256dHash :: hash( & msg. encode( ) [ ..] ) [ ..] ) ;
1298
- Ok ( sign ( & self . secp_ctx , & msg_hash, & self . node_secret ) )
1338
+ Ok ( self . secp_ctx . sign_ecdsa ( & msg_hash, & self . node_secret ) )
1299
1339
}
1300
1340
}
1301
1341
@@ -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