Skip to content

Instantly share code, notes, and snippets.

@d6n13l0l1v3r
Last active December 21, 2023 13:44
Show Gist options
  • Save d6n13l0l1v3r/4a0fe01889e2d554df6f605fb159998a to your computer and use it in GitHub Desktop.
Save d6n13l0l1v3r/4a0fe01889e2d554df6f605fb159998a to your computer and use it in GitHub Desktop.
update rpc password for those services than don't support rpcauth (.cookiesfile)
#!/bin/bash
script_name=$(basename "$0")
service=$1
log() {
logger -t "$script_name" "$1"
}
#start log messages
log "Script $script_name started with service: $service"
#Use awk to extract the user value from .bitcoin/.cookie
user=$(awk -F':' '{print $1}' /data/bitcoin/.cookie)
# Use awk to extract the password value from .bitcoin/.cookie
password=$(awk -F':' '{print $2}' /data/bitcoin/.cookie)
if [ "$service" = "mempool" ]; then
# Update the value in mempool-config.json using jq
jq --arg new_password "$password" --arg new_user "$user" '.CORE_RPC.PASSWORD = $new_password | .SECOND_CORE_RPC.PASSWORD = $new_password |.CORE_RPC.USERNAME= $new_user | .SECOND_CORE_RPC.USERNAME = $new_user' ~/mempool/backend/mempool-config.json > tmp.json && mv tmp.json ~/mempool/backend/mempool-config.json
# Set the file permissions to 600
chmod 600 ~/mempool/backend/mempool-config.json
elif [ "$service" = "teos" ]; then
# Use sed to update the value in .teos/teos.toml
sed -i "s/btc_rpc_user = \".*\"/btc_rpc_user = \"$user\"/" ~/.teos/teos.toml
sed -i "s/btc_rpc_password = \".*\"/btc_rpc_password = \"$password\"/" ~/.teos/teos.toml
# Set the file permissions to 600
chmod 600 ~/.teos/teos.toml
elif [ "$service" = "ckpool" ]; then
# Update the value in ckpool.conf using jq
jq --arg new_password "$password" --arg new_user "$user" '.btcd[0].pass = $new_password | .btcd[0].auth = $new_user' ~/ckpool-solo/ckpool.conf > tmp.conf && mv tmp.conf ~/ckpool-solo/ckpool.conf
# Set the file permissions to 600
chmod 600 ~/ckpool-solo/ckpool.conf
else
log "No service match"
exit 1
fi
# Check if jq and mv commands were successful
if [ $? -ne 0 ]; then
log "Error updating Bitcoin RPC password for ${service}"
exit 1
fi
log "Bitcoin RPC Password update successful for ${service}"
exit 0
@d6n13l0l1v3r
Copy link
Author

d6n13l0l1v3r commented Dec 20, 2023

the user for each service need to be part of bitcoin group

@d6n13l0l1v3r
Copy link
Author

d6n13l0l1v3r commented Dec 20, 2023

sample service definition

# MiniBolt: systemd unit for CKPool
# /etc/systemd/system/CKPool.service

[Unit]
Description=CK Pool
After=bitcoind.service

[Service]
WorkingDirectory=/home/pool/ckpool-solo
ExecStartPre=/bin/sleep 30 ; /bin/bash /home/pool/update_rpcpassword.sh ckpool
ExecStart=/home/pool/ckpool-solo/src/ckpool -k -l 5  -B -c /home/pool/ckpool-solo/ckpool.conf
User=pool

# Restart on failure but no more than default times (DefaultStartLimitBurst=5) every 10 minutes (600 seconds). Otherwise stop
Restart=on-failure
RestartSec=600

# Hardening measures
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target

@d6n13l0l1v3r
Copy link
Author

d6n13l0l1v3r commented Dec 20, 2023

sample output in journalctl

Dec 20 20:29:57 minibolt systemd[1]: Starting CK Pool...
Dec 20 20:30:27 minibolt update_rpcpassword.sh[1465077]: Script update_rpcpassword.sh started with service: ckpool
Dec 20 20:30:27 minibolt update_rpcpassword.sh[1465082]: Bitcoin RPC Password update successful for ckpool
Dec 20 20:30:27 minibolt systemd[1]: Started CK Pool.
Dec 20 20:30:27 minibolt ckpool[1465084]: [2023-12-20 20:30:27.945] ckpool generator starting
Dec 20 20:30:27 minibolt ckpool[1465084]: [2023-12-20 20:30:27.945] ckpool stratifier starting
Dec 20 20:30:27 minibolt ckpool[1465084]: [2023-12-20 20:30:27.945] ckpool connector starting
Dec 20 20:30:27 minibolt ckpool[1465084]: [2023-12-20 20:30:27.947] ckpool connector ready
Dec 20 20:30:28 minibolt ckpool[1465084]: [2023-12-20 20:30:28.313] ckpool generator ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment