SignedEnvelope.fromJson constructor

SignedEnvelope.fromJson(
  1. Map json
)

Parses json, refusing anything that is not structurally an envelope.

An empty signatures array is refused rather than treated as unsigned: an envelope nobody signed is not one that verifies vacuously.

Implementation

factory SignedEnvelope.fromJson(Map json) {
  final payload = json['payload'];
  if (payload is! String) {
    throw AtSigningVerificationException(
        'an envelope must carry its payload as a string');
  }
  final signatures = json['signatures'];
  if (signatures is! List || signatures.isEmpty) {
    throw AtSigningVerificationException(
        'an envelope must carry a non-empty signatures array');
  }
  final parsed = signatures.map(EnvelopeSignature.fromJson).toList();
  // NOTE: over `enid`, not `kid` — an envelope signed under two algorithms
  // carries a different key per entry by construction. What must not differ
  // is the signer, or the entry that verifies and the entry a caller reads
  // `signerEnrollmentId` from can be different entries.
  final enids = {for (final s in parsed) s.enid};
  if (enids.length > 1) {
    throw AtSigningVerificationException(
        'an envelope carries one signer\'s signatures, and this one names '
        '${enids.map((e) => '"$e"').join(', ')} — refusing rather than '
        'verifying under one of them and reporting another');
  }
  final types = {for (final s in parsed) s.typ};
  if (types.length > 1) {
    throw AtSigningVerificationException(
        'an envelope is signed for one purpose, and this one is typed '
        '${types.map((t) => '"$t"').join(', ')} — refusing rather than '
        'enforcing one of them and being read as another');
  }
  return SignedEnvelope._(payload, parsed);
}