Skip to content
Permalink
Browse files

wallet-api: Base16 for hashes

  • Loading branch information...
j-mueller committed Mar 15, 2019
1 parent 073ceb7 commit 4a21843d31645d3edf6aa398a5eb0fa7a6075417

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -105,7 +105,7 @@ import qualified Data.Aeson as JSON
import Data.Bifunctor (first)
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BSS
import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as BSL
import Data.Map (Map)
@@ -197,16 +197,18 @@ instance Serialise (Digest SHA256) where
Just v -> pure v

instance ToJSON (Digest SHA256) where
toJSON = JSON.String . TE.decodeUtf8 . Base64.encode . Write.toStrictByteString . encode
toJSON = JSON.String . TE.decodeUtf8 . Base16.encode . Write.toStrictByteString . encode

instance ToSchema (Digest SHA256) where
declareNamedSchema _ = plain . paramSchemaToSchema $ (Proxy :: Proxy String)

instance FromJSON (Digest SHA256) where
parseJSON = withText "SHA256" $ \s -> do
let ev = do
eun64 <- Base64.decode . TE.encodeUtf8 $ s
first show $ deserialiseOrFail $ BSL.fromStrict eun64
let (eun16, rest) = Base16.decode . TE.encodeUtf8 $ s
if BSS.null rest
then first show $ deserialiseOrFail $ BSL.fromStrict eun16
else fail "failed to decode base16"
case ev of
Left e -> fail e
Right v -> pure v
@@ -270,13 +272,15 @@ evaluateScript :: Script -> ([String], Bool)
evaluateScript (getPlc -> s) = (isJust . evaluationResultToMaybe) <$> evaluateCekTrace s

instance ToJSON Script where
toJSON = JSON.String . TE.decodeUtf8 . Base64.encode . BSL.toStrict . serialise
toJSON = JSON.String . TE.decodeUtf8 . Base16.encode . BSL.toStrict . serialise

instance FromJSON Script where
parseJSON = withText "Script" $ \s -> do
let ev = do
eun64 <- Base64.decode . TE.encodeUtf8 $ s
first show $ deserialiseOrFail $ BSL.fromStrict eun64
let ev =
let (eun16, rest) = Base16.decode . TE.encodeUtf8 $ s in
if BSS.null rest
then first show $ deserialiseOrFail $ BSL.fromStrict eun16
else fail "failed to decode base16"
case ev of
Left e -> fail e
Right v -> pure v
@@ -54,8 +54,9 @@ import Data.Aeson (FromJSON, ToJSON (toJSON), withTe
import qualified Data.Aeson as JSON
import Data.Bifunctor (first)
import qualified Data.ByteString.Lazy.Hash as Hash
import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString as BSS
import Data.Proxy (Proxy (Proxy))
import Data.Swagger.Internal.Schema (ToSchema (declareNamedSchema), paramSchemaToSchema, plain)
import qualified Data.Text.Encoding as TE
@@ -184,13 +185,15 @@ instance ToSchema ValidatorHash where
declareNamedSchema _ = plain . paramSchemaToSchema $ (Proxy :: Proxy String)

instance ToJSON ValidatorHash where
toJSON = JSON.String . TE.decodeUtf8 . Base64.encode . BSL.toStrict . serialise
toJSON = JSON.String . TE.decodeUtf8 . Base16.encode . BSL.toStrict . serialise

instance FromJSON ValidatorHash where
parseJSON = withText "ValidatorScript" $ \s -> do
let ev = do
eun64 <- Base64.decode . TE.encodeUtf8 $ s
first show $ deserialiseOrFail $ BSL.fromStrict eun64
let ev =
let (eun16, rest) = Base16.decode . TE.encodeUtf8 $ s in
if BSS.null rest
then first show $ deserialiseOrFail $ BSL.fromStrict eun16
else fail "failed to decode base16"
case ev of
Left e -> fail e
Right v -> pure v
@@ -20,7 +20,8 @@ import Control.Lens (At (..), Index, IxValue, Ixed (..), len
import Data.Aeson (FromJSON (..), ToJSON (..), withText)
import qualified Data.Aeson as JSON
import Data.Bifunctor (first)
import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString as BSS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as BSL
import Data.Map (Map)
import qualified Data.Map as Map
@@ -49,13 +50,15 @@ newtype AddressMap = AddressMap { getAddressMap :: Map Address (Map TxOutRef TxO
-- than what we have here.

instance ToJSON AddressMap where
toJSON = JSON.String . TE.decodeUtf8 . Base64.encode . Write.toStrictByteString . encode
toJSON = JSON.String . TE.decodeUtf8 . Base16.encode . Write.toStrictByteString . encode

instance FromJSON AddressMap where
parseJSON = withText "AddressMap" $ \s -> do
let ev = do
eun64 <- Base64.decode . TE.encodeUtf8 $ s
first show $ deserialiseOrFail $ BSL.fromStrict eun64
let ev =
let (eun16, rest) = Base16.decode . TE.encodeUtf8 $ s in
if BSS.null rest
then first show $ deserialiseOrFail $ BSL.fromStrict eun16
else fail "failed to decode base16"
either fail pure ev

instance Semigroup AddressMap where
@@ -65,7 +65,7 @@ library
build-depends:
base >=4.9 && <5,
aeson -any,
base64-bytestring -any,
base16-bytestring -any,
bytestring -any,
cborg -any,
containers -any,

0 comments on commit 4a21843

Please sign in to comment.
You can’t perform that action at this time.