DidcommPlaintextMessage.fromJson constructor

DidcommPlaintextMessage.fromJson(
  1. dynamic message
)

Implementation

DidcommPlaintextMessage.fromJson(dynamic message) {
  Map<String, dynamic> decoded = credentialToMap(message);
  id = decoded['id']!;
  type = decoded['type']!;
  replyUrl = decoded['reply_url'];
  if (decoded.containsKey('reply_to') && decoded['reply_to'] != null) {
    replyTo = decoded['reply_to'].cast<String>();
  }
  if (decoded.containsKey('body')) {
    Map tmp = decoded['body'];
    if (tmp.isEmpty) {
      body = {};
    } else {
      body = tmp.cast<String, dynamic>();
    }
  } else {
    body = {};
    if (type != 'https://didcomm.org/empty/1.0') {
      throw Exception('Empty Body only allowed in Empty Message');
    }
  }
  from = decoded['from'];
  to = decoded['to'];
  threadId = decoded['thid'];
  parentThreadId = decoded['pthid'];
  if (decoded.containsKey('typ')) {
    String typTmp = decoded['typ'];
    switch (typTmp) {
      case 'application/didcomm-plain+json':
        typ = DidcommMessageTyp.plain;
        break;
      case 'application/didcomm-signed+json':
        typ = DidcommMessageTyp.signed;
        break;
      case 'application/didcomm-encrypted+json':
        typ = DidcommMessageTyp.encrypted;
        break;
      default:
        throw Exception('Unknown typ field $typTmp');
    }
  }
  var tmp = decoded['created_time'];
  if (tmp != null) {
    createdTime =
        DateTime.fromMillisecondsSinceEpoch(tmp * 1000, isUtc: true);
  }
  tmp = decoded['expires_time'];
  if (tmp != null) {
    expiresTime =
        DateTime.fromMillisecondsSinceEpoch(tmp * 1000, isUtc: true);
  }

  if (decoded.containsKey('from_prior')) {
    fromPrior = FromPriorJWT.fromCompactSerialization(decoded['from_prior']);
    if (fromPrior != null && from != null) {
      if (from != fromPrior!.sub) {
        throw Exception('from value must match from_prior.sub');
      }
    }
  }

  if (decoded.containsKey('attachments')) {
    List tmp = decoded['attachments'];
    if (tmp.isNotEmpty) {
      attachments = [];
      for (var a in tmp) {
        attachments!.add(Attachment.fromJson(a));
      }
    }
  }
  if (decoded.containsKey('please_ack') && decoded['please_ack'] != null) {
    pleaseAck = decoded['please_ack'].cast<String>();
  }
  if (decoded.containsKey('ack') && decoded['ack'] != null) {
    ack = decoded['ack'].cast<String>();
  }

  if (decoded.containsKey('web_redirect') &&
      decoded['web_redirect'] != null) {
    webRedirect = WebRedirect.fromJson(decoded['web_redirect']);
  }

  if (decoded.containsKey('return_route')) {
    var tmp = decoded['return_route'];
    switch (tmp) {
      case 'all':
        returnRoute = ReturnRouteValue.all;
        break;
      case 'none':
        returnRoute = ReturnRouteValue.none;
        break;
      case 'thread':
        returnRoute = ReturnRouteValue.thread;
        break;
    }
  }
  decoded.remove('to');
  decoded.remove('from');
  decoded.remove('id');
  decoded.remove('type');
  decoded.remove('typ');
  decoded.remove('thid');
  decoded.remove('pthid');
  decoded.remove('created_time');
  decoded.remove('expires_time');
  decoded.remove('body');
  decoded.remove('from_prior');
  decoded.remove('attachments');
  decoded.remove('ack');
  decoded.remove('please_ack');
  decoded.remove('reply_to');
  decoded.remove('reply_url');
  decoded.remove('web_redirect');
  decoded.remove('return_route');
  if (decoded.isNotEmpty) additionalHeaders = decoded;
}