From bd447d7157122269e67961e0f4db5af34a7dd7a3 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Mon, 29 Jul 2024 19:15:23 +0530 Subject: [PATCH 1/9] fixed issues --- .../present_proof/v2_0/messages/pres_request.py | 7 +++++++ .../protocols/present_proof/v2_0/routes.py | 17 +++++++++++++++++ plugins | 1 + 3 files changed, 25 insertions(+) create mode 160000 plugins diff --git a/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py b/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py index 51e2d2dfb4..2b56bbf87a 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py @@ -32,6 +32,7 @@ def __init__( _id: str = None, *, comment: str = None, + verifier_did: str = None, will_confirm: bool = None, formats: Sequence[V20PresFormat] = None, request_presentations_attach: Sequence[AttachDecorator] = None, @@ -42,6 +43,7 @@ def __init__( Args: _id (str, optional): The ID of the presentation request. comment (str, optional): An optional comment. + verifier_did (str, optional): The DID of the verifier. will_confirm (bool, optional): A flag indicating whether the presentation request will be confirmed. formats (Sequence[V20PresFormat], optional): A sequence of presentation @@ -53,6 +55,7 @@ def __init__( """ super().__init__(_id=_id, **kwargs) self.comment = comment + self.verifier_did = verifier_did self.will_confirm = will_confirm or False self.formats = list(formats) if formats else [] self.request_presentations_attach = ( @@ -103,6 +106,10 @@ class Meta: required=False, metadata={"description": "Whether verifier will send confirmation ack"}, ) + verifier_did = fields.Str( + required=False, + metadata={"description": "DID of the verifier"}, + ) formats = fields.Nested( V20PresFormatSchema, many=True, diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index b18b6041dc..b7e09a5cb5 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -65,6 +65,7 @@ from .messages.pres_proposal import V20PresProposal from .messages.pres_request import V20PresRequest from .models.pres_exchange import V20PresExRecord, V20PresExRecordSchema +from ....wallet.base import BaseWallet class V20PresentProofModuleResponseSchema(OpenAPISchema): @@ -919,12 +920,20 @@ async def present_proof_create_request(request: web.BaseRequest): body = await request.json() comment = body.get("comment") + verifier_did = body.get("verifier_did") + wallet = profile.inject(BaseWallet) + try: + did_info = await wallet.get_local_did(verifier_did) # noqa: F841 + except WalletNotFoundError as err: + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: await _add_nonce(pres_request_spec[V20PresFormat.Format.INDY.api]) pres_request_message = V20PresRequest( comment=comment, + verifier_did=verifier_did, will_confirm=True, **_formats_attach(pres_request_spec, PRES_20_REQUEST, "request_presentations"), ) @@ -1003,11 +1012,19 @@ async def present_proof_send_free_request(request: web.BaseRequest): raise web.HTTPForbidden(reason=f"Connection {connection_id} not ready") comment = body.get("comment") + verifier_did = body.get("verifier_did") + wallet = profile.inject(BaseWallet) + try: + did_info = await wallet.get_local_did(verifier_did) # noqa: F841 + except WalletNotFoundError as err: + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: await _add_nonce(pres_request_spec[V20PresFormat.Format.INDY.api]) pres_request_message = V20PresRequest( comment=comment, + verifier_did=verifier_did, will_confirm=True, **_formats_attach(pres_request_spec, PRES_20_REQUEST, "request_presentations"), ) diff --git a/plugins b/plugins new file mode 160000 index 0000000000..71ad41e23d --- /dev/null +++ b/plugins @@ -0,0 +1 @@ +Subproject commit 71ad41e23d9a9ef68ca6927135ecf9712f9059ad From 861e2afe7d1280d57d7c6cb6b65a4687435465ca Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Mon, 29 Jul 2024 20:27:57 +0530 Subject: [PATCH 2/9] added did wallet check --- .../protocols/present_proof/v2_0/routes.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index b7e09a5cb5..522fb471d4 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -921,11 +921,12 @@ async def present_proof_create_request(request: web.BaseRequest): comment = body.get("comment") verifier_did = body.get("verifier_did") - wallet = profile.inject(BaseWallet) - try: - did_info = await wallet.get_local_did(verifier_did) # noqa: F841 - except WalletNotFoundError as err: - raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + async with profile.session() as session: + wallet = session.inject(BaseWallet) + try: + await wallet.get_local_did(did=verifier_did) + except WalletNotFoundError as err: + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -1013,11 +1014,12 @@ async def present_proof_send_free_request(request: web.BaseRequest): comment = body.get("comment") verifier_did = body.get("verifier_did") - wallet = profile.inject(BaseWallet) - try: - did_info = await wallet.get_local_did(verifier_did) # noqa: F841 - except WalletNotFoundError as err: - raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + async with profile.session() as session: + wallet = session.inject(BaseWallet) + try: + await wallet.get_local_did(did=verifier_did) + except WalletNotFoundError as err: + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: From e5421b83687ed1617140f683e70793cccc490f6c Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Thu, 1 Aug 2024 19:31:44 +0530 Subject: [PATCH 3/9] fix: fixed bug when no did is there Signed-off-by: aritroCoder --- .../protocols/present_proof/v2_0/routes.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index 522fb471d4..965077afdc 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -1014,12 +1014,13 @@ async def present_proof_send_free_request(request: web.BaseRequest): comment = body.get("comment") verifier_did = body.get("verifier_did") - async with profile.session() as session: - wallet = session.inject(BaseWallet) - try: - await wallet.get_local_did(did=verifier_did) - except WalletNotFoundError as err: - raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + if verifier_did is not None: + async with profile.session() as session: + wallet = session.inject(BaseWallet) + try: + await wallet.get_local_did(did=verifier_did) + except WalletNotFoundError as err: + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -1039,6 +1040,9 @@ async def present_proof_send_free_request(request: web.BaseRequest): context.settings, trace_msg, ) + + ser_pres_request_message = pres_request_message.serialize() + print(ser_pres_request_message, type(ser_pres_request_message)) pres_manager = V20PresManager(profile) pres_ex_record = None From 7bac0c75a04d8a8310b8bba6b033d54fe2f8132e Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Thu, 1 Aug 2024 20:50:58 +0530 Subject: [PATCH 4/9] feat: added verifer did signing on pres request --- .../protocols/present_proof/v2_0/routes.py | 52 +++++++++++++++---- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index 965077afdc..ad126dd0f9 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -921,12 +921,17 @@ async def present_proof_create_request(request: web.BaseRequest): comment = body.get("comment") verifier_did = body.get("verifier_did") - async with profile.session() as session: - wallet = session.inject(BaseWallet) - try: - await wallet.get_local_did(did=verifier_did) - except WalletNotFoundError as err: - raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + verifier_verkey = None + if verifier_did is not None: + async with profile.session() as session: + wallet = session.inject(BaseWallet) + try: + didinfo = await wallet.get_local_did(did=verifier_did) + verifier_verkey = didinfo.verkey + except WalletNotFoundError as err: + raise web.HTTPBadRequest( + reason="DID is not present in wallet!" + ) from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -948,6 +953,18 @@ async def present_proof_create_request(request: web.BaseRequest): trace_msg, ) + if verifier_verkey is not None: + ser_pres_request_message = pres_request_message.serialize() + ser_pres_request_message_bytes = json.dumps(ser_pres_request_message).encode( + "utf-8" + ) + async with profile.session() as session: + wallet = session.inject(BaseWallet) + await wallet.sign_message(ser_pres_request_message_bytes, verifier_verkey) + pres_request_message.set_signature( + "verifier_did", ser_pres_request_message_bytes + ) + pres_manager = V20PresManager(profile) pres_ex_record = None try: @@ -1014,13 +1031,17 @@ async def present_proof_send_free_request(request: web.BaseRequest): comment = body.get("comment") verifier_did = body.get("verifier_did") + verifier_verkey = None if verifier_did is not None: async with profile.session() as session: wallet = session.inject(BaseWallet) try: - await wallet.get_local_did(did=verifier_did) + didinfo = await wallet.get_local_did(did=verifier_did) + verifier_verkey = didinfo.verkey except WalletNotFoundError as err: - raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err + raise web.HTTPBadRequest( + reason="DID is not present in wallet!" + ) from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -1040,9 +1061,18 @@ async def present_proof_send_free_request(request: web.BaseRequest): context.settings, trace_msg, ) - - ser_pres_request_message = pres_request_message.serialize() - print(ser_pres_request_message, type(ser_pres_request_message)) + + if verifier_verkey is not None: + ser_pres_request_message = pres_request_message.serialize() + ser_pres_request_message_bytes = json.dumps(ser_pres_request_message).encode( + "utf-8" + ) + async with profile.session() as session: + wallet = session.inject(BaseWallet) + await wallet.sign_message(ser_pres_request_message_bytes, verifier_verkey) + pres_request_message.set_signature( + "verifier_did", ser_pres_request_message_bytes + ) pres_manager = V20PresManager(profile) pres_ex_record = None From 0177e55a8ff4c5995ef5adf133bd175bf0caa4a6 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Thu, 1 Aug 2024 22:34:58 +0530 Subject: [PATCH 5/9] feat: added holder side code for pres request verification --- .../v2_0/handlers/pres_request_handler.py | 24 +++++++++++++++++++ .../protocols/present_proof/v2_0/routes.py | 18 ++------------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py index 60b1fcff9d..980ae35462 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py @@ -1,5 +1,7 @@ """Presentation request message handler.""" +from aries_cloudagent.resolver.did_resolver import DIDResolver +from aries_cloudagent.wallet.base import BaseWallet from .....anoncreds.holder import AnonCredsHolderError from .....core.oob_processor import OobMessageProcessor from .....indy.holder import IndyHolderError @@ -64,6 +66,28 @@ async def handle(self, context: RequestContext, responder: BaseResponder): profile = context.profile pres_manager = V20PresManager(profile) + pres_request = context.message + if pres_request.verifier_did is not None: + verifier_did = pres_request.verifier_did + did_resolver = profile.inject(DIDResolver) + wallet = profile.inject(BaseWallet) + did_document = await did_resolver.resolve(profile=profile, did=verifier_did) + verification_method_list = did_document.get("verificationMethod", []) + request_verified = False + for method in verification_method_list: + verkey = method.get("publicKeyBase58") + if verkey: + try: + pres_request.verify_signed_field("verifier_did", wallet, verkey) + request_verified = True + break + except Exception: + continue + if not request_verified: + raise HandlerException( + "Presentation request signature verification failed" + ) + # Get pres ex record (holder initiated via proposal) # or create it (verifier sent request first) try: diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index ad126dd0f9..781c23b2ff 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -954,16 +954,9 @@ async def present_proof_create_request(request: web.BaseRequest): ) if verifier_verkey is not None: - ser_pres_request_message = pres_request_message.serialize() - ser_pres_request_message_bytes = json.dumps(ser_pres_request_message).encode( - "utf-8" - ) async with profile.session() as session: wallet = session.inject(BaseWallet) - await wallet.sign_message(ser_pres_request_message_bytes, verifier_verkey) - pres_request_message.set_signature( - "verifier_did", ser_pres_request_message_bytes - ) + pres_request_message.sign_field("verifier_did", verifier_verkey, wallet) pres_manager = V20PresManager(profile) pres_ex_record = None @@ -1063,16 +1056,9 @@ async def present_proof_send_free_request(request: web.BaseRequest): ) if verifier_verkey is not None: - ser_pres_request_message = pres_request_message.serialize() - ser_pres_request_message_bytes = json.dumps(ser_pres_request_message).encode( - "utf-8" - ) async with profile.session() as session: wallet = session.inject(BaseWallet) - await wallet.sign_message(ser_pres_request_message_bytes, verifier_verkey) - pres_request_message.set_signature( - "verifier_did", ser_pres_request_message_bytes - ) + pres_request_message.sign_field("verifier_did", verifier_verkey, wallet) pres_manager = V20PresManager(profile) pres_ex_record = None From 9872811ded1b5ceefc861cbc7abaef32b8d656be Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Thu, 1 Aug 2024 23:12:59 +0530 Subject: [PATCH 6/9] fix: fixed bugs but proof still not working --- .../v2_0/handlers/pres_request_handler.py | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py index 980ae35462..884ab00274 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py @@ -68,26 +68,36 @@ async def handle(self, context: RequestContext, responder: BaseResponder): pres_request = context.message if pres_request.verifier_did is not None: + print("Verifer DID found...verifying signature") verifier_did = pres_request.verifier_did - did_resolver = profile.inject(DIDResolver) - wallet = profile.inject(BaseWallet) - did_document = await did_resolver.resolve(profile=profile, did=verifier_did) - verification_method_list = did_document.get("verificationMethod", []) - request_verified = False - for method in verification_method_list: - verkey = method.get("publicKeyBase58") - if verkey: - try: - pres_request.verify_signed_field("verifier_did", wallet, verkey) - request_verified = True - break - except Exception: - continue - if not request_verified: - raise HandlerException( - "Presentation request signature verification failed" + async with profile.session() as session: + did_resolver = session.inject(DIDResolver) + wallet = session.inject(BaseWallet) + did_document = await did_resolver.resolve( + profile=profile, did=verifier_did ) - + verification_method_list = did_document.get("verificationMethod", []) + request_verified = False + for method in verification_method_list: + verkey = method.get("publicKeyBase58") + if verkey: + try: + res = await pres_request.verify_signed_field( + "verifier_did", wallet, verkey + ) + print(f"\n\nVerification Result: {res}\n\n") + if res == verkey: + request_verified = True + break + else: + print("Verkey does not match. Retrying...") + except Exception: + continue + if not request_verified: + raise HandlerException( + "Presentation request signature verification failed" + ) + # Get pres ex record (holder initiated via proposal) # or create it (verifier sent request first) try: From 8954e910074086d0fd52f0ee388b49f5ce2ce0f6 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Mon, 5 Aug 2024 19:00:41 +0530 Subject: [PATCH 7/9] fix: able to send verkey to holder --- .../v2_0/handlers/pres_request_handler.py | 22 +++--- .../v2_0/messages/pres_request.py | 12 +++ .../protocols/present_proof/v2_0/routes.py | 75 ++++++++----------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py index 884ab00274..50832fc600 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py @@ -80,19 +80,17 @@ async def handle(self, context: RequestContext, responder: BaseResponder): request_verified = False for method in verification_method_list: verkey = method.get("publicKeyBase58") + print(f"Verkey: {verkey}") if verkey: - try: - res = await pres_request.verify_signed_field( - "verifier_did", wallet, verkey - ) - print(f"\n\nVerification Result: {res}\n\n") - if res == verkey: - request_verified = True - break - else: - print("Verkey does not match. Retrying...") - except Exception: - continue + res = await pres_request.verify_signed_field( + "verifier_did", wallet, verkey + ) + print(f"\n\nVerification Result: {res}\n\n") + if res == verkey: + request_verified = True + break + else: + print("Verkey does not match. Retrying...") if not request_verified: raise HandlerException( "Presentation request signature verification failed" diff --git a/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py b/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py index 2b56bbf87a..cf86d0c621 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/messages/pres_request.py @@ -1,5 +1,6 @@ """A presentation request content message.""" +import base64 from typing import Sequence from marshmallow import EXCLUDE, ValidationError, fields, validates_schema @@ -35,6 +36,7 @@ def __init__( verifier_did: str = None, will_confirm: bool = None, formats: Sequence[V20PresFormat] = None, + signature: bytes = None, request_presentations_attach: Sequence[AttachDecorator] = None, **kwargs, ): @@ -48,6 +50,7 @@ def __init__( request will be confirmed. formats (Sequence[V20PresFormat], optional): A sequence of presentation formats. + signature (dict, optional): signature object for verifier did. request_presentations_attach (Sequence[AttachDecorator], optional): A sequence of proof request attachments. kwargs: Additional keyword arguments. @@ -58,6 +61,7 @@ def __init__( self.verifier_did = verifier_did self.will_confirm = will_confirm or False self.formats = list(formats) if formats else [] + self.signature = signature self.request_presentations_attach = ( list(request_presentations_attach) if request_presentations_attach else [] ) @@ -89,6 +93,10 @@ def attachment(self, fmt: V20PresFormat.Format = None) -> dict: else None ) + def add_signature(self, sign: bytes): + """Add signature to request.""" + self.signature = base64.b64encode(sign).decode("utf-8") + class V20PresRequestSchema(AgentMessageSchema): """Presentation request schema.""" @@ -116,6 +124,10 @@ class Meta: required=True, metadata={"description": "Acceptable attachment formats"}, ) + signature = fields.Str( + required=False, + metadata={"description": "signature for verifier did"}, + ) request_presentations_attach = fields.Nested( AttachDecoratorSchema, many=True, diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index 781c23b2ff..432265ba58 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -160,9 +160,7 @@ class V20PresProposalRequestSchema(AdminAPIMessageTracingSchema): allow_none=True, metadata={"description": "Human-readable comment"}, ) - presentation_proposal = fields.Nested( - V20PresProposalByFormatSchema(), required=True - ) + presentation_proposal = fields.Nested(V20PresProposalByFormatSchema(), required=True) auto_present = fields.Boolean( required=False, dump_default=False, @@ -410,9 +408,7 @@ def _formats_attach(by_format: Mapping, msg_type: str, spec: str) -> Mapping: attach = [] for fmt_api, item_by_fmt in by_format.items(): if fmt_api == V20PresFormat.Format.INDY.api: - attach.append( - AttachDecorator.data_base64(mapping=item_by_fmt, ident=fmt_api) - ) + attach.append(AttachDecorator.data_base64(mapping=item_by_fmt, ident=fmt_api)) elif fmt_api == V20PresFormat.Format.DIF.api: attach.append(AttachDecorator.data_json(mapping=item_by_fmt, ident=fmt_api)) return { @@ -605,9 +601,7 @@ async def present_proof_credentials_list(request: web.BaseRequest): input_descriptors_list = dif_pres_request.get( "presentation_definition", {} ).get("input_descriptors") - claim_fmt = dif_pres_request.get("presentation_definition", {}).get( - "format" - ) + claim_fmt = dif_pres_request.get("presentation_definition", {}).get("format") if claim_fmt and len(claim_fmt.keys()) > 0: claim_fmt = ClaimFormat.deserialize(claim_fmt) input_descriptors = [] @@ -659,16 +653,13 @@ async def present_proof_credentials_list(request: web.BaseRequest): elif ( len(proof_types) == 1 and ( - BbsBlsSignature2020.signature_type - not in proof_types + BbsBlsSignature2020.signature_type not in proof_types ) and ( - Ed25519Signature2018.signature_type - not in proof_types + Ed25519Signature2018.signature_type not in proof_types ) and ( - Ed25519Signature2020.signature_type - not in proof_types + Ed25519Signature2020.signature_type not in proof_types ) ): raise web.HTTPBadRequest( @@ -682,16 +673,13 @@ async def present_proof_credentials_list(request: web.BaseRequest): elif ( len(proof_types) >= 2 and ( - BbsBlsSignature2020.signature_type - not in proof_types + BbsBlsSignature2020.signature_type not in proof_types ) and ( - Ed25519Signature2018.signature_type - not in proof_types + Ed25519Signature2018.signature_type not in proof_types ) and ( - Ed25519Signature2020.signature_type - not in proof_types + Ed25519Signature2020.signature_type not in proof_types ) ): raise web.HTTPBadRequest( @@ -707,25 +695,18 @@ async def present_proof_credentials_list(request: web.BaseRequest): proof_format == Ed25519Signature2018.signature_type ): - proof_type = [ - Ed25519Signature2018.signature_type - ] + proof_type = [Ed25519Signature2018.signature_type] break elif ( proof_format == Ed25519Signature2020.signature_type ): - proof_type = [ - Ed25519Signature2020.signature_type - ] + proof_type = [Ed25519Signature2020.signature_type] break elif ( - proof_format - == BbsBlsSignature2020.signature_type + proof_format == BbsBlsSignature2020.signature_type ): - proof_type = [ - BbsBlsSignature2020.signature_type - ] + proof_type = [BbsBlsSignature2020.signature_type] break else: raise web.HTTPBadRequest( @@ -929,9 +910,7 @@ async def present_proof_create_request(request: web.BaseRequest): didinfo = await wallet.get_local_did(did=verifier_did) verifier_verkey = didinfo.verkey except WalletNotFoundError as err: - raise web.HTTPBadRequest( - reason="DID is not present in wallet!" - ) from err + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -956,7 +935,14 @@ async def present_proof_create_request(request: web.BaseRequest): if verifier_verkey is not None: async with profile.session() as session: wallet = session.inject(BaseWallet) - pres_request_message.sign_field("verifier_did", verifier_verkey, wallet) + sr_pres_request_message = pres_request_message.serialize() + sr_pres_request_message_bytes: bytes = json.dumps( + sr_pres_request_message + ).encode("utf-8") + sign = await wallet.sign_message( + sr_pres_request_message_bytes, verifier_verkey + ) + pres_request_message.add_signature(sign) pres_manager = V20PresManager(profile) pres_ex_record = None @@ -1032,9 +1018,7 @@ async def present_proof_send_free_request(request: web.BaseRequest): didinfo = await wallet.get_local_did(did=verifier_did) verifier_verkey = didinfo.verkey except WalletNotFoundError as err: - raise web.HTTPBadRequest( - reason="DID is not present in wallet!" - ) from err + raise web.HTTPBadRequest(reason="DID is not present in wallet!") from err pres_request_spec = body.get("presentation_request") if pres_request_spec and V20PresFormat.Format.INDY.api in pres_request_spec: @@ -1058,7 +1042,14 @@ async def present_proof_send_free_request(request: web.BaseRequest): if verifier_verkey is not None: async with profile.session() as session: wallet = session.inject(BaseWallet) - pres_request_message.sign_field("verifier_did", verifier_verkey, wallet) + sr_pres_request_message = pres_request_message.serialize() + sr_pres_request_message_bytes: bytes = json.dumps( + sr_pres_request_message + ).encode("utf-8") + sign = await wallet.sign_message( + sr_pres_request_message_bytes, verifier_verkey + ) + pres_request_message.add_signature(sign) pres_manager = V20PresManager(profile) pres_ex_record = None @@ -1424,9 +1415,7 @@ async def present_proof_remove(request: web.BaseRequest): try: async with context.profile.session() as session: try: - pres_ex_record = await V20PresExRecord.retrieve_by_id( - session, pres_ex_id - ) + pres_ex_record = await V20PresExRecord.retrieve_by_id(session, pres_ex_id) await pres_ex_record.delete_record(session) except (BaseModelError, ValidationError): storage = session.inject(BaseStorage) From 6179d1dd1d3b09148f81d8d22ce5c448b607bb99 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Mon, 5 Aug 2024 22:00:26 +0530 Subject: [PATCH 8/9] feat: did proof added --- .../v2_0/handlers/pres_request_handler.py | 31 ++++++++++++------- .../protocols/present_proof/v2_0/routes.py | 1 + 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py index 50832fc600..b69ee26013 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py @@ -1,7 +1,10 @@ """Presentation request message handler.""" +import base64 +import json from aries_cloudagent.resolver.did_resolver import DIDResolver from aries_cloudagent.wallet.base import BaseWallet +from aries_cloudagent.wallet.key_type import ED25519 from .....anoncreds.holder import AnonCredsHolderError from .....core.oob_processor import OobMessageProcessor from .....indy.holder import IndyHolderError @@ -68,7 +71,6 @@ async def handle(self, context: RequestContext, responder: BaseResponder): pres_request = context.message if pres_request.verifier_did is not None: - print("Verifer DID found...verifying signature") verifier_did = pres_request.verifier_did async with profile.session() as session: did_resolver = session.inject(DIDResolver) @@ -80,17 +82,24 @@ async def handle(self, context: RequestContext, responder: BaseResponder): request_verified = False for method in verification_method_list: verkey = method.get("publicKeyBase58") - print(f"Verkey: {verkey}") + key_type = ED25519 # need to change this to support other key types + sr_pres_request = pres_request.serialize() + sr_pres_request.pop("~thread", None) + sr_pres_request.pop("signature", None) + sr_pres_request_bytes = json.dumps(sr_pres_request).encode("utf-8") if verkey: - res = await pres_request.verify_signed_field( - "verifier_did", wallet, verkey - ) - print(f"\n\nVerification Result: {res}\n\n") - if res == verkey: - request_verified = True - break - else: - print("Verkey does not match. Retrying...") + try: + request_verified = await wallet.verify_message( + sr_pres_request_bytes, + base64.b64decode(pres_request.signature), + verkey, + key_type, + ) + if request_verified: + break + except Exception as e: + print(f"Error verifying signature: {e}") + continue if not request_verified: raise HandlerException( "Presentation request signature verification failed" diff --git a/aries_cloudagent/protocols/present_proof/v2_0/routes.py b/aries_cloudagent/protocols/present_proof/v2_0/routes.py index 432265ba58..aa8259812a 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/routes.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/routes.py @@ -1046,6 +1046,7 @@ async def present_proof_send_free_request(request: web.BaseRequest): sr_pres_request_message_bytes: bytes = json.dumps( sr_pres_request_message ).encode("utf-8") + print(f"==========================>{sr_pres_request_message}") sign = await wallet.sign_message( sr_pres_request_message_bytes, verifier_verkey ) From 17ff0061bbf0619242d00b176853d9643946c9aa Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Wed, 7 Aug 2024 19:30:04 +0530 Subject: [PATCH 9/9] fix: minor change --- .../present_proof/v2_0/handlers/pres_request_handler.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py index b69ee26013..4abfedf09d 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/handlers/pres_request_handler.py @@ -82,7 +82,7 @@ async def handle(self, context: RequestContext, responder: BaseResponder): request_verified = False for method in verification_method_list: verkey = method.get("publicKeyBase58") - key_type = ED25519 # need to change this to support other key types + key_type = ED25519 # need to change this to support other key types sr_pres_request = pres_request.serialize() sr_pres_request.pop("~thread", None) sr_pres_request.pop("signature", None) @@ -98,11 +98,13 @@ async def handle(self, context: RequestContext, responder: BaseResponder): if request_verified: break except Exception as e: - print(f"Error verifying signature: {e}") + print( + f"Could not verify signature...Retrying with next verification method: {e}" # noqa: E501 + ) continue if not request_verified: raise HandlerException( - "Presentation request signature verification failed" + "Presentation request signature verification failed. DID of verifier is not verifed" # noqa: E501 ) # Get pres ex record (holder initiated via proposal)