Example #1
0
async def test_schema_key_for():
    spec = {'origin-did-specifier-of-choice': 'o-did', 'name': 'schema-name', 'version': '1.0'}
    assert schema_key_for(spec) == SchemaKey('o-did', 'schema-name', '1.0')

    for x_spec in (
            {},
            {'origin-did': 'o-did', 'x-missing-name': 'schema-name', 'version': '1.0'},
            {'name': 'schema-name', 'version': '1.0'},
            {'origin-did': 'o-did', 'name': 'schema-name', 'version': '1.0', 'too-many-keys': ''}):
        try:
            schema_key_for(x_spec)
            assert False
        except SchemaKeySpec:
            pass
Example #2
0
        async def run(schema):
            async with Issuer() as issuer:
                claim_def_json = None

                # Check if schema exists on ledger
                schema_json = await issuer.get_schema(
                    schema_key_for({
                        'origin_did': issuer.did,
                        'name': schema['name'],
                        'version': schema['version']
                    }))

                # If not, send the schema to the ledger, then get result
                if not json.loads(schema_json):
                    schema_json = await issuer.send_schema(json.dumps(schema))

                schema = json.loads(schema_json)

                self.__log_json('schema:', schema)

                # Check if claim definition has been published.
                # If not then publish.
                claim_def_json = await issuer.get_claim_def(
                    schema['seqNo'], issuer.did)
                if not json.loads(claim_def_json):
                    claim_def_json = await issuer.send_claim_def(schema_json)

                claim_def = json.loads(claim_def_json)
                self.__log_json('claim_def:', claim_def)
Example #3
0
def schema_keys_for(claims: dict, referents: list) -> dict:
    """
    Given a claims structure and a list of referents (wallet claim-uuids),
    return dict mapping each referent to its corresponding schema key instance.

    :param claims: claims structure returned by (HolderProver agent) get_claims(), or (equivalently)
        response json structure at ['claims'] to response to POST 'claims-request' message type
    :param referents: list of referents for which to find corresponding schema key
    :return: schema key per referent (empty dict if no such referents present)
    """

    rv = {}
    uuid2claims = claims['attrs']
    for inner_claims in uuid2claims.values():
        for claim in inner_claims:
            referent = claim['referent']
            if (referent not in rv) and (referent in referents):
                rv[referent] = schema_key_for(claim['schema_key'])

    return rv
Example #4
0
        async def run(schema, claim):
            async with Issuer() as issuer:
                for key, value in claim.items():
                    claim[key] = claim_value_pair(value) if value else \
                        claim_value_pair("")

                self.__log_json('Claim:', claim)
                self.__log_json('Schema:', schema)

                # We need schema from ledger
                schema_json = await issuer.get_schema(
                    schema_key_for({
                        'origin_did': issuer.did,
                        'name': schema['name'],
                        'version': schema['version']
                    }))
                schema = json.loads(schema_json)

                self.__log_json('Schema:', schema)

                claim_def_json = await issuer.get_claim_def(
                    schema['seqNo'], issuer.did)
                claim_def = json.loads(claim_def_json)

                self.__log_json('Schema:', schema)

                # tob_did = await convert_seed_to_did(TOB_INDY_SEED)
                tob_did = apps.get_tob_did()
                self.__log('TheOrgBook DID:', tob_did)

                # We create a claim offer
                claim_offer_json = await issuer.create_claim_offer(
                    schema_json, tob_did)
                claim_offer = json.loads(claim_offer_json)

                self.__log_json('Claim Offer:', claim_offer)

                self.__log_json('Requesting Claim Request:', {
                    'claim_offer': claim_offer,
                    'claim_def': claim_def
                })

                response = requests.post(TOB_BASE_URL +
                                         '/bcovrin/generate-claim-request',
                                         json={
                                             'claim_offer': claim_offer_json,
                                             'claim_def': claim_def_json
                                         })

                # Build claim
                claim_request = response.json()

                claim_request_json = json.dumps(claim_request)
                self.__log_json('Claim Request Json:', claim_request)

                (_, claim_json) = await issuer.create_claim(
                    claim_request_json, claim)

                self.__log_json('Claim Json:', json.loads(claim_json))

                # Send claim
                response = requests.post(TOB_BASE_URL + '/bcovrin/store-claim',
                                         json={
                                             'claim_type':
                                             schema['data']['name'],
                                             'claim_data':
                                             json.loads(claim_json)
                                         })

                return response.json()
