From 8e2a30647b9f8dd683b25f39278a09968effcde2 Mon Sep 17 00:00:00 2001 From: Zachary Atkins Date: Sat, 10 Apr 2021 15:26:38 -0500 Subject: [PATCH] added cached keypair to manager --- loc_chain_app/lib/util/keyfile_manager.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/loc_chain_app/lib/util/keyfile_manager.dart b/loc_chain_app/lib/util/keyfile_manager.dart index 97c2136..85bb962 100644 --- a/loc_chain_app/lib/util/keyfile_manager.dart +++ b/loc_chain_app/lib/util/keyfile_manager.dart @@ -3,6 +3,8 @@ import 'package:path_provider/path_provider.dart'; import 'dart:io'; class KeyFileManager { + static KeyPair keyPair = KeyPair(); + static Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; @@ -19,6 +21,9 @@ class KeyFileManager { } static Future readKeyPair() async { + if (keyPair.hasPublicKey() && keyPair.hasPrivateKey()) { + return keyPair; + } try { String privKey = await _privKeyFile.then( (file) => file.readAsString(), @@ -26,11 +31,11 @@ class KeyFileManager { String pubKey = await _pubKeyFile.then( (file) => file.readAsString(), ); - - return KeyPair( + keyPair = KeyPair( privateKey: privKey, publicKey: pubKey, ); + return keyPair; } catch (e) { print(e); return KeyPair(); @@ -38,9 +43,10 @@ class KeyFileManager { } static Future writeKeyPair(KeyPair pair) async { + keyPair = pair; final File privKeyFile = await _privKeyFile; final File pubKeyFile = await _pubKeyFile; - pubKeyFile.writeAsString(pair.publicKey); - privKeyFile.writeAsString(pair.privateKey); + pubKeyFile.writeAsString(keyPair.publicKey); + privKeyFile.writeAsString(keyPair.privateKey); } }