Skip to content

Commit 84ae505

Browse files
feat: Copy All Credential Templates script (#1367)
1 parent 1e6defc commit 84ae505

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

python/samples/templates_demo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import json
3-
import uuid
3+
import random
44

55
from trinsic.proto.services.verifiablecredentials.templates.v1 import (
66
TemplateField,
@@ -25,10 +25,12 @@ async def templates_demo():
2525
trinsic_service = TrinsicService(server_config=trinsic_config())
2626
ecosystem = await trinsic_service.provider.create_ecosystem()
2727

28+
print("Ecosystem Token", trinsic_service.service_options.auth_token)
29+
2830
# create example template
2931
# createTemplate() {
3032
create_request = CreateCredentialTemplateRequest(
31-
name="An Example Credential",
33+
name=f"An Example Credential-{random.Random().randint(0,100)}",
3234
title="Example Credential",
3335
description="A credential for Trinsic's SDK samples",
3436
allow_additional_fields=False,

samples/ecosystem-transfer/main.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trinsic-sdk==1.10.0

0 commit comments

Comments
 (0)