nicehash/rest-clients-demo

Invalid Session in the bash

mcsham opened this issue · 4 comments

Hello.
I'm trying to figure out api and implement bash on Linux. But it doesn’t work out.

set -x 

rand_str2(){
    echo $(od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}')
}

get_server_time(){
    echo $(curl -s 'https://api-test.nicehash.com/api/v2/time'|sed s/[^0-9]//g)
}

get_time(){
    date +%s
}

get_HASH256(){
    local str=''
    str+=$1         #key
    str+='\x00'
    str+=$2         #time
    str+='\x00'
    str+=$3         #x-nonce
    str+='\x00'
    str+='\x00'
    str+=$4         #x-org-id
    str+='\x00'
    str+='\x00'
    str+=$5         #method
    str+='\x00'
    str+=$6         #path
    if [ ! -z "$7" -a "$7" != " " ];then
        str+='\x00'
        str+=$7     #query
    fi
    echo -ne "$str" | openssl dgst -hmac "$8" -sha256 | awk '{print $2}'
}

url_root='https://api-test.nicehash.com'
time=$(get_server_time)

x_nonce=$(rand_str2) # generate uuid random string
key='ac09bbab-1f7a-4893-8a96-50c5417b0726'
secret='e9634354-d781-4e9a-85b1-10ea0b5b83cbe3cc1a4a-d65f-4417-bc61-2440dcd6951c'
org_id='d6396a75-595c-473e-be0b-b94d87c6f750'
#path='/main/api/v2/accounting/accounts'
path='/main/api/v2/trades'
method='GET'
query=''

signature=$(get_HASH256 "$key" "$time" "$x_nonce" "$org_id" "$method" "$path" "$query" "$secret")

url=$url_root$path

h1="X-Time: $time"
h2="X-Nonce: $x_nonce"
h3="X-Organization-Id: $org_id"
h4="X-Auth: $key:$signature"
h5="Accept: application/json"
h6="Content-Type: application/json; charset=utf-8"

curl -s -H "$h1" -H "$h2" -H "$h3" -H "$h4" -H "$h5" -H "$h6" $url

Result:
{"error_id":"79dda1fa-eab1-4e00-8fb9-ed6e81c0ec2a","errors":[{"code":2000,"message":"Invalid Session"}]}

PS sorry for my bad English.

bl4z commented

try your function with samples from this thread -- once your function produces same result as mentioned here session should work ...

#2

try your function with samples from this thread -- once your function produces same result as mentioned here session should work ...

#2

I tested the function on the data in the example in the documentation, everything matches.

bl4z commented

since u paste all your data i tried with your api key and it is working OK -- here is output ... please set time and nonce matching my request and verify hash input and output (in x-auth)

input params: $api_key."\x00".$time."\x00".$nonce."\x00"."\x00".$org_id."\x00"."\x00"."GET"."\x00".$path."\x00";

input values: ac09bbab-1f7a-4893-8a96-50c5417b0726 1570253780480 5d982bd430b78  d6396a75-595c-473e-be0b-b94d87c6f750  GET /main/api/v2/accounting/accounts

Array
(
    [0] => X-Time: 1570253780480
    [1] => X-Nonce: 5d982bd430b78
    [2] => X-Organization-Id: d6396a75-595c-473e-be0b-b94d87c6f750
    [3] => X-Request-Id: 5d982bd430b78
    [4] => X-Auth: ac09bbab-1f7a-4893-8a96-50c5417b0726:2b3150359830af465ba5bc5a930495328d278cd1b6bb0316786383e34e93ca24)

response:

Array
(
    [0] => Array
        (
            [currency] => TBTC
            [ownerId] => d6396a75-595c-473e-be0b-b94d87c6f750
            [balance] => 0.0999
        )...```

https://docs.nicehash.com/main/index.html

input is a byte array composed of ordered fields using zero byte (0x00) as a separator. There is no separator before the first field or after the LAST field. Some fields ........

The solution to the problem was the null character at the end. Although the documentation said that it was not needed. Thanks for the help.