@@ -95,11 +95,42 @@ class LndClient extends SwapClient {
95
95
return ;
96
96
}
97
97
98
+ let lndCert : Buffer | undefined ;
98
99
try {
99
- const lndCert = await fs . readFile ( certpath ) ;
100
- this . credentials = grpc . credentials . createSsl ( lndCert ) ;
101
- this . logger . debug ( `loaded tls cert from ${ certpath } ` ) ;
100
+ lndCert = await fs . readFile ( certpath ) ;
102
101
} catch ( err ) {
102
+ if ( awaitingCreate && err . code === 'ENOENT' ) {
103
+ // if we have not created the lnd wallet yet and the tls.cert file can
104
+ // not be found, we will briefly wait for the cert to be created in
105
+ // case lnd has not been run before and is being started in parallel
106
+ // with xud
107
+ const certDir = path . join ( certpath , '..' ) ;
108
+ const CERT_TIMEOUT = 3000 ;
109
+
110
+ lndCert = await new Promise ( ( resolve ) => {
111
+ this . logger . debug ( `watching ${ certDir } for tls.cert to be created` ) ;
112
+ const timeout = setTimeout ( ( ) => {
113
+ fsWatcher . close ( ) ;
114
+ resolve ( undefined ) ;
115
+ } , CERT_TIMEOUT ) ;
116
+ const fsWatcher = watch ( certDir , ( event , filename ) => {
117
+ if ( event === 'change' && filename === 'tls.cert' ) {
118
+ this . logger . debug ( 'tls.cert was created' ) ;
119
+ fsWatcher . close ( ) ;
120
+ clearTimeout ( timeout ) ;
121
+ fs . readFile ( certpath ) . then ( resolve ) . catch ( ( err ) => {
122
+ this . logger . error ( err ) ;
123
+ resolve ( undefined ) ;
124
+ } ) ;
125
+ }
126
+ } ) ;
127
+ } ) ;
128
+ }
129
+ }
130
+ if ( lndCert ) {
131
+ this . logger . debug ( `loaded tls cert from ${ certpath } ` ) ;
132
+ this . credentials = grpc . credentials . createSsl ( lndCert ) ;
133
+ } else {
103
134
this . logger . error ( `could not load tls cert from ${ certpath } , is lnd installed?` ) ;
104
135
await this . setStatus ( ClientStatus . Misconfigured ) ;
105
136
return ;
@@ -285,8 +316,8 @@ class LndClient extends SwapClient {
285
316
this . watchMacaroonResolve = resolve ;
286
317
} ) ;
287
318
const macaroonDir = path . join ( this . macaroonpath ! , '..' ) ;
288
- const fsWatcher = watch ( macaroonDir , ( _ , filename ) => {
289
- if ( filename === 'admin.macaroon' ) {
319
+ const fsWatcher = watch ( macaroonDir , ( event , filename ) => {
320
+ if ( event === 'change' && filename === 'admin.macaroon' ) {
290
321
this . logger . debug ( 'admin.macaroon was created' ) ;
291
322
if ( this . watchMacaroonResolve ) {
292
323
this . watchMacaroonResolve ( true ) ;
0 commit comments