-
-
Notifications
You must be signed in to change notification settings - Fork 77
Add TextEnvelope class for saving and loading Witness and Transaction files #448
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
base: main
Are you sure you want to change the base?
Conversation
…lope save and load
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #448 +/- ##
==========================================
+ Coverage 88.86% 90.12% +1.25%
==========================================
Files 33 33
Lines 4804 4880 +76
Branches 731 736 +5
==========================================
+ Hits 4269 4398 +129
+ Misses 374 310 -64
- Partials 161 172 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for raising this PR! Enabling classes to save to and load from json is amazing. I think it is possible to simplify the implementation by directly adding the json ability to CBORSerialzable. See the detailed comment below. Let me know if that makes sense to you. Thanks!
@dataclass(repr=False) | ||
class TextEnvelope(CBORSerializable): | ||
"""A base class for TextEnvelope types that can be saved and loaded as JSON.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a good idea to combine this directly with CBORSerializable, which would instantly enable all pycardano classes to save to and load from json. I don't think private fields, key_type
and description
, are necessary, because they usually don't change between instances. If we want to customize them for certain child class, the child class can always override the method json_key_type
or json_description
. Regarding payload
, it becomes redundant once the logic is merged to CBORSerializable. The default implementation can directly use the class name as the key type, something like below:
class CBORSerializable:
....
@property
def json_key_type(self) -> str:
return self.__class__.__name__
@property
def json_description(self) -> str:
return self.__class__.__doc__
def to_json(self) -> str:
return json.dumps(
{
"type": self.json_key_type,
"description": self.json_description,
"cborHex": self.to_cbor_hex(),
}
)
Basic usage

This pull request introduces a new
TextEnvelope
class to add JSON serialization and deserialization capabilities. It is added to theTransaction
andVerificationKeyWitness
classes, while adding new tests to ensure robustness. TheTextEnvelope
class can later be added to the various Certificates to allow them the same save and load functionality. Below are the key changes grouped by theme:Core Enhancements
TextEnvelope
class inpycardano/serialization.py
to enable JSON-based serialization and deserialization with fields liketype
,description
, andcborHex
. This class also includes methods for saving and loading JSON data to and from files.Transaction
andVerificationKeyWitness
classes to inherit fromTextEnvelope
, enabling them to utilize its serialization features. Added customKEY_TYPE
andDESCRIPTION
properties to these classes. [1] [2]Utility and Validation Improvements
is_empty
toTransactionWitnessSet
for checking whether the witness set is empty.__eq__
) and shallow primitive conversion (to_shallow_primitive
) methods toVerificationKeyWitness
to work with TextEnvelope.Testing Enhancements
save
andload
methods ofTransaction
andVerificationKeyWitness
to validate JSON serialization and file handling. [1] [2]DRep
by introducing parameterized tests to handle various input scenarios, including edge cases and invalid inputs.