aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKevin Malakoff <[email protected]>2019-06-14 14:07:48 -0700
committerKevin Malakoff <[email protected]>2019-06-14 14:07:48 -0700
commit4625aca583acb444cf2e08ccc4c6d834312d3080 (patch)
tree19f3c678d455828c535a583d9531860f8468e9bc /lib
parent66145a336bcccee51775e9830fb67e4034949d52 (diff)
downloadcoinos-server-4625aca583acb444cf2e08ccc4c6d834312d3080.tar.gz
coinos-server-4625aca583acb444cf2e08ccc4c6d834312d3080.zip
Removed dependency on forked module
Diffstat (limited to 'lib')
-rw-r--r--lib/createLnrpc.js43
1 files changed, 43 insertions, 0 deletions
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
+ }
+ });
+}