Skip to content

Instantly share code, notes, and snippets.

@vindard
Last active January 24, 2019 04:53
Show Gist options
  • Save vindard/e0933cd542198488dc01a1e700ed4a6f to your computer and use it in GitHub Desktop.
Save vindard/e0933cd542198488dc01a1e700ed4a6f to your computer and use it in GitHub Desktop.
A little refactor I'm pretty proud of 😊
def decode(options):
a = lndecode(options.lnaddress, options.verbose)
#########################
# HANDLE TAGS
#########################
all_tags = {
'r': "Routes",
'd': "Description",
'f': "Fallback",
'h': "Description hash",
'x': "Expiry (seconds)",
}
def tags_by_name(name, tags):
return [t[1] for t in tags if t[0] == name]
#################################
# BUILD `decoded_request` DICT
#################################
decoded_request = {
"Signed with public key": hexlify(a.pubkey.serialize()),
"Currency": a.currency,
"Payment hash": hexlify(a.paymenthash),
"Amount": a.amount,
"Timestamp": a.date,
}
for k,v in all_tags.items():
if tags_by_name(k, a.tags):
if k not in 'h': # list all tags here to be hexlified
decoded_request[v] = tags_by_name(k, a.tags)[0]
else:
decoded_request[v] = hexlify(tags_by_name(k, a.tags)[0])
for t in [t for t in a.tags if t[0] not in all_tags.keys()]:
decoded_request[f"UNKNOWN TAG {t[0]}"] = hexlify(t[1])
#################################
# PRINT `decoded_request` DICT
#################################
for k,v in decoded_request.items():
if k != "Routes":
if v:
if k == "Timestamp":
print(f"{k}: {v} ({time.ctime(v)})")
else:
print(f"{k}: {v}")
if 'Routes' in decoded_request.keys():
for r in decoded_request['Routes']:
print("Route: ",end='')
print(f"{hexlify(r[0])}/{hexlify(r[1])}/{r[2]}/{r[3]}/{r[4]}")
# RETURN
return decoded_request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment