1

The idea is to run this via cron job at some interval and have the excess of funds transfered to cold wallet address. So, I thought it will be easier to open source it so I can give it to the community and have it reviewed at the same time by more eyes. It hasn't been tested yet. So, any issues you see?

#!/usr/bin/bash
BITCOINCLI="/set/path/to/bitcoin-cli"
MINIMUM_TO_KEEP=0.1
RECEIVERS_ADDRESS=xxxyyyyzzzbitcoinaddress

echo bitcoin-cli executable is: $BITCOINCLI

tempOutput=$(mktemp)
tempOutputErr=$(mktemp)
#echo "$tempOutput"
#echo "$tempOutputErr"
"$BITCOINCLI" getbalance > "$tempOutput" 2> "$tempOutputErr"

#echo "output file:"
#cat "$tempOutput" 
#echo "Error file:"
#cat "$tempOutputErr"


outputsize=$(wc -c "$tempOutput" | cut -f 1 -d ' ')
if [ $outputsize -ge 1 ]; then
balance=$(cat ${tempOutput})
else
balance=XYZ
echo "Error: Could not connect to server. Or some other error while tring to find balance. " 
rm "$tempOutput" "$tempOutputErr"
exit
fi

echo Balance is "$balance"

sendamount=$(echo "            $balance  $MINIMUM_TO_KEEP  - p" | tr '\n\r' ' ' | dc   )

#echo comp = "$comp"  x
if [ "$sendamount" > 0 ]; then
    echo "Balance is greater than the amount to be kept, which is $MINIMUM_TO_KEEP"
    echo Sending "$sendamount" BTC to the address "$RECEIVERS_ADDRESS"
    "$BITCOINCLI" sendtoaddress $RECEIVERS_ADDRESS $sendamount > "$tempOutput" 2> "$tempOutputErr"
    outputsize=$(wc -c "$tempOutputErr" | cut -f 1 -d ' ')
    if [ $outputsize -ge 1 ]; then
        echo Error while executing sendtoaddress. Could not send the amount. The error is:
        cat "$tempOutputErr"
    fi
else
    echo "Balance is less than $MINIMUM_TO_KEEP"
fi




rm "$tempOutput" "$tempOutputErr"
3

My thoughts, in no particular order:

  • Consider if you need a hot wallet at all. If you don't need to send funds, just accept them, you could probably use BIP32 to generate addresses. Downside is that this way is more complicated.

  • You should probably bail out of your script if bitcoin-cli produces any errors at all. If your Bitcoin node can't figure out its balance, then it won't be able to send a transaction.

  • It seems like you detect whether bitcoin-cli failed by 1) checking if it produced output, then 2) checking to make sure that it produced no errors. Why not just use its return codes? bitcoin-cli passes on any error codes.

  • Your script will tell Bitcoin to send to your cold wallet if you have any amount of extra Bitcoin, even a single satoshi. So, if some miscreant sent you an extra satoshi every time your script ran, it would generate a transaction and pay a fee. You should have a higher threshold, like

    THRESHOLD=$(echo "$MINIMUM_TO_KEEP 0.01 * p" | dc)
    
  • I don't think that Bash can compare non-integers.

    if [ "$sendamount" > 0 ]; then
    

    I'm not sure what this will do.

    https://stackoverflow.com/questions/11237794/how-to-compare-two-decimal-numbers-in-bash-awk

  • Include this line before the rest of your script:

    "$BITCOINCLI" getblocktemplate || exit
    

    This will cause your script to bail out if your node is still synchronizing.

  • What's with this whitespace?

    sendamount=$(echo "            $balance
    
2
  • Isn't auto replenishment of a hot wallet from a cold wallet defeating the purpose of a cold wallet? – Wizard Of Ozzie May 15 '15 at 8:59
  • @WizardOfOzzie It's the opposite - hot to cold. And not necessarily. If you have some way of checking that the withdrawals from the hot wallet were caused by legitimate customer activity, automatic hot wallet refill could be secure. – Nick ODell May 15 '15 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.