buildTestPayload function

String buildTestPayload({
  1. String fullName = 'Abebe Kebede Alemu',
  2. FaydaGender? gender = FaydaGender.male,
  3. String? fan = '6140123412341234',
  4. DateTime? dateOfBirth,
  5. List<int>? face,
  6. String version = '4',
  7. bool signed = true,
  8. Map<String, String?> overrides = const {},
})

Builds a synthetic Fayda V4 QR payload for tests, so you never need a real person's card.

By default it is signed with the test key: it verifies against faydaTestPublicKeyPem and fails against the bundled NIDP key, which also makes it a ready-made "forged card" fixture.

  • face: WebP bytes, defaults to faydaTestFace. Pass an empty list for a payload without a face.
  • gender, fan, dateOfBirth: null leaves the key out.
  • fan is written as given. Real cards store it in groups separated by two spaces; pass fan: '6140 1234 1234 1234' to match them.
  • overrides: raw key/value fields applied last, to produce malformed data. A null value removes the key: overrides: {'D': '1990/02/31'}, overrides: {'V': '5'}.
  • signed: false leaves the signature empty.

Throws an ArgumentError if fullName contains :, which the format cannot carry.

Implementation

String buildTestPayload({
  String fullName = 'Abebe Kebede Alemu',
  FaydaGender? gender = FaydaGender.male,
  String? fan = '6140123412341234',
  DateTime? dateOfBirth,
  List<int>? face,
  String version = '4',
  bool signed = true,
  Map<String, String?> overrides = const {},
}) {
  if (fullName.contains(':')) {
    throw ArgumentError.value(fullName, 'fullName', 'Must not contain ":"');
  }
  final dob = dateOfBirth ?? DateTime(2002, 11, 26);
  final fields = <String, String?>{
    'V': version,
    'G': gender?.code,
    'A': fan,
    'D': '${_pad(dob.year, 4)}/${_pad(dob.month, 2)}/${_pad(dob.day, 2)}',
    ...overrides,
  }..removeWhere((_, value) => value == null);

  final tail = [
    for (final MapEntry(:key, :value) in fields.entries) '$key:$value',
  ].join(':');
  final facePart = base64UrlEncodeNoPad(face ?? faydaTestFace);
  final external = '$facePart:DLT:$fullName:$tail';
  if (!signed) return '$external:SIGN:';

  final header = base64UrlEncodeNoPad(utf8.encode('{"alg":"RS256"}'));
  final signingInput = '$header.${base64UrlEncodeNoPad(utf8.encode(external))}';
  final signature = rsaPkcs1Sha256Sign(_testKey, utf8.encode(signingInput));
  return '$external:SIGN:$header..${base64UrlEncodeNoPad(signature)}';
}