async def load_schema(self, id_: str, submitter_did: str) -> Schema: body = await self._cache.get_schema( pool_name=self.name, submitter_did=submitter_did, id_=id_, options=CacheOptions() ) return Schema(**body)
async def ensure_schema_exists(self, schema: AnonCredSchema, submitter_did: str) -> Optional[Schema]: try: body = await self._cache.get_schema(pool_name=self.name, submitter_did=submitter_did, id_=schema.id, options=CacheOptions()) ledger_schema = Schema(**body) await self.__ensure_exists_in_storage(ledger_schema, submitter_did) return ledger_schema except LedgerNotFound: pass ok, ledger_schema = await self.register_schema(schema, submitter_did) if ok: return ledger_schema else: return None
async def _extract_credentials_info( self, proof_request, pool_name: str) -> (dict, dict, dict, dict): # Extract credentials from wallet that satisfy to request proof_response = await sirius_sdk.AnonCreds.prover_search_credentials_for_proof_req( proof_request, limit_referents=1) schemas = {} credential_defs = {} rev_states = {} opts = CacheOptions() requested_credentials = { 'self_attested_attributes': {}, 'requested_attributes': {}, 'requested_predicates': {} } all_infos = [] for referent_id, cred_infos in proof_response[ 'requested_attributes'].items(): cred_info = cred_infos[0]['cred_info'] # Get first info = {'cred_id': cred_info['referent'], 'revealed': True} requested_credentials['requested_attributes'][referent_id] = info all_infos.append(cred_info) for referent_id, predicates in proof_response[ 'requested_predicates'].items(): pred_info = predicates[0]['cred_info'] # Get first info = {'cred_id': pred_info['referent']} requested_credentials['requested_predicates'][referent_id] = info all_infos.append(pred_info) for cred_info in all_infos: schema_id = cred_info['schema_id'] cred_def_id = cred_info['cred_def_id'] schema = await sirius_sdk.Cache.get_schema( pool_name=pool_name, submitter_did=self.__verifier.me.did, id_=schema_id, options=opts) cred_def = await sirius_sdk.Cache.get_cred_def( pool_name=pool_name, submitter_did=self.__verifier.me.did, id_=cred_def_id, options=opts) schemas[schema_id] = schema credential_defs[cred_def_id] = cred_def return requested_credentials, schemas, credential_defs, rev_states
async def load_cred_def(self, id_: str, submitter_did: str) -> CredentialDefinition: cred_def_body = await self._cache.get_cred_def( pool_name=self.name, submitter_did=submitter_did, id_=id_, options=CacheOptions() ) tag = cred_def_body.get('tag') schema_seq_no = int(cred_def_body['schemaId']) cred_def_seq_no = int(cred_def_body['id'].split(':')[3]) + 1 txn_request = await self._api.build_get_txn_request( submitter_did=submitter_did, ledger_type=None, seq_no=schema_seq_no ) resp = await self._api.sign_and_submit_request( pool_name=self.name, submitter_did=submitter_did, request=txn_request ) if resp['op'] == 'REPLY': txn_data = resp['result']['data'] schema_body = { 'name': txn_data['txn']['data']['data']['name'], 'version': txn_data['txn']['data']['data']['version'], 'attrNames': txn_data['txn']['data']['data']['attr_names'], 'id': txn_data['txnMetadata']['txnId'], 'seqNo': txn_data['txnMetadata']['seqNo'] } schema_body['ver'] = schema_body['id'].split(':')[-1] schema = Schema(**schema_body) cred_def = CredentialDefinition( tag=tag, schema=schema, body=cred_def_body, seq_no=cred_def_seq_no ) return cred_def else: raise SiriusInvalidPayloadStructure()
async def verify(self, proof_request: dict, translation: List[AttribTranslation] = None, comment: str = None, locale: str = BasePresentProofMessage.DEF_LOCALE, proto_version: str = None) -> bool: """ :param proof_request: Hyperledger Indy compatible proof-request :param translation: human readable attributes translations :param comment: human readable comment from Verifier to Prover :param locale: locale, for example "en" or "ru" :param proto_version: 0037 protocol version, for example 1.0 or 1.1 """ async with self.coprotocol(pairwise=self.__prover): try: # Step-1: Send proof request expires_time = datetime.utcnow() + timedelta( seconds=self.time_to_live) request_msg = RequestPresentationMessage( proof_request=proof_request, translation=translation, comment=comment, locale=locale, expires_time=utc_to_str(expires_time), version=proto_version) request_msg.please_ack = True await self.log(progress=30, message='Send request', payload=dict(request_msg)) # Switch to await participant action presentation = await self.switch(request_msg, [PresentationMessage]) if not isinstance(presentation, PresentationMessage): raise StateMachineTerminatedWithError( RESPONSE_NOT_ACCEPTED, 'Unexpected @type: %s' % str(presentation.type)) await self.log(progress=60, message='Presentation received') # Step-2 Verify identifiers = presentation.proof.get('identifiers', []) schemas = {} credential_defs = {} rev_reg_defs = {} rev_regs = {} opts = CacheOptions() for identifier in identifiers: schema_id = identifier['schema_id'] cred_def_id = identifier['cred_def_id'] rev_reg_id = identifier['rev_reg_id'] if schema_id and schema_id not in schemas: schemas[schema_id] = await sirius_sdk.Cache.get_schema( self.__pool_name, self.__prover.me.did, schema_id, opts) if cred_def_id and cred_def_id not in credential_defs: credential_defs[ cred_def_id] = await sirius_sdk.Cache.get_cred_def( self.__pool_name, self.__prover.me.did, cred_def_id, opts) success = await sirius_sdk.AnonCreds.verifier_verify_proof( proof_request=proof_request, proof=presentation.proof, schemas=schemas, credential_defs=credential_defs, rev_reg_defs=rev_reg_defs, rev_regs=rev_regs) if success: self.__requested_proof = presentation.proof[ 'requested_proof'] ack = Ack(thread_id=presentation.ack_message_id if presentation.please_ack else presentation.id, status=Status.OK) await self.log(progress=100, message='Verifying terminated successfully') await self.send(ack) return True else: await self.log(progress=100, message='Verifying terminated with ERROR') raise StateMachineTerminatedWithError( VERIFY_ERROR, 'Verifying return false') except StateMachineTerminatedWithError as e: self._problem_report = PresentProofProblemReport( e.problem_code, e.explain) await self.log(progress=100, message=f'Terminated with error', problem_code=e.problem_code, explain=e.explain) if e.notify: await self.send(self._problem_report) return False