Skip to content

Commit

Permalink
fix: check if secrets are cached instead of checking sync
Browse files Browse the repository at this point in the history
basically checked everywhere if the accountData update would have been used, if not just removed the checks
  • Loading branch information
td-famedly committed Nov 8, 2023
1 parent 327f95d commit 9ba5952
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
11 changes: 6 additions & 5 deletions lib/encryption/ssss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,9 @@ class SSSS {

final accountDataType = EventTypes.secretStorageKey(keyId);
// noooow we set the account data
final waitForAccountData = client.onSync.stream.firstWhere((syncUpdate) =>
syncUpdate.accountData != null &&
syncUpdate.accountData!
.any((accountData) => accountData.type == accountDataType));

await client.setAccountData(
client.userID!, accountDataType, content.toJson());
await waitForAccountData;

final key = open(keyId);
await key.setPrivateKey(privateKey);
Expand Down Expand Up @@ -747,6 +743,11 @@ class OpenSSSS {
throw Exception('SSSS not unlocked');
}
await ssss.store(type, secret, keyId, privateKey, add: add);

while (!(await ssss.encryption.keyManager.isCached())) {
Logs().v('Wait for secret to come down sync');
await ssss.client.oneShotSync(timeout: Duration(seconds: 3));
}
}

Future<void> validateAndStripOtherKeys(String type, String secret) async {
Expand Down
46 changes: 14 additions & 32 deletions lib/encryption/utils/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,15 @@ class Bootstrap {
}
// 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;
// how useful is this? wouldn't a old ssss be fine here anyway?
while (client.accountData
.where((_, BasicEvent event) =>
event.type == EventTypes.SecretStorageDefaultKey)
.isNotEmpty) {
Logs().v('Waiting accountData to have m.secret_storage.default_key');
await client.oneShotSync(timeout: Duration(seconds: 3));
}
if (oldSsssKeys != null) {
for (final entry in secretMap!.entries) {
Logs().v('Validate and stripe other keys ${entry.key}...');
Expand Down Expand Up @@ -479,33 +482,20 @@ class Bootstrap {
));
Logs().v('Device signing keys have been uploaded.');
// aaaand set the SSSS secrets
final futures = <Future<void>>[];
if (masterKey != null) {
futures.add(
client.onSync.stream
.firstWhere((syncUpdate) =>
masterKey?.publicKey != null &&
client.userDeviceKeys[client.userID]?.masterKey?.ed25519Key ==
masterKey?.publicKey)
.then((_) => Logs().v('New Master Key was created')),
);
while (!(masterKey.publicKey != null &&
client.userDeviceKeys[client.userID]?.masterKey?.ed25519Key ==
masterKey.publicKey)) {
Logs().v('Waiting for master to be created');
await client.oneShotSync(timeout: Duration(seconds: 3));
}
}
for (final entry in secretsToStore.entries) {
futures.add(
client.onSync.stream
.firstWhere((syncUpdate) =>
syncUpdate.accountData != null &&
syncUpdate.accountData!
.any((accountData) => accountData.type == entry.key))
.then((_) =>
Logs().v('New Key with type ${entry.key} was created')),
);
Logs().v('Store new SSSS key ${entry.key}...');
await newSsssKey?.store(entry.key, entry.value);
}
Logs().v(
'Wait for MasterKey and ${secretsToStore.entries.length} keys to be created');
await Future.wait<void>(futures);
final keysToSign = <SignableKey>[];
if (masterKey != null) {
if (client.userDeviceKeys[client.userID]?.masterKey?.ed25519Key !=
Expand Down Expand Up @@ -581,14 +571,6 @@ class Bootstrap {
);
Logs().v('Store the secret...');
await newSsssKey?.store(megolmKey, base64.encode(privKey));
Logs().v('Wait for secret to come down sync');

if (!await encryption.keyManager.isCached()) {
await client.onSync.stream.firstWhere((syncUpdate) =>
syncUpdate.accountData != null &&
syncUpdate.accountData!
.any((accountData) => accountData.type == megolmKey));
}

Logs().v(
'And finally set all megolm keys as needing to be uploaded again...');
Expand Down
4 changes: 2 additions & 2 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1641,8 +1641,8 @@ class Client extends MatrixApi {

/// Immediately start a sync and wait for completion.
/// If there is an active sync already, wait for the active sync instead.
Future<void> oneShotSync() {
return _sync();
Future<void> oneShotSync({Duration? timeout}) {
return _sync(timeout: timeout);
}

/// Pass a timeout to set how long the server waits before sending an empty response.
Expand Down

0 comments on commit 9ba5952

Please sign in to comment.