Files
nutshell/cashu/lightning/macaroon.py
callebtc 84453f5dbe add CoreLightningRestWallet (#362)
* add CoreLightningRestWallet

* add macaroon loader

* add correct config
2023-11-16 09:50:34 -03:00

35 lines
1.0 KiB
Python

import base64
def load_macaroon(macaroon: str) -> str:
"""Returns hex version of a macaroon encoded in base64 or the file path.
:param macaroon: Macaroon encoded in base64 or file path.
:type macaroon: str
:return: Hex version of macaroon.
:rtype: str
"""
# if the macaroon is a file path, load it and return hex version
if macaroon.split(".")[-1] == "macaroon":
try:
with open(macaroon, "rb") as f:
macaroon_bytes = f.read()
return macaroon_bytes.hex()
except FileNotFoundError:
raise Exception(f"Macaroon file not found: {macaroon}")
else:
# if macaroon is a provided string
# check if it is hex, if so, return
try:
bytes.fromhex(macaroon)
return macaroon
except ValueError:
pass
# convert the bas64 macaroon to hex
try:
macaroon = base64.b64decode(macaroon).hex()
except Exception:
pass
return macaroon