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

Add test cases and fix inlineDatum fetching for the Cardano CLI Backend #303

Merged
merged 10 commits into from
Feb 20, 2024
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
Loading