Skip to content

Commit 4b00d4e

Browse files
authored
Merge pull request #447 from kentakayama/add-detached-content-converter
Fix detach&attach not to create new COSEMessage instance
2 parents b279a8c + 659da4b commit 4b00d4e

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

cwt/cose_message.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ def __init__(self, type: COSETypes, msg: List[Any]):
101101
def __eq__(self: COSEMessage, other: object) -> bool:
102102
if not isinstance(other, COSEMessage):
103103
return NotImplemented
104-
return (
105-
self._type == other._type
106-
and self._protected == other._protected
107-
and self._unprotected == other._unprotected
108-
and self._payload == other._payload
109-
and self._other_fields == other._other_fields
110-
)
104+
return self._type == other._type and self._msg == other._msg
111105

112106
def __ne__(self: COSEMessage, other: object) -> bool:
113107
return not self.__eq__(other)
@@ -307,16 +301,17 @@ def detach_payload(self) -> Tuple[COSEMessage, bytes]:
307301
Detach a payload from the COSE message
308302
309303
Returns:
310-
Tuple[COSEMessage, bytes]: A byte string of the encoded COSE or a
311-
cbor2.CBORTag object, and a byte string of the detached payload.
304+
Tuple[COSEMessage, bytes]: The COSE message (self),
305+
and a byte string of the detached payload.
312306
Raises:
313307
ValueError: The payload does not exist.
314308
"""
315309

316-
if not isinstance(self._payload, bytes):
310+
if self._msg[2] is None:
317311
raise ValueError("The payload does not exist.")
318312

319-
return COSEMessage(self._type, [self._msg[0], self._msg[1], None, *self._msg[3:]]), self._payload
313+
self._msg[2] = None
314+
return self, self._payload
320315

321316
def attach_payload(self, payload: bytes) -> COSEMessage:
322317
"""
@@ -330,7 +325,8 @@ def attach_payload(self, payload: bytes) -> COSEMessage:
330325
ValueError: The payload already exist.
331326
"""
332327

333-
if self._payload is not None:
328+
if self._msg[2] is not None:
334329
raise ValueError("The payload already exist.")
330+
self._payload = payload
335331

336-
return COSEMessage(self._type, [self._msg[0], self._msg[1], payload, *self._msg[3:]])
332+
return self

tests/test_cose_message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def test_cose_message_detach_and_attach(self):
502502

503503
detached_content_cose_message, detached_payload = ecdsa_cose_sign1_example.detach_payload()
504504
assert expected_detached_cose_message == detached_content_cose_message
505+
assert expected_detached_cose_message.dumps() == detached_content_cose_message.dumps()
505506
assert expected_payload == detached_payload
506507

507508
reverted_cose_message = detached_content_cose_message.attach_payload(detached_payload)

0 commit comments

Comments
 (0)