Post
Topic
Board Service Discussion
Re: Trade API check? BTC-e
by
rijnsent
on 24/01/2014, 14:54:03 UTC
Hi Sapphire/all,

I'm indeed starting with a "simple" getInfo or TransHistory, 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 your example (and one I could find online) and my hash-function now works like a charm.

What I tried:
-generate a new keypair at BTC-e
-tried getInfo and TransHistory
-I even tried "method=BogusMethod&nonce=12345678", that too came back with {"success":0,"error":"invalid sign"}

So I assume that my SHA512 hasher does something wrong, but the 2 successful responses are weird...

Anybody a clue?

Thanks,

Koen

My total VBA-code (copy paste to any Excel, you might have to set some references in VBA, to e.g. Microsoft WinHttp Services)
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 = "THIS IS WHERE YOUR API-key goes"
SecretKey = "And here goes your private key"
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,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")
        SHAhasher.key = BKey
        computeHash = SHAhasher.computeHash_2(BTxt)
    ElseIf Meth = "SHA256" Then
        Set SHAhasher = CreateObject("System.Security.Cryptography.HMACSHA256")
        SHAhasher.key = BKey
        computeHash = SHAhasher.computeHash_2(BTxt)
    Else
        Set SHAhasher = CreateObject("System.Security.Cryptography.HMACSHA1")
        SHAhasher.key = BKey
        computeHash = SHAhasher.computeHash_2(BTxt)
    End If
End Function