Example #5
0
        async def run(schema, claim):
            logger.warn("schema_manager.submit_claim() >>> start")
            start_time = time.time()

            # TODO we have a legal_entity_id at this point so we can put everything in a virtual wallet
            # TODO this will stop the nonce from stepping on each other when multi-threading
            async with Issuer() as issuer:
                claim_def_json = None

                for key, value in claim.items():
                    claim[key] = claim_value_pair(value) if value else \
                        claim_value_pair("")

                self.__log_json('Claim:', claim)
                self.__log_json('Schema:', schema)

                # We need schema from ledger
                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 0.19
                logger.warn(
                    "schema_manager.submit_claim() >>> get schema from ledger")
                schema_key = schema_key_for({
                    'origin_did': issuer.did,
                    'name': schema['name'],
                    'version': schema['version']
                })
                schema_json = await issuer.get_schema(
                    schema_key_for({
                        'origin_did': issuer.did,
                        'name': schema['name'],
                        'version': schema['version']
                    }))
                schema = json.loads(schema_json)

                self.__log_json('Schema:', schema)

                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 1.00
                logger.warn(
                    "schema_manager.submit_claim() >>> get claim definition")
                claim_def_key = str(schema['seqNo']) + ":" + issuer.did
                claim_def_json = await issuer.get_claim_def(
                    schema['seqNo'], issuer.did)
                claim_def = json.loads(claim_def_json)

                self.__log_json('Schema:', schema)

                # tob_did = await convert_seed_to_did(TOB_INDY_SEED)
                tob_did = apps.get_tob_did()
                self.__log('TheOrgBook DID:', tob_did)

                # We create a claim offer
                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 1.05
                logger.warn(
                    "schema_manager.submit_claim() >>> create a claim offer")
                claim_offer_json = await issuer.create_claim_offer(
                    schema_json, tob_did)
                claim_offer = json.loads(claim_offer_json)

                self.__log_json('Claim Offer:', claim_offer)

                self.__log_json('Requesting Claim Request:', {
                    'claim_offer': claim_offer,
                    'claim_def': claim_def
                })

                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 0.01
                logger.warn(
                    "schema_manager.submit_claim() >>> bcovrin generate claim request: "
                    + TOB_BASE_URL + '/bcovrin/generate-claim-request')
                response = requests.post(TOB_BASE_URL +
                                         '/bcovrin/generate-claim-request',
                                         json={
                                             'claim_offer': claim_offer_json,
                                             'claim_def': claim_def_json
                                         })

                # Build claim
                claim_request = response.json()

                claim_request_json = json.dumps(claim_request)
                self.__log_json('Claim Request Json:', claim_request)

                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 1.53
                logger.warn(
                    "schema_manager.submit_claim() >>> issuer create claim")
                (_, claim_json) = await issuer.create_claim(
                    claim_request_json, claim)

                self.__log_json('Claim Json:', json.loads(claim_json))

                # Send claim
                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 0.07
                logger.warn(
                    "schema_manager.submit_claim() >>> send claim to bcovrin")
                response = requests.post(TOB_BASE_URL + '/bcovrin/store-claim',
                                         json={
                                             'claim_type':
                                             schema['data']['name'],
                                             'claim_data':
                                             json.loads(claim_json)
                                         })
                elapsed_time = time.time() - start_time
                start_time = time.time()
                logger.warn(
                    'Step elapsed time >>> {}'.format(elapsed_time))  # 0.46
                logger.warn("schema_manager.submit_claim() >>> return")

                return response.json()
Example #6
0
def claims_for(claims: dict, filt: dict = None) -> dict:
    """
    Find indy-sdk claims matching input filter from within input claims structure,
    json-loaded as returned via agent get_claims().

    :param claims: claims structure returned by (HolderProver agent) get_claims(), or (equivalently)
        response json structure at ['claims'] to response to POST 'claims-request' message type;
        e.g., {
            "attrs": {
                "attr0_uuid": [
                    {
                        "referent": "claim::00000000-0000-0000-0000-000000000000",
                        "attrs": {
                            "attr0": "2",
                            "attr1": "Hello",
                            "attr2": "World"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    },
                    {
                        "referent": "claim::00000000-0000-0000-0000-111111111111",
                        "attrs": {
                            "attr0": "1",
                            "attr1": "Nice",
                            "attr2": "Tractor"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    }
                ],
                "attr1_uuid": [
                    {
                        "referent": "claim::00000000-0000-0000-0000-000000000000",
                        "attrs": {
                            "attr0": "2",
                            "attr1": "Hello",
                            "attr2": "World"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    },
                    {
                        "referent": "claim::00000000-0000-0000-0000-111111111111",
                        "attrs": {
                            "attr0": "1",
                            "attr1": "Nice",
                            "attr2": "Tractor"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    }
                ],
                "attr2_uuid": [
                    {
                        "referent": "claim::00000000-0000-0000-0000-000000000000",
                        "attrs": {
                            "attr0": "2",
                            "attr1": "Hello",
                            "attr2": "World"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    },
                    {
                        "referent": "claim::00000000-0000-0000-0000-111111111111",
                        "attrs": {
                            "attr0": "1",
                            "attr1": "Nice",
                            "attr2": "Tractor"
                        },
                        "issuer_did": "Q4zqM7aXqm7gDQkUVLng9h",
                        "schema_key": {
                            "did": "Q4zqM7aXqm7gDQkUVLng9h",
                            "name": "bc-reg",
                            "version": "1.0"
                        },
                        "revoc_reg_seq_no": null
                    }
                ]
            }
        }
    :param filt: filter for matching attributes and values; dict mapping each SchemaKey to
        dict mapping attributes to values to match (specify empty dict for no filter). E.g.,
        {
            SchemaKey('Q4zqM7aXqm7gDQkUVLng9h', 'bc-reg', '1.0'): {
                'attr0': '1',
                'attr1': 'Nice'
            },
            ...
        ]
    :return: human-legible dict mapping referent to claim attributes for claims matching input filter
    """

    rv = {}
    if filt is None:
        filt = {}
    uuid2claims = claims['attrs']
    for inner_claims in uuid2claims.values():
        for claim in inner_claims:
            if claim['referent'] in rv:
                continue
            if not filt:
                rv[claim['referent']] = claim['attrs']
                continue
            claim_s_key = schema_key_for(claim['schema_key'])
            if claim_s_key in filt:
                if {k: str(filt[claim_s_key][k]) for k in filt[claim_s_key]}.items() <= claim['attrs'].items():
                    rv[claim['referent']] = claim['attrs']

    return rv