Skip to content

Commit

Permalink
Merge branch 'feat/fix_cardano_cli' into feat/fix_deserialization_of_…
Browse files Browse the repository at this point in the history
…certificates
  • Loading branch information
nielstron committed Feb 17, 2024
2 parents cc4ba50 + 52b18a4 commit 361ae86
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=False)
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 361ae86

Please sign in to comment.