B4J Question Kraken Api B4J

claudiob4

Member
Licensed User
Longtime User
Hi all,
I'm trying to use the KRAKEN-API but can't connect. I used RemoRoth subs (IOS) https://www.b4x.com/android/forum/threads/kraken-api.90473/ but since I use B4J I had to modify the HMACSHA512. Maybe my mistake is in this version. Could anyone tell me if it is correct? Thanks.


IOS HMACSHA512:
Public Sub HMACSHA512(keyb() As Byte, inputb() As Byte) As Byte()
    Dim no As NativeObject = Me
 
    Dim res As Object = no.RunMethod("hmacForKeyAndData512::", Array(no.ArrayToNSData(keyb), no.ArrayToNSData(inputb)))
    Dim resb() As Byte = no.NSDataToArray(res)
 
    Return resb
End Sub

B4J HMACSHA512:
Private Sub HMACSHA512(keyb() As Byte, inputb() As Byte) As Byte()
    
    Dim m As Mac
    Dim k As KeyGenerator
    k.Initialize("HMACSHA512")
    k.KeyFromBytes(keyb)
    m.Initialise("HMACSHA512", k.Key)
    m.Update(inputb)
    Dim b() As Byte
    b = m.Sign
    Return b
    
End Sub
 
Solution
Worked for me on the first run :)

B4X:
Sub AppStart (Args() As String)
    Dim PrivateKey As String = "kQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg=="
    Dim Nonce As String = "1616492376594"
    Dim EncodedPayload As String = "nonce=1616492376594&ordertype=limit&pair=XBTUSD&price=37500&type=buy&volume=1.25"
    Dim URI As String = "/0/private/AddOrder"
    Dim md As MessageDigest
    Dim NonceAndPOST As B4XBytesBuilder
    NonceAndPOST.Initialize
    NonceAndPOST.Append(Nonce.GetBytes("utf8"))
    NonceAndPOST.Append(EncodedPayload.GetBytes("utf8"))
    Dim sha256() As Byte = md.GetMessageDigest(NonceAndPOST.ToArray, "SHA-256")
    Dim full As B4XBytesBuilder
    full.Initialize...

claudiob4

Member
Licensed User
Longtime User
Hi Erel,
thanks for your interest.
Unfortunately I don't have an IOS environment and I don't know how to run the IOS HMACSHA512 subroutine.
I didn't understand where I should use ByteConverter.HexFromBytes.

Kraken provides an example with the correct result. I attach this data and my B4J routine to get the result. I hope you can understand where the mistake lies.



API-Sign​

Authenticated requests should be signed with the "API-Sign" header, using a signature generated with your private key, nonce, encoded payload, and URI path according to:

HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key

Example Signature​

The following is a specific example of a signature generated with a particular private key, nonce, and payload corresponding to a new limit order (buy 1.25 XBTUSD at $37,500). If your code is generating a different signature ("API-Sign") for thie example, then there is likely an issue with your application of the above methodology. Code snippets for generating the signature in Python, Golang and Node.js follow below.


FieldValue
Private KeykQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg==
Nonce1616492376594
Encoded Payloadnonce=1616492376594&ordertype=limit&pair=XBTUSD&price=37500&type=buy&volume=1.25
URI Path/0/private/AddOrder
API-Sign4/dpxb3iT4tp/ZCVEwSnEsLxx0bqyhLpdfOpc6fn7OR8+UClSV5n9E6aSS8MPtnRfp32bAb0nmbRn6H8ndwLUQ==



TestSignature:
Public Sub TestSignature
  
    Dim strSecret As String = "kQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg=="
    Dim strNonce As String = "1616492376594"   
    Dim strPayload As String = "nonce=11616492376594&ordertype=limit&pair=XBTUSD&price=37500&type=buy&volume=1.25"
    Dim strURI As String = "/0/private/AddOrder"
    
    Dim md As MessageDigest
        
    Dim bytesNonce() As Byte = strPayload.GetBytes("utf8")
    
    Dim bytesSHA256Nonce() As Byte = md.GetMessageDigest(bytesNonce, "SHA-256")
  
    Dim bytesURIpath() As Byte = strURI.GetBytes("utf8")
  
    Dim Z() As Byte = JoinBytes(Array(bytesURIpath, bytesSHA256Nonce))

    Dim strUtils As StringUtils
    Dim bytesAPI_Secret() As Byte = strUtils.DecodeBase64(strSecret)
  
    Dim byteSignature() As Byte = HMACSHA512(bytesAPI_Secret, Z)
    
    Dim strSignature As String = strUtils.EncodeBase64(byteSignature)
    Log(strSignature)
    'This is what I get:
    'tNpV+CevAUS3HeJm09lrOiVO1qwluRk7ePhuUnpK34L+dBovuUtS6JAyim4//cu1OlJMdTQT165lmRdXGZPxwg==
    
    'This is the result of Kraken:
    '4/dpxb3iT4tp/ZCVEwSnEsLxx0bqyhLpdfOpc6fn7OR8+UClSV5n9E6aSS8MPtnRfp32bAb0nmbRn6H8ndwLUQ==
    
