@@ -514,6 +514,33 @@ def getnewaddress(self):
514514 info = self .rpc .getaddressinfo (addr )
515515 return info ['unconfidential' ]
516516
517+ class RemoteSignerD (TailableProc ):
518+ def __init__ (self , rsignerd_dir , rsignerd_port ):
519+ TailableProc .__init__ (self , rsignerd_dir )
520+ self .executable = env ("REMOTE_SIGNER_CMD" , 'rsignerd' )
521+ self .opts = [
522+ '--datadir={}' .format (rsignerd_dir ),
523+ '--port={}' .format (rsignerd_port ),
524+ ]
525+ self .prefix = 'rsignerd'
526+ self .rsignerd_port = rsignerd_port
527+
528+ @property
529+ def cmd_line (self ):
530+ return [self .executable ] + self .opts
531+
532+ def start (self , stdin = None , stdout = None , stderr = None ,
533+ wait_for_initialized = True ):
534+ TailableProc .start (self , stdin , stdout , stderr )
535+ # We need to always wait for initialization
536+ self .wait_for_log ("rsignerd [0-9]* ready on .*:{}" .format (self .rsignerd_port ))
537+ logging .info ("rsignerd started" )
538+
539+ def stop (self , timeout = 10 ):
540+ logging .info ("stopping rsignerd" )
541+ rc = TailableProc .stop (self , timeout )
542+ logging .info ("rsignerd stopped" )
543+ return rc
517544
518545class LightningD (TailableProc ):
519546 def __init__ (self , lightning_dir , bitcoindproxy , port = 9735 , random_hsm = False , node_id = 0 ):
@@ -523,7 +550,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
523550 self .port = port
524551 self .cmd_prefix = []
525552 self .disconnect_file = None
526-
553+ self .lightning_dir = lightning_dir
554+ self .rsignerd_dir = os .path .join (lightning_dir , "remotesigner" )
555+
527556 self .rpcproxy = bitcoindproxy
528557
529558 self .opts = LIGHTNINGD_CONFIG .copy ()
@@ -550,6 +579,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
550579 if not os .path .exists (os .path .join (lightning_dir , TEST_NETWORK )):
551580 os .makedirs (os .path .join (lightning_dir , TEST_NETWORK ))
552581
582+ if not os .path .exists (self .rsignerd_dir ):
583+ os .makedirs (self .rsignerd_dir )
584+
553585 # Last 32-bytes of final part of dir -> seed.
554586 seed = (bytes (re .search ('([^/]+)/*$' , lightning_dir ).group (1 ), encoding = 'utf-8' ) + bytes (32 ))[:32 ]
555587 if not random_hsm :
@@ -561,6 +593,8 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
561593 self .prefix = 'lightningd-%d' % (node_id )
562594
563595 def cleanup (self ):
596+ # Make sure the remotesigner is shutdown
597+ self .rsignerd .stop ()
564598 # To force blackhole to exit, disconnect file must be truncated!
565599 if self .disconnect_file :
566600 with open (self .disconnect_file , "w" ) as f :
@@ -583,11 +617,34 @@ def cmd_line(self):
583617
584618 def start (self , stdin = None , stdout = None , stderr = None ,
585619 wait_for_initialized = True ):
586- self .opts ['bitcoin-rpcport' ] = self .rpcproxy .rpcport
587- TailableProc .start (self , stdin , stdout , stderr )
588- if wait_for_initialized :
589- self .wait_for_log ("Server started with public key" )
590- logging .info ("LightningD started" )
620+ try :
621+ # Start the remote signer first
622+ rsignerd_port = reserve ()
623+ self .rsignerd = RemoteSignerD (self .rsignerd_dir , rsignerd_port )
624+ self .rsignerd .start (stdin , stdout , stderr , wait_for_initialized )
625+
626+ # We can't do this in the constructor because we need a new port on each restart.
627+ self .env ['REMOTE_HSMD_ENDPOINT' ] = 'localhost:{}' .format (rsignerd_port )
628+
629+ self .opts ['bitcoin-rpcport' ] = self .rpcproxy .rpcport
630+ TailableProc .start (self , stdin , stdout , stderr )
631+ if wait_for_initialized :
632+ self .wait_for_log ("Server started with public key" )
633+ logging .info ("LightningD started" )
634+ except :
635+ # LightningD didn't start, stop the remotesigner
636+ self .rsignerd .stop ()
637+ raise
638+
639+ def stop (self , timeout = 10 ):
640+ # Stop the remote signer first
641+ self .rsignerd .stop (timeout )
642+ return TailableProc .stop (self , timeout )
643+
644+ def kill (self ):
645+ # Kill the remote signer first
646+ self .rsignerd .kill ()
647+ TailableProc .kill (self )
591648
592649 def wait (self , timeout = 10 ):
593650 """Wait for the daemon to stop for up to timeout seconds
0 commit comments