generateSignatureHeaders static method

Map<String, String> generateSignatureHeaders({
  1. required String clientId,
  2. String? amount,
  3. required String externalTransactionId,
})

Builds the HMAC-SHA256 signature and required headers

Implementation

static Map<String, String> generateSignatureHeaders({
  required String clientId,
  String? amount,
  required String externalTransactionId,
}) {
  // Must be in the correct order expected by the backend
  final dataToSign = (amount != null)
      ? 'clientId=$clientId,amount=$amount,externalTransactionId=$externalTransactionId'
      : 'clientId=$clientId,externalTransactionId=$externalTransactionId';

  final hmacSha256 = Hmac(sha256, utf8.encode(secretKey));
  final digest = hmacSha256.convert(utf8.encode(dataToSign));
  final signature = base64.encode(digest.bytes);

  log(dataToSign);

  return {
    'X-HMAC-Signature': signature,
    'Content-Type': 'application/json',
  };
}