diff options
| author | Kevin Malakoff <[email protected]> | 2019-06-14 14:07:48 -0700 |
|---|---|---|
| committer | Kevin Malakoff <[email protected]> | 2019-06-14 14:07:48 -0700 |
| commit | 4625aca583acb444cf2e08ccc4c6d834312d3080 (patch) | |
| tree | 19f3c678d455828c535a583d9531860f8468e9bc | |
| parent | 66145a336bcccee51775e9830fb67e4034949d52 (diff) | |
| download | coinos-server-4625aca583acb444cf2e08ccc4c6d834312d3080.tar.gz coinos-server-4625aca583acb444cf2e08ccc4c6d834312d3080.zip | |
Removed dependency on forked module
| -rw-r--r-- | index.js | 17 | ||||
| -rw-r--r-- | lib/createLnrpc.js | 43 |
2 files changed, 47 insertions, 13 deletions
@@ -9,9 +9,7 @@ import BitcoinCore from 'bitcoin-core' import cors from 'cors' import express from 'express' import fb from 'fb' -import fs from 'fs' import graphqlHTTP from 'express-graphql' -import grpc from 'grpc' import io from 'socket.io' import jwt from 'jsonwebtoken' import morgan from 'morgan' @@ -24,6 +22,7 @@ import zmq from 'zeromq' import authyVerify from './authy' import config from './config' import whitelist from './whitelist' +import createLnrpc from './lib/createLnrpc' const bitcoin = require('bitcoinjs-lib') const l = console.log @@ -33,17 +32,9 @@ const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); const authy = new Client({ key: config.authy.key }); (async () => { - const ln = async ({ server, tls, macaroon, channelpeers }) => { - const ln = await require('lnrpc')({ server, tls }) - ln.meta = new grpc.Metadata() - ln.meta.add('macaroon', fs.readFileSync(macaroon).toString('hex')) - ln.channelpeers = channelpeers - return ln - } - - const lna = await ln(config.lna) - const lnb = await ln(config.lnb) - + debugger; + const lna = await createLnrpc(config.lna) + const lnb = await createLnrpc(config.lnb) const bc = new BitcoinCore(config.bitcoin) const app = express() diff --git a/lib/createLnrpc.js b/lib/createLnrpc.js new file mode 100644 index 00000000..16a59b3c --- /dev/null +++ b/lib/createLnrpc.js @@ -0,0 +1,43 @@ +import fs from 'fs' +import grpc from 'grpc' +import createLnrpc from 'lnrpc' +import {promisify} from 'util' + +export default async ({ server, tls, macaroon, channelpeers }) => { + const lnrpc = await createLnrpc({ server, tls }) + lnrpc.meta = new grpc.Metadata() + lnrpc.meta.add('macaroon', fs.readFileSync(macaroon).toString('hex')) + lnrpc.channelpeers = channelpeers + + // 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') { + 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 + } + }); +} |
