diff options
author | Joey Hess <joeyh@joeyh.name> | 2017-04-14 10:05:13 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2017-04-14 10:11:54 -0400 |
commit | b5d5f86a88c8dbd1cee9e28a659bfe1c26f38eaa (patch) | |
tree | 1ea7fd10f9619ee20016190cb255c62d408611d5 /Types.hs | |
parent | 2a271b27c65a286882332b6268e8946851c52f2a (diff) | |
download | debug-me-b5d5f86a88c8dbd1cee9e28a659bfe1c26f38eaa.tar.gz |
improve JSON
Most of the time, ByteStrings will be able to be encoded as utf8, so avoid
base64 when not needed.
Adjusted some of the types in order to generate more usual JSON. In particular,
removed StartActivity.
The JSON now looks like this (with the signature still not populated):
{"signature":{"v":""},"prevActivity":{"hashValue":{"v":"3b1abe614dd43bdb2d9a56777884e2d0f3bac9796e2d25c1ad52bb689c117286"},"hashMethod":"SHA256"},"activity":{"echoData":{"v":""},"enteredData":{"v":"l"}}}
203 bytes to send a single keystroke is not great when there's really
only 1+64(hash) bytes of unique data. So, may end up adding a wire
encoding on top of this. But, JSON is good to have for storage of the
proofs, etc.
Also, it does compress well. Two such JSON objects gzip -9 to 219
bytes, and three to 265 bytes. So, 37 bytes per keystroke. This is
*exactly* as efficient as gzip -9 of $c$hash formatted data.
This commit was sponsored by Jack Hill on Patreon.
Diffstat (limited to 'Types.hs')
-rw-r--r-- | Types.hs | 54 |
1 files changed, 16 insertions, 38 deletions
@@ -1,14 +1,14 @@ -{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveGeneric, FlexibleInstances, OverloadedStrings #-} +{-# LANGUAGE DeriveGeneric, FlexibleInstances #-} -module Types where +module Types ( + module Types, + Val(..) +) where + +import Val -import Data.ByteString import GHC.Generics (Generic) import Data.Aeson -import Data.Aeson.Types -import qualified Codec.Binary.Base64 as B64 -import qualified Data.Text as T -import qualified Data.Text.Encoding as T -- | Things that the developer sees. data Seen = Seen @@ -35,9 +35,11 @@ instance FromJSON Entered -- to the Activity before this one. -- -- The Signature is over both the data in the activity, and its pointer. -data Activity a - = Activity a HashPointer Signature - | StartActivity a Signature +data Activity a = Activity + { activity :: a + , prevActivity :: (Maybe HashPointer) + , signature :: Signature + } deriving (Show, Generic) instance ToJSON (Activity Seen) @@ -45,13 +47,7 @@ instance FromJSON (Activity Seen) instance ToJSON (Activity Entered) instance FromJSON (Activity Entered) -activityContent :: Activity a -> a -activityContent (Activity a _ _) = a -activityContent (StartActivity a _) = a - -data Signature = Signature - { signature :: Val - } +newtype Signature = Signature Val deriving (Show, Generic) instance ToJSON Signature @@ -67,28 +63,10 @@ data HashPointer = HashPointer instance ToJSON HashPointer instance FromJSON HashPointer -data HashMethod = SHA256 +-- | We use SHA256. (SHA3 is included to future proof, and because it +-- improves the generated JSON.) +data HashMethod = SHA256 | SHA3 deriving (Show, Generic, Eq) instance ToJSON HashMethod instance FromJSON HashMethod - --- | Newtype of ByteString so we can have JSON instances without orphans. -newtype Val = Val { val :: ByteString } - deriving (Show, Generic, Eq, Monoid) - --- | JSON instances for Val, using base64 encoding. -instance ToJSON Val where - toJSON (Val b) = object [ "b" .= b64 b ] -instance FromJSON Val where - parseJSON (Object v) = Val <$> (unb64 =<< v .: "b") - parseJSON invalid = typeMismatch "ByteString" invalid - -b64 :: ByteString -> T.Text -b64 = T.decodeUtf8 . B64.encode - -unb64 :: Monad m => T.Text -> m ByteString -unb64 t = either - (\_ -> fail "bad base64 data") - return - ( B64.decode $ T.encodeUtf8 t) |