Skip to content

Commit

Permalink
TEMPORARY WORKAROUND: Make TokenName a ByteString internally.
Browse files Browse the repository at this point in the history
This should be removed once PR #2447 is merged.
  • Loading branch information
jonathanknowles committed Jan 25, 2021
1 parent 6f37a7a commit a9044eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import Control.Monad
( (>=>) )
import Data.Aeson
( FromJSON (..), ToJSON (..) )
import Data.Text
( Text )
import Data.ByteString
( ByteString )
import Data.Text.Class
( FromText (..), ToText (..) )
import Data.Text.Encoding
( decodeUtf8, encodeUtf8 )
import Fmt
( Buildable (..) )
import GHC.Generics
Expand Down Expand Up @@ -61,14 +63,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 @@ -77,7 +79,7 @@ instance ToJSON TokenName where
toJSON = toJSON . toText

instance ToText TokenName where
toText = unTokenName
toText = decodeUtf8 . unTokenName

instance FromText TokenName where
fromText = pure . UnsafeTokenName
fromText = pure . (UnsafeTokenName . encodeUtf8)
4 changes: 1 addition & 3 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 @@ -925,7 +924,6 @@ fromCardanoValue = uncurry TokenBundle.fromFlatList . extract

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

unQuantity (Cardano.Quantity q) = q
Expand Down Expand Up @@ -1181,7 +1179,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 a9044eb

Please sign in to comment.