aboutsummaryrefslogtreecommitdiffstats
path: root/lib/createLnrpc.js
blob: 083ad9613a32b6ebd0609a83452762cfdb2a806a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
    }
  });
};