-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from syphon-org/bug-fixes-0.1.4
[0.1.4] Bugs, Performance, and Cache Overhaul
- Loading branch information
Showing
80 changed files
with
2,827 additions
and
1,734 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
|
||
### User Creation | ||
|
||
```dart | ||
/** | ||
* | ||
* https://matrix.org/docs/spec/client_server/latest#id204 | ||
* | ||
* | ||
* Email Request (?) | ||
* https://matrix-client.matrix.org/_matrix/client/r0/register/email/requestToken | ||
* | ||
* Request Token + SID | ||
* {"email":"[email protected]","client_secret":"MDWVwN79p5xIz7bgazVXvO8aabbVD0LN","send_attempt":1,"next_link":"https://app.element.io/#/register?client_secret=MDWVwN79p5xIz7bgazVXvO8aabbVD0LN&hs_url=https%3A%2F%2Fmatrix-client.matrix.org&is_url=https%3A%2F%2Fvector.im&session_id=yGElwHyWRFHwVkChpyWIJqMO"} | ||
* | ||
* Response Token + SID | ||
* {"sid": "UTWiabjnSXWWTAPs"} | ||
* | ||
* | ||
* Send Terms (?) | ||
* {"username":"syphon2","password":"testing again to see","initial_device_display_name":"app.element.io (Chrome, macOS)","auth":{"session":"yGElwHyWRFHwVkChpyWIJqMO","type":"m.login.terms"},"inhibit_login":true} | ||
* | ||
* Send Email Auth (?) | ||
* {"username":"syphon2","password":"testing again to see","initial_device_display_name":"app.element.io (Chrome, macOS)","auth":{"session":"yGElwHyWRFHwVkChpyWIJqMO","type":"m.login.email.identity","threepid_creds":{"sid":"UTWiabjnSXWWTAPs","client_secret":"MDWVwN79p5xIz7bgazVXvO8aabbVD0LN"},"threepidCreds":{"sid":"UTWiabjnSXWWTAPs","client_secret":"MDWVwN79p5xIz7bgazVXvO8aabbVD0LN"}},"inhibit_login":true} | ||
* | ||
*/ | ||
ThunkAction<AppState> createUser({enableErrors = false}) {} | ||
``` | ||
|
||
```dart | ||
// needed to test the recursive messaging 'catch-up' | ||
if (true) { | ||
printError('[fromMessageEvents] *** ${this.name} *** '); | ||
print('[limited] now ${limited}, before ${this.limited}'); | ||
print('[lastHash] now ${lastHash}, before ${this.lastHash}'); | ||
print('[prevHash] now ${prevHash}'); | ||
} | ||
``` | ||
|
||
```dart | ||
// original initStore function without much regar | ||
// for action types. Ideally, it would have none. | ||
Future<Store> initStore() async { | ||
// Configure redux persist instance | ||
final persistor = Persistor<AppState>( | ||
storage: MemoryStorage(), | ||
serializer: CacheSerializer(), | ||
throttleDuration: Duration(milliseconds: 4500), | ||
shouldSave: (Store<AppState> store, dynamic action) { | ||
switch (action.runtimeType) { | ||
case SetSyncing: | ||
case SetSynced: | ||
// debugPrint('[Redux Persist] cache skip'); | ||
return false; | ||
default: | ||
// debugPrint('[Redux Persist] caching'); | ||
return true; | ||
} | ||
}, | ||
); | ||
``` |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0ec68b01ef371337af2fda1b7201140f | ||
4d17cbc20ae4e206cdf903cbe1c6faf1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:hive/hive.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:steel_crypt/steel_crypt.dart'; | ||
import 'package:syphon/global/values.dart'; | ||
|
||
class CacheSecure { | ||
// encryption references (in memory only) | ||
static String ivKey; | ||
static String ivKeyNext; | ||
static String cryptKey; | ||
|
||
// cache refrences | ||
static Box cacheMain; | ||
static Box cacheRooms; | ||
static Box cacheCrypto; | ||
|
||
// cache storage identifiers | ||
static const cacheKeyMain = '${Values.appNameLabel}-main-cache'; | ||
static const cacheKeyRooms = '${Values.appNameLabel}-room-cache'; | ||
static const cacheKeyCrypto = '${Values.appNameLabel}-crypto-cache'; | ||
|
||
// cache key identifiers | ||
static const ivKeyLocation = '${Values.appNameLabel}@ivKey'; | ||
static const ivKeyNextLocation = '${Values.appNameLabel}@ivKeyNext'; | ||
static const cryptKeyLocation = '${Values.appNameLabel}@cryptKey'; | ||
|
||
// background data identifiers | ||
static const roomNamesKey = 'roomNamesKey'; | ||
static const protocolKey = 'protocol'; | ||
static const homeserverKey = 'homeserver'; | ||
static const accessTokenKey = 'accessToken'; | ||
static const lastSinceKey = 'lastSince'; | ||
static const userIdKey = 'userId'; | ||
} | ||
|
||
Future<void> initCache() async { | ||
// Init storage location | ||
final String storageLocation = await initStorageLocation(); | ||
|
||
// Init configuration | ||
Hive.init(storageLocation); | ||
|
||
CacheSecure.cacheMain = await unlockMainCache(); | ||
CacheSecure.cacheRooms = await unlockRoomCache(); | ||
CacheSecure.cacheCrypto = await unlockCryptoCache(); | ||
} | ||
|
||
Future<dynamic> initStorageLocation() async { | ||
var storageLocation; | ||
|
||
try { | ||
if (Platform.isIOS || Platform.isAndroid) { | ||
storageLocation = await getApplicationDocumentsDirectory(); | ||
return storageLocation.path; | ||
} | ||
|
||
if (Platform.isMacOS) { | ||
storageLocation = await File('cache').create().then( | ||
(value) => value.writeAsString( | ||
'{}', | ||
flush: true, | ||
), | ||
); | ||
|
||
return storageLocation.path; | ||
} | ||
|
||
if (Platform.isLinux) { | ||
storageLocation = await getApplicationDocumentsDirectory(); | ||
return storageLocation.path; | ||
} | ||
|
||
debugPrint('[initStorageLocation] no cache support'); | ||
return null; | ||
} catch (error) { | ||
debugPrint('[initStorageLocation] $error'); | ||
return null; | ||
} | ||
} | ||
|
||
// // Closes and saves storage | ||
void closeCache() async { | ||
if (CacheSecure.cacheMain != null && CacheSecure.cacheMain.isOpen) { | ||
CacheSecure.cacheMain.close(); | ||
} | ||
|
||
if (CacheSecure.cacheRooms != null && CacheSecure.cacheRooms.isOpen) { | ||
CacheSecure.cacheRooms.close(); | ||
} | ||
|
||
if (CacheSecure.cacheCrypto != null && CacheSecure.cacheCrypto.isOpen) { | ||
CacheSecure.cacheCrypto.close(); | ||
} | ||
} | ||
|
||
String createIVKey() { | ||
return CryptKey().genDart(); | ||
} | ||
|
||
Future<void> saveIVKey(String ivKey) async { | ||
// Check if storage has been created before | ||
return await FlutterSecureStorage().write( | ||
key: CacheSecure.ivKeyLocation, | ||
value: ivKey, | ||
); | ||
} | ||
|
||
Future<void> saveIVKeyNext(String ivKey) async { | ||
// Check if storage has been created before | ||
return await FlutterSecureStorage().write( | ||
key: CacheSecure.ivKeyNextLocation, | ||
value: ivKey, | ||
); | ||
} | ||
|
||
Future<String> unlockIVKey() async { | ||
// Check if storage has been created before | ||
final storageEngine = FlutterSecureStorage(); | ||
|
||
final ivKeyStored = await storageEngine.read( | ||
key: CacheSecure.ivKeyLocation, | ||
); | ||
|
||
// Create a encryptionKey if a serialized one is not found | ||
return ivKeyStored == null ? createIVKey() : ivKeyStored; | ||
} | ||
|
||
Future<String> unlockIVKeyNext() async { | ||
// Check if storage has been created before | ||
final storageEngine = FlutterSecureStorage(); | ||
|
||
final ivKeyStored = await storageEngine.read( | ||
key: CacheSecure.ivKeyNextLocation, | ||
); | ||
|
||
// Create a encryptionKey if a serialized one is not found | ||
return ivKeyStored == null ? createIVKey() : ivKeyStored; | ||
} | ||
|
||
Future<String> unlockCryptKey() async { | ||
final storageEngine = FlutterSecureStorage(); | ||
|
||
var cryptKey; | ||
|
||
try { | ||
// Check if crypt key already exists | ||
cryptKey = await storageEngine.read( | ||
key: CacheSecure.cryptKeyLocation, | ||
); | ||
} catch (error) { | ||
debugPrint('[unlockCryptKey] ${error}'); | ||
} | ||
|
||
// Create a encryptionKey if a serialized one is not found | ||
if (cryptKey == null) { | ||
cryptKey = CryptKey().genFortuna(len: 32); // 256 bits | ||
|
||
await storageEngine.write( | ||
key: CacheSecure.cryptKeyLocation, | ||
value: cryptKey, | ||
); | ||
} | ||
|
||
return cryptKey; | ||
} | ||
|
||
Future<Box> unlockMainCache() async { | ||
try { | ||
return await Hive.openBox( | ||
CacheSecure.cacheKeyMain, | ||
crashRecovery: true, | ||
compactionStrategy: (entries, deletedEntries) => deletedEntries > 1, | ||
); | ||
} catch (error) { | ||
debugPrint('[unlockMainCache] $error'); | ||
return null; | ||
} | ||
} | ||
|
||
Future<Box> unlockRoomCache() async { | ||
try { | ||
return await Hive.openBox( | ||
CacheSecure.cacheKeyRooms, | ||
crashRecovery: true, | ||
compactionStrategy: (entries, deletedEntries) => deletedEntries > 1, | ||
); | ||
} catch (error) { | ||
debugPrint('[unlockRoomCache] $error'); | ||
return null; | ||
} | ||
} | ||
|
||
Future<Box> unlockCryptoCache() async { | ||
try { | ||
return await Hive.openBox( | ||
CacheSecure.cacheKeyCrypto, | ||
crashRecovery: true, | ||
compactionStrategy: (entries, deletedEntries) => deletedEntries > 1, | ||
); | ||
} catch (error) { | ||
debugPrint('[unlockCryptoCache] $error'); | ||
return null; | ||
} | ||
} |
Oops, something went wrong.