Skip to content

Commit

Permalink
docs/grpc/javascript: updated docs to include macaroon and cipher suites
Browse files Browse the repository at this point in the history
  • Loading branch information
dannypaz authored and Roasbeef committed Apr 18, 2018
1 parent 21b6c62 commit 3c73329
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/grpc/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sources](https://github.com/lightningnetwork/lnd/blob/master/lnrpc/rpc.proto).

In order for the auto-generated code to compile successfully, you'll need to
comment out the following line:

```
//import "google/api/annotations.proto";
```
Expand All @@ -33,6 +34,11 @@ Every time you work with Javascript gRPC, you will have to import `grpc`, load
var grpc = require('grpc');
var fs = require("fs");

// Due to updated ECDSA generated tls.cert we need to let gprc know that
// we need to use that cipher suite otherwise there will be a handhsake
// error when we communicate with the lnd rpc server.
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA'

// Lnd cert is at ~/.lnd/tls.cert on Linux and
// ~/Library/Application Support/Lnd/tls.cert on Mac
var lndCert = fs.readFileSync("~/.lnd/tls.cert");
Expand Down Expand Up @@ -157,6 +163,66 @@ async.series(payment_senders, function() {
```
This example will send a payment of 100 satoshis every 2 seconds.


#### Using Macaroons

To authenticate using macaroons you need to include the macaroon in the metadata of the request.

```js
var fs = require('fs');
var grpc = require('grpc');

process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA'

// Lnd admin macaroon is at ~/.lnd/admin.macaroon on Linux and
// ~/Library/Application Support/Lnd/admin.macaroon on Mac
var m = fs.readFileSync('~/.lnd/admin.macaroon');
var macaroon = m.toString('hex');
var meta = new grpc.Metadata().add('macaroon', macaroon);

var lnrpcDescriptor = grpc.load("rpc.proto");
var lnrpc = lnrpcDescriptor.lnrpc;
var client = new lnrpc.Lightning('some.address:10009', grpc.credentials.createInsecure());

client.getInfo({}, meta);
```

However, this can get tiresome to do for each request, so to avoid explicitly including the macaroon we can update the credentials to include it automatically.

```js
var fs = require('fs');
var grpc = require('grpc');

process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA'

// Lnd admin macaroon is at ~/.lnd/admin.macaroon on Linux and
// ~/Library/Application Support/Lnd/admin.macaroon on Mac
var m = fs.readFileSync('~/.lnd/admin.macaroon');
var macaroon = m.toString('hex');

// build meta data credentials
var metadata = new grpc.Metadata().add('macaroon', macaroon)
var macaroonCreds = grpc.credentials.createFromMetadataGenerator((_args, callback) => {
callback(null, metadata);
});

// build ssl credentials using the cert the same as before
var lndCert = fs.readFileSync("~/.lnd/tls.cert");
var sslCreds = grpc.credentials.createSsl(lndCert);

// combine the cert credentials and the macaroon auth credentials
// such that every call is properly encrypted and authenticated
var credentials = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);

// Pass the crendentials when creating a channel
var lnrpcDescriptor = grpc.load("rpc.proto");
var lnrpc = lnrpcDescriptor.lnrpc;
var client = new lnrpc.Lightning('some.address:10009', credentials);

client.getInfo({}, (err, res) => { ... });
```


### Conclusion

With the above, you should have all the `lnd` related `gRPC` dependencies
Expand Down

0 comments on commit 3c73329

Please sign in to comment.