{-# LANGUAGE OverloadedStrings #-}
module Cometa.Governance.ActionId
( ActionId (..),
encodeCbor,
decodeCbor,
toText,
fromText,
)
where
import qualified Codec.Binary.Bech32 as Bech32
import Cometa.Error (Error (..))
import Cometa.Internal.Governance (ActionId (..))
import qualified Cometa.Internal.Governance as Internal
import qualified Cometa.TransactionId as TransactionId
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Text (Text)
encodeCbor :: ActionId -> ByteString
encodeCbor :: ActionId -> ByteString
encodeCbor = ActionId -> ByteString
Internal.encodeActionId
decodeCbor :: ByteString -> Either Error ActionId
decodeCbor :: ByteString -> Either Error ActionId
decodeCbor = ByteString -> Either Error ActionId
Internal.decodeActionId
toText :: ActionId -> Either Error Text
toText :: ActionId -> Either Error Text
toText (ActionId TransactionId
transaction Word64
actionIndex)
| Word64
actionIndex Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
255 = Error -> Either Error Text
forall a b. a -> Either a b
Left (Word64 -> Error
InvalidGovernanceActionTextIndex Word64
actionIndex)
| Bool
otherwise = do
HumanReadablePart
prefix <- (HumanReadablePartError -> Either Error HumanReadablePart)
-> (HumanReadablePart -> Either Error HumanReadablePart)
-> Either HumanReadablePartError HumanReadablePart
-> Either Error HumanReadablePart
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error HumanReadablePart
-> HumanReadablePartError -> Either Error HumanReadablePart
forall a b. a -> b -> a
const (Error -> Either Error HumanReadablePart
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"Invalid built-in governance action prefix"))) HumanReadablePart -> Either Error HumanReadablePart
forall a b. b -> Either a b
Right (Text -> Either HumanReadablePartError HumanReadablePart
Bech32.humanReadablePartFromText Text
"gov_action")
Text -> Either Error Text
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumanReadablePart -> DataPart -> Text
Bech32.encodeLenient HumanReadablePart
prefix (ByteString -> DataPart
Bech32.dataPartFromBytes (TransactionId -> ByteString
TransactionId.toBytes TransactionId
transaction ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString
B.singleton (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
actionIndex))))
fromText :: Text -> Either Error ActionId
fromText :: Text -> Either Error ActionId
fromText Text
text = do
(HumanReadablePart
prefix, DataPart
part) <- (DecodingError -> Either Error (HumanReadablePart, DataPart))
-> ((HumanReadablePart, DataPart)
-> Either Error (HumanReadablePart, DataPart))
-> Either DecodingError (HumanReadablePart, DataPart)
-> Either Error (HumanReadablePart, DataPart)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error (HumanReadablePart, DataPart)
-> DecodingError -> Either Error (HumanReadablePart, DataPart)
forall a b. a -> b -> a
const Either Error (HumanReadablePart, DataPart)
forall a. Either Error a
failure) (HumanReadablePart, DataPart)
-> Either Error (HumanReadablePart, DataPart)
forall a b. b -> Either a b
Right (Text -> Either DecodingError (HumanReadablePart, DataPart)
Bech32.decodeLenient Text
text)
Bool -> Either Error ()
unlessValid (HumanReadablePart -> Text
Bech32.humanReadablePartToText HumanReadablePart
prefix Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"gov_action")
ByteString
payload <- Either Error ByteString
-> (ByteString -> Either Error ByteString)
-> Maybe ByteString
-> Either Error ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either Error ByteString
forall a. Either Error a
failure ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (DataPart -> Maybe ByteString
Bech32.dataPartToBytes DataPart
part)
Bool -> Either Error ()
unlessValid (ByteString -> Int
B.length ByteString
payload Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
33)
TransactionId
transaction <- ByteString -> Either Error TransactionId
TransactionId.fromBytes (Int -> ByteString -> ByteString
B.take Int
32 ByteString
payload)
case ByteString -> Maybe (Word8, ByteString)
B.uncons (Int -> ByteString -> ByteString
B.drop Int
32 ByteString
payload) of
Just (Word8
actionIndex, ByteString
_) -> ActionId -> Either Error ActionId
forall a b. b -> Either a b
Right (TransactionId -> Word64 -> ActionId
ActionId TransactionId
transaction (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
actionIndex))
Maybe (Word8, ByteString)
Nothing -> Either Error ActionId
forall a. Either Error a
failure
where
failure :: Either Error a
failure :: forall a. Either Error a
failure = Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidGovernanceActionText Text
"Expected a CIP-129 gov_action identifier with a 33-byte payload")
unlessValid :: Bool -> Either Error ()
unlessValid Bool
True = () -> Either Error ()
forall a b. b -> Either a b
Right ()
unlessValid Bool
False = Either Error ()
forall a. Either Error a
failure