-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lnd manager #163
Merged
Merged
Lnd manager #163
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd5bb82
Added support for CLI argument, that disables macaroon support
4e8578a
update README
7c18119
initial implementation of LightningManager component
74acd87
refactored LightningManager, added async/io interface
95f0dd5
added docs
5cb1365
in the middle of refactoring routes
25682e4
refactored final bits of API
5bae11a
Plugs in rest of logic for Lightning Manager
864ed83
added documentation for helper functions
478d728
Added fix
acc464b
patching old Async calls
513c1b6
better logging for errors
08724c6
added hosts and server components
615a485
Merge branch 'master' into lnd-manager
PirosB3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,53 +4,109 @@ const fs = require("fs"); | |
const logger = require("winston"); | ||
const debug = require("debug")("lncliweb:lightning"); | ||
|
||
// expose the routes to our app with module.exports | ||
module.exports = function (protoPath, lndHost, lndCertPath, macaroonPath) { | ||
const LightningError = Object.freeze({ | ||
"WALLET_LOCKED": "WALLET_LOCKED", | ||
"NODE_UNREACHABLE": "NODE_UNREACHABLE", | ||
"UNCATEGORIZED": "UNCATEGORIZED" | ||
}); | ||
|
||
process.env.GRPC_SSL_CIPHER_SUITES = "HIGH+ECDSA"; | ||
|
||
const lnrpcDescriptor = grpc.load(protoPath); | ||
/** | ||
* Defines a wrapper around the Lightning gRPC API, with error support, retry, and async API. | ||
* Every call towards `Lightning` should be handled through the `Call` API. | ||
*/ | ||
class LightningManager { | ||
|
||
if (lndCertPath) { | ||
getActiveClient() { | ||
if (!this.activeClient) { | ||
logger.info("Recreating active client"); | ||
this.credentials = this.generateCredentials(this.lndCert, {macaroonPath: this.macaroonPath}); | ||
this.activeClient = new this.lnrpcDescriptor.lnrpc.Lightning(this.lndHost, this.credentials); | ||
} | ||
return this.activeClient; | ||
} | ||
|
||
if (fs.existsSync(lndCertPath)) { | ||
generateCredentials(lndCert, options) { | ||
let credentials = grpc.credentials.createSsl(lndCert); | ||
|
||
const lndCert = fs.readFileSync(lndCertPath); | ||
const sslCreds = grpc.credentials.createSsl(lndCert); | ||
// If macaroon path was specified load credentials using macaroon metadata. | ||
if (options.macaroonPath) { | ||
if (fs.existsSync(options.macaroonPath)) { | ||
let macaroonCreds = grpc.credentials.createFromMetadataGenerator(function (args, callback) { | ||
let adminMacaroon = fs.readFileSync(options.macaroonPath); | ||
let metadata = new grpc.Metadata(); | ||
metadata.add("macaroon", adminMacaroon.toString("hex")); | ||
callback(null, metadata); | ||
}); | ||
credentials = grpc.credentials.combineChannelCredentials(credentials, macaroonCreds); | ||
} else { | ||
logger.error("The specified macaroon file "+ options.macaroonPath + " was not found.\n" + | ||
"Please add the missing lnd macaroon file or update/remove the path in the application configuration."); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
var credentials; | ||
if (macaroonPath) { | ||
if (fs.existsSync(macaroonPath)) { | ||
var macaroonCreds = grpc.credentials.createFromMetadataGenerator(function (args, callback) { | ||
const adminMacaroon = fs.readFileSync(macaroonPath); | ||
const metadata = new grpc.Metadata(); | ||
metadata.add("macaroon", adminMacaroon.toString("hex")); | ||
callback(null, metadata); | ||
}); | ||
credentials = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds); | ||
} else { | ||
logger.error("The specified macaroon file \"" + macaroonPath + "\" was not found.\n" | ||
+ "Please add the missing lnd macaroon file or update/remove the path in the application configuration."); | ||
process.exit(1); | ||
} | ||
} else { | ||
credentials = sslCreds; | ||
} | ||
return credentials; | ||
} | ||
|
||
return new lnrpcDescriptor.lnrpc.Lightning(lndHost, credentials); | ||
/** | ||
* @constructor | ||
* @param {string} protoPath - the path to the `rpc.proto` file that defined the `Lightning` RPC interface | ||
* @param {string} lndHost - the host and port of the LND node (ex. "locahost:10003") | ||
* @param {string} lndCertPath - the path to the SSL certificate used by LND | ||
* @param {?string} macaroonPath - the path to the macarron file to use. Can be `null` if no macaroon should be used. | ||
*/ | ||
constructor(protoPath, lndHost, lndCertPath, macaroonPath) { | ||
process.env.GRPC_SSL_CIPHER_SUITES = "HIGH+ECDSA"; | ||
|
||
} else { | ||
if (!fs.existsSync(lndCertPath)) { | ||
logger.error("Required lnd certificate path missing from application configuration."); | ||
process.exit(1); | ||
} | ||
|
||
logger.error("The specified lnd certificate file \"" + lndCertPath + "\" was not found.\n" | ||
+ "Please add the missing lnd certificate file or update the path in the application configuration."); | ||
process.exit(1); | ||
// Define credentials for SSL certificate generated by LND, and active client | ||
this.lndHost = lndHost; | ||
this.lndCert = fs.readFileSync(lndCertPath); | ||
this.lnrpcDescriptor = grpc.load(protoPath); | ||
this.macaroonPath = macaroonPath; | ||
this.activeClient = null; | ||
} | ||
|
||
} | ||
/* | ||
* Calls a Lightning gRPC method. | ||
* @param {string} method - the gRPC method to call (ex. "getInfo") | ||
* @param {Object} parameters - optional key/value parameters to supply for the API call | ||
* @returns {Promise} - if successful, response if an Object containing API result payload, otherwise it will fail | ||
with a LightningError. | ||
*/ | ||
async call(method, parameters) { | ||
return new Promise((resolve, reject) => { | ||
let activeClient = this.getActiveClient(); | ||
activeClient[method](parameters, (err, response) => { | ||
if (err) { | ||
|
||
} else { | ||
// drop active client, so that it can be recreated | ||
this.activeClient = null; | ||
|
||
logger.error("Required lnd certificate path missing from application configuration."); | ||
process.exit(1); | ||
switch(err.code) { | ||
case grpc.status.UNIMPLEMENTED: | ||
reject(LightningError.WALLET_LOCKED); | ||
break; | ||
case grpc.status.UNAVAILABLE: | ||
reject(LightningError.NODE_UNREACHABLE); | ||
break; | ||
default: | ||
logger.error("Unrecognized gRPC error: " + err); | ||
reject(LightningError.UNCATEGORIZED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PirosB3 looks like we are losing the original gRPC error here. |
||
} | ||
} else { | ||
logger.debug(method + ":", response); | ||
resolve(response); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
} | ||
}; | ||
|
||
module.exports = LightningManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PirosB3 seems a bit excessive to me to drop the client on every gRPC error we are receiving 🤔