@@ -512,6 +512,33 @@ def getnewaddress(self):
512512 info = self .rpc .getaddressinfo (addr )
513513 return info ['unconfidential' ]
514514
515+ class RemoteSignerD (TailableProc ):
516+ def __init__ (self , rsignerd_dir , rsignerd_port ):
517+ TailableProc .__init__ (self , rsignerd_dir )
518+ self .executable = env ("REMOTE_SIGNER_CMD" , 'rsignerd' )
519+ self .opts = [
520+ '--datadir={}' .format (rsignerd_dir ),
521+ '--port={}' .format (rsignerd_port ),
522+ ]
523+ self .prefix = 'rsignerd'
524+ self .rsignerd_port = rsignerd_port
525+
526+ @property
527+ def cmd_line (self ):
528+ return [self .executable ] + self .opts
529+
530+ def start (self , stdin = None , stdout = None , stderr = None ,
531+ wait_for_initialized = True ):
532+ TailableProc .start (self , stdin , stdout , stderr )
533+ # We need to always wait for initialization
534+ self .wait_for_log ("rsignerd [0-9]* ready on .*:{}" .format (self .rsignerd_port ))
535+ logging .info ("rsignerd started" )
536+
537+ def stop (self , timeout = 10 ):
538+ logging .info ("stopping rsignerd" )
539+ rc = TailableProc .stop (self , timeout )
540+ logging .info ("rsignerd stopped" )
541+ return rc
515542
516543class LightningD (TailableProc ):
517544 def __init__ (self , lightning_dir , bitcoindproxy , port = 9735 , random_hsm = False , node_id = 0 ):
@@ -521,7 +548,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
521548 self .port = port
522549 self .cmd_prefix = []
523550 self .disconnect_file = None
524-
551+ self .lightning_dir = lightning_dir
552+ self .rsignerd_dir = os .path .join (lightning_dir , "remotesigner" )
553+
525554 self .rpcproxy = bitcoindproxy
526555
527556 self .opts = LIGHTNINGD_CONFIG .copy ()
@@ -548,6 +577,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
548577 if not os .path .exists (os .path .join (lightning_dir , TEST_NETWORK )):
549578 os .makedirs (os .path .join (lightning_dir , TEST_NETWORK ))
550579
580+ if not os .path .exists (self .rsignerd_dir ):
581+ os .makedirs (self .rsignerd_dir )
582+
551583 # Last 32-bytes of final part of dir -> seed.
552584 seed = (bytes (re .search ('([^/]+)/*$' , lightning_dir ).group (1 ), encoding = 'utf-8' ) + bytes (32 ))[:32 ]
553585 if not random_hsm :
@@ -559,6 +591,8 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
559591 self .prefix = 'lightningd-%d' % (node_id )
560592
561593 def cleanup (self ):
594+ # Make sure the remotesigner is shutdown
595+ self .rsignerd .stop ()
562596 # To force blackhole to exit, disconnect file must be truncated!
563597 if self .disconnect_file :
564598 with open (self .disconnect_file , "w" ) as f :
@@ -581,11 +615,34 @@ def cmd_line(self):
581615
582616 def start (self , stdin = None , stdout = None , stderr = None ,
583617 wait_for_initialized = True ):
584- self .opts ['bitcoin-rpcport' ] = self .rpcproxy .rpcport
585- TailableProc .start (self , stdin , stdout , stderr )
586- if wait_for_initialized :
587- self .wait_for_log ("Server started with public key" )
588- logging .info ("LightningD started" )
618+ try :
619+ # Start the remote signer first
620+ rsignerd_port = reserve ()
621+ self .rsignerd = RemoteSignerD (self .rsignerd_dir , rsignerd_port )
622+ self .rsignerd .start (stdin , stdout , stderr , wait_for_initialized )
623+
624+ # We can't do this in the constructor because we need a new port on each restart.
625+ self .env ['REMOTE_HSMD_ENDPOINT' ] = 'localhost:{}' .format (rsignerd_port )
626+
627+ self .opts ['bitcoin-rpcport' ] = self .rpcproxy .rpcport
628+ TailableProc .start (self , stdin , stdout , stderr )
629+ if wait_for_initialized :
630+ self .wait_for_log ("Server started with public key" )
631+ logging .info ("LightningD started" )
632+ except :
633+ # LightningD didn't start, stop the remotesigner
634+ self .rsignerd .stop ()
635+ raise
636+
637+ def stop (self , timeout = 10 ):
638+ # Stop the remote signer first
639+ self .rsignerd .stop (timeout )
640+ return TailableProc .stop (self , timeout )
641+
642+ def kill (self ):
643+ # Kill the remote signer first
644+ self .rsignerd .kill ()
645+ TailableProc .kill (self )
589646
590647 def wait (self , timeout = 10 ):
591648 """Wait for the daemon to stop for up to timeout seconds
0 commit comments