buildTestPayload function
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.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)}';
}