pycardano icon indicating copy to clipboard operation
pycardano copied to clipboard

Deserialization from CBOR of arbitrary plutus data

Open nullhashpixel opened this issue 7 months ago • 1 comments

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?

nullhashpixel avatar Jul 22 '24 21:07 nullhashpixel