question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FTX Exchange, Unable to perform authed requests

See original GitHub issue

Hello there,

Looks like there is something wrong with the FTX Exchange signature generation (FTX-SIGN header), I’m trying to perform authed requests with my (US) api key and secret but I always get the next message: Not logged in (HTTP status code: 401) (HTTP status code: 0)

Although if I replicate the request with postman (using the generated params (headers) from the lib) I get the next message: { "success": false, "error": "Not logged in: Invalid signature" }

I’m using the next code to connect to the US FTX Exchange:

val authenticatedClient = ExchangeFactory.INSTANCE.createExchangeWithoutSpecification(FtxExchange::class.java).also {
            val exchangeSpecification = FtxExchange().defaultExchangeSpecification.apply {
                apiKey = "API_KEY"
                secretKey = "API_SECRET"
                sslUri = "https://ftx.us"
                host = "ftx.us"
            }
            it.applySpecification(exchangeSpecification)
        }

EDIT: Looks like the request headers are sent as FTX-KEY, FTX-TS, FTX-SIGN etc, but at the US exchange the headers should be FTXUS-KEY, FTXUS-TS, FTXUS-SIGN, etx, would be good if that can be configured through the exchange specification

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
BernatCarbocommented, Jun 19, 2022

@mystarrocks That could work.

Sadly we ended by dropping XChange from our project and we made our own exchange implementation from the scratch.

1reaction
mystarrockscommented, Jun 19, 2022

Hope someone finds this helpful.

With no easy way to configure the header names for the US version (host and URI can be customized on the ExchangeSpecification), I used a ResCU interceptor to get around. Here’s some sample code from my working setup:

Custom interceptor

package org.knowm.xchange.interceptor;

import com.linus.bitcoin.investments.FtxUsAuthenticated;
import org.knowm.xchange.ftx.FtxAuthenticated;
import si.mazi.rescu.Interceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

public class FtxUsAdapterInterceptor implements Interceptor {

    @Override
    public Object aroundInvoke(InvocationHandler h, Object proxy, Method m, Object[] args)
            throws Throwable {
        if (m.getDeclaringClass() != FtxAuthenticated.class) {
            return h.invoke(proxy, m, args);
        }

        // FTX-TS needs to be there too since it's hardcoded in FtxDigest, so replicate FTXUS-TS as FTX-TS
        var adaptedParameterTypes = new java.util.ArrayList<>(Arrays.stream(m.getParameterTypes()).toList());
        adaptedParameterTypes.add(1, adaptedParameterTypes.get(1));
        var adaptedMethod = FtxUsAuthenticated.class.getMethod(m.getName(), adaptedParameterTypes.toArray(new Class<?>[]{}));

        var adaptedArgs = new java.util.ArrayList<>(Arrays.stream(args).toList());
        adaptedArgs.add(1, adaptedArgs.get(1));

        return h.invoke(proxy, adaptedMethod, adaptedArgs.toArray(new Object[]{}));
    }
}

Interceptor service registration (src/main/resources/META-INF/services)

org.knowm.xchange.interceptor.FtxUsAdapterInterceptor

API path resource class

Just copy/paste FtxAuthenticated, rename headers, and add a header parameter annotation for FTX-TS.

package com.linus.bitcoin.investments;

import org.knowm.xchange.ftx.Ftx;
import org.knowm.xchange.ftx.FtxException;
import org.knowm.xchange.ftx.dto.FtxResponse;
import org.knowm.xchange.ftx.dto.account.*;
import org.knowm.xchange.ftx.dto.trade.CancelAllFtxOrdersParams;
import org.knowm.xchange.ftx.dto.trade.FtxModifyOrderRequestPayload;
import org.knowm.xchange.ftx.dto.trade.FtxOrderDto;
import org.knowm.xchange.ftx.dto.trade.FtxOrderRequestPayload;
import si.mazi.rescu.ParamsDigest;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;

@Path("/api")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface FtxUsAuthenticated extends Ftx {

  @GET
  @Path("/account")
  FtxResponse<FtxAccountDto> getAccountInformation(
      @HeaderParam("FTXUS-KEY") String apiKey,
      @HeaderParam("FTXUS-TS") Long nonce,
      @HeaderParam("FTX-TS") Long nonce1, // this is needed because FTX-TS is hardcoded in FtxDigest
      @HeaderParam("FTXUS-SIGN") ParamsDigest signature,
      @HeaderParam("FTXUS-SUBACCOUNT") String subaccount)
      throws IOException, FtxException;

  // other operations; above method parameters are the same for all operations
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

FTX.us fails to auth · Issue #7461 · ccxt/ccxt - GitHub
py to ftx.us. No need for that, do this instead: import ccxt exchange = ccxt.
Read more >
Whitelisting Withdrawal Addresses - FTX Exchange
Once our team verifies the information provided, the individuals will be listed as authorized representatives and will be able to request new ...
Read more >
The SEC announces it has authorized charges against FTX ...
The US Securities and Exchange Commission said it will file charges against FTX founder Sam Bankman-Fried on Tuesday relating to violations ...
Read more >
FTX's Sam Bankman-Fried Is Arrested in the Bahamas
Separately, the Securities and Exchange Commission said in a statement that ... of FTX reflected a “complete failure of corporate control.
Read more >
FAQ: Account Funding - FTX.US
FTX US is excited to credit an ACH deposit as soon as you make the ACH request. ... issuing bank regarding a timeframe...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found