-
Notifications
You must be signed in to change notification settings - Fork 22
/
bitcoin-cli
executable file
·165 lines (130 loc) · 3.25 KB
/
bitcoin-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
set -euo pipefail
CLI_NAME="$(basename $0)"
CLI_DIR="$(dirname "$(readlink -f "$0")")"
CONTAINER="bitcoind"
RPC_USER=polaruser
RPC_PASS=polarpass
RPC_PORT=43782
BASE_COMMAND=(docker compose exec $CONTAINER bitcoin-cli -rpcport=$RPC_PORT -rpcuser=$RPC_USER -rpcpassword=$RPC_PASS)
DEFAULT_AMOUNT=0.001
show_help() {
cat <<EOF
Shortcuts for bitcoin-cli.
Usage: ${CLI_NAME} <command> [options]
Flags:
-h, --help Show this help message
Commands:
fund <amount> Fund the wallet
mine <amount> [--auto] Generate a number of blocks
send <address> <amount> Send to address or BIP21 URI
getInvoice <amount> Get a new BIP21 URI with a bech32 address
EOF
}
if [ -z ${1+x} ]; then
command=""
else
command="$1"
fi
# Fund the wallet
if [[ "$command" = "fund" ]]; then
"${BASE_COMMAND[@]}" -generate 101
exit
fi
# Mine some blocks
if [[ "$command" = "mine" ]]; then
shift
if [ -z ${1+x} ]; then
echo "Specify the number of blocks to generate."
echo "Usage: \`$CLI_NAME $command <amount>\`"
exit 1
fi
POSITIONAL_ARGS=()
auto=false
while [[ $# -gt 0 ]]; do
case $1 in
-l | --auto)
auto=true
shift
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
# default to 5 seconds
interval=${2:-5}
if $auto; then
printf "Generating a block every $interval seconds. Press [CTRL+C] to stop...\n\n"
while true; do
"${BASE_COMMAND[@]}" -generate 1
sleep $interval
done
else
"${BASE_COMMAND[@]}" -generate "$@"
fi
exit
fi
# Send to a transaction
if [[ "$command" = "send" ]]; then
shift
if [ -z ${1+x} ]; then
read -p "Enter a BIP21 URI or address: " uri
echo
else
uri="$1"
fi
if [ -z ${2+x} ]; then
amount=$DEFAULT_AMOUNT
else
amount="$2"
fi
protocol=$(echo "${uri%%:*}")
if [[ "$protocol" == "bitcoin" ]]; then
# BIP21 URI
# Remove the protocol
url_no_protocol=$(echo "${uri/$protocol/}" | cut -d":" -f2-)
address=$(echo $url_no_protocol | grep "?" | cut -d"/" -f1 | rev | cut -d"?" -f2- | rev || echo $url_no_protocol)
uri_amount=$(echo $url_no_protocol | cut -d'?' -f 2 | cut -d'=' -f 2 | cut -d'&' -f 1)
if echo "$uri_amount" | grep -qE '^[0-9]*\.?[0-9]+$'; then
amount=$uri_amount
fi
else
address=$uri
fi
tx_id=$("${BASE_COMMAND[@]}" -named sendtoaddress address="$address" amount="$amount" fee_rate="25")
echo "Sent $amount BTC to $address"
echo "Transaction ID: $tx_id"
exit
fi
# Get a new BIP21 URI
if [[ "$command" = "getInvoice" ]]; then
shift
if [ -z ${1+x} ]; then
amount=$DEFAULT_AMOUNT
else
amount="$1"
fi
address=$("${BASE_COMMAND[@]}" getnewaddress -addresstype bech32 | tr -d '\r')
uri="bitcoin:$address?amount=$amount"
# print URI
echo $uri
# copy to clipboard (MacOS)
echo $uri | pbcopy
echo "Copied to clipboard."
exit
fi
# Show usage information for this CLI
if [[ "$command" = "--help" ]] || [[ "$command" = "-h" ]]; then
show_help
exit
fi
# If no command specified pass all args straight to bitcoin-cli
"${BASE_COMMAND[@]}" "$@"
exit