Lnd: Example needed: Using REST API with Macaroons

Created on 4 Feb 2018  路  10Comments  路  Source: lightningnetwork/lnd

How do we use Macaroons with the REST Api?

documentation macaroons question

Most helpful comment

I finally figured it out, we need to add the macaroon with a Grpc-Metadata- prefix in the header, and encode the macaroon file in hex:

GET https://localhost:8080/v1/getinfo
Grpc-Metadata-macaroon: hex(admin.macaroon-file)

All 10 comments

HTTP header macaroon...?

I finally figured it out, we need to add the macaroon with a Grpc-Metadata- prefix in the header, and encode the macaroon file in hex:

GET https://localhost:8080/v1/getinfo
Grpc-Metadata-macaroon: hex(admin.macaroon-file)

It took me a while to figure this out too. Turns out there's no Java library that supports macaroons v2, and the hex encoding seems a bit non-standard. Anyway, here's what I ended up doing: https://github.com/ompldr/server/blob/master/src/main/kotlin/org/ompldr/server/models/Invoice.kt#L73-L107

tl;dr: read in the admin macaroon as a byte array, encode it as hex, et voila.

@brndnmtthws how about making a PR adding some docs w.r.t to how to do that generically? (so on the HTTP level).

Ah somehow missed @Richard87's example above. But either way a PR to update the docs would be appreciated, would be merged swiftly.

Have a look at #785. I added a code sample, but if that's too much I can yank it. Or perhaps there should be a separate file that contains code samples in various languages?

If we should add a samples, I think it should be as simple as possible :)

For example just a curl script that lets the users experiment with it and see what happens:

curl --insecure --header "Grpc-Metadata-macaroon: $(xxd -ps -u -c 1000  ./var/lnd/admin.macaroon)" https://localhost:8080/v1/getinfo

Hmm, okay, I'll add that to the doc.

However, in my opinion at least, providing proper examples with 'real code' will make it significantly easier for people to get started with lnd.

Yup, that I completley agree, code samples would be awesome, just Java is so incredibly verbose, that it's hard to see the core of what is needed...

for example Node.js:

var macaroonString = fs.readFileSync(adminMacaroonFile);
var macaroonHexString = Buffer.from(macaroonString, 'utf8').toString('hex');

request.get({
  url: 'https://localhost:8080/v1/getinfo',
  rejectUnauthorized: false,
  headers: {
    'Grpc-Metadata-macaroon': macaroonHexString
  }
});

Note: Untested!

Fixed by #785.

Was this page helpful?
0 / 5 - 0 ratings