getaddress - java

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;

public class Main {
    public static void main(String[] args) {
        getAddress();
    }

    public static void  getAddress() {
        try {
            String url = "https://payment.hyperhashing.com/v1/getaddress";
            String merchantId = "a56e0-0a10c-73015-9f330"; // Your Merchant ID
            String key = "<Your Secret Key>"; // Your Secret Key

            JSONObject data = new JSONObject();
            data.put("symbol", "usdt");
            data.put("usd_price", 120);
            data.put("type", "price");
            data.put("timestamp", System.currentTimeMillis() / 1000L);

            JSONObject requestJson = new JSONObject();
            requestJson.put("merchant_id", merchantId);
            requestJson.put("hash", generateHmacSHA512(data.toString(), key));
            requestJson.put("data", data);

            String response = httpPost(url, requestJson.toString());
            JSONObject jsonResponse = new JSONObject(response);


            System.out.println(jsonResponse.getString("address"));
            System.out.println("<br><img src='https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + jsonResponse.getString("address") + "'/>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String httpPost(String urlStr, String jsonStr) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        con.setDoOutput(true);

        try (OutputStream os = con.getOutputStream()) {
            byte[] input = jsonStr.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        System.out.println("http response code => " + con.getResponseCode());

        StringBuilder response = new StringBuilder();
        try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
            String responseLine = null;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
        }

        con.disconnect();
        System.out.println("http response => " + response.toString());
        return response.toString();
    }

    private static String generateHmacSHA512(String data, String key) throws Exception {
        Mac sha512Hmac = Mac.getInstance("HmacSHA512");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA512");
        sha512Hmac.init(secretKey);
        byte[] hash = sha512Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(hash);
    }
}

Last updated