Skip to content

Commit

Permalink
feat: add script to create cred_def_id (bcgov#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Leach <[email protected]>
  • Loading branch information
jleach authored Mar 6, 2024
1 parent b947d48 commit f1c9c7a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion devops/charts/controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ image:
registry: ghcr.io
repository: bcgov/mobile-attestation-vc-controller/controller
# Overrides the image tag whose default is the chart appVersion.
tag: "a97d583"
tag: "332508f"

env:
TRACTION_BASE_URL: "https://traction-tenant-proxy-dev.apps.silver.devops.gov.bc.ca"
Expand Down
39 changes: 39 additions & 0 deletions scripts/cred_def.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
import os
import re

sys.path.insert(0, "./src")

from traction import get_cred_def, create_cred_def

tag = "bcwallet"
revocation_registry_size = 0
schema_id = f"{os.environ.get('TRACTION_LEGACY_DID')}:2:app_attestation:1.0"
cred_def_id_regex = r"{}:3:CL:[0-9]+:{}".format(
os.environ.get("TRACTION_LEGACY_DID"), tag
)

cred_def_ids = get_cred_def(schema_id)
matches = [
s
for s in cred_def_ids.get("credential_definition_ids")
if re.search(cred_def_id_regex, s)
]

if len(matches) > 0:
print(f"Credential defention {matches.pop()} already exists")
exit(0)

response = create_cred_def(schema_id, tag, revocation_registry_size)

if (
response is not None
and response.get("txn") is not None
and response.get("txn").get("state") == "request_sent"
):
cred_def_id = response.get("txn").get("meta_data").get("context").get("cred_def_id")
print(f"Cred def ID {cred_def_id} created successfully")
exit(0)
else:
print(f"Error creating cred def: {response}")
exit(1)
62 changes: 62 additions & 0 deletions src/traction.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ def get_schema(schema_id):
return response.json()


def get_cred_def(schema_id):
logger.info("get_schema")

base_url = os.environ.get("TRACTION_BASE_URL")
endpoint = "/credential-definitions/created"
url = urljoin(base_url, endpoint)

token = fetch_bearer_token()

headers = {
"Content-Type": "application/json",
"accept": "application/json",
"Authorization": f"Bearer {token}",
}

response = requests.get(url, headers=headers, params={"schema_id": schema_id})

if response.status_code == 200:
logger.info("Schema queried successfully")
else:
logger.info(f"Error quering schema: {response.status_code}")

return response.json()


def create_schema(schema_name, schema_version, attributes):
logger.info("create_schema")

Expand Down Expand Up @@ -205,3 +230,40 @@ def create_schema(schema_name, schema_version, attributes):
logger.info(f"Error creating schema: {response.status_code}")

return response.json()


def create_cred_def(schema_id, tag, revocation_registry_size=0):
logger.info("create_cred_def")

base_url = os.environ.get("TRACTION_BASE_URL")
endpoint = "/credential-definitions"
url = urljoin(base_url, endpoint)

token = fetch_bearer_token()

headers = {
"Content-Type": "application/json",
"accept": "application/json",
"Authorization": f"Bearer {token}",
}

payload = {
"schema_id": schema_id,
"tag": tag,
"support_revocation": revocation_registry_size > 0,
}

if revocation_registry_size > 0:
payload["revocation_registry_size"] = revocation_registry_size

# print(payload)
response = requests.post(url, headers=headers, data=json.dumps(payload))

if response.status_code == 200:
logger.info("Request sent successfully")
return response.json()

else:
logger.info(f"Error creating request: {response.status_code}")

return None

0 comments on commit f1c9c7a

Please sign in to comment.