From d69ea63319b3297c6c2591eea8b3e5a4903114f4 Mon Sep 17 00:00:00 2001 From: voidlizard Date: Fri, 11 Oct 2024 05:41:33 +0300 Subject: [PATCH] MailboxRefKey boilerplate --- hbs2-peer/hbs2-peer.cabal | 1 + hbs2-peer/lib/HBS2/Peer/Proto/Mailbox.hs | 2 + hbs2-peer/lib/HBS2/Peer/Proto/Mailbox/Ref.hs | 39 ++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 hbs2-peer/lib/HBS2/Peer/Proto/Mailbox/Ref.hs diff --git a/hbs2-peer/hbs2-peer.cabal b/hbs2-peer/hbs2-peer.cabal index fbb9bd04..666a75c1 100644 --- a/hbs2-peer/hbs2-peer.cabal +++ b/hbs2-peer/hbs2-peer.cabal @@ -166,6 +166,7 @@ library HBS2.Peer.Proto.Mailbox.Types HBS2.Peer.Proto.Mailbox.Message HBS2.Peer.Proto.Mailbox.Entry + HBS2.Peer.Proto.Mailbox.Ref HBS2.Peer.Proto.BrowserPlugin HBS2.Peer.RPC.Client diff --git a/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox.hs b/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox.hs index ce4327e3..fb11c0de 100644 --- a/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox.hs +++ b/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox.hs @@ -4,6 +4,7 @@ module HBS2.Peer.Proto.Mailbox ( module HBS2.Peer.Proto.Mailbox , module HBS2.Peer.Proto.Mailbox.Message , module HBS2.Peer.Proto.Mailbox.Types + , module HBS2.Peer.Proto.Mailbox.Ref ) where import HBS2.Prelude.Plated @@ -17,6 +18,7 @@ import HBS2.Actors.Peer.Types import HBS2.Peer.Proto.Mailbox.Types import HBS2.Peer.Proto.Mailbox.Message import HBS2.Peer.Proto.Mailbox.Entry +import HBS2.Peer.Proto.Mailbox.Ref import Data.Maybe import Control.Monad.Trans.Cont diff --git a/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox/Ref.hs b/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox/Ref.hs new file mode 100644 index 00000000..1332c9e0 --- /dev/null +++ b/hbs2-peer/lib/HBS2/Peer/Proto/Mailbox/Ref.hs @@ -0,0 +1,39 @@ +{-# Language UndecidableInstances #-} +module HBS2.Peer.Proto.Mailbox.Ref where + + +import HBS2.Prelude +import HBS2.Hash +import HBS2.Base58 +import HBS2.Net.Proto.Types +import HBS2.Data.Types.Refs + +import Data.Maybe (fromMaybe) +import Data.Hashable hiding (Hashed) + +newtype MailboxRefKey s = MailboxRefKey (PubKey 'Sign s) + +instance RefMetaData (MailboxRefKey s) + +deriving stock instance IsRefPubKey s => Eq (MailboxRefKey s) + +instance (IsRefPubKey s) => Hashable (MailboxRefKey s) where + hashWithSalt s k = hashWithSalt s (hashObject @HbSync k) + +instance (IsRefPubKey s) => Hashed HbSync (MailboxRefKey s) where + hashObject (MailboxRefKey pk) = hashObject ("mailboxv1|" <> serialise pk) + +instance IsRefPubKey s => FromStringMaybe (MailboxRefKey s) where + fromStringMay s = MailboxRefKey <$> fromStringMay s + +instance IsRefPubKey s => IsString (MailboxRefKey s) where + fromString s = fromMaybe (error "bad public key base58") (fromStringMay s) + +instance Pretty (AsBase58 (PubKey 'Sign s)) => Pretty (AsBase58 (MailboxRefKey s)) where + pretty (AsBase58 (MailboxRefKey k)) = pretty (AsBase58 k) + +instance Pretty (AsBase58 (PubKey 'Sign s)) => Pretty (MailboxRefKey s) where + pretty (MailboxRefKey k) = pretty (AsBase58 k) + + +