start-bitcoind.sh
1 #!/usr/bin/env bash 2 3 # exit from script if error was raised. 4 set -e 5 6 # error function is used within a bash function in order to send the error 7 # message directly to the stderr output and exit. 8 error() { 9 echo "$1" > /dev/stderr 10 exit 0 11 } 12 13 # return is used within the bash function in order to return the value. 14 return() { 15 echo "$1" 16 } 17 18 # set_default function gives the ability to move the setting of default 19 # env variable from docker file to the script thereby giving the ability to the 20 # user to override it during container start. 21 set_default() { 22 # docker initialized env variables with blank string and we can't just 23 # use -z flag as usually. 24 BLANK_STRING='""' 25 26 VARIABLE="$1" 27 DEFAULT="$2" 28 29 if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then 30 31 if [ -z "$DEFAULT" ]; then 32 error "You should specify default variable" 33 else 34 VARIABLE="$DEFAULT" 35 fi 36 fi 37 38 return "$VARIABLE" 39 } 40 41 # Set default variables if needed. 42 43 # This password is for testing and it is equivalent to `devpass`. 44 # It is generated from (Note: It is fully deterministic, yet the random salt 45 # generated by the script ensures that the resulting hash is always unique.): 46 # https://github.com/bitcoin/bitcoin/blob/master/share/rpcauth/rpcauth.py 47 DEFAULT_PASSWORD='22d3dd348a42d7f040487887b0ea6cc7$79ce831819539c78537884f85b65a09e15b079d79eb8f99447ea9b0a58fa66a6' 48 49 RPCAUTH=$(set_default "$RPCAUTH" "devuser:$DEFAULT_PASSWORD") 50 NETWORK=$(set_default "$NETWORK" "regtest") 51 DEBUG=$(set_default "$BITCOIND_DEBUG" "1") 52 53 PARAMS="" 54 if [ "$NETWORK" != "mainnet" ]; then 55 PARAMS="-$NETWORK" 56 fi 57 58 PARAMS=$(echo $PARAMS \ 59 "-debug"="$DEBUG" \ 60 "-rpcauth"="$RPCAUTH" \ 61 "-datadir"="/data" \ 62 "-debuglogfile"="/data/debug.log" \ 63 "-rpcbind"="0.0.0.0" \ 64 "-rpcallowip"="0.0.0.0/0" \ 65 "-zmqpubrawblock"="tcp://0.0.0.0:28332" \ 66 "-zmqpubrawtx"="tcp://0.0.0.0:28333" \ 67 "-txindex" 68 ) 69 70 # Add user parameters to the command. 71 PARAMS="$PARAMS $@" 72 73 # Print command and start bitcoin node. 74 echo "Command: bitcoind $PARAMS" 75 exec bitcoind $PARAMS