-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitcoin-cli
More file actions
executable file
·493 lines (411 loc) · 12 KB
/
bitcoin-cli
File metadata and controls
executable file
·493 lines (411 loc) · 12 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/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:
getlninvoice [sats] [--msat N] Create Lightning invoice (supports msat precision)
getinfo Show LND node info (for connectivity debugging)
openchannel <node_id> [amount] Open channel from LND to node (default: 500000 sats)
payinvoice <invoice> [amount] Pay a Lightning invoice via LND
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 Lightning invoice (LND)
if [[ "$command" = "getlninvoice" ]]; then
shift
sats=""
msat=""
memo=""
while [[ $# -gt 0 ]]; do
case "$1" in
--msat)
msat="${2:-}"
shift 2
;;
--sats)
sats="${2:-}"
shift 2
;;
-m|--memo)
memo="${2:-}"
shift 2
;;
-*)
echo "Unknown option $1"
echo "Usage: $CLI_NAME getlninvoice [sats] [--msat N] [--memo TEXT]"
exit 1
;;
*)
# Positional value is treated as sats if numeric.
if [[ "$1" =~ ^[0-9]+$ ]]; then
sats="$1"
shift
else
echo "Invalid amount '$1' (expected integer sats)"
exit 1
fi
;;
esac
done
if [[ -n "$sats" && -n "$msat" ]]; then
echo "Use either sats or --msat, not both."
exit 1
fi
if [[ -n "$msat" ]]; then
echo "→ Creating Lightning invoice (${msat} msat)..."
result=$("${LNCLI_CMD[@]}" addinvoice --amt_msat "$msat" --memo "$memo" 2>&1) || true
elif [[ -n "$sats" ]]; then
echo "→ Creating Lightning invoice (${sats} sats)..."
result=$("${LNCLI_CMD[@]}" addinvoice --amt "$sats" --memo "$memo" 2>&1) || true
else
echo "→ Creating amountless Lightning invoice..."
result=$("${LNCLI_CMD[@]}" addinvoice --memo "$memo" 2>&1) || true
fi
payment_request=$(echo "$result" | jq -r '.payment_request // empty' 2>/dev/null) || payment_request=""
if [ -z "$payment_request" ]; then
echo "${result:-LND command produced no output}"
exit 1
fi
echo ""
echo "$payment_request"
echo ""
if command -v pbcopy &>/dev/null; then
echo "$payment_request" | pbcopy
echo "Invoice copied to clipboard."
fi
exit
fi
# Open channel from LND to a node
if [[ "$command" = "openchannel" ]]; then
shift
node_id="${1:-}"
amount="${2:-500000}"
if [ -z "$node_id" ]; then
echo "Usage: $CLI_NAME openchannel <node_id> [amount_sats]"
echo ""
echo " node_id: app's Lightning node ID (Settings > Advanced > Lightning Node Info)"
echo " amount: channel size in sats (default: 500000)"
exit 1
fi
# Check peer connection
echo "→ Checking peer connection..."
peer_count=$("${LNCLI_CMD[@]}" listpeers 2>/dev/null | jq "[.peers[] | select(.pub_key==\"$node_id\")] | length")
if [ "$peer_count" = "0" ]; then
lnd_pubkey=$("${LNCLI_CMD[@]}" getinfo 2>/dev/null | jq -r '.identity_pubkey')
echo "✗ Node is not connected as a peer."
echo ""
echo " Paste this in the app (Settings > Advanced > Channels > Add Connection):"
echo " ${lnd_pubkey}@0.0.0.0:9735"
echo ""
echo " Then re-run this command."
exit 1
fi
echo "✓ Peer connected"
# Fund LND if needed
balance=$("${LNCLI_CMD[@]}" walletbalance 2>/dev/null | jq -r '.confirmed_balance')
echo "→ LND on-chain balance: $balance sats"
if [ "$balance" -lt "$amount" ]; then
echo "→ Funding LND..."
lnd_addr=$("${LNCLI_CMD[@]}" newaddress p2wkh 2>/dev/null | jq -r '.address')
"${BASE_COMMAND[@]}" -named sendtoaddress address="$lnd_addr" amount=1 fee_rate=25 > /dev/null
"${BASE_COMMAND[@]}" -generate 6 > /dev/null
echo "✓ Funded LND with 1 BTC"
sleep 2
fi
# Open channel
echo "→ Opening ${amount} sat channel to ${node_id:0:20}..."
result=$("${LNCLI_CMD[@]}" openchannel --node_key "$node_id" --local_amt "$amount" --private 2>&1) || {
echo "✗ Failed: $result"
exit 1
}
txid=$(echo "$result" | jq -r '.funding_txid // empty' 2>/dev/null)
if [ -z "$txid" ]; then
echo "✗ Failed: $result"
exit 1
fi
echo "✓ Channel opened, funding txid: $txid"
# Mine and wait
echo "→ Mining 6 blocks..."
"${BASE_COMMAND[@]}" -generate 6 > /dev/null
echo "✓ Mined 6 blocks"
echo "→ Waiting for channel to become active..."
for i in $(seq 1 30); do
sleep 2
active=$("${LNCLI_CMD[@]}" listchannels --peer "$node_id" --active_only 2>/dev/null | jq '.channels | length')
if [ "$active" != "0" ]; then
echo "✓ Channel is active!"
break
fi
if [ $((i % 5)) -eq 0 ]; then echo " still waiting... ($i)"; fi
done
if [ "$active" = "0" ]; then
echo "⚠ Channel not active yet. May need more time or app needs to sync."
fi
# Summary
echo ""
echo "══════════════════════════════════"
"${LNCLI_CMD[@]}" channelbalance 2>/dev/null | jq -r '" LND outbound: \(.local_balance.sat) sats (can pay app)\n LND inbound: \(.remote_balance.sat) sats (can receive from app)"'
echo "══════════════════════════════════"
exit
fi
# Pay a Lightning invoice via LND
if [[ "$command" = "payinvoice" ]]; then
shift
invoice="${1:-}"
amount="${2:-}"
if [ -z "$invoice" ]; then
echo "Usage: $CLI_NAME payinvoice <invoice> [amount_sats]"
exit 1
fi
if [ -n "$amount" ]; then
echo "→ Paying invoice via LND (${amount} sats)..."
result=$("${LNCLI_CMD[@]}" payinvoice --force --amt "$amount" "$invoice" 2>&1)
else
echo "→ Paying invoice via LND..."
result=$("${LNCLI_CMD[@]}" payinvoice --force "$invoice" 2>&1)
fi
status=$(echo "$result" | grep -i "status" | head -1)
if echo "$result" | grep -qi "SUCCEEDED"; then
echo "✓ Payment succeeded"
echo "$result" | grep -i "payment_hash\|payment_preimage" | head -2
else
echo "✗ Payment failed"
echo "$result"
exit 1
fi
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