Jump to content


Recommended Posts

Hi all guys,

following  hints found here I tried to manage the private methods to Kraken.com.

Unfortunately I couldn't go over signature check, and I get the following:

Balance: {"error":["EAPI:Invalid signature"]}

This is the code:

#include <Crypt.au3>
#include <Base64.au3>
#include <SHA224_256.au3>

Global Const $CALG_SHA_512 = 0x0000800e

#Region Kraken Const
Global $g_sKrakenURL = "https://api.kraken.com"
Global $g_sKrakenAPIVersion = "0"
#EndRegion Kraken Const

#Region keys
Global Const $g_sAPIKey = ""
Global Const $g_sAPISecret = ""
#EndRegion keys

#Region API Test


Func _TestAPI()
    $sResult = KrakenQueryPrivate("Balance") ;Get Balance
    ConsoleWrite("Balance: " & $sResult & @CRLF & @CRLF)
EndFunc   ;==>_TestAPI

#EndRegion API Test

#Region Kraken Query
Func KrakenQueryPrivate($sMethod, $sParameters = "")
    Local $sNONCE = _GetNonce()
    $sParameters = "nonce=" & $sNONCE & $sParameters
    Local $sURL = StringFormat("/%s/private/%s", $g_sKrakenAPIVersion, $sMethod)
    Local $sSecretDecoded = _Base641Decode($g_sAPISecret)
    Local $sNonceParams = $sNONCE & $sParameters
    Local $bURLBytes = Binary($sURL)
    Local $sHashNonceParams = _SHA256($sNonceParams)
    Local $bMessage = $bURLBytes & $sHashNonceParams
;~     Local $sSignature = HMACSHA512($sSecretDecoded, $bMessage)
    Local $sSignature = HMACSHA512($bMessage, $sSecretDecoded)

    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", $g_sKrakenURL & $sURL, False)
    $oHTTP.SetRequestHeader("API-Key", $g_sAPIKey)
    $oHTTP.SetRequestHeader("API-Sign", _Base64EncodeMod($sSignature))
    Local $sReceived = $oHTTP.ResponseText
    Return $sReceived

EndFunc   ;==>KrakenQueryPrivate

#EndRegion Kraken Query

Func _GetNonce()
;~  Return @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC
    Return 9 & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC ;added 9 at start due while testing I added a big nonce number > (@YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC). With another APIkey Use line above (and comment this)
EndFunc   ;==>_GetNonce

Func _Base64EncodeMod($sData)
    Return StringReplace(_Base641Encode($sData), @LF, "")
EndFunc   ;==>_Base64EncodeMod

Func HMACSHA512($bKey, $bMessage)
    Local Const $iBlockSize = (512 / 8)
    Local Const $oconst = 0x5C, $iconst = 0x36
    Local $a_opad[$iBlockSize], $a_ipad[$iBlockSize]
    Local $opad = Binary(''), $ipad = Binary('')
    If BinaryLen($bKey) > $iBlockSize Then $bKey = _Crypt_HashData($bKey, $CALG_SHA_512)
    For $i = 1 To BinaryLen($bKey)
        $a_ipad[$i - 1] = Number(BinaryMid($bKey, $i, 1))
        $a_opad[$i - 1] = Number(BinaryMid($bKey, $i, 1))
    For $i = 0 To $iBlockSize - 1
        $a_opad[$i] = BitXOR($a_opad[$i], $oconst)
        $a_ipad[$i] = BitXOR($a_ipad[$i], $iconst)
    For $i = 0 To $iBlockSize - 1
        $ipad &= Binary('0x' & Hex($a_ipad[$i], 2))
        $opad &= Binary('0x' & Hex($a_opad[$i], 2))
    Local $bRet = _Crypt_HashData($ipad & ($bMessage), $CALG_SHA_512)
    $bRet = _Crypt_HashData($opad & $bRet, $CALG_SHA_512)
    Return $bRet
EndFunc   ;==>HMACSHA512

I saw @Danyfirex solution but it seems it doesn't work, I removed now OTP and I created a test key.

Thanks everyone,



Edited by marko001
Updated code

Share this post

Link to post
Share on other sites

Share this post

Link to post
Share on other sites
Just now, Danyfirex said:

Dont share then :-S It was just a question... I gonna check maybe I find the Issue.



These key can do almost nothing and once it's fixed I will close them. Thanks a lot, bro.


Share this post

Link to post
Share on other sites


I'm trying the same code to connect to the api of kraken, but I'm experiencing the same problems as above. Was this problem solved and if yes, are you willing to share the solution?

Best regards, Dave

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...