Skip to content

Commit

Permalink
Add test cases and fix inlineDatum fetching for the Cardano CLI Backe…
Browse files Browse the repository at this point in the history
…nd (Python-Cardano#303)

* Add tests for plutus script fetching and inline datums

* Add test case for datum hash

* Add epoch test case

* Formatting

* Fix typing

* Be more lenient in assigning datum hash

This is consistent with ogmios

* Generalize the types inside RawPlutusData

* Add support for RawCBOR primitivization

* Fix limit primitive type to not use a Union type
  • Loading branch information
nielstron authored Feb 20, 2024
1 parent 58e0cb2 commit 8c3a12b
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pycardano/backend/cardano_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from pycardano.hash import DatumHash, ScriptHash
from pycardano.nativescript import NativeScript
from pycardano.network import Network
from pycardano.plutus import PlutusV1Script, PlutusV2Script
from pycardano.plutus import PlutusV1Script, PlutusV2Script, RawPlutusData, Datum
from pycardano.serialization import RawCBOR
from pycardano.transaction import (
Asset,
Expand Down Expand Up @@ -437,16 +437,16 @@ def _utxos(self, address: str) -> List[UTxO]:

datum_hash = (
DatumHash.from_primitive(utxo["datumhash"])
if utxo.get("datumhash") and utxo.get("inlineDatum") is None
if utxo.get("datumhash") is not None
else None
)

datum = None
datum: Optional[Datum] = None

if utxo.get("datum"):
datum = RawCBOR(bytes.fromhex(utxo["datum"]))
elif utxo.get("inlineDatumhash"):
datum = utxo["inlineDatum"]
datum = RawPlutusData.from_dict(utxo["inlineDatum"])

script = None

Expand Down
15 changes: 11 additions & 4 deletions pycardano/plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,14 @@ def __deepcopy__(self, memo):
return self.__class__.from_cbor(self.to_cbor_hex())


RawDatum = Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, CBORTag]


@dataclass(repr=True)
class RawPlutusData(CBORSerializable):
data: CBORTag
data: RawDatum

def to_primitive(self) -> CBORTag:
def to_primitive(self) -> RawDatum:
def _dfs(obj):
if isinstance(obj, list) and obj:
return IndefiniteList([_dfs(item) for item in obj])
Expand Down Expand Up @@ -823,6 +826,8 @@ def _dfs(obj):
elif isinstance(obj, CBORTag):
constructor, fields = get_constructor_id_and_fields(obj)
return {"constructor": constructor, "fields": [_dfs(f) for f in fields]}
elif isinstance(obj, RawCBOR):
return RawPlutusData.from_cbor(obj.cbor).to_dict()
raise TypeError(f"Unexpected type {type(obj)}")

return _dfs(RawPlutusData.to_primitive(self))
Expand All @@ -840,8 +845,10 @@ def to_json(self, **kwargs) -> str:
return json.dumps(RawPlutusData.to_dict(self), **kwargs)

@classmethod
@limit_primitive_type(CBORTag)
def from_primitive(cls: Type[RawPlutusData], value: CBORTag) -> RawPlutusData:
@limit_primitive_type(
PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, CBORTag
) # equal to RawDatum parameter list
def from_primitive(cls: Type[RawPlutusData], value: RawDatum) -> RawPlutusData:
return cls(value)

@classmethod
Expand Down
Loading

0 comments on commit 8c3a12b

Please sign in to comment.