@@ -12,10 +12,18 @@ use std::future::Future;
1212use std:: panic:: RefUnwindSafe ;
1313use std:: path:: PathBuf ;
1414use std:: sync:: Mutex ;
15+
16+ #[ cfg( any( all( jwt_auth_test, feature = "test_utils" ) , sig_auth_test) ) ]
1517use std:: time:: SystemTime ;
1618
19+ #[ cfg( all( sig_auth_test, feature = "test_utils" ) ) ]
20+ use secp256k1:: SecretKey ;
21+
1722#[ cfg( all( jwt_auth_test, feature = "test_utils" ) ) ]
1823use jsonwebtoken:: { encode, Algorithm , EncodingKey , Header } ;
24+ #[ cfg( all( jwt_auth_test, feature = "test_utils" ) ) ]
25+ use serde:: { Deserialize , Serialize } ;
26+
1927use lightning:: events:: ClosureReason ;
2028use lightning:: ln:: functional_test_utils:: {
2129 check_added_monitors, check_closed_event, connect_block, create_announced_chan_between_nodes,
@@ -29,7 +37,6 @@ use lightning::util::test_utils;
2937use lightning:: { check_closed_broadcast, io} ;
3038use rand:: distr:: Alphanumeric ;
3139use rand:: { rng, Rng } ;
32- use serde:: { Deserialize , Serialize } ;
3340
3441type TestMonitorUpdatePersister < ' a , K > = MonitorUpdatingPersister <
3542 & ' a K ,
@@ -41,8 +48,8 @@ type TestMonitorUpdatePersister<'a, K> = MonitorUpdatingPersister<
4148> ;
4249
4350const EXPECTED_UPDATES_PER_PAYMENT : u64 = 5 ;
44- #[ cfg( jwt_auth_test) ]
45- pub const VSS_PRIVATE_PEM : & str = r#"
51+ #[ cfg( all ( jwt_auth_test, feature = "test_utils" ) ) ]
52+ const VSS_PRIVATE_PEM : & str = r#"
4653-----BEGIN PRIVATE KEY-----
4754MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCuIYJJ1dzNmuct
4855fzj+W4EeJXic/A6gkHJOS7MHqAqOqMg49aZfOj6y4kmhU3/fal5OccJ4299ohSnJ
@@ -73,6 +80,10 @@ xrhmGsrdBu3nBkwwpCBps6KZ
7380-----END PRIVATE KEY-----
7481"# ;
7582
83+ #[ cfg( sig_auth_test) ]
84+ const SIGNING_CONSTANT : & ' static [ u8 ] =
85+ b"VSS Signature Authorizer Signing Salt Constant.................." ;
86+
7687pub struct InMemoryStore {
7788 persisted_bytes : Mutex < HashMap < String , HashMap < String , Vec < u8 > > > > ,
7889}
@@ -388,7 +399,7 @@ pub(crate) fn do_test_store<K: KVStoreSync + Sync>(store_0: &K, store_1: &K) {
388399 check_persisted_data ! ( persister_0_max_pending_updates * 2 * EXPECTED_UPDATES_PER_PAYMENT + 1 ) ;
389400}
390401
391- #[ cfg( jwt_auth_test) ]
402+ #[ cfg( all ( jwt_auth_test, feature = "test_utils" ) ) ]
392403#[ derive( Serialize , Deserialize ) ]
393404struct TestClaims {
394405 sub : String ,
@@ -397,7 +408,7 @@ struct TestClaims {
397408 exp : i64 ,
398409}
399410
400- #[ cfg( jwt_auth_test) ]
411+ #[ cfg( all ( jwt_auth_test, feature = "test_utils" ) ) ]
401412pub fn generate_test_jwt ( private_pem : & str , user_id : & str ) -> String {
402413 let now = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) as i64 ;
403414
@@ -410,13 +421,38 @@ pub fn generate_test_jwt(private_pem: &str, user_id: &str) -> String {
410421 encode ( & Header :: new ( Algorithm :: RS256 ) , & claims, & encoding_key) . unwrap ( )
411422}
412423
413- /// Returns a hashmap of fixed headers.
424+ #[ cfg( all( sig_auth_test, feature = "test_utils" ) ) ]
425+ fn build_auth_token ( secret_key : & SecretKey ) -> String {
426+ use bitcoin:: hashes:: sha256:: Hash ;
427+ use bitcoin:: hashes:: Hash as _;
428+ use std:: time:: UNIX_EPOCH ;
429+
430+ use crate :: hex_utils;
431+
432+ let secp = secp256k1:: Secp256k1 :: new ( ) ;
433+ let pubkey = secret_key. public_key ( & secp) ;
434+ let now = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) ;
435+
436+ let mut bytes_to_sign = Vec :: new ( ) ;
437+ bytes_to_sign. extend_from_slice ( SIGNING_CONSTANT ) ;
438+ bytes_to_sign. extend_from_slice ( & pubkey. serialize ( ) ) ;
439+ bytes_to_sign. extend_from_slice ( format ! ( "{now}" ) . as_bytes ( ) ) ;
440+
441+ let hash = Hash :: hash ( & bytes_to_sign) ;
442+ let sig = secret_key. sign_ecdsa ( secp256k1:: Message :: from_digest ( hash. to_byte_array ( ) ) ) ;
443+
444+ format ! ( "{pubkey:x}{}{now}" , hex_utils:: to_string( & sig. serialize_compact( ) ) )
445+ }
446+
447+ /// Returns a hashmap of fixed headers, where, depending on configuration,
448+ /// corresponds to valid headers for no-op, signature-based, or jwt-based
449+ /// authorizers on vss-server.
414450pub fn get_fixed_headers ( ) -> HashMap < String , String > {
415451 #[ cfg( noop_auth_test) ]
416452 {
417453 return HashMap :: new ( ) ;
418454 }
419- #[ cfg( jwt_auth_test) ]
455+ #[ cfg( all ( jwt_auth_test, feature = "test_utils" ) ) ]
420456 {
421457 let token = generate_test_jwt ( VSS_PRIVATE_PEM , "test" ) ;
422458 let mut headers = HashMap :: new ( ) ;
@@ -426,9 +462,13 @@ pub fn get_fixed_headers() -> HashMap<String, String> {
426462
427463 #[ cfg( sig_auth_test) ]
428464 {
429- todo ! ( )
465+ let secret_key = SecretKey :: from_byte_array ( [ 42 ; 32 ] ) . unwrap ( ) ;
466+ let token = build_auth_token ( & secret_key) ;
467+ let mut headers = HashMap :: new ( ) ;
468+ headers. insert ( "Authorization" . to_string ( ) , token) ;
469+ return headers;
430470 }
431471
432- #[ cfg( not( any( noop_auth_test, jwt_auth_test, sig_auth_test) ) ) ]
472+ #[ cfg( not( any( noop_auth_test, all ( jwt_auth_test, feature = "test_utils" ) , sig_auth_test) ) ) ]
433473 HashMap :: new ( )
434474}
0 commit comments