From c27b3c23ecdd53acdbfece747b9bbdb39bf4dae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 27 Aug 2023 18:33:16 +0200 Subject: Replace storedStorage usage with MonadHead --- src/Storage/Key.hs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/Storage/Key.hs') diff --git a/src/Storage/Key.hs b/src/Storage/Key.hs index 28fc989..7d36da3 100644 --- a/src/Storage/Key.hs +++ b/src/Storage/Key.hs @@ -1,10 +1,11 @@ module Storage.Key ( KeyPair(..), - storeKey, loadKey, + storeKey, loadKey, loadKeyMb, ) where import Control.Concurrent.MVar import Control.Monad +import Control.Monad.Except import Data.ByteArray import qualified Data.ByteString.Char8 as BC @@ -34,8 +35,11 @@ storeKey key = do StorageDir { dirPath = dir } -> writeFileOnce (keyFilePath dir spub) (BL.fromStrict $ convert $ keyGetData key) StorageMemory { memKeys = kstore } -> modifyMVar_ kstore $ return . M.insert (refDigest $ storedRef spub) (keyGetData key) -loadKey :: KeyPair sec pub => Stored pub -> IO (Maybe sec) -loadKey spub = do +loadKey :: (KeyPair sec pub, MonadIO m, MonadError String m) => Stored pub -> m sec +loadKey = maybe (throwError "secret key not found") return <=< loadKeyMb + +loadKeyMb :: (KeyPair sec pub, MonadIO m) => Stored pub -> m (Maybe sec) +loadKeyMb spub = liftIO $ do case stBacking $ storedStorage spub of StorageDir { dirPath = dir } -> tryIOError (BC.readFile (keyFilePath dir spub)) >>= \case Right kdata -> return $ keyFromData (convert kdata) spub -- cgit v1.2.3