async def testClaimProofFromToDictPrimaryOnly(prover1, nonce, claimsProver1Gvt): proofInput = ProofInput(['name'], [PredicateGE('age', 18)]) proof, _ = await prover1.presentProof(proofInput, nonce) proofs = [Proof(primaryProof=proof.proofs[0].primaryProof)] proof = proof._replace(proofs=proofs) assert proof == FullProof.fromStrDict(proof.toStrDict())
async def verifyClaimProof(self, msg: Any): body, (frm, ha) = msg link = self.verifyAndGetLink(msg) if not link: raise NotImplementedError claimName = body[NAME] nonce = getNonceForProof(body[NONCE]) proof = FullProof.fromStrDict(body[PROOF_FIELD]) proofInput = ProofInput.fromStrDict(body[PROOF_INPUT_FIELD]) revealedAttrs = fromDictWithStrValues(body[REVEALED_ATTRS_FIELD]) result = await self.verifier.verify(proofInput, proof, revealedAttrs, nonce) status = 'verified' if result else 'failed verification' resp = { TYPE: CLAIM_PROOF_STATUS, DATA: ' Your claim {} {} was received and {}\n'.format( body[NAME], body[VERSION], status), } self.signAndSend(resp, link.localIdentifier, frm, origReqId=body.get(f.REQ_ID.nm)) if result: await self._postClaimVerif(claimName, link, frm)
async def doTestAnonCredsPrimaryOnly(): # 1. Create a Schema schema = await issuer.genSchema('GVT', '1.0', GVT.attribNames()) schemaId = ID(schemaKey=schema.getKey(), schemaId=schema.seqId) # 2. Create keys for the Schema await issuer.genKeys(schemaId, **primes1) # 3. Issue accumulator #TODO: Not implemented yet #await issuer.issueAccumulator(schemaId=schemaId, iA='110', L=5) # 4. set attributes for user1 attrs = GVT.attribs(name='Alex', age=28, height=175, sex='male') proverId = str(prover.proverId) attrRepo.addAttributes(schema.getKey(), proverId, attrs) # 5. request Claims claimsReq = await prover.createClaimRequest(schemaId, proverId, False) claims = await issuer.issueClaim(schemaId, claimsReq) await prover.processClaim(schemaId, claims) # 6. proof Claims proofInput = ProofInput( ['name'], [PredicateGE('age', 18)]) nonce = verifier.generateNonce() proof, revealedAttrs = await prover.presentProof(proofInput, nonce) assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def testGePredicateNegativeForOne(prover1, prover2, verifier, allClaims): proofInput = ProofInput(['name'], [PredicateGE('age', 18), PredicateGE('period', 9)]) assert await presentProofAndVerify(verifier, proofInput, prover2) with pytest.raises(ValueError): await presentProofAndVerify(verifier, proofInput, prover1)
async def testSingleIssuerSingleProver(primes1): # 1. Init entities publicRepo = PublicRepoInMemory() attrRepo = AttributeRepoInMemory() issuer = Issuer(IssuerWalletInMemory('issuer1', publicRepo), attrRepo) # 2. Create a Schema schema = await issuer.genSchema('GVT', '1.0', GVT.attribNames()) schemaId = ID(schema.getKey()) # 3. Create keys for the Schema await issuer.genKeys(schemaId, **primes1) # 4. Issue accumulator await issuer.issueAccumulator(schemaId=schemaId, iA='110', L=5) # 4. set attributes for user1 userId = '111' attrs = GVT.attribs(name='Alex', age=28, height=175, sex='male') attrRepo.addAttributes(schema.getKey(), userId, attrs) # 5. request Claims prover = Prover(ProverWalletInMemory(userId, publicRepo)) claimsReq = await prover.createClaimRequest(schemaId) claims = await issuer.issueClaim(schemaId, claimsReq) await prover.processClaim(schemaId, claims) # 6. proof Claims proofInput = ProofInput(['name'], [PredicateGE('age', 18)]) verifier = Verifier(WalletInMemory('verifier1', publicRepo)) nonce = verifier.generateNonce() proof, revealedAttrs = await prover.presentProof(proofInput, nonce) assert revealedAttrs['name'] == 'Alex' assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def testPrimaryClaimOnlyEmpty(prover1, verifier, claimsProver1Gvt, nonce): proofInput = ProofInput([]) claims, revealedAttrs = await prover1._findClaims(proofInput) claims = {schemaKey: ProofClaims( Claims(primaryClaim=proofClaim.claims.primaryClaim)) for schemaKey, proofClaim in claims.items()} proof = await prover1._prepareProof(claims, nonce) assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def sendProofAsync(self, link: Link, claimPrfReq: ClaimProofRequest): nonce = getNonceForProof(link.invitationNonce) revealedAttrNames = claimPrfReq.verifiableAttributes proofInput = ProofInput(revealedAttrs=revealedAttrNames) proof, revealedAttrs = await self.prover.presentProof(proofInput, nonce) op = { NAME: claimPrfReq.name, VERSION: claimPrfReq.version, NONCE: link.invitationNonce, TYPE: CLAIM_PROOF, PROOF_FIELD: proof.toStrDict(), PROOF_INPUT_FIELD: proofInput.toStrDict(), REVEALED_ATTRS_FIELD: toDictWithStrValues(revealedAttrs) } self.signAndSend(msg=op, linkName=link.name)
async def testPredicatesEmpty(prover1, allClaims, schemaGvtId, attrRepo): proofInput = ProofInput(['name'], []) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) proofClaims = {schemaGvtId.schemaKey: ProofClaims(claimsGvt, ['name'], [])} revealedAttrs = { 'name': attrRepo.getAttributes(schemaGvtId.schemaKey, prover1.proverId)['name'] } assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def testRevealedEmpty(prover1, allClaims, schemaGvtId): proofInput = ProofInput([], [PredicateGE('age', 18)]) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, [], [PredicateGE('age', 18)]) } revealedAttrs = {} assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def sendProofAsync(self, link: Link, proofRequest: ProofRequest): # TODO _F_ this nonce should be from the Proof Request, not from an # invitation nonce = getNonceForProof(link.invitationNonce) revealedAttrNames = proofRequest.verifiableAttributes proofInput = ProofInput(revealedAttrs=revealedAttrNames) # TODO rename presentProof to buildProof or generateProof proof, revealedAttrs = await self.prover.presentProof(proofInput, nonce) revealedAttrs.update(proofRequest.selfAttestedAttrs) op = OrderedDict([ (TYPE, PROOF), (NAME, proofRequest.name), (VERSION, proofRequest.version), (NONCE, link.invitationNonce), (PROOF_FIELD, proof.toStrDict()), (PROOF_INPUT_FIELD, proofInput.toStrDict()), # TODO _F_ why do we need to send this? isn't the same data passed as keys in 'proof'? (REVEALED_ATTRS_FIELD, toDictWithStrValues(revealedAttrs))]) self.signAndSend(msg=op, linkName=link.name)
async def testPrimaryClaimPredicatesOnly(prover1, verifier, claimsProver1Gvt, nonce, schemaGvtId): predicates = [PredicateGE('age', 18)] proofInput = ProofInput(predicates=predicates) claims, revealedAttrs = await prover1._findClaims(proofInput) claims = {schemaKey: ProofClaims( Claims(primaryClaim=proofClaim.claims.primaryClaim), predicates=predicates) for schemaKey, proofClaim in claims.items()} proof = await prover1._prepareProof(claims, nonce) assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def testUParamShouldBeSame(prover1, verifier, issuerGvt, schemaGvtId, attrsProver1Gvt, keysGvt, issueAccumulatorGvt): claimsReq = await prover1.createClaimRequest(schemaGvtId) claimsReq = claimsReq._replace(U=claimsReq.U ** 2) claims = await issuerGvt.issueClaim(schemaGvtId, claimsReq) await prover1.processClaim(schemaGvtId, claims) proofInput = ProofInput(['name'], []) assert not await presentProofAndVerify(verifier, proofInput, prover1)
async def sendProofAsync(self, link: Link, proofRequest: ProofRequest): # TODO _F_ this nonce should be from the Proof Request, not from an # invitation nonce = getNonceForProof(link.invitationNonce) revealedAttrNames = proofRequest.verifiableAttributes proofInput = ProofInput(revealedAttrs=revealedAttrNames) # TODO rename presentProof to buildProof or generateProof proof, revealedAttrs = await self.prover.presentProof(proofInput, nonce) revealedAttrs.update(proofRequest.selfAttestedAttrs) op = OrderedDict([ (TYPE, PROOF), (NAME, proofRequest.name), (VERSION, proofRequest.version), (NONCE, link.invitationNonce), (PROOF_FIELD, proof.toStrDict()), (PROOF_INPUT_FIELD, proofInput.toStrDict()), # TODO _F_ why do we need to send this? isn't the same data passed as keys in 'proof'? (REVEALED_ATTRS_FIELD, toDictWithStrValues(revealedAttrs))]) self.signAndSendToLink(msg=op, linkName=link.name)
async def create_proof(): proof_input = ProofInput( nonce=int(acme_proof_req.nonce), revealedAttrs=acme_proof_req.verifiableAttributes, predicates=acme_proof_req.predicates) proof = await aliceAgent.prover.presentProof(proof_input) msg = get_proof_libsovrin_msg(acme_link, acme_proof_req, proof, str(schema.seqId), schema.seqId) aliceAgent.signAndSendToLink(msg=msg, linkName=acme_link.name)
async def testPrimaryClaimNoPredicates(prover1, verifier, claimsProver1Gvt, nonce, schemaGvtId, attrRepo): revealledAttrs = ['name'] proofInput = ProofInput(revealledAttrs) claims, revealedAttrs = await prover1._findClaims(proofInput) claims = { schemaKey: ProofClaims( Claims(primaryClaim=proofClaim.claims.primaryClaim), revealedAttrs=revealledAttrs) for schemaKey, proofClaim in claims.items()} proof = await prover1._prepareProof(claims, nonce) assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def testMultiplIssuersSingleProver(primes1, primes2): # 1. Init entities publicRepo = PublicRepoInMemory() attrRepo = AttributeRepoInMemory() issuer1 = Issuer(IssuerWalletInMemory('issuer1', publicRepo), attrRepo) issuer2 = Issuer(IssuerWalletInMemory('issuer2', publicRepo), attrRepo) # 2. Create a Schema schema1 = await issuer1.genSchema('GVT', '1.0', GVT.attribNames()) schemaId1 = ID(schema1.getKey()) schema2 = await issuer2.genSchema('XYZCorp', '1.0', XYZCorp.attribNames()) schemaId2 = ID(schema2.getKey()) # 3. Create keys for the Schema await issuer1.genKeys(schemaId1, **primes1) await issuer2.genKeys(schemaId2, **primes2) # 4. Issue accumulator await issuer1.issueAccumulator(schemaId=schemaId1, iA='110', L=5) await issuer2.issueAccumulator(schemaId=schemaId2, iA=9999999, L=5) # 4. set attributes for user1 userId = '111' attrs1 = GVT.attribs(name='Alex', age=28, height=175, sex='male') attrs2 = XYZCorp.attribs(status='FULL', period=8) attrRepo.addAttributes(schema1.getKey(), userId, attrs1) attrRepo.addAttributes(schema2.getKey(), userId, attrs2) # 5. request Claims prover = Prover(ProverWalletInMemory(userId, publicRepo)) claimsReq1 = await prover.createClaimRequest(schemaId1) claimsReq2 = await prover.createClaimRequest(schemaId2) claims1 = await issuer1.issueClaim(schemaId1, claimsReq1) claims2 = await issuer2.issueClaim(schemaId2, claimsReq2) await prover.processClaim(schemaId1, claims1) await prover.processClaim(schemaId2, claims2) # 6. proof Claims proofInput = ProofInput(['name', 'status'], [PredicateGE('age', 18), PredicateGE('period', 5)]) verifier = Verifier(WalletInMemory('verifier1', publicRepo)) nonce = verifier.generateNonce() proof, revealedAttrs = await prover.presentProof(proofInput, nonce) assert revealedAttrs['name'] == 'Alex' assert revealedAttrs['status'] == 'FULL' assert await verifier.verify(proofInput, proof, revealedAttrs, nonce)
async def testRevealedAndPredicateSameIssuer(prover1, allClaims, schemaGvtId, attrRepo): proofInput = ProofInput(['name'], [PredicateGE('age', 18)]) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, ['name'], [PredicateGE('age', 18)]) } revealedAttrs = { 'name': attrRepo.getAttributes(schemaGvtId.schemaKey, prover1.proverId).encoded()['name'] } assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def testMultipledPredicates(prover1, allClaims, schemaGvtId, schemaXyzId): proofInput = ProofInput([], [PredicateGE('age', 18), PredicateGE('period', 8)]) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) claimsXyz = await prover1.wallet.getClaims(schemaXyzId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, [], [PredicateGE('age', 18)]), schemaXyzId.schemaKey: ProofClaims(claimsXyz, [], [PredicateGE('period', 8)]) } revealedAttrs = {} assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def verifyProof(self, msg: Any): body, (frm, _) = msg link = self.verifyAndGetLink(msg) if not link: raise NotImplementedError proofName = body[NAME] nonce = getNonceForProof(body[NONCE]) proof = FullProof.fromStrDict(body[PROOF_FIELD]) proofInput = ProofInput.fromStrDict(body[PROOF_INPUT_FIELD]) revealedAttrs = fromDictWithStrValues(body[REVEALED_ATTRS_FIELD]) result = await self.verifier.verify(proofInput, proof, revealedAttrs, nonce) self.logger.info('Proof "{}" accepted with nonce {}'.format( proofName, nonce)) self.logger.info('Verifying proof "{}" from {}'.format( proofName, link.name)) status = 'verified' if result else 'failed verification' resp = { TYPE: PROOF_STATUS, DATA: ' Your Proof {} {} was received and {}\n'.format( body[NAME], body[VERSION], status), } self.signAndSend(resp, link.localIdentifier, frm, origReqId=body.get(f.REQ_ID.nm)) if result: for attribute in proofInput.revealedAttrs: # Log attributes that were verified self.logger.info('verified {}: {}'.format( attribute, revealedAttrs[attribute])) self.logger.info( 'Verified that proof "{}" contains attributes ' 'from claim(s) issued by: {}'.format( proofName, ", ".join(sorted([sk.issuerId for sk in proof.schemaKeys])))) await self._postProofVerif(proofName, link, frm) else: self.logger.info( 'Verification failed for proof {} from {} '.format( proofName, link.name))
async def testRevealedAndPredicateDifferentIssuers(prover1, allClaims, schemaGvtId, schemaXyzId, attrRepo): proofInput = ProofInput(['status'], [PredicateGE('age', 18)]) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) claimsXyz = await prover1.wallet.getClaims(schemaXyzId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, [], [PredicateGE('age', 18)]), schemaXyzId.schemaKey: ProofClaims(claimsXyz, ['status'], []) } revealedAttrs = { 'status': attrRepo.getAttributes(schemaXyzId.schemaKey, prover1.proverId)['status'] } assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def testMultipledRevealed(prover1, allClaims, schemaGvtId, schemaXyzId, attrRepo): proofInput = ProofInput(['status', 'name'], []) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) claimsXyz = await prover1.wallet.getClaims(schemaXyzId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, ['name'], []), schemaXyzId.schemaKey: ProofClaims(claimsXyz, ['status'], []) } revealedAttrs = { 'name': attrRepo.getAttributes(schemaGvtId.schemaKey, prover1.proverId)['name'], 'status': attrRepo.getAttributes(schemaXyzId.schemaKey, prover1.proverId)['status'], } assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def sendProofAsync(self, link: Link, proofRequest: ProofRequest): # TODO _F_ this nonce should be from the Proof Request, not from an # invitation proofInput = ProofInput( nonce=proofRequest.nonce, revealedAttrs=proofRequest.verifiableAttributes, predicates=proofRequest.predicates) # TODO rename presentProof to buildProof or generateProof proof = await self.prover.presentProof(proofInput) proof.requestedProof.self_attested_attrs.update( proofRequest.selfAttestedAttrs) op = { TYPE: PROOF, NONCE: link.invitationNonce, PROOF_FIELD: proof.to_str_dict(), PROOF_REQUEST_FIELD: proofRequest.to_str_dict() } self.signAndSendToLink(msg=op, linkName=link.name)
async def verifyProof(self, msg: Any): body, (frm, _) = msg link = self.verifyAndGetLink(msg) if not link: raise NotImplementedError proofName = body[NAME] nonce = getNonceForProof(body[NONCE]) proof = FullProof.fromStrDict(body[PROOF_FIELD]) proofInput = ProofInput.fromStrDict(body[PROOF_INPUT_FIELD]) revealedAttrs = fromDictWithStrValues(body[REVEALED_ATTRS_FIELD]) result = await self.verifier.verify(proofInput, proof, revealedAttrs, nonce) self.logger.info('Proof "{}" accepted with nonce {}' .format(proofName, nonce)) self.logger.info('Verifying proof "{}" from {}' .format(proofName, link.name)) status = 'verified' if result else 'failed verification' resp = { TYPE: PROOF_STATUS, DATA: ' Your Proof {} {} was received and {}\n'. format(body[NAME], body[VERSION], status), } self.signAndSend(resp, link.localIdentifier, frm, origReqId=body.get(f.REQ_ID.nm)) if result: for attribute in proofInput.revealedAttrs: # Log attributes that were verified self.logger.info('verified {}: {}'. format(attribute, revealedAttrs[attribute])) self.logger.info('Verified that proof "{}" contains attributes ' 'from claim(s) issued by: {}'.format( proofName, ", ".join( sorted([sk.issuerId for sk in proof.schemaKeys])))) await self._postProofVerif(proofName, link, frm) else: self.logger.info('Verification failed for proof {} from {} ' .format(proofName, link.name))
async def testMultipleAll(prover1, allClaims, schemaGvtId, schemaXyzId, attrRepo): proofInput = ProofInput(['status', 'name'], [PredicateGE('age', 18), PredicateGE('period', 8)]) claimsGvt = await prover1.wallet.getClaims(schemaGvtId) claimsXyz = await prover1.wallet.getClaims(schemaXyzId) proofClaims = { schemaGvtId.schemaKey: ProofClaims(claimsGvt, ['name'], [PredicateGE('age', 18)]), schemaXyzId.schemaKey: ProofClaims(claimsXyz, ['status'], [PredicateGE('period', 8)]) } revealedAttrs = { 'name': attrRepo.getAttributes(schemaGvtId.schemaKey, prover1.proverId).encoded()['name'], 'status': attrRepo.getAttributes(schemaXyzId.schemaKey, prover1.proverId).encoded()['status'], } assert (proofClaims, revealedAttrs) == await prover1._findClaims(proofInput)
async def testEmpty(prover1, allClaims): proofInput = ProofInput([], []) assert ({}, {}) == await prover1._findClaims(proofInput)
async def testNoPredicates(prover1, verifier, claimsProver1Gvt): proofInput = ProofInput(['name'], []) assert await presentProofAndVerify(verifier, proofInput, prover1)
async def testPredicateNotFound(prover1, allClaims): proofInput = ProofInput([], [PredicateGE('age', 18), PredicateGE('aaaa', 8)]) with pytest.raises(ValueError): await prover1._findClaims(proofInput)
async def testAttrNotFound(prover1, allClaims): proofInput = ProofInput(['name', 'aaaa'], []) with pytest.raises(ValueError): await prover1._findClaims(proofInput)
async def testMultipleRevealedAttrs(prover1, verifier, claimsProver1Gvt): proofInput = ProofInput(['name', 'sex'], []) assert await presentProofAndVerify(verifier, proofInput, prover1)
async def testMultipleGePredicate(prover1, verifier, claimsProver1Gvt): proofInput = ProofInput(['name'], [PredicateGE('age', 18), PredicateGE('height', 170)]) assert await presentProofAndVerify(verifier, proofInput, prover1)
async def testGePredicateNegative(prover1, verifier, claimsProver1Gvt): proofInput = ProofInput(['name'], [PredicateGE('age', 29)]) with pytest.raises(ValueError): await presentProofAndVerify(verifier, proofInput, prover1)
async def testGePredicateForEqual(prover1, verifier, claimsProver1Gvt): proofInput = ProofInput(['name'], [PredicateGE('age', 28)]) assert await presentProofAndVerify(verifier, proofInput, prover1)