Skip to content

Commit

Permalink
Fix witness deser
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Oct 12, 2024
1 parent 3034cd9 commit e4d1851
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 59 deletions.
59 changes: 0 additions & 59 deletions pycardano/witness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions test/pycardano/test_plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit e4d1851

Please sign in to comment.