Skip to content

Instantly share code, notes, and snippets.

@zouppen
Last active December 12, 2022 23:11
Show Gist options
  • Save zouppen/05193d1a507cc83f812d7c80e6b3b165 to your computer and use it in GitHub Desktop.
Save zouppen/05193d1a507cc83f812d7c80e6b3b165 to your computer and use it in GitHub Desktop.
Command-line tool to convert Lightning Address to LN invoice

Command-line tool to convert Lightning Address to LN invoice

Usage:

lnaddress user@example.com 1000 "Thanks a lot!"

Probably not 100% compliant with LUDS but should almost fulfill LUD-01, LUD-06, and LUD-12.

Author: Joel Lehtonen, https://github.com/zouppen/

#!/bin/sh -eu
usage () {
echo "Usage: $0 user@example.com amount_msat comment" >&2
echo "Converts Lightning Address to LN invoice" >&2
exit 1
}
test $# -eq 3 || usage
# Convert address to URI
url="$(echo "$1" | sed -nre 's|^([^@]+)@([^@]+)|https://\2/.well-known/lnurlp/\1|p')"
test -n "$url" || usage
# Fetch callback
export json_cb="$(curl --no-progress-meter --fail-with-body "$url")"
cb_url="$(jq -rn 'env.json_cb | fromjson | .callback // (.reason + "\n" | halt_error)')"
# Try to convert it to LN URI
export json_ln="$(curl --no-progress-meter --fail-with-body -G --data-urlencode "amount=$2" --data-urlencode "comment=$3" "$cb_url")"
# Extract invoice, if any
jq -rn 'env.json_ln | fromjson | .pr // (.reason + "\n" | halt_error)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment