Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async onBeforeConnect() {
// Establish a connection.
const { useMacaroon, waitForMacaroon } = this._getConnectionSettings()
await this.establishConnection({ useMacaroon, waitForMacaroon })
// Once connected, make a call to getInfo in order to determine the api version.
const info = await this.getInfo()
grpcLog.info('Connected to Lightning gRPC:', info)
// Determine most relevant proto version and reconnect using the right rpc.proto if we need to.
const { protoPath } = this.lndConfig
const [closestProtoVersion, latestProtoVersion] = await Promise.all([
lndgrpc.getClosestProtoVersion(info.version, { path: protoPath }),
lndgrpc.getLatestProtoVersion({ path: protoPath }),
])
if (closestProtoVersion !== latestProtoVersion) {
grpcLog.info(
'Found better match. Reconnecting using rpc.proto version: %s',
closestProtoVersion
)
this.service.close()
await this.establishConnection({
version: closestProtoVersion,
useMacaroon,
waitForMacaroon,
})
}
}
async establishConnection(options = {}) {
const { version, useMacaroon, waitForMacaroon } = options
const { host, cert, macaroon, protoPath } = this.lndConfig
// Find the most recent rpc.proto file
const versionToUse = version || (await lndgrpc.getLatestProtoVersion({ path: protoPath }))
const filepath = join(protoPath, `${versionToUse}.proto`)
grpcLog.info(`Establishing gRPC connection to ${this.serviceName} with proto file %s`, filepath)
// Load gRPC package definition as a gRPC object hierarchy.
const packageDefinition = await load(filepath, grpcOptions)
const rpc = loadPackageDefinition(packageDefinition)
// Create ssl credentials to use with the gRPC client.
let creds = await createSslCreds(cert)
// Add macaroon to crenentials if service requires macaroons.
if (useMacaroon) {
// If we are trying to connect to the internal lnd, wait up to 20 seconds for the macaroon to be generated.
if (waitForMacaroon) {
await waitForFile(macaroon, 20000)
}
async establishConnection() {
const { host, cert } = this.lndConfig
// Find the most recent rpc.proto file
const version = await lndgrpc.getLatestProtoVersion({ path: lndGpcProtoPath() })
const filepath = join(lndGpcProtoPath(), `${version}.proto`)
mainLog.debug('Establishing gRPC connection with proto file %s', filepath)
// Load gRPC package definition as a gRPC object hierarchy.
const packageDefinition = await load(filepath, grpcOptions)
const rpc = loadPackageDefinition(packageDefinition)
// Create ssl credentials to use with the gRPC client.
const sslCreds = await createSslCreds(cert)
// Create a new gRPC client instance.
this.service = new rpc.lnrpc.WalletUnlocker(host, sslCreds)
// Wait upto 20 seconds for the gRPC connection to be established.
return new Promise((resolve, reject) => {
this.service.waitForReady(getDeadline(20), err => {