2022-05-11 20:04:46 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
|
|
|
|
module ZGoTx where
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import qualified Data.Bson as B
|
2023-03-10 21:31:47 +00:00
|
|
|
import Data.Char
|
|
|
|
import Data.Maybe
|
2022-05-11 20:04:46 +00:00
|
|
|
import qualified Data.Text as T
|
2023-09-27 18:12:02 +00:00
|
|
|
import qualified Data.Text.Encoding as E
|
2023-03-10 21:31:47 +00:00
|
|
|
import qualified Data.UUID as U
|
|
|
|
import Data.Void
|
2022-05-11 20:04:46 +00:00
|
|
|
import Database.MongoDB
|
|
|
|
import GHC.Generics
|
2023-03-10 21:31:47 +00:00
|
|
|
import Text.Megaparsec hiding (State)
|
|
|
|
import Text.Megaparsec.Char
|
2023-09-27 18:12:02 +00:00
|
|
|
import ZcashHaskell.Orchard
|
2023-09-27 18:18:16 +00:00
|
|
|
import ZcashHaskell.Sapling (isValidShieldedAddress)
|
2022-05-11 20:04:46 +00:00
|
|
|
|
|
|
|
-- | Type to model a ZGo transaction
|
2023-09-27 18:12:02 +00:00
|
|
|
data ZGoTx = ZGoTx
|
|
|
|
{ _id :: Maybe ObjectId
|
|
|
|
, address :: T.Text
|
|
|
|
, session :: T.Text
|
|
|
|
, confirmations :: Integer
|
|
|
|
, blocktime :: Integer
|
|
|
|
, amount :: Double
|
|
|
|
, txid :: T.Text
|
|
|
|
, memo :: T.Text
|
|
|
|
} deriving (Eq, Show, Generic)
|
2022-05-11 20:04:46 +00:00
|
|
|
|
|
|
|
parseZGoTxBson :: B.Document -> Maybe ZGoTx
|
|
|
|
parseZGoTxBson d = do
|
|
|
|
i <- B.lookup "_id" d
|
|
|
|
a <- B.lookup "address" d
|
|
|
|
s <- B.lookup "session" d
|
|
|
|
c <- B.lookup "confirmations" d
|
|
|
|
am <- B.lookup "amount" d
|
|
|
|
t <- B.lookup "txid" d
|
|
|
|
m <- B.lookup "memo" d
|
|
|
|
bt <- B.lookup "blocktime" d
|
2022-05-17 17:47:27 +00:00
|
|
|
pure $ ZGoTx i a s c bt am t m
|
2022-05-11 20:04:46 +00:00
|
|
|
|
|
|
|
encodeZGoTxBson :: ZGoTx -> B.Document
|
|
|
|
encodeZGoTxBson (ZGoTx i a s c bt am t m) =
|
|
|
|
if not (null i)
|
2022-05-17 17:47:27 +00:00
|
|
|
then [ "_id" =: i
|
2022-05-11 20:04:46 +00:00
|
|
|
, "address" =: a
|
|
|
|
, "session" =: s
|
|
|
|
, "confirmations" =: c
|
|
|
|
, "blocktime" =: bt
|
|
|
|
, "amount" =: am
|
|
|
|
, "txid" =: t
|
|
|
|
, "memo" =: m
|
|
|
|
]
|
|
|
|
else [ "address" =: a
|
|
|
|
, "session" =: s
|
|
|
|
, "confirmations" =: c
|
|
|
|
, "blocktime" =: bt
|
|
|
|
, "amount" =: am
|
|
|
|
, "txid" =: t
|
|
|
|
, "memo" =: m
|
|
|
|
]
|
2022-05-17 17:47:27 +00:00
|
|
|
|
|
|
|
instance Val ZGoTx where
|
|
|
|
cast' (Doc d) = do
|
|
|
|
i <- B.lookup "_id" d
|
|
|
|
a <- B.lookup "address" d
|
|
|
|
s <- B.lookup "session" d
|
|
|
|
c <- B.lookup "confirmations" d
|
|
|
|
am <- B.lookup "amount" d
|
|
|
|
t <- B.lookup "txid" d
|
|
|
|
m <- B.lookup "memo" d
|
|
|
|
bt <- B.lookup "blocktime" d
|
|
|
|
Just (ZGoTx i a s c bt am t m)
|
|
|
|
cast' _ = Nothing
|
|
|
|
val (ZGoTx i a s c bt am t m) =
|
|
|
|
case i of
|
|
|
|
Just oid ->
|
|
|
|
Doc
|
|
|
|
[ "_id" =: i
|
|
|
|
, "address" =: a
|
|
|
|
, "session" =: s
|
|
|
|
, "confirmations" =: c
|
|
|
|
, "blocktime" =: bt
|
|
|
|
, "amount" =: am
|
|
|
|
, "txid" =: t
|
|
|
|
, "memo" =: m
|
|
|
|
]
|
|
|
|
Nothing ->
|
|
|
|
Doc
|
|
|
|
[ "address" =: a
|
|
|
|
, "session" =: s
|
|
|
|
, "confirmations" =: c
|
|
|
|
, "blocktime" =: bt
|
|
|
|
, "amount" =: am
|
|
|
|
, "txid" =: t
|
|
|
|
, "memo" =: m
|
|
|
|
]
|
2023-03-10 21:31:47 +00:00
|
|
|
|
|
|
|
-- | Type to represent and parse ZGo memos
|
2023-09-27 18:12:02 +00:00
|
|
|
data ZGoMemo = ZGoMemo
|
|
|
|
{ m_session :: Maybe U.UUID
|
|
|
|
, m_address :: Maybe T.Text
|
|
|
|
, m_payment :: Bool
|
2023-10-13 19:20:10 +00:00
|
|
|
, m_orderId :: Maybe T.Text
|
2023-09-27 18:12:02 +00:00
|
|
|
} deriving (Eq, Show)
|
2023-03-10 21:31:47 +00:00
|
|
|
|
|
|
|
data MemoToken
|
|
|
|
= Login !U.UUID
|
|
|
|
| PayMsg !U.UUID
|
|
|
|
| Address !T.Text
|
|
|
|
| Msg !T.Text
|
2023-10-13 19:20:10 +00:00
|
|
|
| OrderId !T.Text
|
2023-03-10 21:31:47 +00:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
type Parser = Parsec Void T.Text
|
|
|
|
|
|
|
|
pSession :: Parser MemoToken
|
|
|
|
pSession = do
|
|
|
|
string "ZGO"
|
|
|
|
pay <- optional $ char 'p'
|
|
|
|
string "::"
|
|
|
|
s <- some $ hexDigitChar <|> char '-'
|
|
|
|
let u = U.fromString s
|
|
|
|
case u of
|
|
|
|
Nothing -> fail "Invalid UUID"
|
|
|
|
Just u' -> do
|
|
|
|
if isJust pay
|
|
|
|
then pure $ PayMsg u'
|
|
|
|
else pure $ Login u'
|
|
|
|
|
|
|
|
pSaplingAddress :: Parser MemoToken
|
|
|
|
pSaplingAddress = do
|
|
|
|
string "zs"
|
|
|
|
a <- some alphaNumChar
|
2023-09-27 18:18:16 +00:00
|
|
|
if isValidShieldedAddress (E.encodeUtf8 $ "zs" <> T.pack a)
|
|
|
|
then pure $ Address $ T.pack ("zs" <> a)
|
|
|
|
else fail "Failed to parse Sapling address"
|
2023-03-10 21:31:47 +00:00
|
|
|
|
2023-09-27 18:12:02 +00:00
|
|
|
pUnifiedAddress :: Parser MemoToken
|
|
|
|
pUnifiedAddress = do
|
|
|
|
string "u1"
|
|
|
|
a <- some alphaNumChar
|
|
|
|
if isValidUnifiedAddress (E.encodeUtf8 $ "u1" <> T.pack a)
|
|
|
|
then pure $ Address $ T.pack ("u1" <> a)
|
|
|
|
else fail "Failed to parse Unified Address"
|
|
|
|
|
2023-10-13 19:20:10 +00:00
|
|
|
pOrderId :: Parser MemoToken
|
|
|
|
pOrderId = do
|
|
|
|
string "ZGo Order::"
|
|
|
|
a <- some hexDigitChar
|
|
|
|
pure $ OrderId . T.pack $ a
|
|
|
|
|
2023-03-10 21:31:47 +00:00
|
|
|
pMsg :: Parser MemoToken
|
|
|
|
pMsg = do
|
2023-05-17 14:44:25 +00:00
|
|
|
msg <-
|
|
|
|
some
|
|
|
|
(alphaNumChar <|> punctuationChar <|> symbolChar <|>
|
|
|
|
charCategory OtherSymbol)
|
|
|
|
pure $ Msg . T.pack $ msg
|
2023-03-10 21:31:47 +00:00
|
|
|
|
|
|
|
pMemo :: Parser MemoToken
|
|
|
|
pMemo = do
|
2023-05-17 14:44:25 +00:00
|
|
|
optional $ some spaceChar
|
2023-10-13 19:20:10 +00:00
|
|
|
t <- pSession <|> pSaplingAddress <|> pUnifiedAddress <|> pOrderId <|> pMsg
|
2023-05-17 14:44:25 +00:00
|
|
|
optional $ some spaceChar
|
|
|
|
return t
|
2023-03-10 21:31:47 +00:00
|
|
|
|
|
|
|
isMemoToken :: T.Text -> MemoToken -> Bool
|
|
|
|
isMemoToken kind t =
|
|
|
|
case kind of
|
|
|
|
"session" ->
|
|
|
|
case t of
|
|
|
|
PayMsg i -> True
|
|
|
|
Login j -> True
|
|
|
|
_ -> False
|
|
|
|
"address" ->
|
|
|
|
case t of
|
|
|
|
Address a -> True
|
|
|
|
_ -> False
|
|
|
|
"payment" ->
|
|
|
|
case t of
|
|
|
|
PayMsg i -> True
|
|
|
|
_ -> False
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
pZGoMemo :: Parser ZGoMemo
|
|
|
|
pZGoMemo = do
|
|
|
|
tks <- some pMemo
|
2023-10-13 19:20:10 +00:00
|
|
|
pure $ ZGoMemo (isSession tks) (isAddress tks) (isPayment tks) (isOrder tks)
|
2023-03-10 21:31:47 +00:00
|
|
|
where
|
2023-10-13 19:20:10 +00:00
|
|
|
isOrder [] = Nothing
|
|
|
|
isOrder tks =
|
|
|
|
if not (null tks)
|
|
|
|
then case head tks of
|
|
|
|
OrderId x -> Just x
|
|
|
|
_ -> isOrder $ tail tks
|
|
|
|
else Nothing
|
2023-03-14 17:55:23 +00:00
|
|
|
isPayment [] = False
|
2023-03-10 21:31:47 +00:00
|
|
|
isPayment tks =
|
|
|
|
not (null tks) &&
|
|
|
|
case head tks of
|
|
|
|
PayMsg x -> True
|
2023-03-14 17:55:23 +00:00
|
|
|
_ -> isPayment $ tail tks
|
|
|
|
isAddress [] = Nothing
|
2023-03-10 21:31:47 +00:00
|
|
|
isAddress tks =
|
|
|
|
if not (null tks)
|
|
|
|
then case head tks of
|
|
|
|
Address x -> Just x
|
2023-03-14 17:55:23 +00:00
|
|
|
_ -> isAddress $ tail tks
|
2023-03-10 21:31:47 +00:00
|
|
|
else Nothing
|
2023-03-14 17:55:23 +00:00
|
|
|
isSession [] = Nothing
|
2023-03-10 21:31:47 +00:00
|
|
|
isSession tks =
|
|
|
|
if not (null tks)
|
|
|
|
then case head tks of
|
|
|
|
Login x -> Just x
|
|
|
|
PayMsg y -> Just y
|
2023-03-14 17:55:23 +00:00
|
|
|
_ -> isSession $ tail tks
|
2023-03-10 21:31:47 +00:00
|
|
|
else Nothing
|