Skip to content

Commit 6398cd5

Browse files
committed
feat(lnd): wait for tls.cert
1 parent 85fab2e commit 6398cd5

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

Diff for: lib/lndclient/LndClient.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,42 @@ class LndClient extends SwapClient {
9595
return;
9696
}
9797

98+
let lndCert: Buffer | undefined;
9899
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);
102101
} 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 {
103134
this.logger.error(`could not load tls cert from ${certpath}, is lnd installed?`);
104135
await this.setStatus(ClientStatus.Misconfigured);
105136
return;
@@ -285,8 +316,8 @@ class LndClient extends SwapClient {
285316
this.watchMacaroonResolve = resolve;
286317
});
287318
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') {
290321
this.logger.debug('admin.macaroon was created');
291322
if (this.watchMacaroonResolve) {
292323
this.watchMacaroonResolve(true);

0 commit comments

Comments
 (0)