@@ -65,7 +65,9 @@ use bitcoin::secp256k1::PublicKey;
6565use bitcoin:: { BlockHash , Network } ;
6666
6767#[ cfg( any( vss, vss_test) ) ]
68- use bitcoin:: bip32:: ChildNumber ;
68+ use bitcoin:: bip32:: { ChildNumber , Xpriv } ;
69+ #[ cfg( any( vss, vss_test) ) ]
70+ use std:: collections:: HashMap ;
6971use std:: convert:: TryInto ;
7072use std:: default:: Default ;
7173use std:: fmt;
@@ -74,6 +76,8 @@ use std::path::PathBuf;
7476use std:: sync:: atomic:: AtomicBool ;
7577use std:: sync:: { Arc , Mutex , RwLock } ;
7678use std:: time:: SystemTime ;
79+ #[ cfg( any( vss, vss_test) ) ]
80+ use vss_client:: headers:: { FixedHeaders , VssHeaderProvider } ;
7781
7882#[ derive( Debug , Clone ) ]
7983enum ChainDataSourceConfig {
@@ -357,36 +361,56 @@ impl NodeBuilder {
357361 self . build_with_store ( kv_store)
358362 }
359363
360- /// Builds a [`Node`] instance with a [`VssStore` ] backend and according to the options
364+ /// Builds a [`Node`] instance with a [VSS ] backend and according to the options
361365 /// previously configured.
366+ ///
367+ /// Uses [`FixedHeaders`] as default method for authentication/authorization.
368+ /// Given `fixed_headers` are included as it is in all the requests made to VSS.
369+ ///
370+ /// **Caution**: VSS support is in **alpha** and is considered experimental.
371+ /// Using VSS (or any remote persistence) may cause LDK to panic if persistence failures are
372+ /// unrecoverable, i.e., if they remain unresolved after internal retries are exhausted.
373+ ///
374+ /// [VSS]: https://github.com/lightningdevkit/vss-server/blob/main/README.md
362375 #[ cfg( any( vss, vss_test) ) ]
363- pub fn build_with_vss_store ( & self , url : String , store_id : String ) -> Result < Node , BuildError > {
364- use bitcoin:: key:: Secp256k1 ;
376+ pub fn build_with_vss_store_and_fixed_headers (
377+ & self , vss_url : String , store_id : String , fixed_headers : HashMap < String , String > ,
378+ ) -> Result < Node , BuildError > {
379+ let header_provider = Arc :: new ( FixedHeaders :: new ( fixed_headers) ) ;
380+
381+ self . build_with_vss_store_and_header_provider ( vss_url, store_id, header_provider)
382+ }
365383
384+ /// Builds a [`Node`] instance with a [VSS] backend and according to the options
385+ /// previously configured.
386+ ///
387+ /// Given `header_provider` is used to attach headers to every request made
388+ /// to VSS.
389+ ///
390+ /// **Caution**: VSS support is in **alpha** and is considered experimental.
391+ /// Using VSS (or any remote persistence) may cause LDK to panic if persistence failures are
392+ /// unrecoverable, i.e., if they remain unresolved after internal retries are exhausted.
393+ ///
394+ /// [VSS]: https://github.com/lightningdevkit/vss-server/blob/main/README.md
395+ #[ cfg( any( vss, vss_test) ) ]
396+ pub fn build_with_vss_store_and_header_provider (
397+ & self , vss_url : String , store_id : String , header_provider : Arc < dyn VssHeaderProvider > ,
398+ ) -> Result < Node , BuildError > {
366399 let logger = setup_logger ( & self . config ) ?;
367400
368401 let seed_bytes = seed_bytes_from_config (
369402 & self . config ,
370403 self . entropy_source_config . as_ref ( ) ,
371404 Arc :: clone ( & logger) ,
372405 ) ?;
373- let config = Arc :: new ( self . config . clone ( ) ) ;
374406
375- let xprv = bitcoin:: bip32:: Xpriv :: new_master ( config. network , & seed_bytes) . map_err ( |e| {
376- log_error ! ( logger, "Failed to derive master secret: {}" , e) ;
377- BuildError :: InvalidSeedBytes
378- } ) ?;
407+ let config = Arc :: new ( self . config . clone ( ) ) ;
379408
380- let vss_xprv = xprv
381- . derive_priv ( & Secp256k1 :: new ( ) , & [ ChildNumber :: Hardened { index : 877 } ] )
382- . map_err ( |e| {
383- log_error ! ( logger, "Failed to derive VSS secret: {}" , e) ;
384- BuildError :: KVStoreSetupFailed
385- } ) ?;
409+ let vss_xprv = derive_vss_xprv ( config. clone ( ) , & seed_bytes, logger. clone ( ) ) ?;
386410
387411 let vss_seed_bytes: [ u8 ; 32 ] = vss_xprv. private_key . secret_bytes ( ) ;
388412
389- let vss_store = Arc :: new ( VssStore :: new ( url , store_id, vss_seed_bytes) ) ;
413+ let vss_store = Arc :: new ( VssStore :: new ( vss_url , store_id, vss_seed_bytes, header_provider ) ) ;
390414 build_with_store_internal (
391415 config,
392416 self . chain_data_source_config . as_ref ( ) ,
@@ -1079,6 +1103,25 @@ fn seed_bytes_from_config(
10791103 }
10801104}
10811105
1106+ #[ cfg( any( vss, vss_test) ) ]
1107+ fn derive_vss_xprv (
1108+ config : Arc < Config > , seed_bytes : & [ u8 ; 64 ] , logger : Arc < FilesystemLogger > ,
1109+ ) -> Result < Xpriv , BuildError > {
1110+ use bitcoin:: key:: Secp256k1 ;
1111+
1112+ let xprv = Xpriv :: new_master ( config. network , seed_bytes) . map_err ( |e| {
1113+ log_error ! ( logger, "Failed to derive master secret: {}" , e) ;
1114+ BuildError :: InvalidSeedBytes
1115+ } ) ?;
1116+
1117+ Ok ( xprv. derive_priv ( & Secp256k1 :: new ( ) , & [ ChildNumber :: Hardened { index : 877 } ] ) . map_err (
1118+ |e| {
1119+ log_error ! ( logger, "Failed to derive VSS secret: {}" , e) ;
1120+ BuildError :: KVStoreSetupFailed
1121+ } ,
1122+ ) ?)
1123+ }
1124+
10821125/// Sanitize the user-provided node alias to ensure that it is a valid protocol-specified UTF-8 string.
10831126pub ( crate ) fn sanitize_alias ( alias_str : & str ) -> Result < NodeAlias , BuildError > {
10841127 let alias = alias_str. trim ( ) ;
0 commit comments