Source code for aries_cloudagent.protocols.present_proof.v2_0.messages.pres

"""A (proof) presentation content message."""

from marshmallow import EXCLUDE, fields, validates_schema, ValidationError
from typing import Sequence

from .....messaging.agent_message import AgentMessage, AgentMessageSchema
from .....messaging.decorators.attach_decorator import (
    AttachDecorator,
    AttachDecoratorSchema,
)

from ..message_types import PRES_20, PROTOCOL_PACKAGE

from .pres_format import V20PresFormat, V20PresFormatSchema

HANDLER_CLASS = f"{PROTOCOL_PACKAGE}.handlers.pres_handler.V20PresHandler"


[docs]class V20Pres(AgentMessage): """Class representing a presentation."""
[docs] class Meta: """Presentation metadata.""" handler_class = HANDLER_CLASS schema_class = "V20PresSchema" message_type = PRES_20
def __init__( self, _id: str = None, *, comment: str = None, formats: Sequence[V20PresFormat] = None, presentations_attach: Sequence[AttachDecorator] = None, **kwargs, ): """ Initialize presentation object. Args: presentations_attach: attachments comment: optional comment """ super().__init__(_id=_id, **kwargs) self.comment = comment self.formats = formats if formats else [] self.presentations_attach = ( list(presentations_attach) if presentations_attach else [] )
[docs] def attachment(self, fmt: V20PresFormat.Format = None) -> dict: """ Return attached presentation item. Args: fmt: format of attachment in list to decode and return """ target_format = ( fmt if fmt else next( filter( lambda ff: ff, [V20PresFormat.Format.get(f.format) for f in self.formats], ), None, ) ) return ( target_format.get_attachment_data(self.formats, self.presentations_attach) if target_format else None )
[docs]class V20PresSchema(AgentMessageSchema): """Presentation schema."""
[docs] class Meta: """Presentation schema metadata.""" model_class = V20Pres unknown = EXCLUDE
comment = fields.Str( description="Human-readable comment", required=False, allow_none=True ) formats = fields.Nested( V20PresFormatSchema, many=True, required=True, description="Acceptable attachment formats", ) presentations_attach = fields.Nested( AttachDecoratorSchema, required=True, many=True, data_key="presentations~attach" ) @validates_schema def validate_fields(self, data, **kwargs): """Validate presentation attachment per format.""" def get_attach_by_id(attach_id): """Return attachment with input attachment identifier.""" for atch in attachments: if atch.ident == attach_id: return atch raise ValidationError(f"No attachment for attach_id {attach_id} in formats") formats = data.get("formats") or [] attachments = data.get("presentations_attach") or [] if len(formats) != len(attachments): raise ValidationError("Formats/attachments length mismatch") for fmt in formats: atch = get_attach_by_id(fmt.attach_id) pres_format = V20PresFormat.Format.get(fmt.format) if pres_format: if isinstance(atch.content, Sequence): for el in atch.content: pres_format.validate_fields(PRES_20, el) else: pres_format.validate_fields(PRES_20, atch.content)