{-# LANGUAGE OverloadedStrings #-}
module Cometa.AssetId
( AssetId (..),
fromBytes,
toBytes,
fromHex,
toHex,
fingerprint,
)
where
import qualified Codec.Binary.Bech32 as Bech32
import Cometa.AssetName (AssetName)
import qualified Cometa.AssetName as AssetName
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Cometa.PolicyId (PolicyId)
import qualified Cometa.PolicyId as PolicyId
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
data AssetId
=
Ada
|
Native !PolicyId !AssetName
deriving (AssetId -> AssetId -> Bool
(AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool) -> Eq AssetId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AssetId -> AssetId -> Bool
== :: AssetId -> AssetId -> Bool
$c/= :: AssetId -> AssetId -> Bool
/= :: AssetId -> AssetId -> Bool
Eq, Eq AssetId
Eq AssetId =>
(AssetId -> AssetId -> Ordering)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> AssetId)
-> (AssetId -> AssetId -> AssetId)
-> Ord AssetId
AssetId -> AssetId -> Bool
AssetId -> AssetId -> Ordering
AssetId -> AssetId -> AssetId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AssetId -> AssetId -> Ordering
compare :: AssetId -> AssetId -> Ordering
$c< :: AssetId -> AssetId -> Bool
< :: AssetId -> AssetId -> Bool
$c<= :: AssetId -> AssetId -> Bool
<= :: AssetId -> AssetId -> Bool
$c> :: AssetId -> AssetId -> Bool
> :: AssetId -> AssetId -> Bool
$c>= :: AssetId -> AssetId -> Bool
>= :: AssetId -> AssetId -> Bool
$cmax :: AssetId -> AssetId -> AssetId
max :: AssetId -> AssetId -> AssetId
$cmin :: AssetId -> AssetId -> AssetId
min :: AssetId -> AssetId -> AssetId
Ord, Int -> AssetId -> ShowS
[AssetId] -> ShowS
AssetId -> String
(Int -> AssetId -> ShowS)
-> (AssetId -> String) -> ([AssetId] -> ShowS) -> Show AssetId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AssetId -> ShowS
showsPrec :: Int -> AssetId -> ShowS
$cshow :: AssetId -> String
show :: AssetId -> String
$cshowList :: [AssetId] -> ShowS
showList :: [AssetId] -> ShowS
Show)
fromBytes :: ByteString -> Either Error AssetId
fromBytes :: ByteString -> Either Error AssetId
fromBytes ByteString
bytes
| Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
28 Bool -> Bool -> Bool
|| Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
60 = Error -> Either Error AssetId
forall a b. a -> Either a b
Left (Int -> Error
InvalidAssetIdLength Int
size)
| Bool
otherwise = PolicyId -> AssetName -> AssetId
Native (PolicyId -> AssetName -> AssetId)
-> Either Error PolicyId -> Either Error (AssetName -> AssetId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error PolicyId
PolicyId.fromBytes ByteString
policy Either Error (AssetName -> AssetId)
-> Either Error AssetName -> Either Error AssetId
forall a b.
Either Error (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> Either Error AssetName
AssetName.fromBytes ByteString
name
where
size :: Int
size = ByteString -> Int
ByteString.length ByteString
bytes
(ByteString
policy, ByteString
name) = Int -> ByteString -> (ByteString, ByteString)
ByteString.splitAt Int
28 ByteString
bytes
toBytes :: AssetId -> Either Error ByteString
toBytes :: AssetId -> Either Error ByteString
toBytes AssetId
Ada = Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"AssetId.toBytes: Ada has no native asset encoding")
toBytes (Native PolicyId
policy AssetName
name) = ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (PolicyId -> ByteString
PolicyId.toBytes PolicyId
policy ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> AssetName -> ByteString
AssetName.toBytes AssetName
name)
fromHex :: Text -> Either Error AssetId
fromHex :: Text -> Either Error AssetId
fromHex Text
text = Either Error AssetId
-> (ByteString -> Either Error AssetId)
-> Maybe ByteString
-> Either Error AssetId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error AssetId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error AssetId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)
toHex :: AssetId -> Either Error Text
toHex :: AssetId -> Either Error Text
toHex AssetId
asset = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> Either Error ByteString -> Either Error Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AssetId -> Either Error ByteString
toBytes AssetId
asset
fingerprint :: AssetId -> IO (Either Error Text)
fingerprint :: AssetId -> IO (Either Error Text)
fingerprint AssetId
Ada = Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Text
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"AssetId.fingerprint: Ada has no CIP-14 fingerprint"))
fingerprint AssetId
asset = case AssetId -> Either Error ByteString
toBytes AssetId
asset of
Left Error
failure -> Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Text
forall a b. a -> Either a b
Left Error
failure)
Right ByteString
bytes -> do
Either Error Blake2bHash
hashed <- Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
20 ByteString
bytes
Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Text -> IO (Either Error Text))
-> Either Error Text -> IO (Either Error Text)
forall a b. (a -> b) -> a -> b
$ do
Blake2bHash
digest <- Either Error Blake2bHash
hashed
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 asset fingerprint prefix"))) HumanReadablePart -> Either Error HumanReadablePart
forall a b. b -> Either a b
Right (Text -> Either HumanReadablePartError HumanReadablePart
Bech32.humanReadablePartFromText Text
"asset")
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 (Blake2bHash -> ByteString
Blake2b.toBytes Blake2bHash
digest)))