createGroupCoreV2 function

Future<GroupInfoDTO?> createGroupCoreV2({
  1. required ChatCreateGroupTypeV2 options,
})

Implementation

Future<GroupInfoDTO?> createGroupCoreV2(
    {required ChatCreateGroupTypeV2 options}) async {
  try {
    options.account ??= getCachedWallet()?.address;
    options.signer ??= getCachedWallet()?.signer;
    options.pgpPrivateKey ??= getCachedWallet()?.pgpPrivateKey;

    if (options.account == null && options.signer == null) {
      throw Exception('At least one from account or signer is necessary!');
    }
    final wallet = getWallet(address: options.account, signer: options.signer);
    final connectedUser = await getConnectedUserV2(
      wallet: wallet,
      privateKey: options.pgpPrivateKey,
    );

    /**
     * VALIDATIONS
     */
    createGroupV2OptionsValidator(options: options);

    final convertedMembers = await Future.wait(
        options.members.map((item) => getUserDID(address: item)));
    final convertedAdmins = await Future.wait(
        options.admins.map((item) => getUserDID(address: item)));

    /**
     * PROFILE VERIFICATION PROOF
     */
    final profileVerificationBody = {
      'groupName': options.groupName,
      'groupDescription': options.groupDescription,
      'groupImage': options.groupImage,
      'rules': options.rules ?? {},
      'isPublic': options.isPublic,
      'groupType': options.groupType,
    };

    final profileHash = generateHash(profileVerificationBody);
    final profileSignature = await sign(
      message: profileHash,
      privateKey: connectedUser.privateKey!,
    );

    final profileVerificationProof =
        'pgpv2:$profileSignature:${connectedUser.did}';

    /**
     * CONFIG VERIFICATION PROOF
     */

    final configVerificationBody = {
      'meta': options.config.meta,
      'scheduleAt': options.config.scheduleAt,
      'scheduleEnd': options.config.scheduleEnd,
      'status': options.config.status,
    };

    final configHash = generateHash(configVerificationBody);
    final configSignature = await sign(
      message: configHash,
      privateKey: connectedUser.privateKey!,
    );

    final configVerificationProof =
        'pgpv2:$configSignature:${connectedUser.did}';

    /**
     * IDEMPOTENT VERIFICATION PROOF
     */
    final idempotentVerificationBody = {
      'members': convertedMembers,
      'admins': convertedAdmins,
    };

    final idempotentHash = generateHash(idempotentVerificationBody);
    final idempotentSignature = await sign(
      message: idempotentHash,
      privateKey: connectedUser.privateKey!,
    );
    final String idempotentVerificationProof =
        'pgpv2:$idempotentSignature:${connectedUser.did}';

    final body = {
      'groupName': options.groupName,
      'groupDescription': options.groupDescription,
      'groupImage': options.groupImage,
      'rules': options.rules ?? {},
      'isPublic': options.isPublic,
      'groupType': options.groupType,
      'profileVerificationProof': profileVerificationProof,
      'config': {
        'meta': options.config.meta,
        'scheduleAt': options.config.scheduleAt,
        'scheduleEnd': options.config.scheduleEnd,
        'status': options.config.status,
        'configVerificationProof': configVerificationProof,
      },
      'members': convertedMembers,
      'admins': convertedAdmins,
      'idempotentVerificationProof': idempotentVerificationProof,
    };

    final result = await http.post(
      path: '/v2/chat/groups',
      data: body,
    );

    if (result == null || result is String) {
      throw Exception(result);
    }
    return GroupInfoDTO.fromJson(result);
  } catch (e) {
    log("[Push SDK] - API  - Error - API createGroup -: $e ");
    rethrow;
  }
}