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

Deserialization from CBOR of arbitrary plutus data #364

Open
nullhashpixel opened this issue Jul 22, 2024 · 1 comment
Open

Deserialization from CBOR of arbitrary plutus data #364

nullhashpixel opened this issue Jul 22, 2024 · 1 comment

Comments

@nullhashpixel
Copy link

I have a CBOR object, already serialized correctly to be part of a datum or redeemer structure. This comes from another library, which ideally should be independent of pycardano.
The goal is to embed it into a more complex structure which is created with pycardano objects, so the external CBOR needs to be deserialized first, but if the outermost level is indefinite list (CBOR tag 0x9f), this fails.

a = bytes.fromhex('9fd87b9f015820276c628281b8513437da6143542dedb497c381e32b0799f4148f6cb6fcd14c465820720c2344d1bc140e926fda330ab4b6d5390e2fa98d30df57f0f72cff5270e8ebffff')
pycardano.plutus.RawPlutusData.from_cbor(a)

a simple example which works (outermost level is CBORTag)

>>> obj = CBORTag(121, [1, 2, 3])
>>> ser = RawPlutusData.from_primitive(obj).to_cbor()
>>> RawPlutusData.from_cbor(ser)
RawPlutusData(data=CBORTag(121, [1, 2, 3]))

but for indefinite lists it doesn't work:

>>> obj = IndefiniteList([1,2,3])
>>> ser = RawPlutusData.from_primitive(obj).to_cbor()
>>> ser.hex()
'9f010203ff'
>>> RawPlutusData.from_cbor(ser)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "venv/lib/python3.8/site-packages/pycardano/serialization.py", line 485, in from_cbor
    return cls.from_primitive(value)
  File "venv/lib/python3.8/site-packages/pycardano/serialization.py", line 159, in wrapper
    raise DeserializeException(
pycardano.exception.DeserializeException: ['PlutusData', 'dict', 'int', 'bytes', 'IndefiniteList', 'RawCBOR', 'CBORTag'] typed value is required for deserialization. Got <class 'list'>: [1, 2, 3]

wrapping to objects into CBORTag also only helps for simple cases, for more complicated ones, like this one it creates CBOR, which Cardano does not like (len>64 bytestrings)

RawPlutusData.from_cbor(bytes.fromhex('d8795f5840276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb45840720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5ff')).to_cbor().hex()
'd8795880276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb4276c628281b8513437da6143542dedb4720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5720c2344d1bc140e926fda330ab4b6d5'

so 5f 58 40 (A) 58 40 (B) did turn into 5f 58 80 (AB) which is invalid.

Is there any way to deserialize arbitrary structures into a PlutusData/RawPlutusData object?

@cffls
Copy link
Collaborator

cffls commented Jul 28, 2024

This issue will be resolved by this PR. I've tested in the custom branch and here is the output:

>>> from pycardano import *
>>> obj = IndefiniteList([1,2,3])
>>> ser = RawPlutusData.from_primitive(obj).to_cbor()
>>> ser.hex()
'9f010203ff'
>>> RawPlutusData.from_cbor(ser)
RawPlutusData(data=[1, 2, 3])
>>> raw = RawPlutusData.from_cbor(ser)
>>> raw.to_cbor_hex()
'9f010203ff'

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

No branches or pull requests

2 participants