publish method
Stores diff as the next patch for target.
patchedHash is the hex SHA-256 of the file the diff turns the release
into -- not of the diff -- because that is what the updater verifies.
For a release registered with a key, hashSignature must be that key's
signature of patchedHash; anything else is refused with a
FormatException, since every device would refuse it too.
Implementation
DVShorebirdPatch publish(
DVShorebirdPatchTarget target, {
required List<int> diff,
required String patchedHash,
String channel = 'stable',
String? hashSignature,
int rolloutPercent = 100,
}) {
if (!_sha256.hasMatch(patchedHash)) {
throw const FormatException('The hash is a hex SHA-256.');
}
_dir(<String>[channel]);
_dir(target._segments);
if (rolloutPercent < 0 || rolloutPercent > 100) {
throw const FormatException('A rollout is 0 to 100 per cent.');
}
final String? key = releaseRecord(
appId: target.appId,
releaseVersion: target.releaseVersion,
platform: target.platform,
)?.patchPublicKey;
if (key != null) {
if (hashSignature == null || hashSignature.isEmpty) {
throw const FormatException(
'This release was built with a signing key, so its devices refuse '
'unsigned patches. Sign the patch with the release\'s private key.',
);
}
if (!DVPatchSigning.verifyHash(patchedHash, hashSignature, key)) {
throw const FormatException(
'The patch\'s signature does not verify against the key this '
'release was built with, so every device would refuse it.',
);
}
}
final List<DVShorebirdPatch> existing = patches(target);
final DVShorebirdPatch patch = DVShorebirdPatch(
number: existing.isEmpty ? 1 : existing.last.number + 1,
hash: patchedHash,
channel: channel,
rolledBack: false,
hashSignature: hashSignature,
rolloutPercent: rolloutPercent,
);
final Directory dir = Directory(
_dir(<String>[...target._segments, '${patch.number}']),
)..createSync(recursive: true);
File('${dir.path}${Platform.pathSeparator}patch.bin')
.writeAsBytesSync(diff);
File('${dir.path}${Platform.pathSeparator}patch.json')
.writeAsStringSync(jsonEncode(patch.toJson()));
return patch;
}