Rijnsent
Well-known Member
- Joined
- Oct 17, 2005
- Messages
- 1,455
- Office Version
- 365
- Platform
- Windows
Dear all,
I'm generally solving issues, but am now bumping into a problem myself. I've created code that should enable me to communicate with the BTC-e API ( https://btc-e.com/api/documentation ) through a POST command. I want to create a "simple" getInfo statement, hopefully later I could enter or cancel a trade... But for now my default response is {"success":0,"error":"invalid sign"}. The most bizarre thing: yesterday I tried running it and all of a sudden I got a positive answer back twice: {"success":1,"return":{"funds":{"usd":0,etc...), in between those answers were some 20 failed runs of exactly that same macro... I double checked my hasher with an example (one I could find online) and my hash-function seems to work.
Anybody a clue what could be wrong?
Thanks in advance for an answer,
Koen
I'm generally solving issues, but am now bumping into a problem myself. I've created code that should enable me to communicate with the BTC-e API ( https://btc-e.com/api/documentation ) through a POST command. I want to create a "simple" getInfo statement, hopefully later I could enter or cancel a trade... But for now my default response is {"success":0,"error":"invalid sign"}. The most bizarre thing: yesterday I tried running it and all of a sudden I got a positive answer back twice: {"success":1,"return":{"funds":{"usd":0,etc...), in between those answers were some 20 failed runs of exactly that same macro... I double checked my hasher with an example (one I could find online) and my hash-function seems to work.
Anybody a clue what could be wrong?
Thanks in advance for an answer,
Koen
Code:
Sub TestPOSTBTCe()
Dim APIkey As String
Dim SecretKey As String
Dim NonceUnique As Long
Dim postData As String
Dim SecretKeyByte() As Byte
Dim messagebyte() As Byte
Dim Sign As String
NonceUnique = DateDiff("s", "1/1/1970", Now)
'BTC-e
TradeApiSite = "https://btc-e.com/tapi/"
APIkey = "MY API KEY GOES HERE"
SecretKey = "MY SECRET KEY GOES HERE"
postData = "method=getInfo&nonce=" & NonceUnique
Sign = HexHash(postData, SecretKey, "SHA512")
' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", TradeApiSite, False
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "Key", APIkey
objHTTP.SetRequestHeader "Sign", Sign
objHTTP.Send (postData)
objHTTP.WaitForResponse
Debug.Print postData, "-", objHTTP.ResponseText
Set objHTTP = Nothing
'{"success":0,"error":"invalid sign"}
'{"success":1,"return":{"funds":{"usd":0,"btc": ETC...
End Sub
Function HexHash(ByVal clearText As String, ByVal key As String, Meth As String) As String
Dim hashedBytes() As Byte
Dim i As Integer
hashedBytes = computeHash(clearText, key, Meth)
HexHash = ""
For i = 0 To UBound(hashedBytes)
HexHash = HexHash & LCase(HEX(hashedBytes(i)))
Next
End Function
Function computeHash(ByVal clearText As String, ByVal key As String, Meth As String) As Byte()
Dim BKey() As Byte
Dim BTxt() As Byte
BTxt = StrConv(clearText, vbFromUnicode)
BKey = StrConv(key, vbFromUnicode)
If Meth = "SHA512" Then
Set SHAhasher = CreateObject("System.Security.Cryptography.HMACSHA512")
ElseIf Meth = "SHA256" Then
Set SHAhasher = CreateObject("System.Security.Cryptography.HMACSHA256")
Else
Set SHAhasher = CreateObject("System.Security.Cryptography.HMACSHA1")
End If
If key <> "" Then
SHAhasher.key = BKey
Else
End If
computeHash = SHAhasher.computeHash_2(BTxt)
Set SHAhaser = Nothing
End Function