diff options
| author | Adam Soltys <[email protected]> | 2020-03-11 22:01:35 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-03-11 22:01:35 -0700 |
| commit | a22b5cd3769ea6d7aac4f34acf142534ee75491c (patch) | |
| tree | 428fb1a3c36d7ddacf1095c3ab1b47b26042d72b /lib | |
| parent | c0888fab024cc9f7c99812bfabf9d7774312dbf7 (diff) | |
| download | coinos-server-a22b5cd3769ea6d7aac4f34acf142534ee75491c.tar.gz coinos-server-a22b5cd3769ea6d7aac4f34acf142534ee75491c.zip | |
drop lnrpc
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/createLnrpc.js | 40 | ||||
| -rw-r--r-- | lib/lnd.js | 36 |
2 files changed, 36 insertions, 40 deletions
diff --git a/lib/createLnrpc.js b/lib/createLnrpc.js deleted file mode 100644 index 083ad961..00000000 --- a/lib/createLnrpc.js +++ /dev/null @@ -1,40 +0,0 @@ -const fs = require("fs"); -const grpc = require("grpc"); -const createLnrpc = require("lnrpc"); -const { promisify } = require("util"); - -module.exports = async ({ server, tls, macaroon }) => { - const lnrpc = await createLnrpc({ server, tls, macaroonPath: macaroon }); - - // Resolve proxy instance - return new Proxy(lnrpc, { - /** - * Promisify each lightning RPC method - * @param {lnrpc.Lightning} target - * @param {String} key - * @return {Promise} {Any} - */ - get(target, key) { - const method = target[key]; - - if (typeof key === "string" && typeof method === "function") { - const streaming = key.match(/openChannel$|sendPayment|subscribe.*/); - let fn; - - if (streaming) { - if (key === "sendPayment" || key === "sendToRoute") { - fn = params => method.call(lnrpc, target.meta, params); - } else { - fn = params => method.call(lnrpc, params, target.meta); - } - } else { - fn = params => promisify(method).call(lnrpc, params, target.meta); - } - - return fn; - } - - return target[key]; // forward - } - }); -}; diff --git a/lib/lnd.js b/lib/lnd.js new file mode 100644 index 00000000..8bb2df48 --- /dev/null +++ b/lib/lnd.js @@ -0,0 +1,36 @@ +const fs = require('fs'); +const path = require('path'); +const grpc = require('grpc'); +const protoLoader = require('@grpc/proto-loader'); +const { promisify } = require('util'); + + +module.exports = ({ tls, macaroon, server }) => { + const packageDefinition = protoLoader.loadSync('config/rpc.proto', { + keepCase: true, + longs: String, + enums: String, + defaults: true, + oneofs: true + }); + const lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc; + process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA' + const lndCert = fs.readFileSync(tls); + const sslCreds = grpc.credentials.createSsl(lndCert); + const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) { + const macaroonFile = fs.readFileSync(macaroon).toString('hex'); + const metadata = new grpc.Metadata() + metadata.add('macaroon', macaroonFile); + callback(null, metadata); + }); + const creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds); + const lnd = new lnrpc.Lightning(server, creds); + + return new Proxy(lnd, { + get(target, key) { + const method = target[key]; + if (typeof method === 'function' && !key.startsWith('sub')) return promisify(method); + return target[key]; + }, + }); +} |
