diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2023-08-27 18:33:16 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2023-08-30 20:53:55 +0200 |
commit | c27b3c23ecdd53acdbfece747b9bbdb39bf4dae9 (patch) | |
tree | 52a4be70840e2691195ec54149f5ac14ec112606 /src/Storage/Key.hs | |
parent | dfddb65ad1abf5ba4171be42d303850ebbc363ee (diff) |
Replace storedStorage usage with MonadHead
Diffstat (limited to 'src/Storage/Key.hs')
-rw-r--r-- | src/Storage/Key.hs | 10 |
1 files changed, 7 insertions, 3 deletions
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 |