Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If the macaroon is already in hex format, add as is.
const isHex = /^[0-9a-fA-F]+$/.test(macaroonPath)
if (isHex) {
metadata.add('macaroon', macaroonPath)
}
// Otherwise, treat it as a file path - load the file and convert to hex.
else {
const macaroon = await readFile(macaroonPath).catch(e => {
const error = new Error(`Macaroon path could not be accessed: ${e.message}`)
error.code = 'LND_GRPC_MACAROON_ERROR'
throw error
})
metadata.add('macaroon', macaroon.toString('hex'))
}
}
return credentials.createFromMetadataGenerator((params, callback) => callback(null, metadata))
}
// If it's not a filepath, then assume it is a base64url encoded string.
else if (macaroonPath === basename(macaroonPath)) {
lndMacaroon = decodeMacaroon(macaroonPath)
}
// Otherwise, treat it as a file path - load the file and convert to hex.
else {
const macaroon = await readFile(untildify(macaroonPath)).catch(e => {
const error = new Error(`Macaroon path could not be accessed: ${e.message}`)
error.code = 'LND_GRPC_MACAROON_ERROR'
throw error
})
lndMacaroon = macaroon.toString('hex')
}
metadata.add('macaroon', lndMacaroon)
}
return credentials.createFromMetadataGenerator((params, callback) => callback(null, metadata))
}