From 6f7cf857b408401abdc4477c888495b4f13162c7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 19 Apr 2017 17:30:32 -0400 Subject: reorganized message types Make Control messages be out-of-band async messages, without a pointer to a previous message. And then followed the type change through the code for hours.. This commit was sponsored by Nick Daly on Patreon. --- Types.hs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 23 deletions(-) (limited to 'Types.hs') diff --git a/Types.hs b/Types.hs index dbbb432..27c9e67 100644 --- a/Types.hs +++ b/Types.hs @@ -36,33 +36,60 @@ data Entered = Entered instance DataSize Entered where dataSize e = dataSize (enteredData e) + dataSize (echoData e) --- | High level protocol. -data Proto a - = Proto a - -- ^ either Entered or Seen - | Rejected (Activity Entered) - -- ^ sent by user to indicate when an Entered value was rejected. +-- | A message in the protocol. +data Message a + = ActivityMessage (Activity a) + | ControlMessage Control deriving (Show, Generic) -instance DataSize a => DataSize (Proto a) where - dataSize (Proto a) = dataSize a - dataSize (Rejected a) = dataSize a +instance DataSize a => DataSize (Message a) where + dataSize (ActivityMessage a) = dataSize a + dataSize (ControlMessage c) = dataSize c --- | A Proto activity (either Entered or Seen) with a pointer --- to the Activity before this one. +-- | An activity (either Entered or Seen) with a pointer +-- to a previous Activity. -- -- The Signature is over both the data in the activity, and its pointer. data Activity a = Activity - { activity :: Proto a - , prevActivity :: (Maybe Hash) - , signature :: Signature + { activity :: a + , prevActivity :: Maybe Hash + , activitySignature :: Signature } deriving (Show, Generic) instance DataSize a => DataSize (Activity a) where dataSize a = dataSize (activity a) + maybe 0 dataSize (prevActivity a) - + dataSize (signature a) + + dataSize (activitySignature a) + +-- | A control message, which can be sent asynchronously. +data Control = Control + { control :: ControlAction + , controlSignature :: Signature + } + deriving (Show, Generic) + +instance DataSize Control where + dataSize c = dataSize (control c) + + dataSize (controlSignature c) + +data ControlAction + = Rejected (Activity Entered) + -- ^ sent by user to indicate when an Entered value was rejected. + | SessionKey PublicKey + -- ^ sent by user at start, and later by developer, + -- to indicate their session key + | SessionKeyAccepted PublicKey + -- ^ sent by the user to in response to SessionKey + | SessionKeyRejected PublicKey + -- ^ sent by the user to in response to SessionKey + deriving (Show, Generic) + +instance DataSize ControlAction where + dataSize (Rejected a) = dataSize a + dataSize (SessionKey k) = dataSize k + dataSize (SessionKeyAccepted k) = dataSize k + dataSize (SessionKeyRejected k) = dataSize k data Hash = Hash { hashMethod :: HashMethod @@ -80,14 +107,30 @@ data HashMethod = SHA256 | SHA3 deriving (Show, Generic, Eq) data Signature - = Ed25519 Val - | Unsigned + = Ed25519Signature Val + | OtherSignature Val -- ^ Not used, but included to future-proof the JSON format. deriving (Show, Generic) instance DataSize Signature where - dataSize (Ed25519 _) = 64 - dataSize Unsigned = 0 + dataSize (Ed25519Signature v) = dataSize v + dataSize (OtherSignature v) = dataSize v + +-- | A public key used for a debug-me session. +-- It may be signed with a gpg key. +data PublicKey = PublicKey Val (Maybe GpgSig) + deriving (Show, Generic) + +instance DataSize PublicKey where + -- ed25519 public keys are 32 bytes + dataSize (PublicKey _ ms) = 32 + maybe 0 dataSize ms + +-- | A signature made with a gpg key. +newtype GpgSig = GpgSig Val + deriving (Show, Generic) + +instance DataSize GpgSig where + dataSize (GpgSig s) = dataSize s instance ToJSON Seen instance FromJSON Seen @@ -97,21 +140,27 @@ instance ToJSON (Activity Seen) instance FromJSON (Activity Seen) instance ToJSON (Activity Entered) instance FromJSON (Activity Entered) +instance ToJSON Control +instance FromJSON Control instance ToJSON Hash instance FromJSON Hash instance ToJSON HashMethod instance FromJSON HashMethod +instance ToJSON PublicKey +instance FromJSON PublicKey +instance ToJSON GpgSig +instance FromJSON GpgSig -instance ToJSON (Proto Seen) where +instance ToJSON (Message Seen) where toJSON = genericToJSON sumOptions toEncoding = genericToEncoding sumOptions -instance FromJSON (Proto Seen) where +instance FromJSON (Message Seen) where parseJSON = genericParseJSON sumOptions -instance ToJSON (Proto Entered) where +instance ToJSON (Message Entered) where toJSON = genericToJSON sumOptions toEncoding = genericToEncoding sumOptions -instance FromJSON (Proto Entered) where +instance FromJSON (Message Entered) where parseJSON = genericParseJSON sumOptions instance ToJSON Signature where @@ -120,3 +169,8 @@ instance ToJSON Signature where instance FromJSON Signature where parseJSON = genericParseJSON sumOptions +instance ToJSON ControlAction where + toJSON = genericToJSON sumOptions + toEncoding = genericToEncoding sumOptions +instance FromJSON ControlAction where + parseJSON = genericParseJSON sumOptions -- cgit v1.2.3