File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4343 Timestamp ,
4444)
4545from tuf .api .serialization import DeserializationError , SerializationError
46- from tuf .api .serialization .json import CanonicalJSONSerializer , JSONSerializer
46+ from tuf .api .serialization .json import JSONSerializer
4747
4848logger = logging .getLogger (__name__ )
4949
@@ -202,7 +202,7 @@ def test_sign_verify(self) -> None:
202202 # Load sample metadata (targets) and assert ...
203203 md_obj = Metadata .from_file (os .path .join (path , "targets.json" ))
204204 sig = md_obj .signatures [targets_keyid ]
205- data = CanonicalJSONSerializer (). serialize ( md_obj .signed )
205+ data = md_obj .signed_bytes
206206
207207 # ... it has a single existing signature,
208208 self .assertEqual (len (md_obj .signatures ), 1 )
@@ -257,7 +257,7 @@ def test_key_verify_failures(self) -> None:
257257 path = os .path .join (self .repo_dir , "metadata" , "timestamp.json" )
258258 md_obj = Metadata .from_file (path )
259259 sig = md_obj .signatures [timestamp_keyid ]
260- data = CanonicalJSONSerializer (). serialize ( md_obj .signed )
260+ data = md_obj .signed_bytes
261261
262262 # Test failure on unknown scheme (securesystemslib
263263 # UnsupportedAlgorithmError)
Original file line number Diff line number Diff line change @@ -150,6 +150,16 @@ def __eq__(self, other: Any) -> bool:
150150 and self .unrecognized_fields == other .unrecognized_fields
151151 )
152152
153+ @property
154+ def signed_bytes (self ) -> bytes :
155+ """Default canonical json byte representation of ``self.signed``."""
156+
157+ # Use local scope import to avoid circular import errors
158+ # pylint: disable=import-outside-toplevel
159+ from tuf .api .serialization .json import CanonicalJSONSerializer
160+
161+ return CanonicalJSONSerializer ().serialize (self .signed )
162+
153163 @classmethod
154164 def from_dict (cls , metadata : Dict [str , Any ]) -> "Metadata[T]" :
155165 """Create ``Metadata`` object from its json/dict representation.
@@ -366,13 +376,9 @@ def sign(
366376 """
367377
368378 if signed_serializer is None :
369- # Use local scope import to avoid circular import errors
370- # pylint: disable=import-outside-toplevel
371- from tuf .api .serialization .json import CanonicalJSONSerializer
372-
373- signed_serializer = CanonicalJSONSerializer ()
374-
375- bytes_data = signed_serializer .serialize (self .signed )
379+ bytes_data = self .signed_bytes
380+ else :
381+ bytes_data = signed_serializer .serialize (self .signed )
376382
377383 try :
378384 signature = signer .sign (bytes_data )
You can’t perform that action at this time.
0 commit comments