End Sub

Private Sub HMACSHA512(keyb() As Byte, inputb() As Byte) As Byte()
    
    Dim m As Mac
    Dim k As KeyGenerator
    k.Initialize("HMACSHA512")
    k.KeyFromBytes(keyb)
    m.Initialise("HMACSHA512", k.Key)
    m.Update(inputb)
    Dim b() As Byte
    b = m.Sign
    Return b
    
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Worked for me on the first run :)

B4X:
Sub AppStart (Args() As String)
    Dim PrivateKey As String = "kQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg=="
    Dim Nonce As String = "1616492376594"
    Dim EncodedPayload As String = "nonce=1616492376594&ordertype=limit&pair=XBTUSD&price=37500&type=buy&volume=1.25"
    Dim URI As String = "/0/private/AddOrder"
    Dim md As MessageDigest
    Dim NonceAndPOST As B4XBytesBuilder
    NonceAndPOST.Initialize
    NonceAndPOST.Append(Nonce.GetBytes("utf8"))
    NonceAndPOST.Append(EncodedPayload.GetBytes("utf8"))
    Dim sha256() As Byte = md.GetMessageDigest(NonceAndPOST.ToArray, "SHA-256")
    Dim full As B4XBytesBuilder
    full.Initialize
    full.Append(URI.GetBytes("utf8")).Append(sha256)
    Dim su As StringUtils
    Dim b() As Byte = HMACSHA512(su.DecodeBase64(PrivateKey), full.ToArray)
    Log(su.EncodeBase64(b))
End Sub

Private Sub HMACSHA512(keyb() As Byte, inputb() As Byte) As Byte()
    Dim m As Mac
    Dim k As KeyGenerator
    k.Initialize("HMACSHA512")
    k.KeyFromBytes(keyb)
    m.Initialise("HMACSHA512", k.Key)
    m.Update(inputb)
    Dim b() As Byte
    b = m.Sign
    Return b
End Sub
 
Upvote 0
Solution

claudiob4

Member
Licensed User
Longtime User
Thanks Erel,
your help was essential and congratulations on your skills. My company JNT srl will be happy to make a contribution to the development of B4J.
I enclose the complete and working call for those who need it.

Kraken PostRequest:
Public Sub PostRequest(strURL As String, strURI As String, strPropsAdditional As String, strAPI_Key As String, strAPI_Secret as String, strJobName As String)
 
    Dim strNonce As String = DateTime.Now
 
    Dim EncodedPayload As String = "nonce=" & strNonce & "&" & strPropsAdditional
 
    Dim md As MessageDigest
    Dim NonceAndPOST As B4XBytesBuilder
    NonceAndPOST.Initialize
    NonceAndPOST.Append(strNonce.GetBytes("utf8"))
    NonceAndPOST.Append(EncodedPayload.GetBytes("utf8"))
    Dim sha256() As Byte = md.GetMessageDigest(NonceAndPOST.ToArray, "SHA-256")
    Dim full As B4XBytesBuilder
    full.Initialize
    full.Append(strURI.GetBytes("utf8")).Append(sha256)
    Dim su As StringUtils
    Dim b() As Byte = HMACSHA512(su.DecodeBase64(strAPI_Secret), full.ToArray)
    Dim strSignature As String = su.EncodeBase64(b)
 
    Dim jobPost As HttpJob
 
    Dim strUrlUri As String = strURL & strURI
 
    jobPost.Initialize(strJobName, Me)
    jobPost.PostString(strUrlUri,EncodedPayload)
    jobPost.GetRequest.SetHeader("API-Key", strAPI_Key)
    jobPost.GetRequest.SetHeader("API-Sign",strSignature )
    jobPost.GetRequest.SetContentType("application/x-www-form-urlencoded")
 
End Sub
 
Last edited:
Upvote 0
Top