Skip to content

Instantly share code, notes, and snippets.

@vindard
Last active July 19, 2022 08:08
Show Gist options
  • Save vindard/aaeb4a96fec8def168aed45cba3542db to your computer and use it in GitHub Desktop.
Save vindard/aaeb4a96fec8def168aed45cba3542db to your computer and use it in GitHub Desktop.
Decode macaroon from hex string and write to file that can be parsed by lncli.

Decoding macaroon strings

Macaroons generated from the $ lncli bakemacaroon method are output as hex-encoded strings by default. To decode these to a binary file for easy usage with lncli these are a few options included here for bash and python.

The output file from these can be passed to the --macaroonpath flag directly when using $ lncli.

# Sample from https://guggero.github.io/cryptography-toolkit/#!/macaroon
sample_macaroon_hex = '02011568747470733a2f2f736f6d652e6c6f636174696f6e020f64656d6f2d6964656e74696669657200020e6970203d203132372e302e302e310000062003e21abfd35932875cb2dee4d619b1d38abbc4825839be09799908b542fcc91f'
out_file = 'my-custom.macaroon'
macaroon_bytes = bytes.fromhex(sample_macaroon_hex)
with open(out_file, 'wb') as f:
f.write(macaroon_bytes)
#!/bin/bash
# For hex
# Sample from https://guggero.github.io/cryptography-toolkit/#!/macaroon
SAMPLE_MACAROON_HEX="02011568747470733a2f2f736f6d652e6c6f636174696f6e020f64656d6f2d6964656e74696669657200020e6970203d203132372e302e302e310000062003e21abfd35932875cb2dee4d619b1d38abbc4825839be09799908b542fcc91f"
OUT_FILE="my-custom.macaroon"
echo $SAMPLE_MACAROON_HEX \
| xxd -r -p - \
> $OUT_FILE
# =========================
# For base64
SAMPLE_MACAROON_B64="AgEDbG5kAvgBAwoQYRN7k+zmnr+6tsZM76sgIRIBMBoWCgdhZGRyZXNzEgRyZWFkEgV3cml0ZRoTCgRpbmZvEgRyZWFkEgV3cml0ZRoXCghpbnZvaWNlcxIEcmVhZBIFd3JpdGUaIQoIbWFjYXJvb24SCGdlbmVyYXRlEgRyZWFkEgV3cml0ZRoWCgdtZXNzYWdlEgRyZWFkEgV3cml0ZRoXCghvZmZjaGFpbhIEcmVhZBIFd3JpdGUaFgoHb25jaGFpbhIEcmVhZBIFd3JpdGUaFAoFcGVlcnMSBHJlYWQSBXdyaXRlGhgKBnNpZ25lchIIZ2VuZXJhdGUSBHJlYWQAAAYgyrArezmYYqcv6AZ4ZqUe4p0C3P8W1rF+L9qF9sEwx6g="
OUT_FILE="my-custom.macaroon"
echo $SAMPLE_MACAROON_B64 \
| base64 -d \
> $OUT_FILE
#!/bin/bash
MACAROON_FILE="admin.macaroon"
# Hex: Can copy the output of the below command
xxd -p $MACAROON_FILE
# Base64: Can copy the output of the below command
base64 $MACAROON_FILE | tr -d '\n\r'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment