The following are the steps for encryption operations:
The following are code examples for implementing encryption:
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.http.converter.FormHttpMessageConverter;
class EncryptionExample {
public static void main(String[] args) {
try {
// ===================== 1. Prepare business parameters =====================
Map<String, Object> requestParams = new HashMap<>();
requestParams.put("tenantUserId", "tenant_001");
requestParams.put("chainName", "ETH");
requestParams.put("symbol", "USDT");
// ===================== 2. JSON serialization =====================
// Perform serialization using JSON utility class, implementation can refer to sample code in relevant utility classes
String jsonData = JsonUtils.toJson(requestParams);
// ===================== 3. RSA public key encryption =====================
// Use the public key provided by WaaS
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ...";
// Use RSA utility class for encryption, implementation can refer to sample code in relevant utility classes
byte[] encryptedData = RSAUtils.encryptByPublicKey(
jsonData.getBytes(),
publicKey
);
// ===================== 4. Base64 encoding =====================
// Perform encoding using Base64 utility class, implementation can refer to sample code in relevant utility classes
String base64Data = Base64Util.encodeByte(encryptedData);
// ===================== 5. Prepare HTTP request =====================
// Create HTTP request headers
HttpHeaders headers = new HttpHeaders();
headers.add("X-API-KEY", "MERCHANT_123456"); // Merchant credentials
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // Form format
// Package request body
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("data", base64Data); // Parameter name must be "data"
// ===================== 6. Send API request =====================
// Create and configure RestTemplate
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
// Send request and get response
APIResult response = restTemplate.postForObject(
"https://waas2-out-api.powersafe-rel.cc/api/v1/api/user/getUserAddress",
new HttpEntity<>(body, headers),
APIResult.class
);
// Process response, content can refer to response examples
System.out.println("\nAPI response result:");
System.out.println(response);
} catch (Exception e) {
System.err.println("\nError occurred during processing: " + e.getMessage());
e.printStackTrace();
}
}
}
The following are the steps for encryption operations:
The following are code examples for implementing encryption:
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.http.converter.FormHttpMessageConverter;
class EncryptionExample {
public static void main(String[] args) {
try {
// ===================== 1. Prepare business parameters =====================
Map<String, Object> requestParams = new HashMap<>();
requestParams.put("tenantUserId", "tenant_001");
requestParams.put("chainName", "ETH");
requestParams.put("symbol", "USDT");
// ===================== 2. JSON serialization =====================
// Perform serialization using JSON utility class, implementation can refer to sample code in relevant utility classes
String jsonData = JsonUtils.toJson(requestParams);
// ===================== 3. RSA public key encryption =====================
// Use the public key provided by WaaS
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ...";
// Use RSA utility class for encryption, implementation can refer to sample code in relevant utility classes
byte[] encryptedData = RSAUtils.encryptByPublicKey(
jsonData.getBytes(),
publicKey
);
// ===================== 4. Base64 encoding =====================
// Perform encoding using Base64 utility class, implementation can refer to sample code in relevant utility classes
String base64Data = Base64Util.encodeByte(encryptedData);
// ===================== 5. Prepare HTTP request =====================
// Create HTTP request headers
HttpHeaders headers = new HttpHeaders();
headers.add("X-API-KEY", "MERCHANT_123456"); // Merchant credentials
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // Form format
// Package request body
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("data", base64Data); // Parameter name must be "data"
// ===================== 6. Send API request =====================
// Create and configure RestTemplate
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
// Send request and get response
APIResult response = restTemplate.postForObject(
"https://waas2-out-api.powersafe-rel.cc/api/v1/api/user/getUserAddress",
new HttpEntity<>(body, headers),
APIResult.class
);
// Process response, content can refer to response examples
System.out.println("\nAPI response result:");
System.out.println(response);
} catch (Exception e) {
System.err.println("\nError occurred during processing: " + e.getMessage());
e.printStackTrace();
}
}
}