parse static method

Future<PassFile> parse(
  1. Uint8List pass
)

Implementation

static Future<PassFile> parse(Uint8List pass) async {
  final codec = ZipDecoder();
  Archive archive;
  try {
    archive = codec.decodeBytes(pass);
  } catch (e) {
    throw InvalidEncodingError();
  }

  Map<String, String> manifest;

  final file =
      archive.files.singleWhere((element) => element.name == 'manifest.json');
  manifest = (json.decode(file.stringContent) as Map).cast<String, String>();

  final folder = <ArchiveFile>[];

  await Future.wait(
    manifest.entries.map(
      (manifestEntry) async {
        final file = archive.files
            .singleWhere((element) => element.name == manifestEntry.key);

        final content = file.byteContent;

        String hash = sha1.convert(content).toString();

        final checksum = manifestEntry.value;

        if (hash != checksum) {
          throw ManifestChecksumError(expected: checksum, actual: hash);
        }

        folder.add(file);
      },
    ),
  );

  final passFile =
      archive.singleWhere((element) => element.name == 'pass.json');

  final PassMetadata metadata = PassMetadata.fromJson(
    json.decode(passFile.stringContent) as Map<String, Object?>,
  );

  return PassFile(metadata, folder);
}