sendMessagePayloadCore function

Future<SendMessagePayload> sendMessagePayloadCore({
  1. required String receiverAddress,
  2. required ConnectedUser senderConnectedUser,
  3. String? senderAddress,
  4. String? senderPgpPrivateKey,
  5. required String messageType,
  6. required String messageContent,
  7. dynamic messageObj,
  8. GroupInfoDTO? group,
  9. required bool isGroup,
})

Implementation

Future<SendMessagePayload> sendMessagePayloadCore({
  required String receiverAddress,
  required ConnectedUser senderConnectedUser,
  String? senderAddress,
  String? senderPgpPrivateKey,
  required String messageType,
  required String messageContent,
  dynamic messageObj,
  GroupInfoDTO? group,
  required bool isGroup,
}) async {
  late String secretKey;
  if (isGroup && group?.encryptedSecret != null && group?.sessionKey != null) {
    secretKey = await pgpDecrypt(
        cipherText: group!.encryptedSecret!,
        privateKeyArmored: senderConnectedUser.privateKey!);
  } else {
    secretKey = generateRandomSecret(15);
  }

  final encryptedMessageContentData = await getEncryptedRequestCore(
    receiverAddress: receiverAddress,
    message: messageContent,
    isGroup: isGroup,
    group: group,
    secretKey: secretKey,
    senderConnectedUser: senderConnectedUser,
  );

  final encryptedMessageContent = encryptedMessageContentData.message;
  final deprecatedSignature =
      removeVersionFromPublicKey(encryptedMessageContentData.signature);

  final encryptedMessageObjData = await getEncryptedRequestCore(
    receiverAddress: receiverAddress,
    message: jsonEncode(messageObj?.toJson()),
    senderConnectedUser: senderConnectedUser,
    isGroup: isGroup,
    group: group,
    secretKey: secretKey,
  );

  final encryptedMessageObj = encryptedMessageObjData.message;
  final encryptionType = encryptedMessageObjData.encryptionType;
  final encryptedMessageObjSecret =
      removeVersionFromPublicKey(encryptedMessageObjData.aesEncryptedSecret);

  return SendMessagePayload(
    fromDID: walletToPCAIP10(senderAddress!),
    toDID: !isGroup ? walletToPCAIP10(receiverAddress) : group?.chatId ?? '',
    fromCAIP10: walletToPCAIP10(senderAddress),
    toCAIP10: !isGroup ? walletToPCAIP10(receiverAddress) : group?.chatId ?? '',
    messageContent: encryptedMessageContent,
    messageObj: encryptionType == 'PlainText'
        ? messageObj?.toJson()
        : encryptedMessageObj,
    messageType: messageType,
    signature: deprecatedSignature,
    verificationProof: "pgp:$deprecatedSignature",
    encType: encryptionType,
    encryptedSecret: encryptedMessageObjSecret,
    sigType: "pgp",
  );
}