@@ -29,6 +29,7 @@ use bitcoin::network::constants::Network;
2929
3030use bitcoin:: hashes:: Hash as TraitImport ;
3131use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
32+ use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
3233use bitcoin:: hash_types:: { BlockHash , WPubkeyHash } ;
3334
3435use lightning:: chain;
@@ -54,10 +55,9 @@ use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters
5455use crate :: utils:: test_logger:: { self , Output } ;
5556use crate :: utils:: test_persister:: TestPersister ;
5657
57- use bitcoin:: secp256k1:: { PublicKey , SecretKey , Scalar } ;
58+ use bitcoin:: secp256k1:: { Message , PublicKey , SecretKey , Scalar , Secp256k1 } ;
5859use bitcoin:: secp256k1:: ecdh:: SharedSecret ;
59- use bitcoin:: secp256k1:: ecdsa:: RecoverableSignature ;
60- use bitcoin:: secp256k1:: Secp256k1 ;
60+ use bitcoin:: secp256k1:: ecdsa:: { RecoverableSignature , Signature } ;
6161
6262use std:: mem;
6363use std:: cmp:: { self , Ordering } ;
@@ -174,45 +174,53 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
174174}
175175
176176struct KeyProvider {
177- node_id : u8 ,
177+ node_secret : SecretKey ,
178178 rand_bytes_id : atomic:: AtomicU32 ,
179179 enforcement_states : Mutex < HashMap < [ u8 ; 32 ] , Arc < Mutex < EnforcementState > > > > ,
180180}
181181
182182impl EntropySource for KeyProvider {
183183 fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
184184 let id = self . rand_bytes_id . fetch_add ( 1 , atomic:: Ordering :: Relaxed ) ;
185- let mut res = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 11 , self . node_id ] ;
185+ let mut res = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 11 , self . node_secret [ 31 ] ] ;
186186 res[ 30 -4 ..30 ] . copy_from_slice ( & id. to_le_bytes ( ) ) ;
187187 res
188188 }
189189}
190190
191191impl NodeSigner for KeyProvider {
192- fn get_node_secret ( & self , _recipient : Recipient ) -> Result < SecretKey , ( ) > {
193- Ok ( SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , self . node_id ] ) . unwrap ( ) )
194- }
195-
196192 fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
197- let secp_ctx = Secp256k1 :: signing_only ( ) ;
198- Ok ( PublicKey :: from_secret_key ( & secp_ctx, & self . get_node_secret ( recipient) ?) )
193+ let node_secret = match recipient {
194+ Recipient :: Node => Ok ( & self . node_secret ) ,
195+ Recipient :: PhantomNode => Err ( ( ) )
196+ } ?;
197+ Ok ( PublicKey :: from_secret_key ( & Secp256k1 :: signing_only ( ) , node_secret) )
199198 }
200199
201200 fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
202- let mut node_secret = self . get_node_secret ( recipient) ?;
201+ let mut node_secret = match recipient {
202+ Recipient :: Node => Ok ( self . node_secret . clone ( ) ) ,
203+ Recipient :: PhantomNode => Err ( ( ) )
204+ } ?;
203205 if let Some ( tweak) = tweak {
204- node_secret = node_secret. mul_tweak ( tweak) . unwrap ( ) ;
206+ node_secret = node_secret. mul_tweak ( tweak) . map_err ( |_| ( ) ) ? ;
205207 }
206208 Ok ( SharedSecret :: new ( other_key, & node_secret) )
207209 }
208210
209211 fn get_inbound_payment_key_material ( & self ) -> KeyMaterial {
210- KeyMaterial ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , self . node_id ] )
212+ KeyMaterial ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , self . node_secret [ 31 ] ] )
211213 }
212214
213215 fn sign_invoice ( & self , _hrp_bytes : & [ u8 ] , _invoice_data : & [ u5 ] , _recipient : Recipient ) -> Result < RecoverableSignature , ( ) > {
214216 unreachable ! ( )
215217 }
218+
219+ fn sign_gossip_message ( & self , msg : lightning:: ln:: msgs:: UnsignedGossipMessage ) -> Result < Signature , ( ) > {
220+ let msg_hash = Message :: from_slice ( & Sha256dHash :: hash ( & msg. encode ( ) [ ..] ) [ ..] ) . map_err ( |_| ( ) ) ?;
221+ let secp_ctx = Secp256k1 :: signing_only ( ) ;
222+ Ok ( secp_ctx. sign_ecdsa ( & msg_hash, & self . node_secret ) )
223+ }
216224}
217225
218226impl SignerProvider for KeyProvider {
@@ -228,13 +236,12 @@ impl SignerProvider for KeyProvider {
228236 let id = channel_keys_id[ 0 ] ;
229237 let keys = InMemorySigner :: new (
230238 & secp_ctx,
231- self . get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
232- SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , self . node_id ] ) . unwrap ( ) ,
233- SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , self . node_id ] ) . unwrap ( ) ,
234- SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , self . node_id ] ) . unwrap ( ) ,
235- SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , self . node_id ] ) . unwrap ( ) ,
236- SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 8 , self . node_id ] ) . unwrap ( ) ,
237- [ id, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , self . node_id ] ,
239+ SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
240+ SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
241+ SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
242+ SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
243+ SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 8 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
244+ [ id, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , self . node_secret [ 31 ] ] ,
238245 channel_value_satoshis,
239246 channel_keys_id,
240247 ) ;
@@ -245,7 +252,7 @@ impl SignerProvider for KeyProvider {
245252 fn read_chan_signer ( & self , buffer : & [ u8 ] ) -> Result < Self :: Signer , DecodeError > {
246253 let mut reader = std:: io:: Cursor :: new ( buffer) ;
247254
248- let inner: InMemorySigner = ReadableArgs :: read ( & mut reader, self . get_node_secret ( Recipient :: Node ) . unwrap ( ) ) ?;
255+ let inner: InMemorySigner = Readable :: read ( & mut reader) ?;
249256 let state = self . make_enforcement_state_cell ( inner. commitment_seed ) ;
250257
251258 Ok ( EnforcingSigner {
@@ -257,14 +264,14 @@ impl SignerProvider for KeyProvider {
257264
258265 fn get_destination_script ( & self ) -> Script {
259266 let secp_ctx = Secp256k1 :: signing_only ( ) ;
260- let channel_monitor_claim_key = SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , self . node_id ] ) . unwrap ( ) ;
267+ let channel_monitor_claim_key = SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , self . node_secret [ 31 ] ] ) . unwrap ( ) ;
261268 let our_channel_monitor_claim_key_hash = WPubkeyHash :: hash ( & PublicKey :: from_secret_key ( & secp_ctx, & channel_monitor_claim_key) . serialize ( ) ) ;
262269 Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & our_channel_monitor_claim_key_hash[ ..] ) . into_script ( )
263270 }
264271
265272 fn get_shutdown_scriptpubkey ( & self ) -> ShutdownScript {
266273 let secp_ctx = Secp256k1 :: signing_only ( ) ;
267- let secret_key = SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , self . node_id ] ) . unwrap ( ) ;
274+ let secret_key = SecretKey :: from_slice ( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , self . node_secret [ 31 ] ] ) . unwrap ( ) ;
268275 let pubkey_hash = WPubkeyHash :: hash ( & PublicKey :: from_secret_key ( & secp_ctx, & secret_key) . serialize ( ) ) ;
269276 ShutdownScript :: new_p2wpkh ( & pubkey_hash)
270277 }
@@ -402,7 +409,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
402409 macro_rules! make_node {
403410 ( $node_id: expr, $fee_estimator: expr) => { {
404411 let logger: Arc <dyn Logger > = Arc :: new( test_logger:: TestLogger :: new( $node_id. to_string( ) , out. clone( ) ) ) ;
405- let keys_manager = Arc :: new( KeyProvider { node_id: $node_id, rand_bytes_id: atomic:: AtomicU32 :: new( 0 ) , enforcement_states: Mutex :: new( HashMap :: new( ) ) } ) ;
412+ let node_secret = SecretKey :: from_slice( & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , $node_id] ) . unwrap( ) ;
413+ let keys_manager = Arc :: new( KeyProvider { node_secret, rand_bytes_id: atomic:: AtomicU32 :: new( 0 ) , enforcement_states: Mutex :: new( HashMap :: new( ) ) } ) ;
406414 let monitor = Arc :: new( TestChainMonitor :: new( broadcast. clone( ) , logger. clone( ) , $fee_estimator. clone( ) ,
407415 Arc :: new( TestPersister {
408416 update_ret: Mutex :: new( ChannelMonitorUpdateStatus :: Completed )
0 commit comments