getCryptoIdentityState method

Future<({bool connected, bool crossSigningEnabled, bool initialized, bool keyBackupEnabled})> getCryptoIdentityState()

Returns the current state of the crypto identity.

keyBackupEnabled and crossSigningEnabled report each half independently. The crypto identity is initialized if both are set up. You can initialize a new account by using Client.initCryptoIdentity().

When initialized is false but the account already holds part of an identity (missing key backup or cross-signing, or incomplete cross-signing keys), heal it in place with Client.initCryptoIdentity( reuseExistingStorageRecoveryKeyOrPassphrase: …) and selective setup* / wipe* flags instead of wiping via a fresh initCryptoIdentity().

The crypto identity is connected if this device has all the secrets cached locally. This usually includes that this device has signed itself. You can use Client.restoreCryptoIdentity() to connect or Client.initCryptoIdentity() to wipe the current identity in case of that you lost your recovery key / passphrase and have no other way to restore.

Implementation

Future<
  ({
    bool keyBackupEnabled,
    bool crossSigningEnabled,
    bool initialized,
    bool connected,
  })
>
getCryptoIdentityState() async {
  await accountDataLoading;
  await firstSyncReceived;

  // Make sure we have the necessary account data types synced. Workaround for
  // https://github.com/element-hq/synapse/issues/15500
  for (final type in [
    EventTypes.MegolmBackup,
    EventTypes.CrossSigningSelfSigning,
    EventTypes.CrossSigningUserSigning,
    EventTypes.CrossSigningMasterKey,
  ]) {
    try {
      accountData[type] ??= BasicEvent(
        content: await getAccountData(userID!, type),
        type: type,
      );
    } on MatrixException catch (e) {
      if (e.error != MatrixError.M_NOT_FOUND) rethrow;
    }
  }

  final keyBackupEnabled = encryption?.keyManager.enabled ?? false;
  final crossSigningEnabled = encryption?.crossSigning.enabled ?? false;
  return (
    keyBackupEnabled: keyBackupEnabled,
    crossSigningEnabled: crossSigningEnabled,
    initialized: keyBackupEnabled && crossSigningEnabled,
    connected:
        ((await encryption?.keyManager.isCached()) ?? false) &&
        ((await encryption?.crossSigning.isCached()) ?? false),
  );
}