raspiblitz/home.admin/BBpayInvoice.sh

106 lines
2.7 KiB
Bash
Raw Normal View History

2018-08-09 14:44:45 +02:00
#!/bin/bash
_temp="./download/dialog.$$"
_error="./.error.out"
# load network and chain info
network=`cat .network`
chain=$(sudo -bitcoin ${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain')
echo ""
echo "*** Precheck ***"
# check if chain is in sync
2018-08-27 11:08:04 +02:00
chainInSync=$(lncli --chain=${network} getinfo | grep '"synced_to_chain": true' -c)
2018-08-09 14:44:45 +02:00
if [ ${chainInSync} -eq 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!"
echo "FAIL - 'lncli getinfo' shows 'synced_to_chain': false"
echo "Wait until chain is sync with LND and try again."
echo "!!!!!!!!!!!!!!!!!!!"
echo ""
exit 1
fi
# check number of connected peers
echo "check for open channels"
2018-08-27 11:08:04 +02:00
openChannels=$(sudo -u bitcoin /usr/local/bin/lncli --chain=${network} listchannels 2>/dev/null | grep chan_id -c)
2018-08-09 14:44:45 +02:00
if [ ${openChannels} -eq 0 ]; then
echo ""
echo "!!!!!!!!!!!!!!!!!!!"
echo "FAIL - You have NO ESTABLISHED CHANNELS .. open a channel first."
echo "!!!!!!!!!!!!!!!!!!!"
echo ""
exit 1
fi
paymentRequestStart="???"
if [ "${network}" = "bitcoin" ]; then
if [ "${chain}" = "main" ]; then
paymentRequestStart="lnbc"
else
paymentRequestStart="lntb"
fi
elif [ "${network}" = "litecoin" ]; then
paymentRequestStart="lnltc"
fi
testSite="???"
if [ "${network}" = "bitcoin" ]; then
if [ "${chain}" = "main" ]; then
testSite="https://satoshis.place"
else
testSite="https://testnet.satoshis.place"
fi
elif [ "${network}" = "litecoin" ]; then
testSite="https://millionlitecoinhomepage.net"
fi
# let user enter the invoice
l1="Copy the LightningInvoice/PaymentRequest into here:"
l2="Its a long string starting with '${paymentRequestStart}'"
l3="To try it out go to: ${testSite}"
dialog --title "Pay thru Lightning Network" \
--inputbox "$l1\n$l2\n$l3" 10 70 2>$_temp
invoice=$(cat $_temp | xargs)
shred $_temp
if [ ${#invoice} -eq 0 ]; then
echo "FAIL - not a valid input (${invoice})"
exit 1
fi
2018-08-09 14:58:44 +02:00
# TODO: maybe try/show the decoded info first by using https://api.lightning.community/#decodepayreq
2018-08-09 14:44:45 +02:00
# build command
2018-08-27 11:08:04 +02:00
command="lncli --chain=${network} sendpayment --pay_req=${invoice}"
2018-08-09 14:44:45 +02:00
# info output
clear
echo "******************************"
echo "Pay Invoice / Payment Request"
echo "******************************"
echo ""
echo "COMMAND LINE: "
echo $command
echo ""
2018-08-09 14:58:44 +02:00
echo "RESULT (may wait in case of timeout):"
2018-08-09 14:44:45 +02:00
# execute command
result=$($command 2>$_error)
error=`cat ${_error}`
#echo "result(${result})"
#echo "error(${error})"
if [ ${#error} -gt 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "FAIL"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "${error}"
else
2018-08-09 14:58:44 +02:00
echo "${result}"
2018-08-09 14:44:45 +02:00
echo "******************************"
echo "WIN"
echo "******************************"
2018-08-09 14:58:44 +02:00
echo "It worked :) - check out the service you were paying."
2018-08-09 14:44:45 +02:00
fi
echo ""