i. Encryption Process Step-by-Step Explanation

The following are the steps for encryption operations:

  1. Prepare business request parameters .(All field definitions refer to the Pre-encryption Request Parameters section)
  2. Serialize into a JSON string.
  3. Encrypt the JSON data using the RSA public key provided by WaaS.
  4. Perform Base64 encoding of the encryption result.

ii. Encryption Code Examples

The following are code examples for implementing encryption:

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(StandardCharsets.UTF_8),
                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();
        }
    }
}