raspiblitz/home.admin/BBcreateInvoice.sh

150 lines
4 KiB
Bash
Raw Normal View History

2018-08-09 14:58:44 +02:00
#!/bin/bash
2020-02-17 14:12:54 +01:00
clear
_temp=$(mktemp -p /dev/shm/)
_error=$(mktemp -p /dev/shm/)
2020-02-17 14:11:46 +01:00
sudo chmod 7777 ${_error} 2>/dev/null
2018-08-09 14:58:44 +02:00
2018-12-05 16:19:06 +01:00
# load raspiblitz config data (with backup from old config)
2019-02-02 23:49:04 +01:00
source /home/admin/raspiblitz.info
source /mnt/hdd/raspiblitz.conf
2018-12-05 16:19:06 +01:00
if [ ${#network} -eq 0 ]; then network=`cat .network`; fi
if [ ${#network} -eq 0 ]; then network="bitcoin"; fi
2018-12-05 16:19:06 +01:00
if [ ${#chain} -eq 0 ]; then
echo "gathering chain info ... please wait"
chain=$(${network}-cli getblockchaininfo | jq -r '.chain')
fi
2018-08-09 14:58:44 +02:00
source <(/home/admin/config.scripts/network.aliases.sh getvars $1 $2)
shopt -s expand_aliases
alias bitcoincli_alias="$bitcoincli_alias"
alias lncli_alias="$lncli_alias"
alias lightningcli_alias="$lightningcli_alias"
2021-05-23 11:01:01 +01:00
# Check if ready (chain in sync and channels open)
2021-05-23 11:01:01 +01:00
./XXchainInSync.sh $network $chain $LNTYPE
if [ $? != 0 ]; then
2018-08-09 14:58:44 +02:00
exit 1
fi
# let user enter the invoice
2021-05-23 11:01:01 +01:00
l1="Enter the AMOUNT IN SATOSHIS to invoice:"
2018-08-09 14:58:44 +02:00
l2="1 ${network} = 100 000 000 SAT"
2021-05-23 11:01:01 +01:00
dialog --title "Request payment through Lightning" \
2018-08-09 15:06:06 +02:00
--inputbox "$l1\n$l2" 9 50 2>$_temp
2018-08-09 14:58:44 +02:00
amount=$(cat $_temp | xargs | tr -dc '0-9')
shred -u $_temp
2018-08-09 14:58:44 +02:00
if [ ${#amount} -eq 0 ]; then
2020-02-17 20:47:37 +01:00
clear
echo
echo "no amount entered - returning to menu ..."
sleep 2
2018-08-09 14:58:44 +02:00
exit 1
fi
# TODO let user enter a description
2018-08-09 14:58:44 +02:00
# build command
2021-05-23 11:01:01 +01:00
if [ $LNTYPE = cln ];then
label=$(date +%s) # seconds since 1970-01-01 00:00:00 UTC
# invoice msatoshi label description [expiry] [fallbacks] [preimage] [exposeprivatechannels] [cltv]
command="$lightningcli_alias invoice ${amount}sat $label ''"
# TODO warn about insufficient liquidity
elif [ $LNTYPE = lnd ];then
command="$lncli_alias addinvoice ${amount}"
fi
2018-08-09 14:58:44 +02:00
# info output
clear
echo "******************************"
echo "Create Invoice / Payment Request"
echo "******************************"
2021-05-23 11:01:01 +01:00
echo
2018-08-09 14:58:44 +02:00
echo "COMMAND LINE: "
echo $command
2021-05-23 11:01:01 +01:00
echo
2018-08-09 14:58:44 +02:00
echo "RESULT:"
2018-08-09 15:08:39 +02:00
sleep 2
2018-08-09 14:58:44 +02:00
# execute command
result=$($command 2>$_error)
2021-05-23 11:01:01 +01:00
error=$(cat ${_error} 2>/dev/null)
2018-08-09 14:58:44 +02:00
2020-02-17 23:30:11 +01:00
#echo "result(${result})"
#echo "error(${error})"
2018-08-09 14:58:44 +02:00
if [ ${#error} -gt 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "FAIL"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "${error}"
else
2021-05-23 11:01:01 +01:00
if [ $LNTYPE = cln ];then
payReq=$(echo "$result" | grep bolt11 | cut -d '"' -f4)
elif [ $LNTYPE = lnd ];then
rhash=$(echo "$result" | grep r_hash | cut -d '"' -f4)
payReq=$(echo "$result" | grep payment_request | cut -d '"' -f4)
fi
2021-04-08 00:02:57 +02:00
/home/admin/config.scripts/blitz.display.sh qr "${payReq}"
2020-01-25 00:04:02 +01:00
if [ $(sudo dpkg-query -l | grep "ii qrencode" | wc -l) = 0 ]; then
sudo apt-get install qrencode -y > /dev/null
fi
echo
echo "********************"
echo "Here is your invoice"
echo "********************"
echo
2020-01-25 00:04:02 +01:00
qrencode -t ANSI256 "${payReq}"
2019-04-04 04:57:57 +01:00
echo
2018-08-09 15:06:06 +02:00
echo "Give this Invoice/PaymentRequest to someone to pay it:"
echo
echo "${payReq}"
echo
2019-07-31 14:38:38 +01:00
echo "Monitoring the Incoming Payment with:"
2021-05-23 11:01:01 +01:00
if [ $LNTYPE = cln ];then
echo "$lightningcli_alias waitinvoice $label"
elif [ $LNTYPE = lnd ];then
echo "$lncli_alias lookupinvoice ${rhash}"
fi
2019-04-04 04:57:57 +01:00
echo "Press x and hold to skip to menu."
2018-08-09 15:06:06 +02:00
2019-04-04 04:57:57 +01:00
while :
do
2021-05-23 11:01:01 +01:00
if [ $LNTYPE = cln ];then
result=$($lightningcli_alias waitinvoice $label)
wasPayed=$(echo $result | grep -c 'paid')
elif [ $LNTYPE = lnd ];then
result=$($lncli_alias lookupinvoice ${rhash})
wasPayed=$(echo $result | grep -c '"settled": true')
fi
2019-04-04 05:03:03 +01:00
if [ ${wasPayed} -gt 0 ]; then
echo
echo $result
echo
2021-05-23 11:01:01 +01:00
echo "OK the Invoice was paid - returning to menu."
2021-04-08 00:02:57 +02:00
/home/admin/config.scripts/blitz.display.sh hide
/home/admin/config.scripts/blitz.display.sh image /home/admin/raspiblitz/pictures/ok.png
2019-04-04 05:16:54 +01:00
sleep 2
2019-04-04 05:03:03 +01:00
break
fi
2019-04-04 04:57:57 +01:00
# wait 2 seconds for key input
read -n 1 -t 2 keyPressed
2018-08-09 15:06:06 +02:00
2019-04-04 04:57:57 +01:00
# check if user wants to abort session
if [ "${keyPressed}" = "x" ]; then
2019-04-04 05:03:03 +01:00
echo
echo $result
echo
echo "Returning to menu - invoice was not payed yet."
2019-04-04 04:57:57 +01:00
break
fi
done
2021-04-08 00:02:57 +02:00
/home/admin/config.scripts/blitz.display.sh hide
2019-04-04 04:57:57 +01:00
2020-02-17 20:50:06 +01:00
fi
echo "Press ENTER to return to main menu."
read key