@@ -89,19 +89,23 @@ class Statement:
89
89
See: <https://github.com/in-toto/attestation/blob/main/spec/v1/statement.md>
90
90
"""
91
91
92
- def __init__ (self , contents : bytes ) -> None :
92
+ def __init__ (self , contents : bytes | _Statement ) -> None :
93
93
"""
94
94
Construct a new Statement.
95
95
96
96
This takes an opaque `bytes` containing the statement; use
97
97
`StatementBuilder` to manually construct an in-toto statement
98
98
from constituent pieces.
99
99
"""
100
- self ._contents = contents
101
- try :
102
- self ._inner = _Statement .model_validate_json (contents )
103
- except ValidationError :
104
- raise Error ("malformed in-toto statement" )
100
+ if isinstance (contents , bytes ):
101
+ self ._contents = contents
102
+ try :
103
+ self ._inner = _Statement .model_validate_json (contents )
104
+ except ValidationError :
105
+ raise Error ("malformed in-toto statement" )
106
+ else :
107
+ self ._contents = contents .model_dump_json (by_alias = True ).encode ()
108
+ self ._inner = contents
105
109
106
110
def _matches_digest (self , digest : Hashed ) -> bool :
107
111
"""
@@ -130,7 +134,7 @@ def _pae(self) -> bytes:
130
134
return _pae (Envelope ._TYPE , self ._contents )
131
135
132
136
133
- class _StatementBuilder :
137
+ class StatementBuilder :
134
138
"""
135
139
A builder-style API for constructing in-toto Statements.
136
140
"""
@@ -142,27 +146,27 @@ def __init__(
142
146
predicate : Optional [Dict [str , Any ]] = None ,
143
147
):
144
148
"""
145
- Create a new `_StatementBuilder `.
149
+ Create a new `StatementBuilder `.
146
150
"""
147
151
self ._subjects = subjects or []
148
152
self ._predicate_type = predicate_type
149
153
self ._predicate = predicate
150
154
151
- def subjects (self , subjects : list [_Subject ]) -> _StatementBuilder :
155
+ def subjects (self , subjects : list [_Subject ]) -> StatementBuilder :
152
156
"""
153
157
Configure the subjects for this builder.
154
158
"""
155
159
self ._subjects = subjects
156
160
return self
157
161
158
- def predicate_type (self , predicate_type : str ) -> _StatementBuilder :
162
+ def predicate_type (self , predicate_type : str ) -> StatementBuilder :
159
163
"""
160
164
Configure the predicate type for this builder.
161
165
"""
162
166
self ._predicate_type = predicate_type
163
167
return self
164
168
165
- def predicate (self , predicate : dict [str , Any ]) -> _StatementBuilder :
169
+ def predicate (self , predicate : dict [str , Any ]) -> StatementBuilder :
166
170
"""
167
171
Configure the predicate for this builder.
168
172
"""
@@ -183,7 +187,7 @@ def build(self) -> Statement:
183
187
except ValidationError as e :
184
188
raise Error (f"invalid statement: { e } " )
185
189
186
- return Statement (stmt . model_dump_json ( by_alias = True ). encode () )
190
+ return Statement (stmt )
187
191
188
192
189
193
class Envelope :
0 commit comments