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;

  try {
    final file = archive.files
        .singleWhere((element) => element.name == 'manifest.json');
    manifest = (_jsonCodec.decode(
      _utf8codec.decode(
        file.rawContent?.toUint8List() ?? (file.content as Uint8List),
      ),
    ) as Map)
        .cast<String, String>();
  } catch (e) {
    throw ManifestNotFoundError();
  }

  final folder = <ArchiveFile>[];

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

        final content =
            file.rawContent?.toUint8List() ?? file.content as Uint8List;

        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(
    _jsonCodec.decode(passFile.stringContent) as Map<String, Object?>,
  );

  return PassFile(metadata, folder);
}