wcDecodeType2 function

String wcDecodeType2(
  1. String encoded
)

Decode a type-2 (link-mode) envelope back to its plaintext message. Tolerates URL-safe alphabet and missing padding (deep links may strip it).

Implementation

String wcDecodeType2(String encoded) {
  final urlSafe = encoded.replaceAll('+', '-').replaceAll('/', '_');
  final bytes = base64Url.decode(_padBase64(urlSafe));
  if (bytes.isEmpty || bytes[0] != 2) {
    throw ArgumentError('not a type-2 envelope');
  }
  return utf8.decode(bytes.sublist(1));
}