From 80278834b8178ac74681cbe55dd1ba9fba061710 Mon Sep 17 00:00:00 2001 From: Dmitry Zuikov Date: Fri, 15 Sep 2023 10:59:54 +0300 Subject: [PATCH] minor-fix: extracted SignedBox and code sweeped --- hbs2-core/lib/HBS2/Data/Types/SignedBox.hs | 36 ++++++++++++++++++++++ hbs2-core/lib/HBS2/Net/Proto/RefChan.hs | 31 ------------------- hbs2-peer/app/CLI/RefChan.hs | 1 + 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/hbs2-core/lib/HBS2/Data/Types/SignedBox.hs b/hbs2-core/lib/HBS2/Data/Types/SignedBox.hs index d814eb50..c8c4b0f9 100644 --- a/hbs2-core/lib/HBS2/Data/Types/SignedBox.hs +++ b/hbs2-core/lib/HBS2/Data/Types/SignedBox.hs @@ -1,4 +1,5 @@ {-# LANGUAGE UndecidableInstances #-} +{-# LANGUAGE AllowAmbiguousTypes #-} module HBS2.Data.Types.SignedBox where import HBS2.Prelude.Plated @@ -8,6 +9,10 @@ import HBS2.Net.Auth.Credentials import Codec.Serialise import Data.Hashable import Data.ByteString (ByteString) +import Data.ByteString.Lazy qualified as LBS +import Control.Monad.Trans.Maybe +import Data.Function +import Control.Monad.Identity data SignedBox p e = SignedBox (PubKey 'Sign (Encryption e)) ByteString (Signature (Encryption e)) @@ -33,3 +38,34 @@ type ForSignedBox e = ( Serialise ( PubKey 'Sign (Encryption e)) instance ForSignedBox e => Serialise (SignedBox p e) +makeSignedBox :: forall e p . (Serialise p, ForSignedBox e, Signatures (Encryption e)) + => PubKey 'Sign (Encryption e) + -> PrivKey 'Sign (Encryption e) + -> p + -> SignedBox p e + +makeSignedBox pk sk msg = SignedBox @p @e pk bs sign + where + bs = LBS.toStrict (serialise msg) + sign = makeSign @(Encryption e) sk bs + + +unboxSignedBox0 :: forall p e . (Serialise p, ForSignedBox e, Signatures (Encryption e)) + => SignedBox p e + -> Maybe (PubKey 'Sign (Encryption e), p) + +unboxSignedBox0 (SignedBox pk bs sign) = runIdentity $ runMaybeT do + guard $ verifySign @(Encryption e) pk sign bs + p <- MaybeT $ pure $ deserialiseOrFail @p (LBS.fromStrict bs) & either (const Nothing) Just + pure (pk, p) + +unboxSignedBox :: forall p e . (Serialise p, ForSignedBox e, Signatures (Encryption e)) + => LBS.ByteString + -> Maybe (PubKey 'Sign (Encryption e), p) + +unboxSignedBox bs = runIdentity $ runMaybeT do + + box <- MaybeT $ pure $ deserialiseOrFail @(SignedBox p e) bs + & either (pure Nothing) Just + + MaybeT $ pure $ unboxSignedBox0 box diff --git a/hbs2-core/lib/HBS2/Net/Proto/RefChan.hs b/hbs2-core/lib/HBS2/Net/Proto/RefChan.hs index c777283d..ead0d71b 100644 --- a/hbs2-core/lib/HBS2/Net/Proto/RefChan.hs +++ b/hbs2-core/lib/HBS2/Net/Proto/RefChan.hs @@ -857,37 +857,6 @@ makeProposeTran creds chan box1 = do let sk = view peerSignSk creds pure $ makeSignedBox @e pk sk tran -makeSignedBox :: forall e p . (Serialise p, ForRefChans e, Signatures (Encryption e)) - => PubKey 'Sign (Encryption e) - -> PrivKey 'Sign (Encryption e) - -> p - -> SignedBox p e - -makeSignedBox pk sk msg = SignedBox @p @e pk bs sign - where - bs = LBS.toStrict (serialise msg) - sign = makeSign @(Encryption e) sk bs - - -unboxSignedBox0 :: forall p e . (Serialise p, ForRefChans e, Signatures (Encryption e)) - => SignedBox p e - -> Maybe (PubKey 'Sign (Encryption e), p) - -unboxSignedBox0 (SignedBox pk bs sign) = runIdentity $ runMaybeT do - guard $ verifySign @(Encryption e) pk sign bs - p <- MaybeT $ pure $ deserialiseOrFail @p (LBS.fromStrict bs) & either (const Nothing) Just - pure (pk, p) - -unboxSignedBox :: forall p e . (Serialise p, ForRefChans e, Signatures (Encryption e)) - => LBS.ByteString - -> Maybe (PubKey 'Sign (Encryption e), p) - -unboxSignedBox bs = runIdentity $ runMaybeT do - - box <- MaybeT $ pure $ deserialiseOrFail @(SignedBox p e) bs - & either (pure Nothing) Just - - MaybeT $ pure $ unboxSignedBox0 box instance ForRefChans e => FromStringMaybe (RefChanHeadBlock e) where fromStringMay str = RefChanHeadBlockSmall <$> version diff --git a/hbs2-peer/app/CLI/RefChan.hs b/hbs2-peer/app/CLI/RefChan.hs index c21ad6c4..68404e10 100644 --- a/hbs2-peer/app/CLI/RefChan.hs +++ b/hbs2-peer/app/CLI/RefChan.hs @@ -6,6 +6,7 @@ import HBS2.Net.Auth.Credentials import HBS2.Net.Proto.Definition() import HBS2.Net.Proto.RefChan import HBS2.Net.Proto.Types +import HBS2.Data.Types.SignedBox import HBS2.OrDie