|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from grpclib import GRPCError |
| 4 | +from trinsic.proto.services.verifiablecredentials.templates.v1 import ListCredentialTemplatesRequest, \ |
| 5 | + CreateCredentialTemplateRequest |
| 6 | +from trinsic.trinsic_service import TrinsicService |
| 7 | +from trinsic.trinsic_util import trinsic_config, set_eventloop_policy |
| 8 | + |
| 9 | +FROM_AUTH_TOKEN = "TODO - Insert your source ecosystem api token here - base64 encoded" |
| 10 | +TO_AUTH_TOKEN = "TODO - Insert your destination ecosystem api token here - base64 encoded" |
| 11 | + |
| 12 | + |
| 13 | +async def copy_credential_templates(): |
| 14 | + from_trinsic = TrinsicService(server_config=trinsic_config(FROM_AUTH_TOKEN)) |
| 15 | + to_trinsic = TrinsicService(server_config=trinsic_config(TO_AUTH_TOKEN)) |
| 16 | + |
| 17 | + template_id_mapping = dict() |
| 18 | + |
| 19 | + continuation_token = "" |
| 20 | + query = "SELECT * FROM _" |
| 21 | + while True: |
| 22 | + list_response = await from_trinsic.template.list( |
| 23 | + request=ListCredentialTemplatesRequest(query=query, continuation_token=continuation_token)) |
| 24 | + continuation_token = list_response.continuation_token |
| 25 | + print(f"Found {len(list_response.templates)} templates") |
| 26 | + for src_template in list_response.templates: |
| 27 | + # TODO - Logging |
| 28 | + print(f"Attempting to copy {src_template.name}({src_template.id})") |
| 29 | + try: |
| 30 | + create_response = await to_trinsic.template.create(request=CreateCredentialTemplateRequest( |
| 31 | + name=src_template.name, |
| 32 | + allow_additional_fields=src_template.allow_additional_fields, |
| 33 | + fields=src_template.fields |
| 34 | + )) |
| 35 | + # Update the mapping |
| 36 | + template_id_mapping[src_template.id] = create_response.data.id |
| 37 | + except GRPCError as grpc_err: |
| 38 | + # TODO - Logging |
| 39 | + if "name already exists" in grpc_err.message: |
| 40 | + print(f"Template Name `{src_template.name}` already exists - skipped") |
| 41 | + else: |
| 42 | + print("Error on template creation", grpc_err) |
| 43 | + # TODO - Ignore only "template name exists" |
| 44 | + pass |
| 45 | + # Do this at the end of the loop in case there were entries in the last loop |
| 46 | + if not list_response.has_more_results: |
| 47 | + print("No (more) templates found") |
| 48 | + break |
| 49 | + |
| 50 | + # Return an id mapping table |
| 51 | + print("Mapping id table", template_id_mapping) |
| 52 | + |
| 53 | + |
| 54 | +# Press the green button in the gutter to run the script. |
| 55 | +if __name__ == '__main__': |
| 56 | + # TODO - Command line arguments? |
| 57 | + set_eventloop_policy() |
| 58 | + asyncio.run(copy_credential_templates()) |
| 59 | + |
| 60 | +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ |
0 commit comments