From e4d1851807b7b16870f5dff71e691a42fdb050a7 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 12 Oct 2024 13:04:55 -0700 Subject: [PATCH 1/2] Fix witness deser --- pycardano/witness.py | 59 ----------------------------------- test/pycardano/test_plutus.py | 9 ++++++ 2 files changed, 9 insertions(+), 59 deletions(-) diff --git a/pycardano/witness.py b/pycardano/witness.py index e47ccf3b..5dfa1598 100644 --- a/pycardano/witness.py +++ b/pycardano/witness.py @@ -94,62 +94,3 @@ class TransactionWitnessSet(MapCBORSerializable): plutus_v3_script: Optional[List[PlutusV3Script]] = field( default=None, metadata={"optional": True, "key": 7} ) - - @classmethod - @limit_primitive_type(dict, list) - def from_primitive( - cls: Type[TransactionWitnessSet], values: Union[dict, list, tuple] - ) -> TransactionWitnessSet | None: - def _get_vkey_witnesses(data: Any): - return ( - [VerificationKeyWitness.from_primitive(witness) for witness in data] - if data - else None - ) - - def _get_native_scripts(data: Any): - return ( - [NativeScript.from_primitive(script) for script in data] - if data - else None - ) - - def _get_plutus_v1_scripts(data: Any): - return [PlutusV1Script(script) for script in data] if data else None - - def _get_plutus_v2_scripts(data: Any): - return [PlutusV2Script(script) for script in data] if data else None - - def _get_redeemers(data: Any): - if not data: - return None - if isinstance(data, dict): - redeemer_map = RedeemerMap() - for (tag, index), value in data.items(): - key = RedeemerKey(RedeemerTag(tag), index) - redeemer_value = RedeemerValue(value[0], ExecutionUnits(*value[1])) - redeemer_map[key] = redeemer_value - return redeemer_map - elif isinstance(data, list): - return [Redeemer.from_primitive(redeemer) for redeemer in data] - else: - raise ValueError(f"Unexpected redeemer data format: {type(data)}") - - def _get_cls(data: Any): - return cls( - vkey_witnesses=_get_vkey_witnesses(data.get(0)), - native_scripts=_get_native_scripts(data.get(1)), - bootstrap_witness=data.get(2), - plutus_v1_script=_get_plutus_v1_scripts(data.get(3)), - plutus_data=data.get(4), - redeemer=_get_redeemers(data.get(5)), - plutus_v2_script=_get_plutus_v2_scripts(data.get(6)), - ) - - if isinstance(values, dict): - return _get_cls(values) - elif isinstance(values, list): - # TODO: May need to handle this differently - values = dict(values) - return _get_cls(values) - return None diff --git a/test/pycardano/test_plutus.py b/test/pycardano/test_plutus.py index f339a9f5..26a5ac08 100644 --- a/test/pycardano/test_plutus.py +++ b/test/pycardano/test_plutus.py @@ -9,6 +9,7 @@ import pytest from cbor2 import CBORTag +from pycardano import TransactionWitnessSet from pycardano.exception import DeserializeException from pycardano.plutus import ( COST_MODELS, @@ -584,3 +585,11 @@ def test_redeemer_map(): assert deserialized[key1].ex_units == value1.ex_units assert deserialized[key2].data == value2.data assert deserialized[key2].ex_units == value2.ex_units + + +def test_empty_map_deser(): + empty_map = RedeemerMap() + witness = TransactionWitnessSet(redeemer=empty_map) + serialized = witness.to_primitive() + deserialized = TransactionWitnessSet.from_primitive(serialized) + assert deserialized.redeemer == empty_map From af5a6f1bd88b1cbc8f8b82f1e3b1bbb682328dfc Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 12 Oct 2024 13:15:57 -0700 Subject: [PATCH 2/2] Remove unused imports --- pycardano/witness.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pycardano/witness.py b/pycardano/witness.py index 5dfa1598..ff47a96d 100644 --- a/pycardano/witness.py +++ b/pycardano/witness.py @@ -8,17 +8,11 @@ from pycardano.key import ExtendedVerificationKey, VerificationKey from pycardano.nativescript import NativeScript from pycardano.plutus import ( - ExecutionUnits, PlutusV1Script, PlutusV2Script, PlutusV3Script, RawPlutusData, - Redeemer, - RedeemerKey, - RedeemerMap, Redeemers, - RedeemerTag, - RedeemerValue, ) from pycardano.serialization import ( ArrayCBORSerializable,