Support for "any" amount for LN invoice and LN pay

This commit is contained in:
kexkey
2019-12-06 15:25:06 -05:00
committed by kexkey
parent 1be01c301e
commit f69f80389a
3 changed files with 34 additions and 14 deletions

View File

@@ -786,12 +786,14 @@ Proxy response:
### Create a Lightning Network invoice (called by application)
Returns a LN invoice. Label must be unique. Description will be used by your user for payment. Expiry is in seconds.
Returns a LN invoice. Label must be unique. Description will be used by your user for payment. Expiry is in seconds. If msatoshi is not supplied, will use "any" (ie donation invoice).
```http
POST http://cyphernode:8888/ln_create_invoice
with body...
{"msatoshi":10000,"label":"koNCcrSvhX3dmyFhW","description":"Bylls order #10649","expiry":900}
or
{"label":"koNCcrSvhX3dmyFhW","description":"Bylls order #10649","expiry":900}
```
Proxy response:
@@ -806,7 +808,7 @@ Proxy response:
### Pay a Lightning Network invoice (called by application)
Make a LN payment. expected_msatoshi and expected_description are respectively the amount and description you gave your user for her to create the invoice; they must match the given bolt11 invoice supplied by your user.
Make a LN payment. expected_msatoshi and expected_description are respectively the amount and description you gave your user for her to create the invoice; they must match the given bolt11 invoice supplied by your user. If the bolt11 invoice doesn't contain an amount, then the expected_msatoshi supplied here will be used as the paid amount.
```http
POST http://cyphernode:8888/ln_pay

View File

@@ -1387,7 +1387,6 @@ paths:
schema:
type: "object"
required:
- "msatoshi"
- "label"
- "description"
- "expiry"
@@ -1440,7 +1439,6 @@ paths:
required:
- "bolt11"
- "expected_msatoshi"
- "expected_description"
properties:
bolt11:
type: "string"

View File

@@ -28,8 +28,13 @@ ln_create_invoice() {
# "bolt11": "lnbc100n1pwzllqgpp55a8xen9sdcntehwr93pkwnuu8nmtqx9yew0flalcxhx9nvy34crqdq9wsckgxqzpucqp2rzjqt04ll5ft3mcuy8hws4xcku2pnhma9r9mavtjtadawyrw5kgzp7g7zr745qq3mcqqyqqqqlgqqqqqzsqpcr85k33shzaxscpj29fadmjmfej6y2p380x9w4kxydqpxq87l6lshy69fry9q2yrtu037nt44x77uhzkdyn8043n5yj8tqgluvmcl69cquaxr68"
#}
trace "[ln_create_invoice] ./lightning-cli invoice ${msatoshi} \"${label}\" \"${description}\" ${expiry}"
result=$(./lightning-cli invoice ${msatoshi} "${label}" "${description}" ${expiry})
if [ "${msatoshi}" = "null" ]; then
trace "[ln_create_invoice] ./lightning-cli invoice \"any\" \"${label}\" \"${description}\" ${expiry}"
result=$(./lightning-cli invoice "any" "${label}" "${description}" ${expiry})
else
trace "[ln_create_invoice] ./lightning-cli invoice ${msatoshi} \"${label}\" \"${description}\" ${expiry}"
result=$(./lightning-cli invoice ${msatoshi} "${label}" "${description}" ${expiry})
fi
returncode=$?
trace_rc ${returncode}
trace "[ln_create_invoice] result=${result}"
@@ -47,7 +52,11 @@ ln_create_invoice() {
# Let's get the connect string if provided in configuration
local connectstring=$(get_connection_string)
sql "INSERT OR IGNORE INTO ln_invoice (label, bolt11, callback_url, payment_hash, expires_at, msatoshi, description, status) VALUES (\"${label}\", \"${bolt11}\", \"${callback_url}\", \"${payment_hash}\", ${expires_at}, ${msatoshi}, \"${description}\", \"unpaid\")"
if [ "${msatoshi}" = "null" ]; then
sql "INSERT OR IGNORE INTO ln_invoice (label, bolt11, callback_url, payment_hash, expires_at, description, status) VALUES (\"${label}\", \"${bolt11}\", \"${callback_url}\", \"${payment_hash}\", ${expires_at}, \"${description}\", \"unpaid\")"
else
sql "INSERT OR IGNORE INTO ln_invoice (label, bolt11, callback_url, payment_hash, expires_at, msatoshi, description, status) VALUES (\"${label}\", \"${bolt11}\", \"${callback_url}\", \"${payment_hash}\", ${expires_at}, ${msatoshi}, \"${description}\", \"unpaid\")"
fi
trace_rc $?
id=$(sql "SELECT id FROM ln_invoice WHERE bolt11=\"${bolt11}\"")
trace_rc $?
@@ -60,7 +69,9 @@ ln_create_invoice() {
fi
data="${data}\"callback_url\":\"${callback_url}\","
data="${data}\"payment_hash\":\"${payment_hash}\","
data="${data}\"msatoshi\":${msatoshi},"
if [ "${msatoshi}" != "null" ]; then
data="${data}\"msatoshi\":${msatoshi},"
fi
data="${data}\"status\":\"unpaid\","
data="${data}\"description\":\"${description}\","
data="${data}\"expires_at\":${expires_at}}"
@@ -306,12 +317,15 @@ ln_pay() {
local invoice_description=$(echo "${result}" | jq ".description")
trace "[ln_pay] invoice_description=${invoice_description}"
# The amount must match
if [ "${expected_msatoshi}" != "${invoice_msatoshi}" ]; then
# The amount must match if not "any"
# If the amount is not in the invoice and not supplied as expected_msatoshi, then both will be null, that's ok!
# Same thing goes for the description.
if [ "${expected_msatoshi}" != "${invoice_msatoshi}" ] && [ "${invoice_msatoshi}" != "null" ]; then
# If invoice_msatoshi is null, that means "any" was supplied, so the amounts don't have to match!
result="{\"result\":\"error\",\"expected_msatoshi\":${expected_msatoshi},\"invoice_msatoshi\":${invoice_msatoshi}}"
returncode=1
elif [ "${expected_description}" != '""' ] && [ "${expected_description}" != "${invoice_description}" ]; then
# If expected description is empty, we accept any description on the invoice. Amount is the important thing.
elif [ -n "${expected_description}" ] && [ "${expected_description}" != "null" ] && [ "${expected_description}" != "${invoice_description}" ]; then
# If expected description is empty, we accept any description on the invoice. Amount is the most important thing.
result="{\"result\":\"error\",\"expected_description\":${expected_description},\"invoice_description\":${invoice_description}}"
returncode=1
@@ -319,8 +333,14 @@ ln_pay() {
# Amount and description is as expected, let's pay!
trace "[ln_pay] Amount and description are as expected, let's try to pay!"
trace "[ln_pay] ./lightning-cli pay -k bolt11=${bolt11} retry_for=15"
result=$(./lightning-cli pay -k bolt11=${bolt11} retry_for=15)
if [ "${invoice_msatoshi}" = "null" ]; then
# "any" amount on the invoice, we force paying the expected_msatoshi provided to ln_pay by the user
trace "[ln_pay] ./lightning-cli pay -k bolt11=${bolt11} msatoshi=${expected_msatoshi} retry_for=15"
result=$(./lightning-cli pay -k bolt11=${bolt11} msatoshi=${expected_msatoshi} retry_for=15)
else
trace "[ln_pay] ./lightning-cli pay -k bolt11=${bolt11} retry_for=15"
result=$(./lightning-cli pay -k bolt11=${bolt11} retry_for=15)
fi
returncode=$?
trace_rc ${returncode}
trace "[ln_pay] result=${result}"