|
| 1 | +from aries_cloudagent.messaging.base_handler import BaseHandler, BaseResponder, RequestContext |
| 2 | +from aries_cloudagent.storage.record import StorageRecord |
| 3 | +from aries_cloudagent.wallet.base import BaseWallet |
| 4 | +from aries_cloudagent.wallet.indy import IndyWallet |
| 5 | + |
| 6 | +from ..messages.data_agreement_verify import DataAgreementVerify |
| 7 | +from ..messages.data_agreement_verify_response import DataAgreementVerifyResponse |
| 8 | +from ..models.exchange_records.data_agreement_record import DataAgreementV1Record |
| 9 | +from ..manager import ADAManager, ADAManagerError |
| 10 | +from ..utils.did.mydata_did import DIDMyData |
| 11 | +from ..utils.jsonld.data_agreement import verify_data_agreement, verify_data_agreement_with_proof_chain |
| 12 | +from ..models.exchange_records.auditor_didcomm_transaction_record import AuditorDIDCommTransactionRecord |
| 13 | + |
| 14 | +import json |
| 15 | + |
| 16 | + |
| 17 | +class DataAgreementVerifyHandler(BaseHandler): |
| 18 | + """ |
| 19 | + Handler for data agreement verify request. |
| 20 | + """ |
| 21 | + |
| 22 | + async def handle(self, context: RequestContext, responder: BaseResponder): |
| 23 | + """ |
| 24 | + Message handler logic for data agreement verify. |
| 25 | + """ |
| 26 | + |
| 27 | + # Assert if received message is of type DataAgreementVerify |
| 28 | + assert isinstance(context.message, DataAgreementVerify) |
| 29 | + |
| 30 | + self._logger.info( |
| 31 | + "Received data-agreement-proofs/1.0/verify-request message: \n%s", |
| 32 | + json.dumps(context.message.serialize(), indent=4) |
| 33 | + ) |
| 34 | + |
| 35 | + # Check if the connection is ready. |
| 36 | + if not context.connection_ready: |
| 37 | + self._logger.info( |
| 38 | + "Connection not active, skipping data-agreement-proofs/1.0/verify-request handler: %s", |
| 39 | + context.message_receipt.sender_did, |
| 40 | + ) |
| 41 | + return |
| 42 | + |
| 43 | + data_agreement_verify_request = context.message |
| 44 | + data_agreement = data_agreement_verify_request.body.data_agreement |
| 45 | + |
| 46 | + # Wallet instance from request context |
| 47 | + wallet: IndyWallet = await context.inject(BaseWallet) |
| 48 | + |
| 49 | + # Initialize ADA manager |
| 50 | + ada_manager = ADAManager(context) |
| 51 | + |
| 52 | + # Create auditor didcomm transaction record |
| 53 | + auditor_didcomm_transaction_record = AuditorDIDCommTransactionRecord( |
| 54 | + thread_id=data_agreement_verify_request._id, |
| 55 | + messages_list=[data_agreement_verify_request.serialize()], |
| 56 | + connection_id=context.connection_record.connection_id, |
| 57 | + ) |
| 58 | + |
| 59 | + await auditor_didcomm_transaction_record.save(context) |
| 60 | + |
| 61 | + try: |
| 62 | + |
| 63 | + data_controller_did = DIDMyData.from_did( |
| 64 | + data_agreement.event[0].did) |
| 65 | + principle_did = DIDMyData.from_did(data_agreement.principle_did) |
| 66 | + |
| 67 | + # Verify data controller did |
| 68 | + # Fetch controller did from did registry |
| 69 | + await ada_manager.resolve_remote_mydata_did(mydata_did=data_controller_did.did) |
| 70 | + |
| 71 | + valid = False |
| 72 | + |
| 73 | + if len(data_agreement.event) == 3: |
| 74 | + verkeys = [] |
| 75 | + |
| 76 | + for event in data_agreement.event: |
| 77 | + temp_verkey = DIDMyData.from_did(event.did).public_key_b58 |
| 78 | + verkeys.append(temp_verkey) |
| 79 | + |
| 80 | + valid = await verify_data_agreement( |
| 81 | + data_agreement.serialize(), |
| 82 | + verkeys[-1], |
| 83 | + wallet, |
| 84 | + drop_proof_chain=False |
| 85 | + ) |
| 86 | + |
| 87 | + # drop last event and proof |
| 88 | + data_agreement.event.pop() |
| 89 | + data_agreement.proof_chain.pop() |
| 90 | + |
| 91 | + if len(data_agreement.event) == 2: |
| 92 | + # Verify signatures on data agreement |
| 93 | + valid = await verify_data_agreement_with_proof_chain( |
| 94 | + data_agreement.serialize(), |
| 95 | + [ |
| 96 | + data_controller_did.public_key_b58, |
| 97 | + principle_did.public_key_b58 |
| 98 | + ], |
| 99 | + wallet |
| 100 | + ) |
| 101 | + |
| 102 | + # drop last event and proof |
| 103 | + data_agreement.event.pop() |
| 104 | + data_agreement.proof_chain.pop() |
| 105 | + |
| 106 | + # Convert proof chain to single proof |
| 107 | + data_agreement.proof = data_agreement.proof_chain[0] |
| 108 | + data_agreement.proof_chain = None |
| 109 | + |
| 110 | + if len(data_agreement.event) == 1: |
| 111 | + # Verify signatures on data agreement |
| 112 | + valid = await verify_data_agreement( |
| 113 | + data_agreement.serialize(), |
| 114 | + data_controller_did.public_key_b58, |
| 115 | + wallet |
| 116 | + ) |
| 117 | + |
| 118 | + if valid: |
| 119 | + |
| 120 | + # Verification successful |
| 121 | + |
| 122 | + self._logger.info( |
| 123 | + "Data-agreement-proofs/1.0/verify-request message verification successful: \n%s", |
| 124 | + json.dumps(data_agreement.serialize(), indent=4) |
| 125 | + ) |
| 126 | + |
| 127 | + data_agreement_verify_response = DataAgreementVerifyResponse( |
| 128 | + status="OK", |
| 129 | + explain=f"Signature verification successful." |
| 130 | + ) |
| 131 | + |
| 132 | + data_agreement_verify_response.assign_thread_id( |
| 133 | + thid=data_agreement_verify_request._id |
| 134 | + ) |
| 135 | + |
| 136 | + auditor_didcomm_transaction_record.messages_list.append( |
| 137 | + data_agreement_verify_response.serialize() |
| 138 | + ) |
| 139 | + |
| 140 | + await auditor_didcomm_transaction_record.save(context) |
| 141 | + |
| 142 | + await responder.send_reply(data_agreement_verify_response) |
| 143 | + |
| 144 | + else: |
| 145 | + |
| 146 | + # Verification failed |
| 147 | + |
| 148 | + data_agreement_verify_response = DataAgreementVerifyResponse( |
| 149 | + status="NOT OK", |
| 150 | + explain=f"Signature verification failed." |
| 151 | + ) |
| 152 | + |
| 153 | + data_agreement_verify_response.assign_thread_id( |
| 154 | + thid=data_agreement_verify_request._id |
| 155 | + ) |
| 156 | + |
| 157 | + auditor_didcomm_transaction_record.messages_list.append( |
| 158 | + data_agreement_verify_response.serialize() |
| 159 | + ) |
| 160 | + |
| 161 | + await auditor_didcomm_transaction_record.save(context) |
| 162 | + |
| 163 | + await responder.send_reply(data_agreement_verify_response) |
| 164 | + |
| 165 | + except (ADAManagerError, Exception) as err: |
| 166 | + self._logger.exception("Failed to verify data agreement") |
| 167 | + |
| 168 | + data_agreement_verify_response = DataAgreementVerifyResponse( |
| 169 | + status="NOT OK", |
| 170 | + explain=f"Failed to verify data agreement: {err}" |
| 171 | + ) |
| 172 | + |
| 173 | + data_agreement_verify_response.assign_thread_id( |
| 174 | + thid=data_agreement_verify_request._id |
| 175 | + ) |
| 176 | + |
| 177 | + auditor_didcomm_transaction_record.messages_list.append( |
| 178 | + data_agreement_verify_response.serialize() |
| 179 | + ) |
| 180 | + |
| 181 | + await auditor_didcomm_transaction_record.save(context) |
| 182 | + |
| 183 | + await responder.send_reply(data_agreement_verify_response) |
| 184 | + |
| 185 | + return |
0 commit comments