-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin-cli
More file actions
executable file
·295 lines (243 loc) · 6.3 KB
/
bitcoin-cli
File metadata and controls
executable file
·295 lines (243 loc) · 6.3 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env bash
set -euo pipefail
CLI_NAME="$(basename $0)"
BITCOIN_CONTAINER="bitcoind"
LND_CONTAINER="lnd"
LND_DIR="/home/lnd/.lnd"
RPC_USER=polaruser
RPC_PASS=polarpass
RPC_PORT=43782
BASE_COMMAND=(docker compose exec $BITCOIN_CONTAINER bitcoin-cli -rpcport=$RPC_PORT -rpcuser=$RPC_USER -rpcpassword=$RPC_PASS)
LNCLI_CMD=(docker compose exec -T $LND_CONTAINER lncli --lnddir "$LND_DIR" --network regtest)
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 [count] [--auto] Generate blocks (default: 1)
send [amount] [address] [-m N] Send to address and optionally mine N blocks (default: 1)
getInvoice <amount> Get a new BIP21 URI with a bech32 address
LND:
getinfo Show LND node info (for connectivity debugging)
holdinvoice [amount] [-m memo] Create a hold invoice
settleinvoice <preimage> Reveal a preimage and use it to settle the corresponding invoice
cancelinvoice <payment_hash> Cancels a currently open invoice
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
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 1 block if not specified
count=${1:-1}
# Default to 5 seconds interval for auto mode
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 "$count"
fi
exit
fi
# Send to a transaction
if [[ "$command" = "send" ]]; then
shift
mine_blocks=0
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-m|--mine)
# Check if next arg is a number
if [[ "${2:-}" =~ ^[0-9]+$ ]]; then
mine_blocks="$2"
shift 2
else
mine_blocks=1
shift
fi
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [ -z ${1+x} ]; then
read -p "Enter a BIP21 URI or address: " uri
echo
amount=$DEFAULT_AMOUNT
else
amount="$1"
if [ -z ${2+x} ]; then
read -p "Enter a BIP21 URI or address: " uri
echo
else
uri="$2"
fi
fi
protocol="${uri%%:*}"
if [[ "$protocol" == "bitcoin" ]]; then
# BIP21 URI: bitcoin:address?amount=X&message=Y&...
url_no_protocol="${uri#bitcoin:}"
# Extract address (everything before ?)
address="${url_no_protocol%%\?*}"
# Extract amount if present (look for amount= parameter specifically)
if [[ "$url_no_protocol" == *"?"* ]]; then
query_string="${url_no_protocol#*\?}"
if [[ "$query_string" =~ (^|&)amount=([0-9]*\.?[0-9]+) ]]; then
amount="${BASH_REMATCH[2]}"
fi
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"
# Mine blocks if requested
if [[ $mine_blocks -gt 0 ]]; then
"${BASE_COMMAND[@]}" -generate "$mine_blocks"
echo "Mined $mine_blocks block(s)"
fi
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 LND node info (LND)
if [[ "$command" = "getinfo" ]]; then
"${LNCLI_CMD[@]}" getinfo
exit
fi
# Create a hold invoice (LND)
if [[ "$command" = "holdinvoice" ]]; then
shift
amount=""
memo=""
while [[ $# -gt 0 ]]; do
case $1 in
-m|--memo)
memo="${2:-}"
shift 2
;;
-*)
echo "Unknown option $1"
exit 1
;;
*)
if [[ "$1" =~ ^[0-9]+$ ]]; then
amount="$1"
fi
shift
;;
esac
done
preimage=$(openssl rand -hex 32)
hash=$(echo -n "$preimage" | xxd -r -p | openssl dgst -sha256 | awk '{print $2}')
if [[ -n "$amount" ]]; then
result=$("${LNCLI_CMD[@]}" addholdinvoice --amt "$amount" --memo "$memo" "$hash" 2>&1) || true
else
result=$("${LNCLI_CMD[@]}" addholdinvoice --memo "$memo" "$hash" 2>&1) || true
fi
payment_request=$(echo "$result" | jq -r '.payment_request // empty' 2>/dev/null) || payment_request=""
if [ -z "$payment_request" ]; then
payment_request=$(echo "$result" | sed -n 's/.*"payment_request"[^:]*:[^"]*"\([^"]*\)".*/\1/p') || payment_request=""
fi
if [ -n "$payment_request" ]; then
echo "Hold invoice created:"
echo ""
echo "$payment_request"
echo ""
echo "Hash: $hash"
echo "Preimage: $preimage"
echo ""
if command -v pbcopy &>/dev/null; then
echo "$payment_request" | pbcopy
echo "Invoice copied to clipboard."
fi
else
echo "${result:-LND command produced no output}"
exit 1
fi
exit
fi
# Settle a hold invoice (LND)
if [[ "$command" = "settleinvoice" ]]; then
shift
if [ -z ${1+x} ]; then
echo "Usage: $CLI_NAME settleinvoice <preimage_hex>"
exit 1
fi
preimage="$1"
"${LNCLI_CMD[@]}" settleinvoice "$preimage"
exit
fi
# Cancel a hold invoice (LND)
if [[ "$command" = "cancelinvoice" ]]; then
shift
if [ -z ${1+x} ]; then
echo "Usage: $CLI_NAME cancelinvoice <payment_hash>"
exit 1
fi
"${LNCLI_CMD[@]}" cancelinvoice "$1"
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