Files
mines/rin/wallet/cmd/rpc_call.sh
2025-09-30 01:08:20 +03:00

89 lines
2.1 KiB
Bash

#!/bin/bash
# General script for RIN RPC calls
# Usage: ./rpc_call.sh <method> [param1] [param2] ... [rpc_user] [rpc_password] [rpc_host] [rpc_port]
if [ $# -lt 1 ]; then
echo "Usage: $0 <method> [param1] [param2] ... [rpc_user] [rpc_password] [rpc_host] [rpc_port]"
echo "Examples:"
echo " $0 getinfo"
echo " $0 getnewaddress myaccount"
echo " $0 listtransactions \"*\" 10"
echo " $0 gettransaction txid"
exit 1
fi
METHOD=$1
shift
# Parse parameters - last 4 optional args are RPC connection details
PARAMS=()
RPC_USER="rinrpc"
RPC_PASSWORD="password"
RPC_HOST="localhost"
RPC_PORT="8332"
# Check if last 4 args are RPC connection details
if [ $# -ge 4 ]; then
# Assume last 4 are RPC details
RPC_PORT="${@: -1}"
RPC_HOST="${@: -2}"
RPC_PASSWORD="${@: -3}"
RPC_USER="${@: -4}"
PARAMS=("${@:1:$#-4}")
else
PARAMS=("$@")
fi
# Build JSON parameters array
PARAMS_JSON=""
if [ ${#PARAMS[@]} -gt 0 ]; then
PARAMS_JSON="["
for param in "${PARAMS[@]}"; do
# Try to parse as number, otherwise treat as string
if [[ $param =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
PARAMS_JSON="$PARAMS_JSON$param,"
else
PARAMS_JSON="$PARAMS_JSON\"$param\","
fi
done
PARAMS_JSON="${PARAMS_JSON%,}]"
else
PARAMS_JSON="[]"
fi
# JSON-RPC request
RPC_REQUEST='{
"jsonrpc": "2.0",
"id": "'"$METHOD"'",
"method": "'"$METHOD"'",
"params": '"$PARAMS_JSON"'
}'
echo "Calling RPC method: $METHOD"
echo "Parameters: $PARAMS_JSON"
# Make the RPC call
RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d "$RPC_REQUEST" \
"http://$RPC_HOST:$RPC_PORT")
# Check for errors
ERROR=$(echo "$RESPONSE" | jq -r '.error' 2>/dev/null)
if [ "$ERROR" != "null" ] && [ -n "$ERROR" ]; then
echo "Error: $(echo "$ERROR" | jq -r '.message')"
exit 1
fi
# Display result
RESULT=$(echo "$RESPONSE" | jq -r '.result' 2>/dev/null)
if [ "$RESULT" != "null" ]; then
echo "Result:"
echo "$RESPONSE" | jq '.result'
else
echo "Response:"
echo "$RESPONSE"
fi