decodePayload function

Future<Map<String, dynamic>> decodePayload(
  1. Uint8List bytes,
  2. SerializerStrategy serializer,
  3. CryptoStrategy? crypto,
  4. SecretKey? secretKey,
)

Shared decode: strip taint (0xAA), decrypt if marked (0xAE), deserialize.

Implementation

Future<Map<String, dynamic>> decodePayload(
  Uint8List bytes,
  SerializerStrategy serializer,
  CryptoStrategy? crypto,
  SecretKey? secretKey,
) async {
  Uint8List decoded = bytes;
  if (decoded.isNotEmpty && decoded[0] == 0xAA) {
    decoded = decoded.sublist(1);
  }
  if (crypto != null && secretKey != null && decoded.isNotEmpty && decoded[0] == 0xAE) {
    decoded = await crypto.decrypt(decoded.sublist(1), secretKey);
  }
  return serializer.deserialize(decoded);
}