Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSA and Ed25519 signatures #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
logs/
target/
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,10 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.binance.connector.futures.client.impl.cm_futures.CMAccount;
import com.binance.connector.futures.client.impl.cm_futures.CMUserData;
import com.binance.connector.futures.client.impl.cm_futures.CMPortfolioMargin;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

public class CMFuturesClientImpl extends FuturesClientImpl {
private static String defaultBaseUrl = DefaultUrls.COINM_PROD_URL;
Expand Down Expand Up @@ -34,14 +35,18 @@ public CMFuturesClientImpl(String apiKey, String secretKey, String baseUrl) {
super(apiKey, secretKey, baseUrl, cmProduct);
}

public CMFuturesClientImpl(String apiKey, SignatureGenerator signatureGenerator, String baseUrl, boolean showLimitUsage) {
super(apiKey, signatureGenerator, baseUrl, cmProduct, showLimitUsage);
}

@Override
public CMMarket market() {
return new CMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public CMAccount account() {
return new CMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
return new CMAccount(getProductUrl(), getApiKey(), getSignatureGenerator(), getShowLimitUsage(), getProxy());
}

@Override
Expand All @@ -51,6 +56,6 @@ public CMUserData userData() {

@Override
public CMPortfolioMargin portfolioMargin() {
return new CMPortfolioMargin(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
return new CMPortfolioMargin(getProductUrl(), getApiKey(), getSignatureGenerator(), getShowLimitUsage(), getProxy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.binance.connector.futures.client.FuturesClient;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.signaturegenerator.HmacSignatureGenerator;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

public abstract class FuturesClientImpl implements FuturesClient {
private final String apiKey;
private final String secretKey;
private final SignatureGenerator signatureGenerator;
private final String baseUrl;
private final String productUrl;
private boolean showLimitUsage;
Expand All @@ -16,7 +18,7 @@ public FuturesClientImpl(String baseUrl, String product) {
}

public FuturesClientImpl(String baseUrl, String product, boolean showLimitUsage) {
this(null, null, baseUrl, product, showLimitUsage);
this(null, (String) null, baseUrl, product, showLimitUsage);
}

public FuturesClientImpl(String apiKey, String secretKey, String baseUrl, String product) {
Expand All @@ -25,7 +27,19 @@ public FuturesClientImpl(String apiKey, String secretKey, String baseUrl, String

public FuturesClientImpl(String apiKey, String secretKey, String baseUrl, String product, boolean showLimitUsage) {
this.apiKey = apiKey;
this.secretKey = secretKey;
if (secretKey != null) {
this.signatureGenerator = new HmacSignatureGenerator(secretKey.trim());
} else {
this.signatureGenerator = null;
}
this.baseUrl = baseUrl;
this.productUrl = baseUrl + product;
this.showLimitUsage = showLimitUsage;
}

public FuturesClientImpl(String apiKey, SignatureGenerator signatureGenerator, String baseUrl, String product, boolean showLimitUsage) {
this.apiKey = apiKey;
this.signatureGenerator = signatureGenerator;
this.baseUrl = baseUrl;
this.productUrl = baseUrl + product;
this.showLimitUsage = showLimitUsage;
Expand All @@ -35,8 +49,8 @@ public String getApiKey() {
return this.apiKey;
}

public String getSecretKey() {
return this.secretKey;
public SignatureGenerator getSignatureGenerator() {
return this.signatureGenerator;
}

public String getBaseUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.impl.um_futures.UMAccount;
import com.binance.connector.futures.client.impl.um_futures.UMMarket;
import com.binance.connector.futures.client.impl.um_futures.UMUserData;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

public class UMFuturesClientImpl extends FuturesClientImpl {
private static String defaultBaseUrl = DefaultUrls.USDM_PROD_URL;
Expand Down Expand Up @@ -33,14 +34,18 @@ public UMFuturesClientImpl(String apiKey, String secretKey, String baseUrl) {
super(apiKey, secretKey, baseUrl, umProduct);
}

public UMFuturesClientImpl(String apiKey, SignatureGenerator signatureGenerator, String baseUrl, boolean showLimitUsage) {
super(apiKey, signatureGenerator, baseUrl, umProduct, showLimitUsage);
}

@Override
public UMMarket market() {
return new UMMarket(getProductUrl(), getBaseUrl(), getApiKey(), getShowLimitUsage(), getProxy());
}

@Override
public UMAccount account() {
return new UMAccount(getProductUrl(), getApiKey(), getSecretKey(), getShowLimitUsage(), getProxy());
return new UMAccount(getProductUrl(), getApiKey(), getSignatureGenerator(), getShowLimitUsage(), getProxy());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.Account;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

/**
* <h2>Coin-Margined Trade Endpoints</h2>
Expand All @@ -15,8 +16,8 @@
* Response will be returned in <i>String format</i>.
*/
public class CMAccount extends Account {
public CMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
public CMAccount(String productUrl, String apiKey, SignatureGenerator signatureGenerator, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, signatureGenerator, showLimitUsage, proxy);
}

private final String ORDER = "/v1/order";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.PortfolioMargin;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

/**
* <h2>Coin-Margined Portfolio Margin Endpoints</h2>
Expand All @@ -13,8 +14,8 @@
* Response will be returned in <i>String format</i>.
*/
public class CMPortfolioMargin extends PortfolioMargin {
public CMPortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
public CMPortfolioMargin(String productUrl, String apiKey, SignatureGenerator signatureGenerator, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, signatureGenerator, showLimitUsage, proxy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ParameterChecker;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;
import java.util.LinkedHashMap;

/**
Expand All @@ -15,9 +16,9 @@ public abstract class Account {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public Account(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
public Account(String productUrl, String apiKey, SignatureGenerator signatureGenerator, boolean showLimitUsage, ProxyAuth proxy) {
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
this.requestHandler = new RequestHandler(apiKey, signatureGenerator, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand All @@ -37,8 +38,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
public void setRequestHandler(String apiKey, SignatureGenerator signatureGenerator, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, signatureGenerator, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ParameterChecker;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;
import java.util.LinkedHashMap;

/**
Expand Down Expand Up @@ -47,8 +48,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
new RequestHandler(apiKey, secretKey, proxy);
public void setRequestHandler(String apiKey, SignatureGenerator signatureGenerator, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, signatureGenerator, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.RequestHandler;
import com.binance.connector.futures.client.utils.ParameterChecker;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;
import java.util.LinkedHashMap;

/**
Expand All @@ -15,9 +16,9 @@ public abstract class PortfolioMargin {
private RequestHandler requestHandler;
private boolean showLimitUsage;

public PortfolioMargin(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
public PortfolioMargin(String productUrl, String apiKey, SignatureGenerator signatureGenerator, boolean showLimitUsage, ProxyAuth proxy) {
this.productUrl = productUrl;
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
this.requestHandler = new RequestHandler(apiKey, signatureGenerator, proxy);
this.showLimitUsage = showLimitUsage;
}

Expand All @@ -37,8 +38,8 @@ public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}

public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
public void setRequestHandler(String apiKey, SignatureGenerator signatureGenerator, ProxyAuth proxy) {
this.requestHandler = new RequestHandler(apiKey, signatureGenerator, proxy);
}

public void setShowLimitUsage(boolean showLimitUsage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.impl.futures.Account;
import com.binance.connector.futures.client.utils.ProxyAuth;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;

/**
* <h2>USDⓈ-Margined Trade Endpoints</h2>
Expand All @@ -15,8 +16,8 @@
* Response will be returned in <i>String format</i>.
*/
public class UMAccount extends Account {
public UMAccount(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, secretKey, showLimitUsage, proxy);
public UMAccount(String productUrl, String apiKey, SignatureGenerator signatureGenerator, boolean showLimitUsage, ProxyAuth proxy) {
super(productUrl, apiKey, signatureGenerator, showLimitUsage, proxy);
}

private final String MULTI_ASSETS_MARGIN = "/v1/multiAssetsMargin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@
import com.binance.connector.futures.client.enums.HttpMethod;
import com.binance.connector.futures.client.enums.RequestType;
import com.binance.connector.futures.client.exceptions.BinanceConnectorException;
import java.util.LinkedHashMap;
import com.binance.connector.futures.client.utils.signaturegenerator.Ed25519SignatureGenerator;
import com.binance.connector.futures.client.utils.signaturegenerator.HmacSignatureGenerator;
import com.binance.connector.futures.client.utils.signaturegenerator.RsaSignatureGenerator;
import com.binance.connector.futures.client.utils.signaturegenerator.SignatureGenerator;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashMap;

public class RequestHandler {
private final String apiKey;
private final String secretKey;
private final SignatureGenerator signatureGenerator;
private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class);
private final ProxyAuth proxy;

public RequestHandler(String apiKey, ProxyAuth proxy) {
this.apiKey = apiKey;
this.secretKey = null;
this.proxy = proxy;
this(apiKey, null, proxy);
}

public RequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
public RequestHandler(String apiKey, SignatureGenerator signatureGenerator, ProxyAuth proxy) {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.signatureGenerator = signatureGenerator;
this.proxy = proxy;
}

/**
* Build request based on request type and send the requests to server.
* @param baseUrl base url
* @param urlPath url path
* @param signature the signature
* @param parameters parameters
* @param httpMethod https method
*
* @param baseUrl base url
* @param urlPath url path
* @param signature the signature
* @param parameters parameters
* @param httpMethod https method
* @param requestType request type
* @return String - response from server
*/
Expand Down Expand Up @@ -71,12 +74,15 @@ public String sendWithApiKeyRequest(String baseUrl, String urlPath, LinkedHashMa

public String sendSignedRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
HttpMethod httpMethod, boolean showLimitUsage) {
if (null == secretKey || secretKey.isEmpty() || null == apiKey || apiKey.isEmpty()) {
if (signatureGenerator.getClass() == HmacSignatureGenerator.class && (null == apiKey || apiKey.isEmpty())) {
throw new BinanceConnectorException("[RequestHandler] Secret key/API key cannot be null or empty!");
}
if ((signatureGenerator.getClass() == RsaSignatureGenerator.class || signatureGenerator.getClass() == Ed25519SignatureGenerator.class) && (null == apiKey || apiKey.isEmpty())) {
throw new BinanceConnectorException("[RequestHandler] Private key/API key cannot be null or empty!");
}
parameters.put("timestamp", UrlBuilder.buildTimestamp());
String queryString = UrlBuilder.joinQueryParameters(parameters);
String signature = SignatureGenerator.getSignature(queryString, secretKey);
String signature = signatureGenerator.getSignature(queryString);
return sendApiRequest(baseUrl, urlPath, signature, parameters, httpMethod, RequestType.SIGNED, showLimitUsage);
}
}

This file was deleted.

Loading