Skip to content

Commit

Permalink
Merge branch 'uint8lists' into signature-creation
Browse files Browse the repository at this point in the history
# Conflicts:
#	passkit/lib/src/order/pk_order.dart
#	passkit/lib/src/pkpass/pkpass.dart
  • Loading branch information
ueman committed Sep 18, 2024
2 parents ed84eea + 1a7a61d commit b05ec75
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/lib/import_pass/import_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PkPassImportSource {

final String? contentResolverPath;
final String? filePath;
final List<int>? bytes;
final Uint8List? bytes;

Future<PkPass> getPass() async {
if (contentResolverPath != null) {
Expand Down
6 changes: 3 additions & 3 deletions passkit/lib/src/apple_wwdr_certificate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ X509 get wwdrG4 =>
/// More info at:
/// https://developer.apple.com/help/account/reference/wwdr-intermediate-certificates/
/// https://www.apple.com/certificateauthority/
// ignore: constant_identifier_names
const worldwide_Developer_Relations_G4 = [
// ignore: constant_identifier_names, non_constant_identifier_names
final worldwide_Developer_Relations_G4 = Uint8List.fromList([
48,
130,
4,
Expand Down Expand Up @@ -1127,4 +1127,4 @@ const worldwide_Developer_Relations_G4 = [
207,
242,
159,
];
]);
21 changes: 8 additions & 13 deletions passkit/lib/src/archive_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:crypto/crypto.dart';
import 'package:passkit/src/archive_file_extension.dart';
import 'package:passkit/src/pkpass/exceptions.dart';
import 'package:passkit/src/pkpass/pk_pass_image.dart';
import 'package:passkit/src/strings_parser/naive_strings_file_parser.dart';
Expand All @@ -15,13 +16,8 @@ import 'package:passkit/src/strings_parser/naive_strings_file_parser.dart';
final _utf8JsonDecoder = const Utf8Decoder().fuse(const JsonDecoder());

extension ArchiveX on Archive {
List<int>? findBytesForFile(String fileName) =>
findFile(fileName)?.content as List<int>?;

Uint8List? findUint8ListForFile(String fileName) {
final data = findBytesForFile(fileName);
return data == null ? null : Uint8List.fromList(data);
}
Uint8List? findBytesForFile(String fileName) =>
findFile(fileName)?.binaryContent;

Map<String, dynamic>? findFileAndReadAsJson(String fileName) {
final bytes = findBytesForFile(fileName);
Expand All @@ -33,9 +29,9 @@ extension ArchiveX on Archive {

PkImage? loadImage(String name) {
return PkImage.fromImages(
image1: findUint8ListForFile('$name.png'),
image2: findUint8ListForFile('$name@2.png'),
image3: findUint8ListForFile('$name@3.png'),
image1: findBytesForFile('$name.png'),
image2: findBytesForFile('$name@2.png'),
image3: findBytesForFile('$name@3.png'),
);
}

Expand All @@ -52,8 +48,7 @@ extension ArchiveX on Archive {
for (final languageFile in translationFiles) {
final language = languageFile.name.split('.').first;

languageData[language] =
parseStringsFile(languageFile.content as List<int>);
languageData[language] = parseStringsFile(languageFile.binaryContent);
}
return languageData;
}
Expand Down Expand Up @@ -88,7 +83,7 @@ extension ArchiveX on Archive {

for (final file in filesWithoutSignatureAndManifest) {
final checksumInManifest = manifest[file.name] as String?;
final digest = sha1.convert(file.content as List<int>);
final digest = sha1.convert(file.binaryContent);
if (checksumInManifest != digest.toString()) {
throw ChecksumMismatchException(file.name);
}
Expand Down
7 changes: 7 additions & 0 deletions passkit/lib/src/archive_file_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'dart:typed_data';

import 'package:archive/archive.dart';

extension ArchiveFileX on ArchiveFile {
Uint8List get binaryContent => Uint8List.fromList(content as List<int>);
}
14 changes: 6 additions & 8 deletions passkit/lib/src/order/pk_order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:passkit/src/archive_extensions.dart';
import 'package:passkit/src/archive_file_extension.dart';
import 'package:passkit/src/pkpass/exceptions.dart';
import 'package:passkit/src/signature_verification.dart';

Expand All @@ -20,7 +21,7 @@ class PkOrder {
/// verification and validation.
// TODO(ueman): Provide an async method for this.
static PkOrder fromBytes(
final List<int> bytes, {
final Uint8List bytes, {
bool skipChecksumVerification = false,
bool skipSignatureVerification = false,
}) {
Expand All @@ -37,12 +38,9 @@ class PkOrder {
archive.checkSha1Checksums(manifest);

if (skipSignatureVerification) {
final manifestContent = Uint8List.fromList(
archive.findFile('manifest.json')!.content as List<int>,
);
final signatureContent = Uint8List.fromList(
archive.findFile('signature')!.content as List<int>,
);
final manifestContent =
archive.findFile('manifest.json')!.binaryContent;
final signatureContent = archive.findFile('signature')!.binaryContent;

verifySignature(
signatureBytes: signatureContent,
Expand Down Expand Up @@ -78,7 +76,7 @@ class PkOrder {
final Map<String, Map<String, dynamic>>? languageData;

/// The bytes of this PkPass
final List<int> sourceData;
final Uint8List sourceData;

/// Indicates whether a webservices is available.
bool get isWebServiceAvailable => order.webServiceURL != null;
Expand Down
46 changes: 19 additions & 27 deletions passkit/lib/src/pkpass/pkpass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'package:archive/archive.dart';
import 'package:crypto/crypto.dart';
import 'package:meta/meta.dart';
import 'package:passkit/src/archive_file_extension.dart';
import 'package:passkit/src/pkpass/exceptions.dart';
import 'package:passkit/src/pkpass/pass_data.dart';
import 'package:passkit/src/pkpass/pass_type.dart';
Expand Down Expand Up @@ -65,7 +66,7 @@ class PkPass {
/// certificate.
// TODO(any): Provide an async method for this.
static PkPass fromBytes(
final List<int> bytes, {
final Uint8List bytes, {
bool skipChecksumVerification = false,
bool skipSignatureVerification = false,
}) {
Expand All @@ -81,12 +82,9 @@ class PkPass {
if (!skipChecksumVerification) {
archive.checkSha1Checksums(manifest);
if (!skipSignatureVerification) {
final manifestContent = Uint8List.fromList(
archive.findFile('manifest.json')!.content as List<int>,
);
final signatureContent = Uint8List.fromList(
archive.findFile('signature')!.content as List<int>,
);
final manifestContent =
archive.findFile('manifest.json')!.binaryContent;
final signatureContent = archive.findFile('signature')!.binaryContent;

if (!verifySignature(
signatureBytes: signatureContent,
Expand Down Expand Up @@ -134,7 +132,7 @@ class PkPass {
// gracefully fall back to just parsing the PkPass file.
// TODO(ueman): Provide an async method for this.
static List<PkPass> passesFromBytes(
final List<int> bytes, {
final Uint8List bytes, {
bool skipChecksumVerification = false,
bool skipSignatureVerification = false,
}) {
Expand All @@ -148,7 +146,7 @@ class PkPass {
return pkPasses
.map(
(file) => fromBytes(
file.content as List<int>,
file.binaryContent,
skipChecksumVerification: skipChecksumVerification,
skipSignatureVerification: skipSignatureVerification,
),
Expand Down Expand Up @@ -229,7 +227,7 @@ class PkPass {
final Map<String, Map<String, dynamic>>? languageData;

/// The bytes of this PkPass
final List<int> sourceData;
final Uint8List sourceData;

/// Indicates whether a webservices is available.
bool get isWebServiceAvailable => pass.webServiceURL != null;
Expand Down Expand Up @@ -288,7 +286,7 @@ class PkPass {

final manifest = <String, String>{};
for (final file in archive.files) {
manifest[file.name] = sha1.convert(file.content as List<int>).toString();
manifest[file.name] = sha1.convert(file.binaryContent).toString();
}

final manifestContent = encoder.convert(manifest);
Expand Down Expand Up @@ -324,13 +322,8 @@ class PkPass {
// This is intentionally not exposed to keep this an implementation detail.
// Tests should be written against the PkPass class directly.
extension on Archive {
List<int>? findBytesForFile(String fileName) =>
findFile(fileName)?.content as List<int>?;

Uint8List? findUint8ListForFile(String fileName) {
final data = findBytesForFile(fileName);
return data == null ? null : Uint8List.fromList(data);
}
Uint8List? findBytesForFile(String fileName) =>
findFile(fileName)?.binaryContent;

/// Returns a map of locale to a map of resolution to image bytes.
/// Returns null, if no image is localized
Expand Down Expand Up @@ -359,11 +352,11 @@ extension on Archive {
}

if (fileName.endsWith('@2x.png')) {
map[language]![2] = Uint8List.fromList(file.content as List<int>);
map[language]![2] = file.binaryContent;
} else if (fileName.endsWith('@3x.png')) {
map[language]![3] = Uint8List.fromList(file.content as List<int>);
map[language]![3] = file.binaryContent;
} else {
map[language]![1] = Uint8List.fromList(file.content as List<int>);
map[language]![1] = file.binaryContent;
}
}

Expand All @@ -387,9 +380,9 @@ extension on Archive {

PkImage? loadImage(String name) {
return PkImage.fromImages(
image1: findUint8ListForFile('$name.png'),
image2: findUint8ListForFile('$name@2x.png'),
image3: findUint8ListForFile('$name@3x.png'),
image1: findBytesForFile('$name.png'),
image2: findBytesForFile('$name@2x.png'),
image3: findBytesForFile('$name@3x.png'),
localizedImages: loadLocalizedImage(name),
);
}
Expand All @@ -407,8 +400,7 @@ extension on Archive {
for (final languageFile in translationFiles) {
final language = languageFile.name.split('.').first;

languageData[language] =
parseStringsFile(languageFile.content as List<int>);
languageData[language] = parseStringsFile(languageFile.binaryContent);
}
return languageData;
}
Expand Down Expand Up @@ -460,7 +452,7 @@ extension on Archive {

for (final file in filesWithoutSignatureAndManifest) {
final checksumInManifest = manifest[file.name] as String?;
final digest = sha1.convert(file.content as List<int>);
final digest = sha1.convert(file.binaryContent);
if (checksumInManifest != digest.toString()) {
throw ChecksumMismatchException(file.name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:convert';
import 'dart:typed_data';

/// Parses [content] to a [Map<String, String>] which contains the
/// key-value-pairs for translations.
Map<String, String> parseStringsFile(List<int> content) {
Map<String, String> parseStringsFile(Uint8List content) {
final string = _stringsFileDecoder.convert(content);
return naiveStringsFileParser(string);
}
Expand Down

0 comments on commit b05ec75

Please sign in to comment.