From 7bf2aeebf7b05ac3197415c46fce0a04c361b6b9 Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 6 Aug 2018 15:53:14 +0200 Subject: [PATCH 1/2] add switchNet --- home.admin/00mainMenu.sh | 6 ++++- home.admin/95switchNetEnv.sh | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 home.admin/95switchNetEnv.sh diff --git a/home.admin/00mainMenu.sh b/home.admin/00mainMenu.sh index f395baa..8f15732 100755 --- a/home.admin/00mainMenu.sh +++ b/home.admin/00mainMenu.sh @@ -66,7 +66,8 @@ else CONNECT "Connect to a Peer" \ lnbalance "Detailed Wallet Balances" \ lnchannels "Lightning Channel List" \ - RECKLES "Console / Terminal") + RECKLES "Console / Terminal"\ + REKT "Go Reckless! Switch Testnet/Mainnet") fi @@ -132,6 +133,9 @@ case $CHOICE in RECKLES) lncli -h echo "SUCH WOW come back with ./00mainMenu.sh" + ;; + REKT) # switch configs + sudo ./95switchNetEnv.sh; ;; X) # unlock ./AAunlockLND.sh diff --git a/home.admin/95switchNetEnv.sh b/home.admin/95switchNetEnv.sh new file mode 100755 index 0000000..bf85e6a --- /dev/null +++ b/home.admin/95switchNetEnv.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +BITCOIN_CONFIG="/home/bitcoin/.bitcoin/bitcoin.conf" +LND_CONFIG="/home/bitcoin/.lnd/lnd.conf" +# function to detect main/testnet + +function isMainnet(){ + grep "^#testnet=1$" -q $BITCOIN_CONFIG && return 1 + return 0 +} + +function switchToMainnet { + echo "switching to mainnet" + sed -i 's/^testnet=1/#testnet=1/g' $BITCOIN_CONFIG && \ + sed -i 's/^bitcoin.testnet=1/#bitcoin.testnet=1/g' $LND_CONFIG && \ + sed -i 's/^#bitcoin.mainnet=1/bitcoin.mainnet=1/g' $LND_CONFIG + echo "switched to mainnet" +} + +function switchToTestnet { + echo "switching to testnet" + sed -i 's/^#testnet=1/testnet=1/g' $BITCOIN_CONFIG && \ + sed -i 's/^#bitcoin.testnet=1/bitcoin.testnet=1/g' $LND_CONFIG && \ + sed -i 's/^bitcoin.mainnet=1/#bitcoin.mainnet=1/g' $LND_CONFIG + echo "switched to testnet" +} + +# change to test: check both configs for commented out testnet arguments and mainnet +echo "stopping lnd client" +systemctl stop lnd +sleep 4 +echo "stopping bitcoind client" +systemctl stop bitcoind +sleep 4 +isMainnet +if [ $? -eq 1 ]; then + echo "switching from mainnet to testnet" + switchToTestnet +else + echo "switching from testnet to mainnet" + switchToMainnet +fi +echo "copying over config to bitcoin user" +cp $BITCOIN_CONFIG /home/admin/.bitcoin/ +systemctl start bitcoind +echo "started bitcoind back up, giving it a minute to come up" +sleep 60 +systemctl start lnd +echo "started lnd back up, giving it a minute, you will have to unlock your wallet" +sleep 60 +echo "finished config switch - started back up" +echo "you will still have to unlock your LND wallet" +./00mainMenu.sh \ No newline at end of file From e27395dccc670ff1ea543be310a8eb51e7dcffc7 Mon Sep 17 00:00:00 2001 From: rootzoll Date: Wed, 8 Aug 2018 17:36:04 +0200 Subject: [PATCH 2/2] merged and extended pr33 --- home.admin/00infoLCD.sh | 2 +- home.admin/00mainMenu.sh | 29 +++++--- home.admin/70initLND.sh | 15 ++-- home.admin/95switchMainTest.sh | 116 +++++++++++++++++++++++++++++++ home.admin/95switchNetEnv.sh | 52 -------------- home.admin/BBcloseAllChannels.sh | 53 ++++++++++++++ home.admin/BBfundWallet.sh | 13 +++- 7 files changed, 208 insertions(+), 72 deletions(-) create mode 100755 home.admin/95switchMainTest.sh delete mode 100755 home.admin/95switchNetEnv.sh create mode 100755 home.admin/BBcloseAllChannels.sh diff --git a/home.admin/00infoLCD.sh b/home.admin/00infoLCD.sh index b5daa34..45ad81e 100755 --- a/home.admin/00infoLCD.sh +++ b/home.admin/00infoLCD.sh @@ -87,7 +87,7 @@ if [ "$USER" = "pi" ]; then # RaspiBlitz is full Setup chain=$(sudo -u bitcoin ${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') - locked=$(sudo tail -n 1 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log | grep -c unlock) + locked=$(sudo tail -n 1 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log 2>/dev/null | grep -c unlock) if [ ${locked} -gt 0 ]; then # special case: LND wallet is locked ---> show unlock info diff --git a/home.admin/00mainMenu.sh b/home.admin/00mainMenu.sh index 8f15732..cbdf280 100755 --- a/home.admin/00mainMenu.sh +++ b/home.admin/00mainMenu.sh @@ -56,18 +56,24 @@ else # LOCK SCREEN MENU="!!! YOUR WALLET IS LOCKED !!!" - OPTIONS+=(X "Unlock your Lightning Wallet with 'lncli unlock'") + OPTIONS+=(U "Unlock your Lightning Wallet with 'lncli unlock'") else - # REGULAR MENU + chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') + switchOption="to MAINNET" + if [ "${chain}" = "test" ]; then + switchOption="back to TESTNET" + fi + + # REGULAR MENU OPTIONS+=(INFO "RaspiBlitz Status Screen" \ FUNDING "Fund your Wallet" \ CONNECT "Connect to a Peer" \ lnbalance "Detailed Wallet Balances" \ lnchannels "Lightning Channel List" \ - RECKLES "Console / Terminal"\ - REKT "Go Reckless! Switch Testnet/Mainnet") + SWITCH "Switch ${switchOption}" + X "Console / Terminal") fi @@ -130,14 +136,17 @@ case $CHOICE in read key ./00mainMenu.sh ;; - RECKLES) + SWITCH) + sudo ./95switchMainTest.sh + echo "Press ENTER to return to main menu." + read key + ./00mainMenu.sh + ;; + X) lncli -h echo "SUCH WOW come back with ./00mainMenu.sh" - ;; - REKT) # switch configs - sudo ./95switchNetEnv.sh; - ;; - X) # unlock + ;; + U) # unlock ./AAunlockLND.sh ./00mainMenu.sh ;; diff --git a/home.admin/70initLND.sh b/home.admin/70initLND.sh index 2c285ad..0acb142 100755 --- a/home.admin/70initLND.sh +++ b/home.admin/70initLND.sh @@ -4,6 +4,9 @@ echo "" # load network network=`cat .network` +# get chain +chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') + # verify that bitcoin is running echo "*** Checking ${network} ***" bitcoinRunning=$(sudo -u bitcoin ${network}-cli getblockchaininfo | grep -c blocks) @@ -71,8 +74,8 @@ if [ ${lndRunning} -eq 0 ]; then sudo cp /home/admin/assets/lnd.service /etc/systemd/system/lnd.service sudo systemctl enable lnd sudo systemctl start lnd - echo "Started LND .. waiting 30 seconds for init ..." - sleep 30 + echo "Started LND .. waiting 60 seconds for init ..." + sleep 60 fi ###### Check LND is running @@ -88,9 +91,8 @@ echo "OK - LND is running" echo "" ###### Instructions on Creating LND Wallet -setupStep=0 -setupStep=$(sudo cat "/home/admin/.setup") -if [ ${setupStep} -lt 65 ]; then +walletExists=$(sudo ls /mnt/hdd/lnd/data/chain/${network}/${cahin}net/wallet.db 2>/dev/null | grep wallet.db -c) +if [ ${walletExists} -eq 0 ]; then # setup state signals, that no wallet has been created yet dialog --backtitle "RaspiBlitz - LND Lightning Wallet" --msgbox " ${network} and Lighthing Services are installed. @@ -122,6 +124,7 @@ Press OK and follow the 'Helping Instructions'. # set SetupState to 75 (mid thru this process) echo "65" > /home/admin/.setup fi + echo "--> lets wait 30 seconds for LND to get ready" sleep 30 @@ -150,7 +153,6 @@ fi ###### Unlock Wallet (if needed) echo "*** Check Wallet Lock ***" -chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') locked=$(sudo tail -n 1 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log | grep -c unlock) if [ ${locked} -gt 0 ]; then echo "OK - Wallet is locked ... starting unlocking dialog" @@ -163,7 +165,6 @@ fi echo "" echo "*** Check LND Sync ***" item=0 -chain="$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain')" lndSyncing=$(sudo -u bitcoin lncli getinfo | jq -r '.synced_to_chain' | grep -c true) if [ ${lndSyncing} -eq 0 ]; then echo "OK - wait for LND to be synced" diff --git a/home.admin/95switchMainTest.sh b/home.admin/95switchMainTest.sh new file mode 100755 index 0000000..d4b9188 --- /dev/null +++ b/home.admin/95switchMainTest.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# based on pull request from vnnkl + +# load network +network=`cat .network` + +echo "" +echo "*** Switch between Testnet/Mainnet ***" + +# allow only on bitcoin network +if [ "${network}" = "bitcoin" ]; then + echo "Bitcoin network can be switched between testnet/mainnet ..." +else + echo "FAIL - Only Bitcoin Network can be switched between man/tast at the moment." + exit 1 +fi + +NETWORK_CONFIG="/home/bitcoin/.${network}/${network}.conf" +NETWORK_TEMPLATE="/home/admin/assets/${network}.conf" +LND_CONFIG="/home/bitcoin/.lnd/lnd.conf" +LND_TEMPLATE="/home/admin/assets/lnd.${network}.conf" +echo "NETWORK_CONFIG(${NETWORK_CONFIG})" +echo "LND_CONFIG(${LND_CONFIG})" +echo "NETWORK_TEMPLATE(${NETWORK_TEMPLATE})" +echo "LND_TEMPLATE(${LND_TEMPLATE})" + +# function to detect main/testnet +function isMainnet(){ + grep "^#testnet=1$" -q $NETWORK_CONFIG && return 1 + return 0 +} + +function switchToMainnet { + echo "switching to mainnet" + sed -i 's/^testnet=1/#testnet=1/g' $NETWORK_CONFIG + sed -i 's/^testnet=1/#testnet=1/g' $NETWORK_TEMPLATE + sed -i 's/^${network}.testnet=1/#${network}.testnet=1/g' $LND_CONFIG + sed -i 's/^#${network}.mainnet=1/${network}.mainnet=1/g' $LND_CONFIG + sed -i 's/^${network}.testnet=1/#${network}.testnet=1/g' $LND_TEMPLATE + sed -i 's/^#${network}.mainnet=1/${network}.mainnet=1/g' $LND_TEMPLATE + echo "OK switched to mainnet" +} + +function switchToTestnet { + echo "switching to testnet" + sed -i 's/^#testnet=1/testnet=1/g' $NETWORK_CONFIG + sed -i 's/^#testnet=1/testnet=1/g' $NETWORK_TEMPLATE + sed -i 's/^#${network}.testnet=1/${network}.testnet=1/g' $LND_CONFIG + sed -i 's/^${network}.mainnet=1/#${network}.mainnet=1/g' $LND_CONFIG + sed -i 's/^#${network}.testnet=1/${network}.testnet=1/g' $LND_TEMPLATE + sed -i 's/^${network}.mainnet=1/#${network}.mainnet=1/g' $LND_TEMPLATE + echo "OK switched to testnet" +} + +# LND Service +lndInstalled=$(systemctl status lnd.service | grep loaded -c) +if [ ${lndInstalled} -gt 0 ]; then + + echo "check for open channels" + openChannels=$(lncli listchannels 2>/dev/null | grep chan_id -c) + if [ ${openChannels} -gt 0 ]; then + echo "FAIL - You have still open channels and could loose funds !! - close those first with lncli closeallchannels" + exit 1 + else + echo "no open channels found" + fi + + echo "stopping lnd client" + systemctl stop lnd + sleep 4 + +else + echo "LND not running" +fi + +# NETWORK Service +networkInstalled=$(systemctl status ${network}d.service | grep loaded -c) +if [ ${networkInstalled} -gt 0 ]; then + echo "stopping bitcoind client" + systemctl stop bitcoind + sleep 4 +else + echo "Network ${network} not running" +fi + +# TURN THE SWITCH +isMainnet +if [ $? -eq 1 ]; then + echo "switching from mainnet to testnet" + switchToTestnet +else + echo "switching from testnet to mainnet" + switchToMainnet +fi + + +echo "copying over config to bitcoin user" +cp $NETWORK_CONFIG /home/admin/.${network}/ + +# restarting network +if [ ${networkInstalled} -gt 0 ]; then + + # start network + systemctl start bitcoind + echo "started ${network}d back up, giving it a 120 SECONDS to prepare" + sleep 120 + + # set setup info again + echo "60" > /home/admin/.setup + + # run again the complete LND init procedure + ./70initLND.sh + +else + echo "No starting of network, because it was not running before" +fi \ No newline at end of file diff --git a/home.admin/95switchNetEnv.sh b/home.admin/95switchNetEnv.sh deleted file mode 100755 index bf85e6a..0000000 --- a/home.admin/95switchNetEnv.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env bash -BITCOIN_CONFIG="/home/bitcoin/.bitcoin/bitcoin.conf" -LND_CONFIG="/home/bitcoin/.lnd/lnd.conf" -# function to detect main/testnet - -function isMainnet(){ - grep "^#testnet=1$" -q $BITCOIN_CONFIG && return 1 - return 0 -} - -function switchToMainnet { - echo "switching to mainnet" - sed -i 's/^testnet=1/#testnet=1/g' $BITCOIN_CONFIG && \ - sed -i 's/^bitcoin.testnet=1/#bitcoin.testnet=1/g' $LND_CONFIG && \ - sed -i 's/^#bitcoin.mainnet=1/bitcoin.mainnet=1/g' $LND_CONFIG - echo "switched to mainnet" -} - -function switchToTestnet { - echo "switching to testnet" - sed -i 's/^#testnet=1/testnet=1/g' $BITCOIN_CONFIG && \ - sed -i 's/^#bitcoin.testnet=1/bitcoin.testnet=1/g' $LND_CONFIG && \ - sed -i 's/^bitcoin.mainnet=1/#bitcoin.mainnet=1/g' $LND_CONFIG - echo "switched to testnet" -} - -# change to test: check both configs for commented out testnet arguments and mainnet -echo "stopping lnd client" -systemctl stop lnd -sleep 4 -echo "stopping bitcoind client" -systemctl stop bitcoind -sleep 4 -isMainnet -if [ $? -eq 1 ]; then - echo "switching from mainnet to testnet" - switchToTestnet -else - echo "switching from testnet to mainnet" - switchToMainnet -fi -echo "copying over config to bitcoin user" -cp $BITCOIN_CONFIG /home/admin/.bitcoin/ -systemctl start bitcoind -echo "started bitcoind back up, giving it a minute to come up" -sleep 60 -systemctl start lnd -echo "started lnd back up, giving it a minute, you will have to unlock your wallet" -sleep 60 -echo "finished config switch - started back up" -echo "you will still have to unlock your LND wallet" -./00mainMenu.sh \ No newline at end of file diff --git a/home.admin/BBcloseAllChannels.sh b/home.admin/BBcloseAllChannels.sh new file mode 100755 index 0000000..79c23f2 --- /dev/null +++ b/home.admin/BBcloseAllChannels.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# load network and chain info +network=`cat .network` +chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') + +command="lncli closeallchannels -f" + +clear +echo "***********************************" +echo "Closing All Channels (EXPERIMENTAL)" +echo "***********************************" +echo "" +echo "COMMAND LINE: " +echo $command +echo "" +echo "RESULT:" + +# PRECHECK) check if chain is in sync +chainInSync=$(lncli getinfo | grep '"synced_to_chain": true' -c) +if [ ${chainInSync} -eq 0 ]; then + command="" + result="FAIL PRECHECK - lncli getinfo shows 'synced_to_chain': false - wait until chain is sync " +fi + +# TODO PRECHECK) are any channels open at all + +# TODO PRECHECK) are there INACTIVE channels that would need a force close (and manual YES) +# remember that for info below + +# execute command +if [ ${#command} -gt 0 ]; then + result=$($command) +fi + +# on no result TODO: check if there is any result at all +if [ ${#result} -eq 0 ]; then + echo "Sorry something went wrong - thats unusual." + echo "" + exit 1 +fi + +# when result is available +echo "$result" + +# TODO parse out closing transactions and monitor those with blockchain for confirmations + +# TODO give final info - let user know if its now safe to update RaspiBlitz or change test/main +# ask to make sure user has list for seed words still safe +echo "" +echo "******************************" +echo "INFO" +echo "******************************" diff --git a/home.admin/BBfundWallet.sh b/home.admin/BBfundWallet.sh index c3a1eb5..0322248 100755 --- a/home.admin/BBfundWallet.sh +++ b/home.admin/BBfundWallet.sh @@ -2,7 +2,7 @@ # load network and chain info network=`cat .network` -chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo 2>/dev/null | jq -r '.chain') +chain=$(${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.chain') command="lncli newaddress np2wkh" @@ -16,8 +16,17 @@ echo $command echo "" echo "RESULT:" +# PRECHECK) check if chain is in sync +chainInSync=$(lncli getinfo | grep '"synced_to_chain": true' -c) +if [ ${chainInSync} -eq 0 ]; then + command="" + result="FAIL PRECHECK - lncli getinfo shows 'synced_to_chain': false - wait until chain is sync " +fi + # execute command -result=$($command) +if [ ${#command} -gt 0 ]; then + result=$($command) +fi # on no result if [ ${#result} -eq 0 ]; then