getDiscussionKeyAccess method

Future<Uint8List> getDiscussionKeyAccess({
  1. required ApiService apiService,
  2. required String discussionSCAddress,
  3. required KeyPair keyPair,
})

This method get the AES Key used to encrypt and decrypt informations in the discussion (messages, discussion's properties)

Implementation

Future<Uint8List> getDiscussionKeyAccess({
  required ApiService apiService,
  required String discussionSCAddress,
  required KeyPair keyPair,
}) async {
  // Get message key from SC secret
  final mapTransactionOwnerships =
      await apiService.getTransactionOwnerships([discussionSCAddress]);
  final ownerships = mapTransactionOwnerships[discussionSCAddress];
  if (ownerships == null && ownerships!.isEmpty) {
    throw Exception();
  }

  final authorizedPublicKey = ownerships[1].authorizedPublicKeys.firstWhere(
        (AuthorizedKey authKey) =>
            authKey.publicKey!.toUpperCase() ==
            uint8ListToHex(Uint8List.fromList(keyPair.publicKey!))
                .toUpperCase(),
        orElse: AuthorizedKey.new,
      );
  if (authorizedPublicKey.encryptedSecretKey == null) {
    throw Exception();
  }
  final aesKey = ecDecrypt(
    authorizedPublicKey.encryptedSecretKey,
    Uint8List.fromList(keyPair.privateKey!),
  );
  return aesDecrypt(ownerships[1].secret, aesKey);
}