newSsss method

Future<void> newSsss([
  1. String? passphrase
])

Implementation

Future<void> newSsss([String? passphrase]) async {
  if (state != BootstrapState.askNewSsss) {
    throw BootstrapBadStateException('Wrong State');
  }
  state = BootstrapState.loading;
  try {
    Logs().v('Create key...');
    newSsssKey = await encryption.ssss.createKey(passphrase);
    if (oldSsssKeys != null) {
      // alright, we have to re-encrypt old secrets with the new key
      final secrets = analyzeSecrets();
      Set<String> removeKey(String key) {
        final s = secrets.entries
            .where((e) => e.value.contains(key))
            .map((e) => e.key)
            .toSet();
        secrets.removeWhere((k, v) => v.contains(key));
        return s;
      }

      secretMap = <String, String>{};
      for (final entry in oldSsssKeys!.entries) {
        final key = entry.value;
        final keyId = entry.key;
        if (!key.isUnlocked) {
          continue;
        }
        for (final s in removeKey(keyId)) {
          Logs().v('Get stored key of type $s...');
          secretMap![s] = await key.getStored(s);
          Logs().v('Store new secret with this key...');
          await newSsssKey!.store(s, secretMap![s]!, add: true);
        }
      }
      // alright, we re-encrypted all the secrets. We delete the dead weight only *after* we set our key to the default key
    }
    final updatedAccountData = client.onSync.stream.firstWhere((syncUpdate) =>
        syncUpdate.accountData != null &&
        syncUpdate.accountData!.any((accountData) =>
            accountData.type == EventTypes.SecretStorageDefaultKey));
    await encryption.ssss.setDefaultKeyId(newSsssKey!.keyId);
    await updatedAccountData;
    if (oldSsssKeys != null) {
      for (final entry in secretMap!.entries) {
        Logs().v('Validate and stripe other keys ${entry.key}...');
        await newSsssKey!.validateAndStripOtherKeys(entry.key, entry.value);
      }
      Logs().v('And make super sure we have everything cached...');
      await newSsssKey!.maybeCacheAll();
    }
  } catch (e, s) {
    Logs().e('[Bootstrapping] Error trying to migrate old secrets', e, s);
    state = BootstrapState.error;
    return;
  }
  // alright, we successfully migrated all secrets, if needed

  checkCrossSigning();
}