diff --git a/src/Simplex/Chat/Badges.hs b/src/Simplex/Chat/Badges.hs index 62683788d5..f34ba8ef93 100644 --- a/src/Simplex/Chat/Badges.hs +++ b/src/Simplex/Chat/Badges.hs @@ -295,7 +295,13 @@ data ProofPresHeader | PHFileDescr {chatBinding :: ByteString, fileSize :: Int64, descrHash :: ByteString, fileExpires :: Maybe UTCTime} | PHUnknown Char ByteString deriving (Eq, Show) - deriving (ToJSON, FromJSON) via (StrJSON "ProofPresHeader" ProofPresHeader) + +instance ToJSON ProofPresHeader where + toJSON = toJSON . BBSPresHeader . strEncode + toEncoding = toEncoding . BBSPresHeader . strEncode + +instance FromJSON ProofPresHeader where + parseJSON v = parseJSON v >>= \(BBSPresHeader ph) -> either fail pure $ strDecode ph instance StrEncoding ProofPresHeader where strEncode = \case diff --git a/tests/BadgeTests.hs b/tests/BadgeTests.hs index bd8f62d3fd..ced51a5bbf 100644 --- a/tests/BadgeTests.hs +++ b/tests/BadgeTests.hs @@ -51,6 +51,7 @@ badgeTests = do it "should accept unknown badge types" testUnknownBadgeType it "credential serializes to a paste-able token and back" testCredentialSerialization it "presentation headers encode and decode" testPresHeaderEncoding + it "presentation headers with binary bytes round-trip through JSON" testPresHeaderJSON it "should reject a proof presented under another chat binding" testOtherChatBinding describe "redemption codes" $ do it "a generated code reads back" testCodeRoundTrip @@ -209,6 +210,21 @@ testPresHeaderEncoding = PHUnknown 'Z' "payload" ] +testPresHeaderJSON :: IO () +testPresHeaderJSON = + mapM_ + ( \ph -> do + J.eitherDecode (J.encode ph) `shouldBe` Right ph + J.toJSON ph `shouldBe` J.toJSON (BBSPresHeader $ strEncode ph) + ) + [ PHChat binaryBytes, + PHFileInv {chatBinding = binaryBytes, fileSize = 139737}, + PHFileDescr {chatBinding = binaryBytes, fileSize = 139737, descrHash = binaryBytes, fileExpires = Just futureTime}, + PHUnknown 'Z' binaryBytes + ] + where + binaryBytes = "\0\1\31\34\92\127\128\195\255" + testOtherChatBinding :: IO () testOtherChatBinding = do let ph = PHFileInv {chatBinding = aliceBinding, fileSize = 139737}