Webhook Deposit
Signature Verification Example
@RestController
public class SignatureVerificationController {
private static final String PLATFORM_ID = "your-platform-id";
private static final String SECRET = "your-api-key";
// Helper function to generate HMAC-SHA256 signature
private String generateSignature(String signatureContract, String secret) throws NoSuchAlgorithmException, InvalidKeyException {
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256HMAC.init(secretKey);
byte[] hash = sha256HMAC.doFinal(signatureContract.getBytes(StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
for (byte b : hash) {
result.append(String.format("%02x", b));
}
return result.toString();
}
// Platform notification URL
@PostMapping("/notification")
public ResponseEntity<String> notificationWebHook(@RequestBody Map<String, Object> body, @RequestHeader HttpHeaders headers) throws NoSuchAlgorithmException, InvalidKeyException, JsonProcessingException {
// Signature Verification
ObjectMapper objectMapper = new ObjectMapper();
String sortedBodyJson = objectMapper.writeValueAsString(body);
// Generate the signature contract string
String signatureContract = PLATFORM_ID + ";" + sortedBodyJson + ";" + SECRET;
// Generate HMAC-SHA256 signature
String signature = generateSignature(signatureContract, SECRET);
// Get the signature from the headers
String receivedSignature = headers.getFirst("x-signature");
// Compare the generated signature with the received signature
if (StringUtils.hasText(receivedSignature) && signature.equals(receivedSignature)) {
return ResponseEntity.ok("Signature is valid");
} else {
return ResponseEntity.status(400).body("Invalid signature");
}
}
}
Headers
Name
Type
Description
Request Body
Name
Type
Description
Resending Webhook Notifications
Last updated