createGroup function

Future<GroupDTO?> createGroup({
  1. String? account,
  2. Signer? signer,
  3. required String groupName,
  4. required String groupDescription,
  5. String? groupImage,
  6. required List<String> members,
  7. required List<String> admins,
  8. required bool isPublic,
  9. String? contractAddressNFT,
  10. int? numberOfNFTs,
  11. String? contractAddressERC20,
  12. int? numberOfERC20,
  13. String? pgpPrivateKey,
  14. String? meta,
  15. String? groupType,
  16. DateTime? scheduleAt,
  17. DateTime? scheduleEnd,
})

Implementation

Future<GroupDTO?> createGroup({
  String? account,
  Signer? signer,
  required String groupName,
  required String groupDescription,
  String? groupImage,
  required List<String> members,
  required List<String> admins,
  required bool isPublic,
  String? contractAddressNFT,
  int? numberOfNFTs,
  String? contractAddressERC20,
  int? numberOfERC20,
  String? pgpPrivateKey,
  String? meta,
  String? groupType,
  DateTime? scheduleAt,
  DateTime? scheduleEnd,
}) async {
  try {
    account ??= getCachedWallet()?.address;
    signer ??= getCachedWallet()?.signer;
    pgpPrivateKey ??= getCachedWallet()?.pgpPrivateKey;

    if (account == null && signer == null) {
      throw Exception('At least one from account or signer is necessary!');
    }
    final wallet = getWallet(address: account, signer: signer);
    String address = getAccountAddress(wallet);

    createGroupRequestValidator(
        groupName: groupName,
        groupDescription: groupDescription,
        members: members,
        admins: admins);

    final convertedMembersDIDList =
        await Future.wait(members.map((item) => getUserDID(address: item)));
    final convertedAdminsDIDList =
        await Future.wait(admins.map((item) => getUserDID(address: item)));

    final connectedUser = await getConnectedUserV2(
      wallet: wallet,
      privateKey: pgpPrivateKey,
    );
    final bodyToBeHashed = {
      'groupName': groupName,
      'groupDescription': groupDescription,
      'members': convertedMembersDIDList,
      'groupImage': groupImage,
      'admins': convertedAdminsDIDList,
      'isPublic': isPublic,
      'contractAddressNFT': contractAddressNFT,
      'numberOfNFTs': numberOfNFTs ?? 0,
      'contractAddressERC20': contractAddressERC20,
      'numberOfERC20': numberOfERC20 ?? 0,
      'groupCreator': "eip155:$address",
    };

    final hash = generateHash(bodyToBeHashed);
    final signature = await sign(
      message: hash,
      privateKey: connectedUser.privateKey!,
    );

    const sigType = 'pgp';

    final String verificationProof = '$sigType:$signature';

    groupType ??= "default";

    final body = {
      'groupName': groupName,
      'groupDescription': groupDescription,
      'members': convertedMembersDIDList,
      'groupImage': groupImage,
      'admins': convertedAdminsDIDList,
      'isPublic': isPublic,
      'contractAddressNFT': contractAddressNFT,
      'numberOfNFTs': numberOfNFTs ?? 0,
      'contractAddressERC20': contractAddressERC20,
      'numberOfERC20': numberOfERC20 ?? 0,
      'groupCreator': "eip155:$address",
      'verificationProof': verificationProof,
      'groupType': groupType,
      'scheduleAt': scheduleAt?.toIso8601String(),
      'scheduleEnd': scheduleEnd?.toIso8601String(),
    };
    if (meta != null) {
      body['meta'] = meta;
    }

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

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