I'm trying to get the Bittrex API 1.1 working in Excel but for whatever reason I don't get it right .
There seems to be something wrong in they way I calculate the signature.
I'm getting the follwing error:
Code:
{"success":false,"message":"INVALID_SIGNATURE","result":null}
This is my current Macro code:
Code:
Public Sub getopenorders()
Dim apikey As String
Dim apisecret As String
Dim uri As String
Dim sign As String
Dim response As String
Dim params As String
'For Debugging
Range("B3").Value = ""
Range("B4").Value = ""
Range("B5").Value = ""
apikey = "BITTREX API KEY"
apisecret = "BITTREX API SECRET"
uri = "https://bittrex.com/api/v1.1/market/getopenorders"
params = "?" + "apikey=" + apikey + "&nonce=" + getNonce
sign = createSignature(apisecret, uri + params)
response = getResponse(uri, "apisign", sign, params)
Range("B3").Value = response
End Sub
Function getResponse(ByVal pURL As String, sendVarKey As String, sendVarValue As String, params As String) As String
Dim oRequest As WinHttp.WinHttpRequest
Set oRequest = GetHttpObj("POST", pURL + params, False, sendVarKey, sendVarValue)
oRequest.send ""
getResponse = oRequest.responseText
End Function
Public Function GetHttpObj(httpMethod As String, uri As String, async As Boolean, _
sendVarKey As String, sendVarValue As String, _
Optional contentType As String = "application/json") As WinHttp.WinHttpRequest
Dim httpObj As New WinHttp.WinHttpRequest
With httpObj
.Open httpMethod, uri, async
.setRequestHeader "origin", "pamsXL"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Content-type", contentType
.setRequestHeader "cache-control", "no-cache"
End With
Range("B4").Value = uri
Range("B5").Value = sendVarValue
httpObj.setRequestHeader sendVarKey, sendVarValue
Set GetHttpObj = httpObj
End Function
Private Function createSignature(keyString As String, url As String) As String
createSignature = sha512(keyString, url)
End Function
Private Function sha512(ByVal keyString As String, ByVal str As String) As String
Dim encode As Object, encrypt As Object, s As String, _
t() As Byte, b() As Byte, privateKeyBytes() As Byte
Set encode = CreateObject("System.Text.UTF8Encoding")
Set encrypt = CreateObject("System.Security.Cryptography.HMACSHA512")
s = keyString
privateKeyBytes = decodeBase64(s)
encrypt.Key = privateKeyBytes
t = encode.Getbytes_4(str)
b = encrypt.ComputeHash_2((t))
s = tob64(b)
sha512 = Replace(s, vbLf, "")
Set encode = Nothing
Set encrypt = Nothing
End Function
Private Function tob64(ByRef arrData() As Byte) As String
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
tob64 = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function
Private Function decodeBase64(ByVal strData As String) As Byte()
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.Text = strData
decodeBase64 = objNode.nodeTypedValue
Set objNode = Nothing
Set objXML = Nothing
End Function
Function getNonce() As String
getNonce = CStr(DateDiff("S", "1/1/1970", Now()))
End Function
Thanks a lot for your help.