mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: May 2014
Posts: 36
P
Perl Offline OP
Ameglian cow
OP Offline
Ameglian cow
P
Joined: May 2014
Posts: 36
hello, I'm trying to connect to an api, from a house of virtual currencies, I have collected information from other old posts, but even so, I have an error and I can not read anything from the server, if someone could what could be the failure ... I do not know what else to try .. I put the code.

Code:

alias btc {
  var %sockname = btc. $+ $nonce
  echo -s %sockname
  sockopen -e %sockname poloniex.com 443
}

on *:sockopen:btc.*:{
  var %api_key = apikey
  var %secret = secret

  var %nonce = $nonce
  var %data = method=getInfo&nonce= $+ %nonce
  var %sign = $hmac(sha512,%secret,%data,512)

  var %a = sockwrite -n $sockname

  %a POST /tradingApi HTTP/1.0
  %a Key: %api_key
  %a Sign: %sign
  %a Content-Type: application/x-www-form-urlencoded
  %a Host: poloniex.com
  %a Content-Length: $len(%data)
  %a Connection: keep-alive
  %a $+($crlf,%data)
}


alias sha512 return $dll(sha2.dll,sha512,$calc($2) $1)

alias hmac {
  if (!$1) || ($2 == $null) || ($3 == $null) { return }
  var %size = $iif($4,$calc($4 / 4),64)
  bunset &key &h1 &h1_hash &h2

  if ($len($2) > %size) { bset &key 1 $regsubex($($+($,$1,$chr(40),$2,$chr(41)),2),/(..)/g,$base(\1,16,10) $+ $chr(32)) }
  else { bset -t &key 1 $2 }

  var %x = 1
  while (%x <= %size) {
    var %c = $iif($bvar(&key,%x),$v1,0)
    bset &h1 %x $xor(54,%c)
    bset &h2 %x $xor(92,%c)
    inc %x
  }
  bset -t &h1 $calc($bvar(&h1,0)+1) $3
  bset &h1_hash 1 $regsubex($($+($,$1,$chr(40),&h1,$chr(44),1,$chr(41)),2),/(..)/g,$base(\1,16,10) $+ $chr(32))
  bset &h2 $calc($bvar(&h2,0)+1) $bvar(&h1_hash,1-)
  return $($+($,$1,$chr(40),&h2,$chr(44),1,$chr(41)),2)
  echo $($+($,$1,$chr(40),&h2,$chr(44),1,$chr(41)),2)

}

on *:sockread:btc.*:{
  var %header, %read
  if ($sock($sockname).mark != header.finished) {
    sockread %header
    while (%header != $null) {
      echo -ag %header
      sockread %header
    }
    if ($sockbr) sockmark $sockname header.finished
  }
  if ($sock($sockname).mark == header.finished) {
    sockread -f %read
    while ($sockbr) {
      if (%read != $null) echo -ag %read
      sockread -f %read
    }
  }
}

alias nonce {
  if (!%nonce) set -u1 %nonce $ctime $+ $calc($ticks % 10000)
  else inc %nonce
  return %nonce
}




the error that comes out is :

* Invalid parameters: $hmac

it also does not show any information from the server

here https://poloniex.com/support/api/ the web comes information of the api, and some examples in other languages, but I can not adapt it.




Last edited by Perl; 21/12/17 12:32 AM.
Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
/help says: $hmac(text|&binvar|filename, key, hash, N)

you're using $hmac(sha512,%secret,%data,512)

aka $hmac(hash, key, text|&binvar|filename, Invalid N)

Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
You have a few issues:
1. mirc supports HMAC-SHA512 so those aliases are useless in this case. (Infact, the specified aliases are never called in favor of mIRC's builtin aliases of the same name)
2. poloniex returns a 'invalid method' for getInfo once auhtorization is passed

Regardless, here's the fixed up script to get passed the auth stuff

Code:
alias -l api_key your_api_key_here
alias -l api_secret yoru_api_secret_here

alias btc {
  var %sock = $ticks $+ 000
  while ($sock(btc. $+ %sock)) inc %sock
  %sock = btc. $+ %sock

  sockopen -e %sock poloniex.com 443
}

on *:SOCKOPEN:btc.*:{
  if ($sockerr) {
    echo -s [btc] Sock open Error
    return
  }

  var %data = method=getInfo

  var %a = sockwrite -n $sockname
  %a POST /tradingApi HTTP/1.1
  %a Host: poloniex.com
  %a Key: $api_key

  ;; use mirc's built in HMAC-SHA512 hashing
  %a Sign: $hmac(%data, $api_secret, sha512, 0)


  %a Content-Type: application/x-www-form-urlencoded
  %a Content-Length: $len(%data)
  %a Connection: Close
  %a
  %a %data
}

on *:SOCKWRITE:btc.*:{
  if ($sockerr) {
    echo -s [btc] Failed to write to socket
  }
}

on *:SOCKREAD:btc.*:{
  if ($sockerr) {
    echo -s [btc] Failed to read from socket
    return
  }

  var %header, %read
  if ($sock($sockname).mark != header.finished) {
    sockread %header
    while (%header != $null) {
      echo -ag %header
      sockread %header
    }
    if ($sockbr) sockmark $sockname header.finished
  }
  if ($sock($sockname).mark == header.finished) {
    sockread -f %read
    while ($sockbr) {
      if (%read != $null) echo -ag %read
      sockread -f %read
    }
  }
}

on *:SOCKCLOSE:btc.*:{
  if ($sockerr) {
    echo -s [btc] Socket closed unexpectedly
  }
}

Last edited by FroggieDaFrog; 21/12/17 05:11 AM.

I am SReject
My Stuff
Joined: May 2014
Posts: 36
P
Perl Offline OP
Ameglian cow
OP Offline
Ameglian cow
P
Joined: May 2014
Posts: 36
Thank you very much, I thank you very much for your help, I did not know that mirc had integrated the hmac, I thought the error was the use of the dll, it works really well, now to work in the second part ...
Thanks again and best regards.


Link Copied to Clipboard