<p>My thoughts, in no particular order:</p>

<ul>
<li><p>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.</p></li>
<li><p>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.</p></li>
<li><p>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.</p></li>
<li><p>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</p>

<pre><code>THRESHOLD=$(echo "$MINIMUM_TO_KEEP 0.01 * p" | dc)
</code></pre></li>
<li><p>I don't think that Bash can compare non-integers.</p>

<pre><code>if [ "$sendamount" &gt; 0 ]; then
</code></pre>

<p>I'm not sure what this will do.</p>

<p><a href="https://stackoverflow.com/questions/11237794/how-to-compare-two-decimal-numbers-in-bash-awk">https://stackoverflow.com/questions/11237794/how-to-compare-two-decimal-numbers-in-bash-awk</a></p></li>
<li><p>Include this line before the rest of your script:</p>

<pre><code>"$BITCOINCLI" getblocktemplate || exit
</code></pre>

<p>This will cause your script to bail out if your node is still synchronizing.</p></li>
<li><p>What's with this whitespace?</p>

<pre><code>sendamount=$(echo "            $balance
</code></pre></li>
</ul>

CC BY-SA 3.0
2015-05-15T08:59:12.643Z
Isn't auto replenishment of a hot wallet from a cold wallet defeating the purpose of a cold wallet?
Wizard Of Ozzie
CC BY-SA 3.0
2015-05-15T16:47:14.707Z
@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
CC BY-SA 3.0
2015-05-14T17:21:08.950Z
2015-05-14T17:21:08.950Z
2017-05-23T12:34:59.703Z
Nick ODell
<p>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? </p>

<pre><code>#!/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 &gt; "$tempOutput" 2&gt; "$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" &gt; 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 &gt; "$tempOutput" 2&gt; "$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"
</code></pre>

<bash>
Here is bash script that auto transfers from hot to cold wallet upon threshhold met. Do you see issues?
CryptoPanda
# Here is bash script that auto transfers from hot to cold wallet upon threshhold met. Do you see issues?

(Asked by: username_0 on 2015-05-14)

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"

```

**Tags:** bash


---

## 1 answers

### Answer by username_1 on 2015-05-14. Score: 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

  ```

> **Comments:**
> **username_2**: Isn't auto replenishment of a hot wallet from a cold wallet defeating the purpose of a cold wallet?  
> **username_1**: @username_2 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.  

---

