Skip to content

Commit

Permalink
Merge pull request #183 from syphon-org/bug-fixes-0.1.4
Browse files Browse the repository at this point in the history
[0.1.4] Bugs, Performance, and Cache Overhaul
  • Loading branch information
ereio authored Oct 29, 2020
2 parents 50289df + 5a2b5ce commit f1aa154
Show file tree
Hide file tree
Showing 80 changed files with 2,827 additions and 1,734 deletions.
65 changes: 65 additions & 0 deletions assets/cheatsheet.md
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 modified assets/design/branding.sketch
Binary file not shown.
10 changes: 0 additions & 10 deletions fastlane/metadata/android/en-US/long_description.txt

This file was deleted.

2 changes: 1 addition & 1 deletion ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0ec68b01ef371337af2fda1b7201140f
4d17cbc20ae4e206cdf903cbe1c6faf1
2 changes: 1 addition & 1 deletion ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ import Flutter
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
}
24 changes: 16 additions & 8 deletions lib/global/algos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import 'dart:convert';

import 'package:flutter/material.dart';

/**
* Clock functions in code
*
* final stopwatch = Stopwatch()..start();
* print('[fetchRooms] TIMESTAMP ${stopwatch.elapsed}');
* stopwatch.stop();
*/

List<int> fibonacci(int n) {
if (n == 0) {
return [0];
Expand All @@ -28,3 +20,19 @@ void printJson(Map jsonMap) {
String prettyEvent = encoder.convert(jsonMap);
debugPrint(prettyEvent, wrapWidth: 2048);
}

// time functions by wrapping them here - needs testing
Future<void> timeWrapper(
Future<dynamic> Function() function, {
String name,
}) async {
Stopwatch stopwatch = new Stopwatch()..start();

dynamic result = await function();

final stoptime = stopwatch.elapsed;

debugPrint('[$name TIMER] ${function.runtimeType} $stoptime');

return result;
}
208 changes: 208 additions & 0 deletions lib/global/cache/index.dart
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;
}
}
Loading

0 comments on commit f1aa154

Please sign in to comment.