Skip to content

Commit

Permalink
Change TokenName to be a ByteString
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jan 22, 2021
1 parent e89864f commit e0c3652
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
21 changes: 16 additions & 5 deletions lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ import Control.Monad
( (>=>) )
import Data.Aeson
( FromJSON (..), ToJSON (..) )
import Data.Bifunctor
( bimap )
import Data.ByteArray.Encoding
( Base (Base16), convertFromBase, convertToBase )
import Data.ByteString
( ByteString )
import Data.Text
( Text )
import Data.Text.Class
( FromText (..), ToText (..) )
( FromText (..), TextDecodingError (..), ToText (..) )
import Fmt
( Buildable (..) )
import GHC.Generics
( Generic )
import Quiet
( Quiet (..) )

import qualified Data.Text.Encoding as T

-- | Token policy identifiers, represented by the hash of the monetary policy
-- script.
newtype TokenPolicyId =
Expand Down Expand Up @@ -63,14 +71,14 @@ instance FromText TokenPolicyId where
-- | Token names, defined by the monetary policy script.
newtype TokenName =
-- | Construct a 'TokenName' without any validation.
UnsafeTokenName { unTokenName :: Text }
UnsafeTokenName { unTokenName :: ByteString }
deriving stock (Eq, Ord, Generic)
deriving (Read, Show) via (Quiet TokenName)

instance NFData TokenName

instance Buildable TokenName where
build = build . unTokenName
build = build . toText

instance FromJSON TokenName where
parseJSON = parseJSON >=> either (fail . show) pure . fromText
Expand All @@ -79,10 +87,13 @@ instance ToJSON TokenName where
toJSON = toJSON . toText

instance ToText TokenName where
toText = unTokenName
toText = T.decodeLatin1 . convertToBase Base16 . unTokenName

instance FromText TokenName where
fromText = pure . UnsafeTokenName
fromText = bimap err UnsafeTokenName . convertFromBase Base16 . T.encodeUtf8
where
err msg = TextDecodingError $
"TokenName hash is not hex-encoded: " ++ msg

-- | Information about an asset, from a source external to the chain.
newtype AssetMetadata = AssetMetadata
Expand Down
7 changes: 2 additions & 5 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ import qualified Data.ByteString.Short as SBS
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text.Encoding as T
import qualified Data.Text.Encoding.Error as T
import qualified Ouroboros.Consensus.Shelley.Ledger as O
import qualified Ouroboros.Network.Block as O
import qualified Ouroboros.Network.Point as Point
Expand Down Expand Up @@ -922,9 +921,7 @@ fromCardanoValue = uncurry TokenBundle.fromFlatList . extract
]

mkPolicyId = W.UnsafeTokenPolicyId . W.Hash . Cardano.serialiseToRawBytes
mkTokenName = W.UnsafeTokenName
. T.decodeUtf8With T.lenientDecode
. Cardano.serialiseToRawBytes
mkTokenName = W.UnsafeTokenName . Cardano.serialiseToRawBytes

unQuantity (Cardano.Quantity q) = q

Expand Down Expand Up @@ -1176,7 +1173,7 @@ toCardanoValue tb = Cardano.valueFromList $
toCardanoPolicyId (W.UnsafeTokenPolicyId (W.Hash pid)) = just "PolicyId" $
Cardano.deserialiseFromRawBytes Cardano.AsPolicyId pid
toCardanoAssetName (W.UnsafeTokenName name) = just "TokenName" $
Cardano.deserialiseFromRawBytes Cardano.AsAssetName $ T.encodeUtf8 name
Cardano.deserialiseFromRawBytes Cardano.AsAssetName name

just :: String -> Maybe a -> a
just t = fromMaybe $ error $
Expand Down

0 comments on commit e0c3652

Please sign in to comment.