Exemplo n.º 1
0
async def test_requested_proof_from_to_dict(prover1, nonce, claimsProver1Gvt):
    proofRequest = ProofRequest(
        "proof1",
        "1.0",
        1,
        verifiableAttributes={'attr_uuid': AttributeInfo(name='name')},
        predicates={'predicate_uuid': PredicateGE('age', 18)})

    proof = await prover1.presentProof(proofRequest)

    requested_proof_serialized = {
        'revealed_attrs': {
            'attr_uuid':
            ['1', 'Alex', '1139481716457488690172217916278103335']
        },
        'predicates': {
            'predicate_uuid': '1'
        },
        'self_attested_attrs': {},
        'unrevealed_attrs': {}
    }

    assert proof.requestedProof.to_str_dict() == requested_proof_serialized
    assert proof.requestedProof == RequestedProof.from_str_dict(
        requested_proof_serialized)
    assert proof.requestedProof == RequestedProof.from_str_dict(
        proof.requestedProof.to_str_dict())
Exemplo n.º 2
0
    async def verifyProof(self, msg: Any):
        body, (frm, _) = msg
        link = self.verifyAndGetLink(msg)
        if not link:
            raise NotImplementedError

        proof = body[PROOF_FIELD]
        proofRequest = ProofRequest.from_str_dict(body[PROOF_REQUEST_FIELD])
        nonce = getNonceForProof(body[NONCE])
        proofName = proofRequest.name

        proofs = {}

        for key, p in proof['proofs'].items():
            schema = await self.verifier.wallet.getSchema(
                ID(schemaId=int(p['schema_seq_no'])))
            pk = await self.verifier.wallet.getPublicKey(
                ID(schemaKey=schema.getKey()))
            proofs[key] = ProofInfo.from_str_dict(p, str(pk.N))

        proof = FullProof(
            proofs, AggregatedProof.from_str_dict(proof['aggregated_proof']),
            RequestedProof.from_str_dict(proof['requested_proof']))

        result = await self.verifier.verify(proofRequest, proof)

        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(
                proofRequest.name, proofRequest.version, status),
        }
        self.signAndSend(resp,
                         link.localIdentifier,
                         frm,
                         origReqId=body.get(f.REQ_ID.nm))

        if result:
            for uuid, attribute in proofRequest.verifiableAttributes.items():
                # Log attributes that were verified
                self.logger.info('verified {}: {}'.format(
                    attribute.name,
                    proof.requestedProof.revealed_attrs[uuid][1]))
            self.logger.info(
                'Verified that proof "{}" contains attributes '
                'from claim(s) issued by: {}'.format(
                    proofName, ", ".join(
                        sorted([v.issuer_did
                                for k, v in proof.proofs.items()]))))
            await self._postProofVerif(proofName, link, frm)
        else:
            self.logger.info(
                'Verification failed for proof {} from {} '.format(
                    proofName, link.name))
Exemplo n.º 3
0
async def testOneRevealedFromSpecificSchemaAndIssuer(prover1, allClaims,
                                                     schemaGvt, schemaGvtId,
                                                     attrRepo, keysGvt):
    proofRequest = ProofRequest("proof1",
                                "1.0",
                                1,
                                verifiableAttributes={
                                    'uuid':
                                    AttributeInfo(
                                        name='name',
                                        schema_seq_no=schemaGvt.seqId,
                                        issuer_did=schemaGvt.issuerId)
                                })
    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)

    proofClaims = {schemaGvt.seqId: ProofClaims(claimsGvt, ['name'], [])}
    attr = attrRepo.getAttributes(schemaGvtId.schemaKey,
                                  prover1.proverId)['name']
    requestedProof = RequestedProof(revealed_attrs={
        'uuid': [str(schemaGvt.seqId), attr,
                 str(encodeAttr(attr))]
    })

    assert proofClaims, requestedProof == await prover1._findClaims(
        proofRequest)

    assert proofClaims, requestedProof == await prover1._findClaims(
        proofRequest)
Exemplo n.º 4
0
async def testMultipledPredicates(prover1, allClaims, schemaGvtId, schemaXyzId,
                                  schemaGvt):
    proofRequest = ProofRequest("proof1",
                                "1.0",
                                1,
                                predicates={
                                    'predicate_uuid1': PredicateGE('age', 18),
                                    'predicate_uuid2':
                                    PredicateGE('period', 8)
                                })

    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)
    claimsXyz = await prover1.wallet.getClaimSignature(schemaXyzId)
    proofClaims = {
        schemaGvt.seqId: ProofClaims(claimsGvt, [], [PredicateGE('age', 18)]),
        schemaGvt.seqId: ProofClaims(claimsXyz, [], [PredicateGE('period', 8)])
    }

    requestedProof = RequestedProof(predicates={
        'predicate_uuid1': schemaGvt.seqId,
        'predicate_uuid2': schemaGvt.seqId
    })

    assert proofClaims, requestedProof == await prover1._findClaims(
        proofRequest)
