LeMoussel
LeMoussel

Reputation: 5767

Golang Gorilla CEX.IO Websocket authentication error

I'm currently trying to connect to the CEX.IO bitcoin exchange's websocket. Websocket connection is OK but at the time of authentication, I have the error: Timestamp is not in 20sec range. I don't know what this error.

Test case 1 & 2 for createSignature is OK (https://cex.io/websocket-api#authentication).

Authentication Go code :

func toHmac256(message string, secret string) string {
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(message))
    return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}

func Signature() (string, string) {
    nonce := time.Now().Unix()  // Edit with Cerise Limón answer
    message := strconv.FormatInt(nonce, 10) + "API-KEY"
    signature := api.toHmac256(message, "SECRET-KEY")
    return signature, nonce
}

Upvotes: 0

Views: 472

Answers (1)

Thundercat
Thundercat

Reputation: 120941

The error message is telling you that the timestamp is more than 20 seconds away from the current time.

The API expects time in seconds, not nanoseconds. Use Unix to get the time in seconds.

     nonce := time.Now().Unix()

Unix time is is seconds since Jan 01 1970 UTC.

If this fails, then check that your system time is set correctly down to the second.

Upvotes: 1

Related Questions