diff --git a/pycardano/address.py b/pycardano/address.py index ba7a6dd7..61547edc 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -416,6 +416,7 @@ def save( path: str, key_type: Optional[str] = None, description: Optional[str] = None, + **kwargs, ): """ Save the Address object to a file. @@ -427,6 +428,7 @@ def save( path (str): The file path to save the object to. key_type (str, optional): Not used in this context, but can be included for consistency. description (str, optional): Not used in this context, but can be included for consistency. + **kwargs: Additional keyword arguments (not used here). Raises: IOError: If the file already exists and is not empty. diff --git a/pycardano/key.py b/pycardano/key.py index e8bce690..d2d728d8 100644 --- a/pycardano/key.py +++ b/pycardano/key.py @@ -73,7 +73,7 @@ def to_primitive(self) -> bytes: def from_primitive(cls: Type["Key"], value: bytes) -> Key: return cls(value) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Serialize the key to JSON. The json output has three fields: "type", "description", and "cborHex". @@ -90,7 +90,7 @@ def to_json(self, **kwargs) -> str: ) @classmethod - def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: + def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: # type: ignore """Restore a key from a JSON string. Args: diff --git a/pycardano/plutus.py b/pycardano/plutus.py index 435c8f4f..83c66705 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -652,7 +652,7 @@ def _dfs(obj): return _dfs(self) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Convert to a json string Args: @@ -847,7 +847,7 @@ def _dfs(obj): return _dfs(RawPlutusData.to_primitive(self)) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Convert to a json string Args: diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 63e13586..22f51a92 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -561,29 +561,35 @@ def json_description(self) -> str: """ return self.__class__.__doc__ or "Generated with PyCardano" - def to_json(self, **kwargs) -> str: + def to_json( + self, + key_type: Optional[str] = None, + description: Optional[str] = None, + **kwargs, + ) -> str: """ Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex. This method returns a JSON representation of the object, including its type, description, and CBOR hex encoding. Args: - **kwargs: Additional keyword arguments that can include: - - key_type (str): The type to use in the JSON output. Defaults to the class name. - - description (str): The description to use in the JSON output. Defaults to the class docstring. + key_type (str): The type to use in the JSON output. Defaults to the class name. + description (str): The description to use in the JSON output. Defaults to the class docstring. + **kwargs: Extra key word arguments to be passed to `json.dumps()` Returns: str: The JSON string representation of the object. """ - key_type = kwargs.pop("key_type", self.json_type) - description = kwargs.pop("description", self.json_description) + if "indent" not in kwargs: + kwargs["indent"] = 2 + return json.dumps( { - "type": key_type, - "description": description, + "type": key_type or self.json_type, + "description": description or self.json_description, "cborHex": self.to_cbor_hex(), }, - indent=2, + **kwargs, ) @classmethod @@ -616,6 +622,7 @@ def save( path: str, key_type: Optional[str] = None, description: Optional[str] = None, + **kwargs, ): """ Save the CBORSerializable object to a file in JSON format. @@ -625,8 +632,9 @@ def save( Args: path (str): The file path to save the object to. - key_type (str, optional): The type to use in the JSON output. - description (str, optional): The description to use in the JSON output. + key_type (str, optional): The type to use in the JSON output. Defaults to the class name. + description (str, optional): The description to use in the JSON output. Defaults to the class docstring. + **kwargs: Extra key word arguments to be passed to `json.dumps()` Raises: IOError: If the file already exists and is not empty. @@ -634,7 +642,7 @@ def save( if os.path.isfile(path) and os.stat(path).st_size > 0: raise IOError(f"File {path} already exists!") with open(path, "w") as f: - f.write(self.to_json(key_type=key_type, description=description)) + f.write(self.to_json(key_type=key_type, description=description, **kwargs)) @classmethod def load(cls, path: str): diff --git a/pycardano/transaction.py b/pycardano/transaction.py index d9f5515f..a193baf7 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -698,7 +698,7 @@ class Transaction(ArrayCBORSerializable): def json_type(self) -> str: return ( "Unwitnessed Tx ConwayEra" - if self.transaction_witness_set.is_empty() + if self.transaction_witness_set.vkey_witnesses is None else "Signed Tx ConwayEra" )