added cached keypair to manager

This commit is contained in:
Zachary Atkins 2021-04-10 15:26:38 -05:00
parent ffd477bc6f
commit 8e2a30647b

View File

@ -3,6 +3,8 @@ import 'package:path_provider/path_provider.dart';
import 'dart:io'; import 'dart:io';
class KeyFileManager { class KeyFileManager {
static KeyPair keyPair = KeyPair();
static Future<String> get _localPath async { static Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory(); final directory = await getApplicationDocumentsDirectory();
return directory.path; return directory.path;
@ -19,6 +21,9 @@ class KeyFileManager {
} }
static Future<KeyPair> readKeyPair() async { static Future<KeyPair> readKeyPair() async {
if (keyPair.hasPublicKey() && keyPair.hasPrivateKey()) {
return keyPair;
}
try { try {
String privKey = await _privKeyFile.then( String privKey = await _privKeyFile.then(
(file) => file.readAsString(), (file) => file.readAsString(),
@ -26,11 +31,11 @@ class KeyFileManager {
String pubKey = await _pubKeyFile.then( String pubKey = await _pubKeyFile.then(
(file) => file.readAsString(), (file) => file.readAsString(),
); );
keyPair = KeyPair(
return KeyPair(
privateKey: privKey, privateKey: privKey,
publicKey: pubKey, publicKey: pubKey,
); );
return keyPair;
} catch (e) { } catch (e) {
print(e); print(e);
return KeyPair(); return KeyPair();
@ -38,9 +43,10 @@ class KeyFileManager {
} }
static Future<void> writeKeyPair(KeyPair pair) async { static Future<void> writeKeyPair(KeyPair pair) async {
keyPair = pair;
final File privKeyFile = await _privKeyFile; final File privKeyFile = await _privKeyFile;
final File pubKeyFile = await _pubKeyFile; final File pubKeyFile = await _pubKeyFile;
pubKeyFile.writeAsString(pair.publicKey); pubKeyFile.writeAsString(keyPair.publicKey);
privKeyFile.writeAsString(pair.privateKey); privKeyFile.writeAsString(keyPair.privateKey);
} }
} }