2021-10-29 15:51:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Qortal defaults
|
|
|
|
host="localhost"
|
2022-08-12 12:38:03 +00:00
|
|
|
port=12391
|
2021-10-29 15:51:41 +00:00
|
|
|
|
|
|
|
if [ -z "$*" ]; then
|
|
|
|
echo "Usage:"
|
|
|
|
echo
|
|
|
|
echo "Host/update data:"
|
2021-11-17 18:57:46 +00:00
|
|
|
echo "qdata POST [service] [name] PATH [dirpath] <identifier>"
|
|
|
|
echo "qdata POST [service] [name] STRING [data-string] <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
|
|
|
|
|
2022-01-14 11:46:26 +00:00
|
|
|
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
|
|
|
|
if [ -f "apikey.txt" ]; then
|
|
|
|
apikey=$(cat "apikey.txt")
|
|
|
|
elif [ -f "${script_dir}/../apikey.txt" ]; then
|
|
|
|
apikey=$(cat "${script_dir}/../apikey.txt")
|
2022-01-20 20:31:16 +00:00
|
|
|
elif [ -f "${HOME}/qortal/apikey.txt" ]; then
|
|
|
|
apikey=$(cat "${HOME}/qortal/apikey.txt")
|
2022-01-14 11:46:26 +00:00
|
|
|
fi
|
|
|
|
|
2021-10-29 15:51:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-11-16 19:36:24 +00:00
|
|
|
if [[ "${method}" == "POST" ]]; then
|
2021-11-17 18:57:46 +00:00
|
|
|
type=$4
|
|
|
|
data=$5
|
|
|
|
identifier=$6
|
2021-10-29 15:51:41 +00:00
|
|
|
|
2021-11-17 18:57:46 +00:00
|
|
|
if [ -z "${data}" ]; then
|
|
|
|
if [[ "${type}" == "PATH" ]]; then
|
|
|
|
echo "Error: missing directory"; exit
|
|
|
|
elif [[ "${type}" == "STRING" ]]; then
|
|
|
|
echo "Error: missing data string"; exit
|
|
|
|
else
|
|
|
|
echo "Error: unrecognized type"; exit
|
|
|
|
fi
|
2021-10-29 15:51:41 +00:00
|
|
|
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
|
|
|
|
|
2021-12-22 13:59:08 +00:00
|
|
|
if [ -z "${identifier}" ]; then
|
|
|
|
identifier="default"
|
2021-11-17 18:57:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create type component in URL
|
|
|
|
if [[ "${type}" == "PATH" ]]; then
|
|
|
|
type_component=""
|
|
|
|
elif [[ "${type}" == "STRING" ]]; then
|
|
|
|
type_component="/string"
|
|
|
|
fi
|
|
|
|
|
2021-10-29 15:51:41 +00:00
|
|
|
echo "Creating transaction - this can take a while..."
|
2022-01-14 11:46:26 +00:00
|
|
|
tx_data=$(curl --silent --insecure -X ${method} "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}${type_component}" -H "X-API-KEY: ${apikey}" -d "${data}")
|
2021-11-17 18:57:46 +00:00
|
|
|
|
2021-10-29 15:51:41 +00:00
|
|
|
if [[ "${tx_data}" == *"error"* || "${tx_data}" == *"ERROR"* ]]; then
|
|
|
|
echo "${tx_data}"; exit
|
2021-11-17 18:57:46 +00:00
|
|
|
elif [ -z "${tx_data}" ]; then
|
|
|
|
echo "Error: no transaction data returned"; exit
|
2021-10-29 15:51:41 +00:00
|
|
|
fi
|
|
|
|
|
2021-12-03 17:33:07 +00:00
|
|
|
echo "Computing nonce..."
|
2022-01-14 11:46:26 +00:00
|
|
|
computed_tx_data=$(curl --silent --insecure -X POST "http://${host}:${port}/arbitrary/compute" -H "Content-Type: application/json" -H "X-API-KEY: ${apikey}" -d "${tx_data}")
|
2021-12-03 17:33:07 +00:00
|
|
|
if [[ "${computed_tx_data}" == *"error"* || "${computed_tx_data}" == *"ERROR"* ]]; then
|
|
|
|
echo "${computed_tx_data}"; exit
|
|
|
|
fi
|
|
|
|
|
2021-10-29 15:51:41 +00:00
|
|
|
echo "Signing..."
|
2021-12-03 17:33:07 +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\":\"${computed_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 [[ "${filepath}" == "default" ]]; then
|
|
|
|
filepath=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We use a different API depending on whether or not an identifier is supplied
|
|
|
|
if [ -n "${identifier}" ]; then
|
2022-01-14 11:46:26 +00:00
|
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}?rebuild=${rebuild}&filepath=${filepath}" -H "X-API-KEY: ${apikey}")
|
2021-11-12 17:42:21 +00:00
|
|
|
else
|
2022-01-14 11:46:26 +00:00
|
|
|
response=$(curl --silent --insecure -X GET "http://${host}:${port}/arbitrary/${service}/${name}?rebuild=${rebuild}&filepath=${filepath}" -H "X-API-KEY: ${apikey}")
|
2021-11-12 17:42:21 +00:00
|
|
|
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
|