Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions c2pie/c2pa/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
)
from c2pie.utils.content_types import jumbf_content_types

_ALLOWED_ACTIONS = ["c2pa.created", "c2pa.opened"]


class Assertion(SuperBox):
"""Universal assertion superbox (one content box)."""
Expand Down Expand Up @@ -94,3 +96,26 @@ def set_hash_data_length(self, length: int) -> None:
)
]
self.sync_payload()


class ActionsAssertion(Assertion):
"""c2pa.actions.v2 assertion of actions on an asset."""

def __init__(
self,
action: str,
Comment thread
aasheptunov marked this conversation as resolved.
parameters: dict[str, list[dict[str, Any]]] | None = None,
):
if action not in _ALLOWED_ACTIONS:
raise ValueError(f"Invalid action {action!r}. Must be one of: {_ALLOWED_ACTIONS}")

schema: dict[str, Any] = {
"actions": [
{"action": action},
],
}

if parameters:
schema["actions"][0]["parameters"] = parameters

super().__init__(C2PA_AssertionTypes.actions, schema)
9 changes: 8 additions & 1 deletion c2pie/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import uuid

from c2pie.c2pa.assertion import Assertion, HashDataAssertion
from c2pie.c2pa.assertion import ActionsAssertion, Assertion, HashDataAssertion
from c2pie.c2pa.assertion_store import AssertionStore
from c2pie.c2pa.claim import Claim
from c2pie.c2pa.claim_signature import ClaimSignature
Expand All @@ -23,6 +23,13 @@ def c2pie_GenerateHashDataAssertion(cai_offset: int, hashed_data: bytes) -> Hash
return HashDataAssertion(cai_offset, hashed_data)


def c2pie_GenerateActionsAssertion(
action: str,
parameters: dict[str, list[dict[str, str]]] | None = None,
) -> ActionsAssertion:
return ActionsAssertion(action, parameters)


def c2pie_GenerateManifest(
assertions: list,
private_key: bytes,
Expand Down
7 changes: 6 additions & 1 deletion c2pie/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from c2pie.interface import (
C2PA_AssertionTypes,
c2pie_EmplaceManifest,
c2pie_GenerateActionsAssertion,
c2pie_GenerateAssertion,
c2pie_GenerateHashDataAssertion,
c2pie_GenerateManifest,
Expand Down Expand Up @@ -208,7 +209,11 @@ def sign_file(
cai_offset=cai_offset, hashed_data=hashlib.sha256(raw_bytes).digest()
)

assertions = [creative_work_assertion, hash_data_assertion]
# This section should be replaced with the content generation logic once the relevant
# functionality is available (example, action 'c2pa.opened' for Ingredient Assertion)
actions_assertion = c2pie_GenerateActionsAssertion(action="c2pa.created")

assertions = [creative_work_assertion, hash_data_assertion, actions_assertion]

manifest = c2pie_GenerateManifest(
assertions=assertions,
Expand Down
7 changes: 7 additions & 0 deletions c2pie/utils/assertion_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class C2PA_AssertionTypes(enum.Enum):
creative_work = 0
data_hash = 1
thumbnail = 2
actions = 3


def json_to_bytes(json_object: dict[str, Any]) -> bytes:
Expand All @@ -28,6 +29,8 @@ def get_assertion_content_type(assertion_type: C2PA_AssertionTypes) -> bytes:
return jumbf_content_types["cbor"]
elif assertion_type == C2PA_AssertionTypes.thumbnail:
return jumbf_content_types["codestream"]
elif assertion_type == C2PA_AssertionTypes.actions:
return jumbf_content_types["cbor"]
else:
return b""

Expand All @@ -39,6 +42,8 @@ def get_assertion_content_box_type(assertion_type: C2PA_AssertionTypes) -> str:
return b"cbor".hex()
elif assertion_type == C2PA_AssertionTypes.thumbnail:
return b"codestream".hex() # figure out which content type should be
elif assertion_type == C2PA_AssertionTypes.actions:
return b"cbor".hex()
else:
return b"".hex()

Expand All @@ -50,5 +55,7 @@ def get_assertion_label(assertion_type: C2PA_AssertionTypes) -> str:
return "c2pa.hash.data"
elif assertion_type == C2PA_AssertionTypes.thumbnail:
return "c2pa.thumbnail.claim.jpg"
elif assertion_type == C2PA_AssertionTypes.actions:
return "c2pa.actions.v2"
else:
return ""
56 changes: 56 additions & 0 deletions tests/c2pa/assertions/actions_assertion_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from c2pie.c2pa.assertion import ActionsAssertion
from c2pie.utils.assertion_schemas import cbor_to_bytes
from c2pie.utils.content_types import jumbf_content_types


def test_actions_assertion_content_type_is_cbor():
actions_assertion = ActionsAssertion(action="c2pa.created")
assert actions_assertion.get_content_type() == jumbf_content_types["cbor"]


def test_actions_assertion_label():
actions_assertion = ActionsAssertion(action="c2pa.created")
assert actions_assertion.get_label() == "c2pa.actions.v2"


def test_actions_assertion_schema_fields():
actions_assertion = ActionsAssertion(action="c2pa.created")
assert actions_assertion.schema["actions"][0]["action"] == "c2pa.created"


def test_actions_assertion_schema_has_no_parameters_by_default():
actions_assertion = ActionsAssertion(action="c2pa.created")
assert "parameters" not in actions_assertion.schema["actions"][0]


def test_actions_assertion_schema_fields_with_parameters():
expected_parameters = {
"ingredients": [
{
"url": "self#jumbf=c2pa.assertions/c2pa.ingredient.v3",
"alg": "sha256",
"hash": "B-X0kqzQsdIOC9ZxU7M/FRaaLlQ8Ap1U95//TucEjkT=",
}
]
}

actions_assertion = ActionsAssertion(action="c2pa.opened", parameters=expected_parameters)
assert actions_assertion.schema["actions"][0]["action"] == "c2pa.opened"
assert actions_assertion.schema["actions"][0]["parameters"] == expected_parameters


def test_actions_assertion_serializes_as_cbor():
actions_assertion = ActionsAssertion(action="c2pa.created")

expected_payload = cbor_to_bytes(
{
"actions": [
{
"action": "c2pa.created",
},
],
},
)

assert len(actions_assertion.content_boxes) == 1
assert actions_assertion.content_boxes[0].payload == expected_payload
File renamed without changes.