海妖专用API重击,curl和openssl

时间:2018-09-22 04:05:25

标签: bash api curl openssl

我正在使用bash和curl来操作Kraken API。

当我尝试添加订单时,得到响应:

{"error":["EGeneral:Invalid arguments"]}

当我使用相同的代码查找天平时,我没有收到错误消息。

我正在使用的URL如下所示: https://api.kraken.com/0/private/AddOrder?pair=XXBTZUSD&type=buy&ordertype=limit&price=6000&volume=1&userref=1537588642&validate=1

代码看起来像这样(最后是openssl和curl命令):

FUNC="AddOrder"
FN="/tmp/kraken$FUNC.json"

# URL
PRICE=$1
VOL=$2
ORDERTYPE=limit
ID=`date +%s`
URLPATH="/0/private/$FUNC"
URLPATH="$URLPATH?pair=XXBTZUSD&type=buy&ordertype=$ORDERTYPE&price=$PRICE&volume=$VOL&userref=$ID&validate=1"
echo $URLPATH
curlPost $URLPATH $FUNC $FN

curlPost(){
  # API Key:
  APIKEY=`cat creds.key`
  # Private Key:
  PRIVATEKEY=`cat creds.private`

  URLHOST="https://api.kraken.com"
  NONCE=$(date +%s%N)
  echo ${1}
  echo -n "${1}" > /tmp/kraken.$2.bin
  echo -n "${NONCE}nonce=${NONCE}" | openssl sha256 -binary >> /tmp/kraken.$2.bin

  PRIVATEKEYHEX=$(echo -n "${PRIVATEKEY}" | base64 -d | hexdump -v -e '/1 "%02X"');

   cat /tmp/kraken.$2.bin | openssl sha512 -binary -mac HMAC -macopt hexkey:${PRIVATEKEYHEX} > sign.$2.bin
  APISIGN=$(cat sign.$2.bin | base64 -w 0)
  curl -X POST -H "Accept: application/json" -H "API-Key: ${APIKEY}" -H "API-Sign: ${APISIGN}" -d "nonce=$NONCE" ${URLHOST}${URLPATH}  > $3
}

1 个答案:

答案 0 :(得分:0)

我已经解决了。 curl argumnet -d用于数据,如下所示:

URLPATH="/0/private/$FUNC"
DATA="pair=XXBTZUSD&type=buy&ordertype=$ORDERTYPE&price=$PRICE&volume=$VOL&userref=$ID&validate=1"

  if [ -z "$DATA" ]; then
    curl -X POST -H "Accept: application/json" -H "API-Key: ${APIKEY}" -H "API-Sign: ${APISIGN}" -d "nonce=$NONCE" "${URLHOST}${URLPATH}"
  else
    curl -d ${DATA} -X POST -H "Accept: application/json" -H "API-Key: ${APIKEY}" -H "API-Sign: ${APISIGN}" -d "nonce=$NONCE" "${URLHOST}${URLPATH}"
  fi