From ac433b1527f7d7ae5abed9f52fd121eb7321df4b Mon Sep 17 00:00:00 2001 From: crowetic <5431064+crowetic@users.noreply.github.com> Date: Tue, 27 Jun 2023 13:29:10 -0700 Subject: [PATCH] Add files via upload --- tools/qdn | 122 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 105 insertions(+), 17 deletions(-) diff --git a/tools/qdn b/tools/qdn index ea52e3c9..adbe861d 100755 --- a/tools/qdn +++ b/tools/qdn @@ -5,10 +5,10 @@ host="localhost" port=12391 if [ -z "$*" ]; then - echo "Usage:" + echo "Usage:" echo echo "Host/update data:" - echo "qdn POST [service] [name] PATH [dirpath] " + echo "qdn POST [service] [name] PATH [dirpath] <description> <tags=tag1,tag2,tag3> <category> <fee> <preview (true or false)>" echo "qdn POST [service] [name] STRING [data-string] <identifier>" echo echo "Fetch data:" @@ -22,6 +22,21 @@ if [ -z "$*" ]; then exit fi + +# Default ports for Qortal +mainnet_port=12391 +testnet_port=62391 + +# Check if the '-t' operator is passed, if so change to utilizing testnet. +if [[ "$1" == "-t" ]]; then + # Use testnet port + port=$testnet_port + shift +else + # Use mainnet port + port=$mainnet_port +fi + script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) if [ -f "apikey.txt" ]; then @@ -37,32 +52,46 @@ service=$2 name=$3 if [ -z "${method}" ]; then - echo "Error: missing method"; exit + echo "Error: missing method" + exit 1 fi if [ -z "${service}" ]; then - echo "Error: missing service"; exit + echo "Error: missing service" + exit 1 fi if [ -z "${name}" ]; then - echo "Error: missing name"; exit + echo "Error: missing name" + exit 1 fi - if [[ "${method}" == "POST" ]]; then type=$4 data=$5 identifier=$6 + title=$7 + description=$8 + tags=$9 + category=${10} + fee=${11} + preview=${12} + + if [ -z "${data}" ]; then if [[ "${type}" == "PATH" ]]; then - echo "Error: missing directory"; exit + echo "Error: missing directory - please use a path to a directory with a SINGLE file wishing to be published" + exit 1 elif [[ "${type}" == "STRING" ]]; then - echo "Error: missing data string"; exit + echo "Error: missing data string - please input the data string you wish to publish" + exit 1 else - echo "Error: unrecognized type"; exit + echo "Error: unrecognized type" + exit 1 fi fi if [ -z "${QORTAL_PRIVKEY}" ]; then - echo "Error: missing private key. Set it by running: export QORTAL_PRIVKEY=privkeyhere"; exit + echo "Error: missing private key. Set it by running: export QORTAL_PRIVKEY=privkeyhere" + exit 1 fi if [ -z "${identifier}" ]; then @@ -75,30 +104,88 @@ if [[ "${method}" == "POST" ]]; then elif [[ "${type}" == "STRING" ]]; then type_component="/string" fi + + # Create tags component in URL, comma-separated list of tags, will be added to the tags call. + tags_component="" + if [ -n "${tags}" ]; then + IFS=',' read -ra tag_array <<< "${tags}" + for tag in "${tag_array[@]}"; do + tags_component+="&tags=${tag}" + done + fi + + if [ -z ${tags_component} ]; then + tags_component="" + echo "nothing in tags, using empty tags" + fi + + #Create category component with pre-defined list of categories. Error if category is specified but not in list. + allowed_categories=("ART" "AUTOMOTIVE" "BEAUTY" "BOOKS" "BUSINESS" "COMMUNICATIONS" "CRYPTOCURRENCY" "CULTURE" "DATING" "DESIGN" "ENTERTAINMENT" "EVENTS" "FAITH" "FASHION" "FINANCE" "FOOD" "GAMING" "GEOGRAPHY" "HEALTH" "HISTORY" "HOME" "KNOWLEDGE" "LANGUAGE" "LIFESTYLE" "MANUFACTURING" "MAPS" "MUSIC" "NEWS" "OTHER" "PETS" "PHILOSOPHY" "PHOTOGRAPHY" "POLITICS" "PRODUCE" "PRODUCTIVITY" "PSYCHOLOGY" "QORTAL" "SCIENCE" "SELF_CARE" "SELF_SUFFICIENCY" "SHOPPING" "SOCIAL" "SOFTWARE" "SPIRITUALITY" "SPORTS" "STORYTELLING" "TECHNOLOGY" "TOOLS" "TRAVEL" "UNCATEGORIZED" "VIDEO" "WEATHER") + + if [[ -n "$category" && ! " ${allowed_categories[@]} " =~ " $category " ]]; then + echo "Error: Invalid category. Allowed categories are: ${allowed_categories[*]} be sure to place your overall script inputs in the correct order" + exit 1 + elif [ -z "$category" ]; then + category="" + echo "No category is being set" + fi + + if [ -n "$fee" ]; then + if [[ "$fee" == "1" || "$fee" == ".001" ]]; then + fee="100000" + elif [ -z "$fee" ]; then + fee="" + else + echo "Error: Invalid fee value. Expected '1', '.001' or no input." + exit 1 + fi + final_fee="${fee}" + fi + + + # check that preview is true/false + if [[ -n "$preview" && ! ( "$preview" == "true" || "$preview" == "false" ) ]]; then + echo "Error: Invalid preview value. Expected 'true' or 'false'. Please retry with boolean as preview entry." + exit 1 + elif [ -z "$preview" ]; then + preview="" + fi + + # Build the API URL + api_url="http://${host}:${port}/arbitrary/${service}/${name}/${identifier}${type_component}" + api_url+="?title=${title}&description=${description}&tags=${tags_component}&category=${category}&fee=${final_fee}&preview=${preview}" + echo "Creating transaction - this can take a while..." - tx_data=$(curl --silent --insecure -X ${method} "http://${host}:${port}/arbitrary/${service}/${name}/${identifier}${type_component}" -H "X-API-KEY: ${apikey}" -d "${data}") + tx_data=$(curl --silent --insecure -X ${method} "${api_url}" -H "accept: text/plain" -H "X-API-KEY: ${apikey}" -H "Content-Type: text/plain" -d "${data}") if [[ "${tx_data}" == *"error"* || "${tx_data}" == *"ERROR"* ]]; then - echo "${tx_data}"; exit + echo "Error creating transaction: ${tx_data}" + exit 1 elif [ -z "${tx_data}" ]; then - echo "Error: no transaction data returned"; exit + echo "Error: no transaction data returned" + exit 1 fi echo "Computing nonce..." 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}") + if [[ "${computed_tx_data}" == *"error"* || "${computed_tx_data}" == *"ERROR"* ]]; then - echo "${computed_tx_data}"; exit + echo "Error computing nonce: ${computed_tx_data}" + exit 1 fi echo "Signing..." 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}\"}") + if [[ "${signed_tx_data}" == *"error"* || "${signed_tx_data}" == *"ERROR"* ]]; then - echo "${signed_tx_data}"; exit + echo "Error signing transaction: ${signed_tx_data}" + exit 1 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 @@ -131,9 +218,10 @@ elif [[ "${method}" == "GET" ]]; then echo "Empty response from ${host}:${port}" fi if [[ "${response}" == *"error"* || "${response}" == *"ERROR"* ]]; then - echo "${response}"; exit + echo "${response}" + exit 1 fi echo "${response}" - fi +