getEncryptedRequestCore function

Future<IEncryptedRequest> getEncryptedRequestCore({
  1. required String receiverAddress,
  2. required ConnectedUser senderConnectedUser,
  3. required String message,
  4. required bool isGroup,
  5. GroupInfoDTO? group,
  6. required String secretKey,
})

Implementation

Future<IEncryptedRequest> getEncryptedRequestCore({
  required String receiverAddress,
  required ConnectedUser senderConnectedUser,
  required String message,
  required bool isGroup,
  GroupInfoDTO? group,
  required String secretKey,
}) async {
  if (!isGroup) {
    if (!isValidETHAddress(receiverAddress)) {
      throw Exception('Invalid receiver address!');
    }

    User? receiverCreatedUser = await getUser(address: receiverAddress);

    if (receiverCreatedUser != null || receiverCreatedUser?.publicKey != null) {
      await createUserService(
        user: receiverAddress,
        publicKey: '',
        encryptedPrivateKey: '',
        wallet: getWallet(address: receiverAddress),
      );

      // If the user is being created here, that means that user don't have a PGP keys. So this intent will be in plaintext

      final signature = await signMessageWithPGPCore(
        message: message,
        privateKeyArmored: senderConnectedUser.privateKey!,
      );

      return IEncryptedRequest(
        message: message,
        encryptionType: 'PlainText',
        aesEncryptedSecret: '',
        signature: signature,
      );
    } else {
      // It's possible for a user to be created but the PGP keys still not created

      if (receiverCreatedUser != null &&
          !receiverCreatedUser.publicKey!
              .contains('-----BEGIN PGP PUBLIC KEY BLOCK-----')) {
        final signature = await signMessageWithPGPCore(
          message: message,
          privateKeyArmored: senderConnectedUser.privateKey!,
        );

        return IEncryptedRequest(
          message: message,
          encryptionType: 'PlainText',
          aesEncryptedSecret: '',
          signature: signature,
        );
      } else {
        final response = await encryptAndSignCore(
          plainText: message,
          keys: [
            receiverCreatedUser!.publicKey!,
            senderConnectedUser.publicKey!
          ],
          senderPgpPrivateKey: senderConnectedUser.privateKey!,
          secretKey: secretKey,
        );

        return IEncryptedRequest(
            message: response['cipherText']!,
            encryptionType: 'pgp',
            aesEncryptedSecret: response['encryptedSecret']!,
            signature: response['signature']!);
      }
    }
  } else if (group != null) {
    if (group.isPublic) {
      final signature = await signMessageWithPGPCore(
        message: message,
        privateKeyArmored: senderConnectedUser.privateKey!,
      );

      return IEncryptedRequest(
          message: message,
          encryptionType: 'PlainText',
          aesEncryptedSecret: '',
          signature: signature);
    } else {
      // Private Groups

      // 1. Private Groups with session keys
      if (group.sessionKey != null && group.encryptedSecret != null) {
        final cipherText = await aesEncrypt(
          plainText: message,
          secretKey: secretKey,
        );

        final signature = await pgphelper.sign(
          message: cipherText,
          privateKey: senderConnectedUser.privateKey!,
        );
        return IEncryptedRequest(
          message: cipherText,
          encryptionType: 'pgpv1:group',
          aesEncryptedSecret: '',
          signature: signature,
        );
      } else {
        final members =
            await getAllGroupMembersPublicKeys(chatId: group.chatId);
        final publicKeys = members.map((e) => e.publicKey).toList();

        final response = await encryptAndSignCore(
          plainText: message,
          keys: publicKeys,
          senderPgpPrivateKey: senderConnectedUser.privateKey!,
          secretKey: secretKey,
        );

        return IEncryptedRequest(
            message: response['cipherText']!,
            encryptionType: 'pgp',
            aesEncryptedSecret: response['encryptedSecret']!,
            signature: response['signature']!);
      }
    }
  } else {
    throw Exception('Error in encryption');
  }
}