oobOfferCredential function

OutOfBandMessage oobOfferCredential({
  1. required Map<String, dynamic> credential,
  2. required String oobId,
  3. required String threadId,
  4. required List<String> replyTo,
  5. required String issuerDid,
  6. required String connectionDid,
  7. String proofType = 'Ed25519Signature',
})

offers a credential using oob

a credential is understood as a template having the following syntax

{
  "@context": ["context1", "context2"],
  "type": ["type1", "type2"],

}

Implementation

OutOfBandMessage oobOfferCredential({
  required Map<String, dynamic> credential,
  required String oobId,
  required String threadId,
  required List<String> replyTo,
  required String issuerDid,
  required String connectionDid,
  String proofType = 'Ed25519Signature',
}) {
  try {
    addElementToListOrInit(
        credential, ['@context'], 'https://www.w3.org/2018/credentials/v1');
  } on JsonPathException catch (e) {
    throw OobTemplateWrongValueException(
        'The @context field is invalid.\n'
        'Details: $e',
        code: 234234543);
  }

  // have @context only having unique entries
  credential['@context'] = (credential['@context'] as List).toSet().toList();

  try {
    forceAsList(credential, ['type']);
  } on JsonPathException catch (e) {
    throw OobTemplateMissingValueException(
        'The credential must have a `type`'
        ' field set.\nDetails: $e',
        code: 34583495834);
  }

  if (!credential.containsKey('id')) {
    credential['id'] = 'did:key:000';
  }

  String? expirationDateStr = credential.remove('expirationDate');
  DateTime? expirationDate;

  if (expirationDateStr != null) {
    try {
      expirationDate = DateTime.parse(expirationDateStr);
    } catch (e) {
      throw OobTemplateWrongValueException(
          "The expirationDate could not be parsed.\n"
          'Details: `$e`',
          code: 84758394);
    }
  }

  String? issuanceDateStr = credential.remove('issuanceDate');
  DateTime? issuanceDate;

  if (issuanceDateStr != null) {
    try {
      issuanceDate = DateTime.parse(issuanceDateStr);
    } catch (e) {
      throw OobTemplateWrongValueException(
          "The issuanceDate could not be parsed"
          'Details: `$e`',
          code: 989043853904);
    }
  }

  if (!credential.containsKey('credentialSubject')) {
    throw OobTemplateMissingValueException(
        "The credential must have a `credentialSubject`"
        " field set.",
        code: 84309583490);
  }

  if (credential['credentialSubject'] is! Map) {
    throw OobTemplateWrongValueException(
        "The credentialSubject must be a mapping.",
        code: 543453499);
  }

  var issuer = credential['issuer'];
  if (issuer == null) {
    issuer = issuerDid;
  } else if (issuer is Map) {
    issuer['id'] = issuerDid;
  }

  var vc = VerifiableCredential(
      context: (credential.remove('@context') as List).cast<String>(),
      type: ['VerifiableCredential', ...credential.remove('type')],
      issuer: issuer,
      expirationDate: expirationDate,
      credentialSubject: credential['credentialSubject'],
      issuanceDate: issuanceDate ?? DateTime.now());

  var offer = OfferCredential(
      id: oobId,
      threadId: threadId,
      detail: [
        LdProofVcDetail(
            credential: vc,
            options: LdProofVcDetailOptions(proofType: proofType))
      ],
      replyTo: replyTo);

  return OutOfBandMessage(
      id: oobId,
      threadId: threadId,
      from: connectionDid,
      replyTo: replyTo,
      goalCode: 'streamlined-vc',
      attachments: [Attachment(data: AttachmentData(json: offer.toJson()))]);
}