Exemplo n.º 5
0
async def testMultipledRevealed(prover1, allClaims, schemaGvtId, schemaXyzId,
                                attrRepo, schemaGvt):
    proofRequest = ProofRequest("proof1",
                                "1.0",
                                1,
                                verifiableAttributes={
                                    'attr_uuid1': AttributeInfo(name='status'),
                                    'attr_uuid2': AttributeInfo(name='name')
                                })

    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)
    claimsXyz = await prover1.wallet.getClaimSignature(schemaXyzId)
    proofClaims = {
        schemaGvt.seqId: ProofClaims(claimsGvt, ['name'], []),
        schemaGvt.seqId: ProofClaims(claimsXyz, ['status'], [])
    }

    attr1 = attrRepo.getAttributes(schemaXyzId.schemaKey,
                                   prover1.proverId)['status']
    attr2 = attrRepo.getAttributes(schemaGvtId.schemaKey,
                                   prover1.proverId)['name']
    requestedProof = RequestedProof(
        revealed_attrs={
            'attr_uuid1': [schemaGvt.seqId, attr1,
                           str(encodeAttr(attr1))],
            'attr_uuid2': [schemaGvt.seqId, attr2,
                           str(encodeAttr(attr2))]
        })

    assert proofClaims, requestedProof == await prover1._findClaims(
        proofRequest)
Exemplo n.º 6
0
async def testRevealedAndPredicateDifferentIssuers(prover1, allClaims,
                                                   schemaGvtId, schemaXyzId,
                                                   attrRepo, schemaGvt):
    proofRequest = ProofRequest(
        "proof1",
        "1.0",
        1,
        verifiableAttributes={'attr_uuid': AttributeInfo(name='status')},
        predicates={'predicate_uuid': PredicateGE('age', 18)})

    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)
    claimsXyz = await prover1.wallet.getClaimSignature(schemaXyzId)
    proofClaims = {
        schemaGvt.seqId: ProofClaims(claimsGvt, [], [PredicateGE('age', 18)]),
        schemaGvt.seqId: ProofClaims(claimsXyz, ['status'], [])
    }

    attr = attrRepo.getAttributes(schemaXyzId.schemaKey,
                                  prover1.proverId)['status']
    requestedProof = RequestedProof(
        revealed_attrs={
            'attr_uuid': [schemaGvt.seqId, attr,
                          str(encodeAttr(attr))]
        },
        predicates={'predicate_uuid': schemaGvt.seqId})

    assert proofClaims, requestedProof == await prover1._findClaims(
        proofRequest)
Exemplo n.º 7
0
async def testEmpty(prover1):
    proofRequest = ProofRequest("proof1",
                                "1.0",
                                1,
                                verifiableAttributes={},
                                predicates={})
    assert ({}, RequestedProof([], [], [],
                               [])) == await prover1._findClaims(proofRequest)
async def testOnePredicateOnly(prover1, allClaims, schemaGvtId, schemaGvt):
    proofRequest = ProofRequest("proof1", "1.0", 1, predicates={'uuid': PredicateGE('age', 18)})

    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)
    proofClaims = {schemaGvt.seqId:
                       ProofClaims(claimsGvt, [], [PredicateGE('age', 18)])}

    requestedProof = RequestedProof(predicates={'uuid': schemaGvt.seqId})

    assert proofClaims, requestedProof == await prover1._findClaims(proofRequest)
Exemplo n.º 9
0
async def testRequestedProofFromToDict(prover1, nonce, claimsProver1Gvt):
    proofRequest = ProofRequest("proof1", "1.0", 1,
                                verifiableAttributes={
                                    'attr_uuid': AttributeInfo(name='name')},
                                predicates={'predicate_uuid': PredicateGE('age', 18)})

    proof = await prover1.presentProof(proofRequest)

    assert proof.requestedProof == RequestedProof.fromStrDict(
        proof.requestedProof.toStrDict())
