Advertisement
Guest User

Untitled

a guest
Mar 8th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. type RelayURL = Text
  2.  
  3. newtype EventId =
  4. EventId
  5. { getEventId :: ByteString
  6. }
  7. deriving (Eq)
  8.  
  9. instance Show EventId where
  10. showsPrec _ = shows . B16.encodeBase16 . getEventId
  11.  
  12. instance ToJSON EventId where
  13. toJSON e = AesonTypes.String $ pack $ exportEventId e
  14.  
  15. instance FromJSON EventId where
  16. parseJSON = withObject "EventId" $ \i -> do
  17. i' <- i .: "id"
  18. case eventId' i' of
  19. Just e -> return e
  20. _ -> fail "invalid event id"
  21.  
  22. eventId' :: Text -> Maybe EventId
  23. eventId' t = do
  24. bs <- Schnorr.decodeHex t
  25. case BS.length bs of
  26. 32 -> Just $ EventId bs
  27. _ -> Nothing
  28.  
  29. instance FromJSON XOnlyPubKey where
  30. parseJSON = withObject "XOnlyPubKey" $ \i -> do
  31. i' <- i .: "id"
  32. case xOnlyPubKey' i' of
  33. Just e -> return e
  34. _ -> fail "invalid XOnlyPubKey"
  35.  
  36. xOnlyPubKey' :: Text -> Maybe XOnlyPubKey
  37. xOnlyPubKey' t = do
  38. bs <- Schnorr.decodeHex t
  39. case BS.length bs of
  40. 32 -> Schnorr.xOnlyPubKey bs
  41. _ -> Nothing
  42.  
  43. data Tag
  44. = ETag (EventId, RelayURL)
  45. | PTag (XOnlyPubKey, RelayURL)
  46. deriving (Eq, Show)
  47.  
  48. newtype TagList = TagList [Tag] deriving (Show)
  49.  
  50. instance FromJSON TagList where
  51. parseJSON (AesonTypes.Object o) =
  52. TagList <$> (o .: "tags")
  53. parseJSON _ = mzero
  54.  
  55. instance FromJSON Tag where
  56. parseJSON (AesonTypes.Array v)
  57. | V.length v == 3 = do
  58. t <- parseJSON $ v V.! 0
  59. case t of
  60. "e" -> do
  61. e <- parseJSON $ v V.! 1
  62. r <- parseJSON $ v V.! 2
  63. return $ ETag (e, r)
  64. "p" -> do
  65. x <- parseJSON $ v V.! 1
  66. r <- parseJSON $ v V.! 2
  67. return $ PTag (x, r)
  68. _ -> mzero
  69. | otherwise = mzero
  70. parseJSON _ = mzero
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement