@@ -101,6 +101,32 @@ def to_dict(self) -> Dict[str, Any]:
101
101
"""
102
102
raise NotImplementedError
103
103
104
+ def _to_dict (self ) -> Dict [str , Any ]:
105
+ """Serialization helper to add base Key fields to a dict.
106
+
107
+ Key implementations may call this in their to_dict, which they must
108
+ still provide, in order to avoid unnoticed serialization accidents.
109
+ """
110
+ return {
111
+ "keytype" : self .keytype ,
112
+ "scheme" : self .scheme ,
113
+ "keyval" : self .keyval ,
114
+ ** self .unrecognized_fields ,
115
+ }
116
+
117
+ @staticmethod
118
+ def _from_dict (key_dict : Dict [str , Any ]) -> Tuple [str , str , Dict [str , Any ]]:
119
+ """Deserialization helper to pop base Key fields off the dict.
120
+
121
+ Key implementations may call this in their from_dict, in order to parse
122
+ out common fields. But they have to create the Key instance themselves.
123
+ """
124
+ keytype = key_dict .pop ("keytype" )
125
+ scheme = key_dict .pop ("scheme" )
126
+ keyval = key_dict .pop ("keyval" )
127
+
128
+ return keytype , scheme , keyval
129
+
104
130
@abstractmethod
105
131
def verify_signature (self , signature : Signature , data : bytes ) -> None :
106
132
"""Raises if verification of signature over data fails.
@@ -143,9 +169,7 @@ def from_securesystemslib_key(cls, key_dict: Dict[str, Any]) -> "SSlibKey":
143
169
144
170
@classmethod
145
171
def from_dict (cls , keyid : str , key_dict : Dict [str , Any ]) -> "SSlibKey" :
146
- keytype = key_dict .pop ("keytype" )
147
- scheme = key_dict .pop ("scheme" )
148
- keyval = key_dict .pop ("keyval" )
172
+ keytype , scheme , keyval = cls ._from_dict (key_dict )
149
173
150
174
if "public" not in keyval or not isinstance (keyval ["public" ], str ):
151
175
raise ValueError (f"public key string required for scheme { scheme } " )
@@ -154,12 +178,7 @@ def from_dict(cls, keyid: str, key_dict: Dict[str, Any]) -> "SSlibKey":
154
178
return cls (keyid , keytype , scheme , keyval , key_dict )
155
179
156
180
def to_dict (self ) -> Dict [str , Any ]:
157
- return {
158
- "keytype" : self .keytype ,
159
- "scheme" : self .scheme ,
160
- "keyval" : self .keyval ,
161
- ** self .unrecognized_fields ,
162
- }
181
+ return self ._to_dict ()
163
182
164
183
def verify_signature (self , signature : Signature , data : bytes ) -> None :
165
184
try :
0 commit comments