@@ -29,6 +29,7 @@ use bitcoin::network::constants::Network;
29
29
30
30
use bitcoin:: hashes:: Hash as TraitImport ;
31
31
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
32
+ use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
32
33
use bitcoin:: hash_types:: { BlockHash , WPubkeyHash } ;
33
34
34
35
use lightning:: chain;
@@ -54,10 +55,9 @@ use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters
54
55
use crate :: utils:: test_logger:: { self , Output } ;
55
56
use crate :: utils:: test_persister:: TestPersister ;
56
57
57
- use bitcoin:: secp256k1:: { PublicKey , SecretKey , Scalar } ;
58
+ use bitcoin:: secp256k1:: { Message , PublicKey , SecretKey , Scalar , Secp256k1 } ;
58
59
use bitcoin:: secp256k1:: ecdh:: SharedSecret ;
59
- use bitcoin:: secp256k1:: ecdsa:: RecoverableSignature ;
60
- use bitcoin:: secp256k1:: Secp256k1 ;
60
+ use bitcoin:: secp256k1:: ecdsa:: { RecoverableSignature , Signature } ;
61
61
62
62
use std:: mem;
63
63
use std:: cmp:: { self , Ordering } ;
@@ -174,23 +174,23 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
174
174
}
175
175
176
176
struct KeyProvider {
177
- node_id : u8 ,
177
+ node_secret : SecretKey ,
178
178
rand_bytes_id : atomic:: AtomicU32 ,
179
179
enforcement_states : Mutex < HashMap < [ u8 ; 32 ] , Arc < Mutex < EnforcementState > > > > ,
180
180
}
181
181
182
182
impl EntropySource for KeyProvider {
183
183
fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
184
184
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 ] ] ;
186
186
res[ 30 -4 ..30 ] . copy_from_slice ( & id. to_le_bytes ( ) ) ;
187
187
res
188
188
}
189
189
}
190
190
191
191
impl NodeSigner for KeyProvider {
192
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 ( ) )
193
+ Ok ( self . node_secret . clone ( ) )
194
194
}
195
195
196
196
fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
@@ -207,12 +207,18 @@ impl NodeSigner for KeyProvider {
207
207
}
208
208
209
209
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 ] )
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_secret [ 31 ] ] )
211
211
}
212
212
213
213
fn sign_invoice ( & self , _hrp_bytes : & [ u8 ] , _invoice_data : & [ u5 ] , _recipient : Recipient ) -> Result < RecoverableSignature , ( ) > {
214
214
unreachable ! ( )
215
215
}
216
+
217
+ fn sign_gossip_message ( & self , msg : lightning:: ln:: msgs:: UnsignedGossipMessage ) -> Result < Signature , ( ) > {
218
+ let msg_hash = Message :: from_slice ( & Sha256dHash :: hash ( & msg. encode ( ) [ ..] ) [ ..] ) . map_err ( |_| ( ) ) ?;
219
+ let secp_ctx = Secp256k1 :: signing_only ( ) ;
220
+ Ok ( secp_ctx. sign_ecdsa ( & msg_hash, & self . node_secret ) )
221
+ }
216
222
}
217
223
218
224
impl SignerProvider for KeyProvider {
@@ -229,12 +235,12 @@ impl SignerProvider for KeyProvider {
229
235
let keys = InMemorySigner :: new (
230
236
& secp_ctx,
231
237
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 ] ,
238
+ 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 ( ) ,
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 , 5 , 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 , 6 , 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 , 7 , 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 , 8 , self . node_secret [ 31 ] ] ) . unwrap ( ) ,
243
+ [ 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 ] ] ,
238
244
channel_value_satoshis,
239
245
channel_keys_id,
240
246
) ;
@@ -257,14 +263,14 @@ impl SignerProvider for KeyProvider {
257
263
258
264
fn get_destination_script ( & self ) -> Script {
259
265
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 ( ) ;
266
+ 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 ( ) ;
261
267
let our_channel_monitor_claim_key_hash = WPubkeyHash :: hash ( & PublicKey :: from_secret_key ( & secp_ctx, & channel_monitor_claim_key) . serialize ( ) ) ;
262
268
Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & our_channel_monitor_claim_key_hash[ ..] ) . into_script ( )
263
269
}
264
270
265
271
fn get_shutdown_scriptpubkey ( & self ) -> ShutdownScript {
266
272
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 ( ) ;
273
+ 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 ( ) ;
268
274
let pubkey_hash = WPubkeyHash :: hash ( & PublicKey :: from_secret_key ( & secp_ctx, & secret_key) . serialize ( ) ) ;
269
275
ShutdownScript :: new_p2wpkh ( & pubkey_hash)
270
276
}
@@ -402,7 +408,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
402
408
macro_rules! make_node {
403
409
( $node_id: expr, $fee_estimator: expr) => { {
404
410
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( ) ) } ) ;
411
+ 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( ) ;
412
+ let keys_manager = Arc :: new( KeyProvider { node_secret, rand_bytes_id: atomic:: AtomicU32 :: new( 0 ) , enforcement_states: Mutex :: new( HashMap :: new( ) ) } ) ;
406
413
let monitor = Arc :: new( TestChainMonitor :: new( broadcast. clone( ) , logger. clone( ) , $fee_estimator. clone( ) ,
407
414
Arc :: new( TestPersister {
408
415
update_ret: Mutex :: new( ChannelMonitorUpdateStatus :: Completed )
0 commit comments