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

Fix witness deser #385

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions pycardano/witness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -94,62 +88,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
Loading