-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbitcoin-cli.sh
executable file
·76 lines (68 loc) · 2.06 KB
/
bitcoin-cli.sh
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
#!/bin/bash
# Helper to connect to the running bitcoind (by default running regtest).
# Function to show usage instructions
show_usage() {
cat <<EOF
Usage: bitcoin-cli-helper COMMAND [ARGS]
Commands:
getnewaddress Generate a new address
generatetoaddress BLOCKS ADDRESS Generate new blocks and send the reward to the specified address
generate NUM_BLOCKS Generate a specified number of blocks
settxfee FEE Set the transaction fee for this network
sendtoaddress ADDRESS AMOUNT Send funds to an address
EOF
}
# Check if a valid command is provided
validate_command() {
local valid_commands=("getnewaddress" "generatetoaddress" "generate" "settxfee" "sendtoaddress")
local command=$1
for valid_cmd in "${valid_commands[@]}"; do
if [ "$valid_cmd" = "$command" ]; then
return 0
fi
done
return 1
}
# Validate the number of arguments
if [ "$#" -lt 1 ]; then
echo "Error: Missing command."
show_usage
exit 1
fi
# Check if the provided command is valid
if ! validate_command "$1"; then
echo "Error: Invalid command."
show_usage
exit 1
fi
# Execute the command with docker
if [ "$1" = "generate" ]; then
if [ "$#" -lt 2 ]; then
echo "Error: Missing number of blocks to generate."
show_usage
exit 1
fi
# Execute it right away, as we must pass `-`
docker exec prestashop_bitcoind bitcoin-cli -datadir="/data" -generate "$2"
exit 0
elif [ "$1" = "generatetoaddress" ]; then
if [ "$#" -lt 3 ]; then
echo "Error: Missing blocks to mine and/or address."
show_usage
exit 1
fi
elif [ "$1" = "settxfee" ]; then
if [ "$#" -lt 2 ]; then
echo "Error: Missing transaction fee value."
show_usage
exit 1
fi
elif [ "$1" = "sendtoaddress" ]; then
if [ "$#" -lt 3 ]; then
echo "Error: Missing address and/or amount to send."
show_usage
exit 1
fi
fi
# Execute the command with docker
docker exec prestashop_bitcoind bitcoin-cli -datadir="/data" "$@"