async def __StoreClaim(self, claim): self.__logger.debug(claim.json) legal_entity_id = None try: legal_entity_id = claim.getField('legal_entity_id') self.__logger.debug('Claim for legal_entity_id: %s' % legal_entity_id) except Error as e: # no-op self.__logger.debug('Claim for NO legal_entity_id') async with Holder(legal_entity_id) as holder: self.__logger.debug("Storing the claim in the wallet ...") self.__logger.debug( "\n============================================================================\n" + "Credential definition:\n" + "----------------------------------------------------------------------------\n" + "{0}\n".format(json.dumps(claim.credDef, indent=2)) + "============================================================================\n" ) self.__logger.debug( "\n============================================================================\n" + "Credential definition metadata:\n" + "----------------------------------------------------------------------------\n" + "{0}\n".format(json.dumps(claim.credDefMeta, indent=2)) + "============================================================================\n" ) await holder.store_cred(claim.json, json.dumps(claim.credDefMeta))
async def __StoreClaimRequest(self): self.__logger.debug(">>> Storing claim request ...") self.__logger.debug( "\n============================================================================\n" + "Creating credential request:\n" + "----------------------------------------------------------------------------\n" + "{0}\n".format( json.dumps(json.loads(self.__claimDefParser.claimOffer), indent=2)) + "----------------------------------------------------------------------------\n" + "{0}\n".format( json.dumps(json.loads(self.__claimDefParser.claimDefinition), indent=2)) + "============================================================================\n" ) async with Holder() as holder: (credential_request, credential_request_metadata_json) = await holder.create_cred_req( self.__claimDefParser.claimOffer, self.__claimDefParser.claimDefinition) self.__logger.debug("<<< Storing claim request.") return (credential_request, json.loads(credential_request_metadata_json))
async def __StoreClaimRequest(self): self.__logger.debug(">>> Storing claim request ...") async with Holder() as holder: claim_request = await holder.store_claim_req( self.__claimDefParser.claimOffer, self.__claimDefParser.claimDefinition) self.__logger.debug("<<< Storing claim request.") return claim_request
async def __StoreClaim(self, claim): self.__logger.debug(claim) legal_entity_id = None try: legal_entity_id = json.loads(claim)["values"]["legal_entity_id"][0] self.__logger.debug('Claim for legal_entity_id: %s' % legal_entity_id) except Error as e: # no-op self.__logger.debug('Claim for NO legal_entity_id') async with Holder(legal_entity_id) as holder: self.__logger.debug("Storing the claim in the wallet ...") await holder.store_claim(claim)
async def __ConstructProof(self): self.__logger.debug("Constructing Proof ...") # # We keep a reference to schemas that we discover and retrieve from the # # ledger. We will need these again later. # schema_cache = {'by_key': {}} # # The client is sending the proof request in an upcoming format. # # This shim allows Permitify to declare its proof requests format # # in the latest format. Once von-agent is update to support the new # # format, this shim can be removed. # for attr in self.__proof_request['requested_attrs']: # # new format expects restrictions with "schema_key" # # Current format simply wants the seq_no of schema # schema_key = self.__proof_request['requested_attrs'][ # attr]['restrictions'][0]['schema_key'] # # This is offensive... # # Get the schema from the ledger directly by name/version # # After upgrading von-agent we can loosen restrictions using # # schema_key # # try: # # for line in self.ledger.splitlines(): # # entry = json.loads(line)[1] # # if entry['type'] == "101": # # if entry['data']['name'] == schema_key['name'] and \ # # entry['data']['version'] == schema_key['version']: # # schema_key['did'] = entry['identifier'] # # break # # except: # # raise Exception('Could not correlate schema name and version to did.') # # Ugly cache for now... # if '%s::%s::%s' % ( # schema_key['did'], # schema_key['name'], # schema_key['version']) in schema_cache['by_key']: # schema = schema_cache['by_key']['%s::%s::%s' % ( # schema_key['did'], # schema_key['name'], # schema_key['version'])] # else: # # Not optimal. von-agent should cache this. # async with Holder() as holder: # schema_json = await holder.get_schema( # schema_key['did'], # schema_key['name'], # schema_key['version'] # ) # schema = json.loads(schema_json) # self.__logger.debug("Using Schema key {}".format(schema_key)) # if not schema: # raise NotAcceptable( # 'No schema found for did:{} name:{} version:{}'.format( # schema_key['did'], # schema_key['name'], # schema_key['version'] # ) # ) # schema_cache[schema['seqNo']] = schema # schema_cache['by_key']['%s::%s::%s' % ( # schema_key['did'], # schema_key['name'], # schema_key['version'])] = schema # self.__proof_request['requested_attrs'][ # attr]['schema_seq_no'] = schema['seqNo'] # del self.__proof_request['requested_attrs'][attr]['restrictions'] # self.__logger.debug('Schema cache: %s' % json.dumps(schema_cache)) self.__logger.debug('Proof request: %s' % json.dumps(self.__proof_request)) # Get claims for proof request from wallet async with Holder() as holder: claims = await holder.get_claims(json.dumps(self.__proof_request)) claims = json.loads(claims[1]) self.__logger.debug( 'Wallet returned the following claims for proof request: %s' % json.dumps(claims)) # If any of the claims for proof are empty, we cannot construct a proof for attr in claims['attrs']: if 'legal_entity_id' in self.__filters: my_claims = [] for claim in claims['attrs'][attr]: if 'legal_entity_id' in claim['attrs'] and claim['attrs'][ 'legal_entity_id'] == self.__filters[ 'legal_entity_id']: my_claims.append(claim) break claims['attrs'][attr] = my_claims if not claims['attrs'][attr]: raise NotAcceptable('No claims found for attr %s' % attr) def get_claim_by_filter(clms, key, value): for clm in clms: if clm["attrs"][key] == value: return clm raise NotAcceptable('No claims found for filter %s = %s' % (key, value)) self.__logger.debug( 'Filtering for claims returned the following claims for proof request: %s' % json.dumps(claims)) # TODO: rework to support other filters other than legal_entity_id requested_claims = { 'self_attested_attributes': {}, 'requested_attrs': { attr: [ # Either we get the first claim found # by the provided filter get_claim_by_filter( claims["attrs"][attr], 'legal_entity_id', self.__filters['legal_entity_id'])["referent"] # Or we use the first claim found if 'legal_entity_id' in self.__filters else claims["attrs"][attr][0]["referent"], True ] for attr in claims["attrs"] }, 'requested_predicates': {} } self.__logger.debug('Built requested claims: %s' % json.dumps(requested_claims)) # Build schemas json def wallet_claim_by_referent(clms, referent): for clm in clms: if clm['referent'] == referent: return clm # schemas = { # requested_claims['requested_attrs'][attr][0]: # schema_cache[ # wallet_claim_by_referent( # claims["attrs"][attr], # requested_claims['requested_attrs'][attr][0] # )['schema_seq_no'] # ] # for attr in requested_claims['requested_attrs'] # } # self.__logger.debug( # 'Built schemas: %s' % # json.dumps(schemas)) # claim_defs_cache = {} # claim_defs = {} # for attr in requested_claims['requested_attrs']: # # claim uuid # referent = requested_claims['requested_attrs'][attr][0] # if referent not in claim_defs_cache: # async with Holder() as holder: # claim_defs_cache[referent] = \ # json.loads(await holder.get_claim_def( # wallet_claim_by_referent( # claims["attrs"][attr], # referent # )["schema_seq_no"], # wallet_claim_by_referent( # claims["attrs"][attr], # referent # )["issuer_did"] # )) # claim_defs[referent] = claim_defs_cache[referent] # self.__logger.debug( # 'Claim def cache: %s' % # json.dumps(claim_defs_cache)) # self.__logger.debug( # 'Built claim_defs: %s' % # json.dumps(claim_defs)) self.__logger.debug("Creating proof ...") async with Holder() as holder: proof = await holder.create_proof(self.__proof_request, claims, requested_claims) self.__logger.debug('Created proof: %s' % json.dumps(proof)) return {'proof': json.loads(proof)}
async def __StoreClaim(self, claim): async with Holder() as holder: self.__logger.debug("Storing the claim in the wallet ...") await holder.store_claim(claim)
async def __ConstructProof(self): self.__logger.debug("Constructing Proof ...") # We keep a reference to schemas that we discover and retrieve from the # ledger. We will need these again later. schema_cache = {'by_key': {}} # The client is sending the proof request in an upcoming format. # This shim allows Permitify to declare its proof requests format # in the latest format. Once von-agent is update to support the new # format, this shim can be removed. for attr in self.__proof_request['requested_attrs']: # new format expects restrictions with "schema_key" # Current format simply wants the seq_no of schema schema_key = self.__proof_request['requested_attrs'][attr][ 'restrictions'][0]['schema_key'] # Ugly cache for now... if '%s::%s::%s' % ( schema_key['did'], schema_key['name'], schema_key['version']) in schema_cache['by_key']: schema = schema_cache['by_key']['%s::%s::%s' % (schema_key['did'], schema_key['name'], schema_key['version'])] else: # Not optimal. von-agent should cache this. async with Holder() as holder: schema_json = await holder.get_schema( schema_key['did'], schema_key['name'], schema_key['version']) schema = json.loads(schema_json) schema_cache[schema['seqNo']] = schema schema_cache['by_key']['%s::%s::%s' % (schema_key['did'], schema_key['name'], schema_key['version'])] = schema self.__proof_request['requested_attrs'][attr][ 'schema_seq_no'] = schema['seqNo'] del self.__proof_request['requested_attrs'][attr]['restrictions'] self.__logger.debug('Schema cache: %s' % json.dumps(schema_cache)) self.__logger.debug('Proof request: %s' % json.dumps(self.__proof_request)) # Get claims for proof request from wallet async with Holder() as holder: claims = await holder.get_claims(json.dumps(self.__proof_request)) claims = json.loads(claims[1]) self.__logger.debug( 'Wallet returned the following claims for proof request: %s' % json.dumps(claims)) # If any of the claims for proof are empty, we cannot construct a proof for attr in claims['attrs']: if not claims['attrs'][attr]: raise NotAcceptable('No claims found for attr %s' % attr) def get_claim_by_filter(clms, key, value): for clm in clms: if clm["attrs"][key] == value: return clm raise NotAcceptable('No claims found for filter %s = %s' % (key, value)) # TODO: rework to support other filters other than legal_entity_id requested_claims = { 'self_attested_attributes': {}, 'requested_attrs': { attr: [ # Either we get the first claim found # by the provided filter get_claim_by_filter( claims["attrs"][attr], 'legal_entity_id', self.__filters['legal_entity_id'])["claim_uuid"] # Or we use the first claim found if 'legal_entity_id' in self.__filters else claims["attrs"][attr][0]["claim_uuid"], True ] for attr in claims["attrs"] }, 'requested_predicates': {} } self.__logger.debug('Built requested claims: %s' % json.dumps(requested_claims)) # Build schemas json def wallet_claim_by_claim_uuid(clms, claim_uuid): for clm in clms: if clm['claim_uuid'] == claim_uuid: return clm schemas = { requested_claims['requested_attrs'][attr][0]: schema_cache[wallet_claim_by_claim_uuid( claims["attrs"][attr], requested_claims['requested_attrs'][attr][0])['schema_seq_no']] for attr in requested_claims['requested_attrs'] } self.__logger.debug('Built schemas: %s' % json.dumps(schemas)) claim_defs_cache = {} claim_defs = {} for attr in requested_claims['requested_attrs']: # claim uuid claim_uuid = requested_claims['requested_attrs'][attr][0] if claim_uuid not in claim_defs_cache: async with Holder() as holder: claim_defs_cache[claim_uuid] = \ json.loads(await holder.get_claim_def( wallet_claim_by_claim_uuid( claims["attrs"][attr], claim_uuid )["schema_seq_no"], wallet_claim_by_claim_uuid( claims["attrs"][attr], claim_uuid )["issuer_did"] )) claim_defs[claim_uuid] = claim_defs_cache[claim_uuid] self.__logger.debug('Claim def cache: %s' % json.dumps(claim_defs_cache)) self.__logger.debug('Built claim_defs: %s' % json.dumps(claim_defs)) self.__logger.debug("Creating proof ...") async with Holder() as holder: proof = await holder.create_proof(json.dumps(self.__proof_request), json.dumps(schemas), json.dumps(claim_defs), requested_claims) self.__logger.debug('Created proof: %s' % json.dumps(proof)) return { 'proof': json.loads(proof), 'schemas': schemas, 'claim_defs': claim_defs }