askSetupOnlineKeyBackup method

Future<void> askSetupOnlineKeyBackup(
  1. bool setup
)

Implementation

Future<void> askSetupOnlineKeyBackup(bool setup) async {
  if (state != BootstrapState.askSetupOnlineKeyBackup) {
    throw BootstrapBadStateException();
  }
  if (!setup) {
    state = BootstrapState.done;
    return;
  }
  try {
    final keyObj = olm.PkDecryption();
    String pubKey;
    Uint8List privKey;
    try {
      pubKey = keyObj.generate_key();
      privKey = keyObj.get_private_key();
    } finally {
      keyObj.free();
    }
    Logs().v('Create the new backup version...');
    await client.postRoomKeysVersion(
      BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2,
      <String, dynamic>{
        'public_key': pubKey,
      },
    );
    Logs().v('Store the secret...');
    await newSsssKey?.store(megolmKey, base64.encode(privKey));

    Logs().v(
      'And finally set all megolm keys as needing to be uploaded again...',
    );
    await client.database?.markInboundGroupSessionsAsNeedingUpload();
    Logs().v('And uploading keys...');
    await client.encryption?.keyManager.uploadInboundGroupSessions();
  } catch (e, s) {
    Logs().e('[Bootstrapping] Error setting up online key backup', e, s);
    state = BootstrapState.error;
    encryption.client.onEncryptionError.add(
      SdkError(exception: e, stackTrace: s),
    );
    return;
  }
  state = BootstrapState.done;
}