Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to decode CBOR-encoded properties in CIP-0068 metadata #59

Open
euonymos opened this issue Dec 21, 2023 · 0 comments · May be fixed by #60
Open

Try to decode CBOR-encoded properties in CIP-0068 metadata #59

euonymos opened this issue Dec 21, 2023 · 0 comments · May be fixed by #60
Assignees

Comments

@euonymos
Copy link

The current implementation decodes only known fields from the metadata part of the CIP-0068 datums, leaving all additional properties encoded. It drops the availability of this metadata for parties who can't (or don't) parse CBOR for some reason.

At the same time, the standard itself in the sections "Retrieve metadata as 3rd party" (for all 222, 333, and 444) clearly states:

Convert to JSON and encode all string entries to UTF-8 if possible, otherwise leave them in hex.

As a makeshift solution, we are currently using the following decoder. It would be great to have this done on the server's side.

decodeCborValues :: Value -> Identity Value
decodeCborValues (Object obj) = Object <$> traverse decodeCborValues obj
decodeCborValues (Array arr) = Array <$> traverse decodeCborValues arr
decodeCborValues (EncodedData value) = decodeCborValues value
decodeCborValues (NonEncodedData decoded) = pure decoded

pattern EncodedData :: Value -> Value
pattern EncodedData decoded <- (tryDecode -> Just decoded)
pattern NonEncodedData :: Value -> Value
pattern NonEncodedData value <- value
{-# COMPLETE Object, Array, EncodedData, NonEncodedData #-}

tryDecode :: Value -> Maybe Value
tryDecode (String text) = do
  bs <- decodeHex text
  pd <- eitherToMaybe (deserialiseOrFail @Data $ fromStrict bs)
  pdToValue pd
tryDecode _ = Nothing

pdToValue :: Data -> Maybe Value
pdToValue (Map pairs) = Object . Aeson.fromList <$> mapM mapElemToValue pairs
pdToValue (List ds) = Array . Vector.fromList <$> mapM pdToValue ds
pdToValue (I int) = Just $ Number $ fromInteger int
pdToValue (B bs) = eitherToMaybe $ String <$> decodeUtf8' bs
pdToValue (Constr _ _) = Nothing

mapElemToValue :: (Data, Data) -> Maybe (Key, Value)
mapElemToValue (B bs, value) = do
  key <- eitherToMaybe $ decodeUtf8' bs
  value <- pdToValue value
  pure (Aeson.fromText key, value)
mapElemToValue (_, _) = Nothing
@euonymos euonymos changed the title Try to decode CBOR-encoded proerties in CIP-0068 metadata Try to decode CBOR-encoded properties in CIP-0068 metadata Dec 21, 2023
@slowbackspace slowbackspace self-assigned this Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants