Macaroons are fancy cookies. You use LND to create custom macaroons that limit their permissions with great granularity, down to the exact RPC calls.
LND, Loop, Pool and litd all use macaroons to authenticate RPC calls. Macaroons are similar to cookies in that they are bearer instruments, but they can be more easily verified by the server using HMACs and a root key alone. They can also be attenuated, both by the server and by the user. This greatly simplifies how LND authenticates RPC calls while expanding the detail in which authority over specific RPC calls is permissioned.
By default, LND will generate eight macaroons, created for specific purposes. You can inspect the permissions of each macaroon with the command
lncli printmacaroon --macaroon_file ~/path/to/macaroon
The process of creating a custom macaroon is called “baking.” For this process, LND includes the LND macaroon bakery that can be invoked with
For instance, a macaroon that is only allowed to manage peers could be created with the command:
lncli bakemacaroon peers:read peers:write
For even more granularity, it is possible to specify individual RPC calls.
lncli bakemacaroon uri:/lnrpc.Lightning/GetInfo uri:/verrpc.Versioner/GetVersion
To get a list of all available restrictions, run
By default, LND will generate new macaroons with the root key 0. You can specify another root key ID, even one that does not yet exist, using the flag
--root_key_id. To save your macaroon to a file rather than returning its hex value, use the
--save_to flag. Additionally, macaroons can be bound by IP address as well.
LND supports adding external permissions, even if LND does not understand these permissions, with the
LND does not include a tool to convert a macaroon back to its hex value, but you may run the
xxdutility if it is installed on your system.
xxd -ps -u -c 1000 /path/to.macaroon
Using the macaroon bakery, you can take any existing macaroon and restrain it further, even if the macaroon was not issued by you.
For example, we can limit our admin macaroon to only be valid for calls made from localhost, as well as take away its authority to perform on-chain actions:
lncli constrainmacaroon --ip_address 127.0.0.1 --custom_caveat_name onchain --custom_caveat_condition read admin.macaroon constrained.macaroon
We can now inspect the permissions of this new macaroon with:
lncli printmacaroon --macaroon_file constrained.macaroon
"lnd-custom onchain read"
To revoke a macaroon, it is not sufficient to delete the macaroon. Instead, its root key has to be deleted. Which root key is used for a macaroon can be found out using the
lncli printmacarooncommand above.
lncli deletemacaroonid root_key_id
When interacting with
lndusing the GRPC interface, the macaroons are encoded as a hex string over the wire and can be passed to
lndby specifying the hex-encoded macaroon as GRPC metadata:
<macaroon>is the hex encoded binary data from the macaroon file itself.
A very simple example using
curlmay look something like this:
⛰ curl --insecure --header "Grpc-Metadata-macaroon: $(xxd -ps -u -c 1000 $HOME/.lnd/data/chain/bitcoin/simnet/admin.macaroon)" https://localhost:8080/v1/getinfo
As mentioned above, by default
lndcreates several macaroon files in its directory. These are unencrypted and in case of the
admin.macaroonprovide full access to the daemon. This can be seen as quite a big security risk if the
lnddaemon runs in an environment that is not fully trusted.
The macaroon files are the only files with highly sensitive information that are not encrypted (unlike the wallet file and the macaroon database file that contains the root key, these are always encrypted, even if no password is used).
To avoid leaking the macaroon information,
lndsupports the so called
- The three startup commands
lncliall have a flag called
--stateless_initthat instructs the daemon notto create
- The two operations
changepasswordthat actually create/updatethe macaroon database will return the admin macaroon in the RPC call.Assuming the daemon and the
lncliare not used on the same machine, thiswill leave no unencrypted information on the machine where
- To be more precise: By default, when using the
changepasswordcommand, themacaroon root key in the macaroon DB is just re-encrypted with the newpassword. But the key remains the same and therefore the macaroons issuedbefore the
changepasswordcommand still remain valid. If a user wants toinvalidate all previously created macaroons, the
changepasswordcommand should be used!
- An user of
lncliwill see the returned admin macaroon printed to the screenor saved to a file if the parameter
- Important: By default,
lndwill create the macaroon files during the
unlockphase, if the
--stateless_initflag is not used. So to avoidleakage of the macaroon information, use the stateless initialization flagfor all three startup commands of the wallet unlocker service!
- Create a new wallet stateless (first run):⛰ lncli create --stateless_init --save_to=/safe/location/admin.macaroon
- Unlock a wallet that has previously been initialized stateless:⛰ lncli unlock --stateless_init
- Use the created macaroon:⛰ lncli --macaroonpath=/safe/location/admin.macaroon getinfo