summaryrefslogtreecommitdiff
path: root/src/PubKey.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2019-05-16 20:31:51 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2019-05-16 20:31:51 +0200
commitf609499402160aa908e6435b8a61f7cb1f258cfe (patch)
treed73a0ec33e0dca2bbbc1fda716aad627915fa941 /src/PubKey.hs
parent779c6fbd4d73b718ea64f22114f1d12463479d67 (diff)
Key storage interface
Diffstat (limited to 'src/PubKey.hs')
-rw-r--r--src/PubKey.hs22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/PubKey.hs b/src/PubKey.hs
index 787ada3..0022343 100644
--- a/src/PubKey.hs
+++ b/src/PubKey.hs
@@ -1,7 +1,7 @@
module PubKey (
PublicKey, SecretKey,
+ KeyPair(generateKeys), loadKey,
Signature(sigKey), Signed, signedData, signedSignature,
- generateKeys,
sign, signAdd,
) where
@@ -11,10 +11,12 @@ import Control.Monad.Except
import Crypto.Error
import qualified Crypto.PubKey.Ed25519 as ED
+import Data.ByteArray
import Data.ByteString (ByteString)
import qualified Data.Text as T
import Storage
+import Storage.Key
data PublicKey = PublicKey ED.PublicKey
deriving (Show)
@@ -39,6 +41,17 @@ signedData = signedData_
signedSignature :: Signed a -> [Stored Signature]
signedSignature = signedSignature_
+instance KeyPair SecretKey PublicKey where
+ keyGetPublic (SecretKey _ pub) = pub
+ keyGetData (SecretKey sec _) = convert sec
+ keyFromData kdata spub = SecretKey <$> maybeCryptoError (ED.secretKey kdata) <*> pure spub
+ generateKeys st = do
+ secret <- ED.generateSecretKey
+ public <- wrappedStore st $ PublicKey $ ED.toPublic secret
+ let pair = SecretKey secret public
+ storeKey pair
+ return (pair, public)
+
instance Storable PublicKey where
store' (PublicKey pk) = storeRec $ do
storeText "type" $ T.pack "ed25519"
@@ -76,13 +89,6 @@ instance Storable a => Storable (Signed a) where
throwError "signature verification failed"
return $ Signed sdata sigs
-
-generateKeys :: Storage -> IO (SecretKey, Stored PublicKey)
-generateKeys st = do
- secret <- ED.generateSecretKey
- public <- wrappedStore st $ PublicKey $ ED.toPublic secret
- return (SecretKey secret public, public)
-
sign :: SecretKey -> Stored a -> IO (Signed a)
sign secret val = signAdd secret $ Signed val []