2021-10-29 15:51:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Qortal defaults
|
|
|
|
host="localhost"
|
|
|
|
port=12393
|
|
|
|
|
|
|
|
if [ -z "$*" ]; then
|
|
|
|
echo "Usage:"
|
|
|
|
echo
|
|
|
|
echo "Host/update data:"
|
2021-11-12 17:42:21 +00:00
|
|
|
echo "qdata [PUT/PATCH] [service] [name] [dirpath] <identifier>"
|
2021-10-29 15:51:41 +00:00
|
|
|
echo
|
|
|
|
echo "Fetch data:"
|
2021-11-12 17:42:21 +00:00
|
|
|
echo "qdata GET [service] [name] <identifier-or-default> <filepath-or-default> <rebuild>"
|
|
|
|
echo
|
|
|
|
echo "Notes:"
|
2021-11-12 18:22:10 +00:00
|
|
|
echo "- When requesting a resource, please use 'default' to indicate a file with no identifier."
|
2021-11-12 17:42:21 +00:00
|
|
|
echo "- The same applies when specifying the relative path to a file within the data structure; use 'default'"
|
|
|
|
echo " to indicate a single file resource."
|
2021-10-29 15:51:41 +00:00
|
|
|
echo
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
method=$1
|
|
|
|
service=$2
|
|
|
|
name=$3
|
|
|
|
|
|
|
|
if [ -z "${method}" ]; then
|
|
|
|
echo "Error: missing method"; exit
|
|
|
|
fi
|
|
|
|
if [ -z "${service}" ]; then
|
|
|
|
echo "Error: missing service"; exit
|
|
|
|
fi
|
|
|
|
if [ -z "${name}" ]; then
|
|
|
|
echo "Error: missing name"; exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if [[ "${method}" == "PUT" || "${method}" == "PATCH" ]]; then
|
|
|
|
directory=$4
|
2021-11-12 17:42:21 +00:00
|
|
|
identifier=$5
|
2021-10-29 15:51:41 +00:00
|
|
|
|
|
|
|
if [ -z "${directory}" ]; then
|
|
|
|
echo "Error: missing directory"; exit
|
|
|
|
fi
|
2021-10-29 16:24:22 +00:00
|
|
|
if [ -z "${QORTAL_PRIVKEY}" ]; then
|
|
|
|
echo "Error: missing private key. Set it by running: export QORTAL_PRIVKEY=privkeyhere"; exit
|
2021-10-29 15:51:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Creating transaction - this can take a while..."
|
2021-11-12 17:42:21 +00:00
|
|
|
tx_data=$(curl --silent --insecure -X ${method} "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}" -d "${directory}")
|
2021-10-29 15:51:41 +00:00
|
|
|
if [[ "${tx_data}" == *"error"* || "${tx_data}" == *"ERROR"* ]]; then
|
|
|
|
echo "${tx_data}"; exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Signing..."
|
2021-10-29 16:24:22 +00:00
|
|
|
signed_tx_data=$(curl --silent --insecure -X POST "http://${host}:${port}/transactions/sign" -H "Content-Type: application/json" -d "{\"privateKey\":\"${QORTAL_PRIVKEY}\",\"transactionBytes\":\"${tx_data}\"}")
|
2021-10-29 15:51:41 +00:00
|
|
|
if [[ "${signed_tx_data}" == *"error"* || "${signed_tx_data}" == *"ERROR"* ]]; then
|
|
|
|
echo "${signed_tx_data}"; exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Broadcasting..."
|
|
|
|
success=$(curl --silent --insecure -X POST "http://${host}:${port}/transactions/process" -H "Content-Type: text/plain" -d "${signed_tx_data}")
|
|
|
|
if [[ "${success}" == "true" ]]; then
|
|
|
|
echo "Transaction broadcast successfully"
|
|
|
|
else
|
|
|
|
echo "Error when broadcasting transaction. Please try again."
|
2021-10-29 16:26:40 +00:00
|
|
|
echo "Response: ${success}"
|
2021-10-29 15:51:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ "${method}" == "GET" ]]; then
|
2021-11-12 17:42:21 +00:00
|
|
|
identifier=$4
|
|
|
|
filepath=$5
|
|
|
|
rebuild=$6
|
2021-10-29 15:51:41 +00:00
|
|
|
|
|
|
|
if [ -z "${rebuild}" ]; then
|
|
|
|
rebuild="false"
|
|
|
|
fi
|
|
|
|
|
2021-11-12 17:42:21 +00:00
|
|
|
# Handle default
|
|
|
|
if [[ "${identifier}" == "default" ]]; then
|
|
|
|
identifier=""
|
|
|
|
fi
|
|
|
|
if [[ "${filepath}" == "default" ]]; then
|
|
|
|
filepath=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We use a different API depending on whether or not an identifier is supplied
|
|
|
|
if [ -n "${identifier}" ]; then
|
|
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}?rebuild=${rebuild}&filepath=${filepath}")
|
|
|
|
else
|
|
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}?rebuild=${rebuild}&filepath=${filepath}")
|
|
|
|
fi
|
|
|
|
|
2021-10-29 17:58:59 +00:00
|
|
|
if [ -z "${response}" ]; then
|
|
|
|
echo "Empty response from ${host}:${port}"
|
|
|
|
fi
|
2021-10-29 15:51:41 +00:00
|
|
|
if [[ "${response}" == *"error"* || "${response}" == *"ERROR"* ]]; then
|
|
|
|
echo "${response}"; exit
|
|
|
|
fi
|
|
|
|
|
2021-10-29 17:52:05 +00:00
|
|
|
echo "${response}"
|
2021-10-29 15:51:41 +00:00
|
|
|
|
|
|
|
fi
|