Exemplo n.º 10
0
    async def _findClaims(self, proofRequest: ProofRequest) -> (
            Dict[SchemaKey, ProofClaims], Dict[str, Any]):
        revealedAttrs, predicates = proofRequest.verifiableAttributes, proofRequest.predicates

        foundRevealedAttrs = {}
        foundPredicates = {}
        proofClaims = {}
        schemas = {}
        allClaimsAttributes = await self.wallet.getAllClaimsAttributes()

        async def addProof():
            revealedAttrsForClaim = [a for a in revealedAttrs.values() if a.name in claim.keys()]
            revealedPredicatesForClaim = [p for p in predicates.values() if p.attrName in claim.keys()]

            claims = await self.wallet.getClaimSignature(ID(schemaId=schemaId))
            proofClaim = ProofClaims(claims=claims, revealedAttrs=revealedAttrsForClaim,
                                     predicates=revealedPredicatesForClaim)

            proofClaims[schemaId] = proofClaim

        for schemaKey, c in allClaimsAttributes.items():
            schemas[schemaKey] = (await self.wallet.getSchema(ID(schemaKey)))

        for uuid, revealedAttr in revealedAttrs.items():
            matches = [(schemas[key].seqId, c) for key, c in allClaimsAttributes.items() if revealedAttr.name in c
                       and (schemas[key].seqId == revealedAttr.schema_seq_no if revealedAttr.schema_seq_no else True)
                       and (schemas[key].issuerId == revealedAttr.issuer_did if revealedAttr.issuer_did else True)]

            if len(matches) == 0:
                raise ValueError("A claim isn't found for the following attributes: {}", revealedAttr.name)

            schemaId, claim = matches[0]
            foundRevealedAttrs[uuid] = [str(schemaId), str(claim[revealedAttr.name].raw),
                                        str(claim[revealedAttr.name].encoded)]

            if schemaId not in proofClaims:
                await addProof()

        for uuid, predicate in predicates.items():
            matches = [(schemas[key].seqId, c) for key, c in allClaimsAttributes.items() if predicate.attrName in c
                       and (schemas[key].seqId == predicate.schema_seq_no if predicate.schema_seq_no else True)
                       and (schemas[key].issuerId == predicate.issuer_did if predicate.issuer_did else True)]

            if len(matches) == 0:
                raise ValueError("A claim isn't found for the following predicate: {}", predicate)

            schemaId, claim = matches[0]
            foundPredicates[uuid] = str(schemaId)

            if schemaId not in proofClaims:
                await addProof()

        requestedProof = RequestedProof(revealed_attrs=foundRevealedAttrs, predicates=foundPredicates)

        return proofClaims, requestedProof
async def testPredicatesEmpty(prover1, allClaims, schemaGvtId, attrRepo, schemaGvt):
    proofRequest = ProofRequest("proof1", "1.0", 1,
                                verifiableAttributes={'uuid': AttributeInfo(name='name')}, predicates={})

    claimsGvt = await prover1.wallet.getClaimSignature(schemaGvtId)

    proofClaims = {schemaGvt.seqId: ProofClaims(claimsGvt, ['name'], [])}

    attr = attrRepo.getAttributes(schemaGvtId.schemaKey, prover1.proverId)['name']
    requestedProof = RequestedProof(revealed_attrs={'uuid': [schemaGvt.seqId, attr, str(encodeAttr(attr))]})

    assert proofClaims, requestedProof == await prover1._findClaims(proofRequest)
Exemplo n.º 12
0
    async def verifyProof(self, msg: Any):
        body, (frm, _) = msg
        link = self.verifyAndGetLink(msg)
        if not link:
            raise NotImplementedError

        proof = body[PROOF_FIELD]
        proofRequest = ProofRequest.from_str_dict(body[PROOF_REQUEST_FIELD])
        nonce = getNonceForProof(body[NONCE])
        proofName = proofRequest.name

        proofs = {}

        for key, p in proof['proofs'].items():
            schema = await self.verifier.wallet.getSchema(ID(schemaId=int(p['schema_seq_no'])))
            pk = await self.verifier.wallet.getPublicKey(ID(schemaKey=schema.getKey()))
            proofs[key] = ProofInfo.from_str_dict(p, str(pk.N))

        proof = FullProof(
            proofs, AggregatedProof.from_str_dict(
                proof['aggregated_proof']), RequestedProof.from_str_dict(
                proof['requested_proof']))

        result = await self.verifier.verify(proofRequest, proof)

        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(proofRequest.name, proofRequest.version, status),
        }
        self.signAndSend(resp, link.localIdentifier, frm,
                         origReqId=body.get(f.REQ_ID.nm))

        if result:
            for uuid, attribute in proofRequest.verifiableAttributes.items():
                # Log attributes that were verified
                self.logger.info(
                    'verified {}: {}'. format(
                        attribute.name,
                        proof.requestedProof.revealed_attrs[uuid][1]))
            self.logger.info('Verified that proof "{}" contains attributes '
                             'from claim(s) issued by: {}'.format(
                                 proofName, ", ".join(
                                     sorted([v.issuer_did for k, v in proof.proofs.items()]))))
            await self._postProofVerif(proofName, link, frm)
        else:
            self.logger.info('Verification failed for proof {} from {} '
                             .format(proofName, link.name))