· 4 years ago · Dec 12, 2020, 01:16 PM
1package com.example.test;
2
3import lombok.RequiredArgsConstructor;
4import org.springframework.boot.CommandLineRunner;
5import org.springframework.http.HttpHeaders;
6import org.springframework.http.HttpMethod;
7import org.springframework.http.RequestEntity;
8import org.springframework.http.ResponseEntity;
9import org.springframework.stereotype.Component;
10import org.springframework.web.client.RestTemplate;
11
12import javax.crypto.Mac;
13import javax.crypto.spec.SecretKeySpec;
14import java.net.URI;
15import java.security.InvalidKeyException;
16import java.security.NoSuchAlgorithmException;
17import java.util.Date;
18
19import static java.nio.charset.StandardCharsets.UTF_8;
20
21@Component
22@RequiredArgsConstructor
23public class Runner implements CommandLineRunner {
24
25 private static final String SECRET_KEY = "p6Z5v0VKsgLTLsmnhTxyfC1SW2DIZDQ3vL3p-04p";
26 // private static final String SECRET_KEY = "T4lPid48QtjNxjLUFOcUZghD7CUJ7sTVsfuvQZF2";
27 private static final String API_KEY = "b-aqcfx5jN3TnCVsXKgJ7pP_hvvgPOBMo8R-87F3";
28 private static final String URL = "https://ftx.com";
29
30 @Override
31 public void run(String... args) throws Exception {
32 HttpMethod method = HttpMethod.GET;
33 String endpoint = "/api/account";
34 long timestamp = new Date().getTime();
35 String signaturePayload = timestamp + method.toString().toUpperCase() + endpoint;
36 String signature = hmacDigest(signaturePayload);
37
38 HttpHeaders headers = new HttpHeaders();
39 headers.add("FTX-KEY", API_KEY);
40 headers.add("FTX-SIGN", signature);
41 headers.add("FTX-TS", String.valueOf(timestamp));
42 headers.add("user-agent",
43 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0" +
44 ".2840.99 Safari/537.36");
45
46 ResponseEntity<String> responseEntity = new RestTemplate().exchange(
47 RequestEntity.method(method, URI.create(URL+endpoint)).headers(headers).build(), String.class
48 );
49 }
50
51 private String hmacDigest(String msg) throws NoSuchAlgorithmException, InvalidKeyException {
52 String digest;
53 SecretKeySpec key = new SecretKeySpec((SECRET_KEY).getBytes(UTF_8), "HmacSHA256");
54 Mac mac = Mac.getInstance("HmacSHA256");
55 mac.init(key);
56
57 byte[] bytes = mac.doFinal(msg.getBytes(UTF_8));
58
59 StringBuilder hash = new StringBuilder();
60 for (byte aByte : bytes) {
61 String hex = Integer.toHexString(0xFF & aByte);
62 if (hex.length() == 1) {
63 hash.append('0');
64 }
65 hash.append(hex);
66 }
67 digest = hash.toString();
68 return digest;
69 }
70}
71