Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
connect(options) {
if (this.grpc && this.grpc.state !== 'ready') {
throw new Error('Can not connect (already connected)')
}
this.options = options
// Create a new grpc instance using settings from init options.
const grpcOptions = this.getConnectionSettings()
this.grpc = new LndGrpc(grpcOptions)
// Set up service accessors.
this.services = this.grpc.services
// Inject helper methods.
Object.assign(this.services.Lightning, lightningMethods)
Object.assign(this.services.Lightning, lightningSubscriptions)
// Setup gRPC event handlers.
this.grpc.on('locked', () => {
this.emit(GRPC_WALLET_UNLOCKER_SERVICE_ACTIVE)
})
this.grpc.on('active', () => {
this.emit(GRPC_LIGHTNING_SERVICE_ACTIVE)
this.subscribeAll()
})
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 => {
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,
})
}
}