-3

I'm trying to access balance of Bitstamp account with API.

#!/bin/bash

# Bitstamp API credentials
API_KEY="name_of_my_API_key"
API_SECRET="private_API_key"
CUSTOMER_ID="ID_number"

# Build the nonce
NONCE=$(date +%s%N)

# Sign the message
echo -e "${NONCE}\t${CUSTOMER_ID}\t${API_KEY}\t${API_SECRET}"
SIGNATURE=$(echo -n "${NONCE}${CUSTOMER_ID}${API_KEY}" | openssl dgst -sha256 -hmac "${API_SECRET}" | sed 's/^.* //')
echo ${SIGNATURE}
RESULT=$(curl -s -X POST https://www.bitstamp.net/api/v2/account_balances/usd/ \
     -d "key=${API_KEY}" \
     -d "signature=${SIGNATURE}" \
     -d "nonce=${NONCE}")

# Check if the order was successful
STATUS=$(echo ${RESULT} | jq -r '.status')
if [ "${STATUS}" != "success" ]; then
    echo "Error: Order failed - $(echo ${RESULT} | jq -r '.reason')"
fi
BALANCE_AVAILABLE=$(echo ${RESULT} | jq -r '.available')
echo "${BALANCE_AVAILABLE}"

... but I'm getting error: "Invalid signature"

Is there any mistake in SIGNATURE construction please?

pa-well
  • 1
  • 1
  • 1
    [Don't use UPPER case variables](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – Gilles Quénot Apr 07 '23 at 12:51
  • 4
    To get some useful hints paste your script at http://www.shellcheck.net/. – Cyrus Apr 07 '23 at 12:52
  • 1
    Please read the tags you use. [tag:bash] specifically says "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here." – Ed Morton Apr 07 '23 at 13:22
  • According to https://www.bitstamp.net/api/#api-authentication, your API key is generated in the site, and then you put it in your code. The page has examples in other languages. Using these might be easier. – Nic3500 Apr 09 '23 at 06:22

1 Answers1

0

solution: convert final ${SIGNATURE} to upper case:

...
SIGNATURE=$(echo -n "${NONCE}${CUSTOMER_ID}${API_KEY}" | \
    openssl dgst -sha256 -hmac "${API_SECRET}" | \
    sed 's/^.* //' | \
    tr '[:lower:]' '[:upper:]')
...
pa-well
  • 1
  • 1