Skip to content

Commit 8cbf815

Browse files
Fix plutus script deserialization (#293)
* Added extended signing key support for cip8 * Fixed unused imports, flake8 checks pass. * Fixed mypy error for overloaded variable * Remove extraneous parameter for verify * Added ByteString to _restored_typed_primitive * Added type checking * Fixed bug where script inputs for transactions could not be deserialized * Stronger assumption in test for script serialization --------- Co-authored-by: Niels Mündler <[email protected]>
1 parent d1ec439 commit 8cbf815

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pycardano/serialization.py

+5
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ def _restore_typed_primitive(
536536
if not isinstance(v, bytes):
537537
raise DeserializeException(f"Expected type bytes but got {type(v)}")
538538
return ByteString(v)
539+
elif isclass(t) and t.__name__ in ["PlutusV1Script", "PlutusV2Script"]:
540+
if not isinstance(v, bytes):
541+
raise DeserializeException(f"Expected type bytes but got {type(v)}")
542+
return t(v)
543+
539544
elif isclass(t) and issubclass(t, IndefiniteList):
540545
try:
541546
return IndefiniteList(v)

test/pycardano/test_plutus.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
from pycardano.exception import DeserializeException
1313
from pycardano.plutus import (
1414
COST_MODELS,
15+
Datum,
1516
ExecutionUnits,
1617
PlutusData,
1718
RawPlutusData,
1819
Redeemer,
1920
RedeemerTag,
20-
plutus_script_hash,
21-
id_map,
22-
Datum,
2321
Unit,
22+
id_map,
23+
plutus_script_hash,
2424
)
25-
from pycardano.serialization import IndefiniteList, RawCBOR, ByteString
25+
from pycardano.serialization import ByteString, IndefiniteList, RawCBOR
2626

2727

2828
@dataclass

test/pycardano/test_serialization.py

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pycardano import Datum, RawPlutusData
99
from pycardano.exception import DeserializeException
10+
from pycardano.plutus import PlutusV1Script, PlutusV2Script
1011
from pycardano.serialization import (
1112
ArrayCBORSerializable,
1213
CBORSerializable,
@@ -286,3 +287,17 @@ class Test2(MapCBORSerializable):
286287

287288
with pytest.raises(TypeError):
288289
Test2(a=Test1(a=1)).to_cbor_hex()
290+
291+
292+
def test_script_deserialize():
293+
@dataclass
294+
class Test(MapCBORSerializable):
295+
script_1: PlutusV1Script
296+
script_2: PlutusV2Script
297+
298+
datum = Test(
299+
script_1=PlutusV1Script(b"dummy test script"),
300+
script_2=PlutusV2Script(b"dummy test script"),
301+
)
302+
303+
assert datum == datum.from_cbor(datum.to_cbor())

0 commit comments

Comments
 (0)