|
| 1 | +from .base import BaseController |
| 2 | +from ..models.errors import InputError |
| 3 | + |
| 4 | +from aiohttp import ( |
| 5 | + ClientSession, |
| 6 | +) |
| 7 | +import asyncio |
| 8 | + |
| 9 | + |
| 10 | +class RevocationController(BaseController): |
| 11 | + |
| 12 | + def __init__(self, admin_url: str, client_session: ClientSession): |
| 13 | + super().__init__(admin_url, client_session) |
| 14 | + self.base_url = "/revocation" |
| 15 | + self.revocation_states = set([ |
| 16 | + "init", "generated", "posted", "active", "full" |
| 17 | + ]) |
| 18 | + |
| 19 | + async def revoke_credential(self, cred_ex_id: str = "", cred_rev_id: str = "", rev_reg_id: str = "", publish: bool = False): |
| 20 | + """ |
| 21 | + Revoke an issued credential. |
| 22 | +
|
| 23 | + This method does not publish revocation to the ledger immediately by default - instead, marks it as pending. |
| 24 | + Either (cred_ex_id) OR (cred_rev_id AND rev_reg_id) are required. |
| 25 | + """ |
| 26 | + |
| 27 | + req_body = { |
| 28 | + "publish": publish |
| 29 | + } |
| 30 | + if cred_ex_id: |
| 31 | + req_body["cred_ex_id"] = cred_ex_id |
| 32 | + elif cred_rev_id and rev_reg_id: |
| 33 | + req_body["cred_rev_id"] = cred_rev_id |
| 34 | + req_body["rev_reg_id"] = rev_reg_id |
| 35 | + else: |
| 36 | + raise InputError( |
| 37 | + "Either (cred_ex_id) OR (cred_rev_id AND rev_reg_id) are required." |
| 38 | + ) |
| 39 | + |
| 40 | + return await self.admin_POST(f"{self.base_url}/revoke", json_data=req_body) |
| 41 | + |
| 42 | + async def publish_pending_revocations(self, pending_revs): |
| 43 | + """ |
| 44 | + Publish pending revocations to ledger. |
| 45 | +
|
| 46 | + Takes in a list of dicts with str key-values, transforms into the correct |
| 47 | + API schema. |
| 48 | +
|
| 49 | + eg. pending_revs = [{ "sample-rev-reg-id": "sample-cred-rev-id" }] |
| 50 | + """ |
| 51 | + |
| 52 | + req_body = { |
| 53 | + "rrid2crid": {} |
| 54 | + } |
| 55 | + |
| 56 | + for rev in pending_revs: |
| 57 | + # check if revocation registry id already exists in map, if so, simply append to array |
| 58 | + if rev.rev_reg_id in req_body["rrid2crid"]: |
| 59 | + req_body["rrid2crid"][rev.rev_reg_id].append(rev.cred_rev_id) |
| 60 | + else: |
| 61 | + req_body["rrid2crid"][rev.rev_reg_id] = [rev.cred_rev_id] |
| 62 | + |
| 63 | + return await self.admin_POST(f"{self.base_url}/publish-revocations", json_data=req_body) |
| 64 | + |
| 65 | + async def clear_pending_revocations(self, pending_revs): |
| 66 | + """ |
| 67 | + Clear pending revocations. |
| 68 | +
|
| 69 | + Takes in a list of dicts with str key-values, transforms into the correct |
| 70 | + API schema. |
| 71 | +
|
| 72 | + eg. pending_revs = [{ "sample-rev-reg-id": "sample-cred-rev-id" }] |
| 73 | + """ |
| 74 | + |
| 75 | + req_body = { |
| 76 | + "purge": {} |
| 77 | + } |
| 78 | + |
| 79 | + for rev in pending_revs: |
| 80 | + # check if revocation registry id already exists in map, if so, simply append to array |
| 81 | + if rev.rev_reg_id in req_body["purge"]: |
| 82 | + req_body["purge"][rev.rev_reg_id].append(rev.cred_rev_id) |
| 83 | + else: |
| 84 | + req_body["purge"][rev.rev_reg_id] = [rev.cred_rev_id] |
| 85 | + |
| 86 | + return await self.admin_POST(f"{self.base_url}/clear-pending-revocations", json_data=req_body) |
| 87 | + |
| 88 | + async def get_credential_revocation_status(self, cred_ex_id: str, cred_rev_id: str, rev_reg_id: str): |
| 89 | + """ |
| 90 | + Get credential revocation status. |
| 91 | + """ |
| 92 | + |
| 93 | + params = {} |
| 94 | + if cred_ex_id: |
| 95 | + params["cred_ex_id"] = cred_ex_id |
| 96 | + if cred_rev_id: |
| 97 | + params["cred_rev_id"] = cred_rev_id |
| 98 | + if rev_reg_id: |
| 99 | + params["rev_reg_id"] = rev_reg_id |
| 100 | + |
| 101 | + return await self.admin_GET(f"{self.base_url}/credential-record", params=params) |
| 102 | + |
| 103 | + async def get_created_revocation_registries(self, cred_def_id: str, rev_reg_state: str): |
| 104 | + """ |
| 105 | + Search for matching revocation registries that current agnet created. |
| 106 | + """ |
| 107 | + |
| 108 | + params = {} |
| 109 | + if cred_def_id: |
| 110 | + params["cred_def_id"] = cred_def_id |
| 111 | + if rev_reg_state: |
| 112 | + if not self.__validate_revocation_registry_state(rev_reg_state): |
| 113 | + raise InputError("invalid revocation registry state input") |
| 114 | + params["state"] = rev_reg_state |
| 115 | + |
| 116 | + return await self.admin_GET(f"{self.base_url}/registries/created", params=params) |
| 117 | + |
| 118 | + async def get_revocation_registry(self, rev_reg_id: str): |
| 119 | + """ |
| 120 | + Get revocation registry by revocation registry id |
| 121 | + """ |
| 122 | + |
| 123 | + return await self.admin_GET(f"{self.base_url}/registry/{rev_reg_id}") |
| 124 | + |
| 125 | + async def update_revocation_registry_tails_file(self, rev_reg_id: str, tail_file_uri: str): |
| 126 | + """ |
| 127 | + Update revocation registry by revocation registry id. |
| 128 | + """ |
| 129 | + |
| 130 | + req_body = { |
| 131 | + "tails_public_uri": tail_file_uri |
| 132 | + } |
| 133 | + |
| 134 | + return await self.admin_PATCH(f"{self.base_url}/registry/{rev_reg_id}", json_data=req_body) |
| 135 | + |
| 136 | + async def get_active_revocation_registry_by_cred_def(self, cred_def_id: str): |
| 137 | + """ |
| 138 | + Get current active revocation registry by credential definition id. |
| 139 | + """ |
| 140 | + |
| 141 | + return await self.admin_GET(f"{self.base_url}/active-registry/{cred_def_id}") |
| 142 | + |
| 143 | + async def get_num_credentials_issued_by_revocation_registry(self, rev_reg_id: str): |
| 144 | + """ |
| 145 | + Get number of credentials issued against revocation registry. |
| 146 | + """ |
| 147 | + |
| 148 | + response = await self.admin_GET(f"{self.base_url}/registry/{rev_reg_id}/issued") |
| 149 | + return response["result"] |
| 150 | + |
| 151 | + async def create_revocation_registry(self, cred_def_id: str, max_cred_num: int): |
| 152 | + """ |
| 153 | + Creates a new revocation registry. |
| 154 | + """ |
| 155 | + |
| 156 | + req_body = { |
| 157 | + "credential_definition_id": cred_def_id, |
| 158 | + "max_cred_num": max_cred_num |
| 159 | + } |
| 160 | + |
| 161 | + return await self.admin_POST(f"{self.base_url}/create-registry", json_data=req_body) |
| 162 | + |
| 163 | + async def send_revocation_registry_definition(self, rev_reg_id: str): |
| 164 | + """ |
| 165 | + Send revocation registry definition to ledger. |
| 166 | + """ |
| 167 | + |
| 168 | + return await self.admin_POST(f"{self.base_url}/registry/{rev_reg_id}/definition") |
| 169 | + |
| 170 | + async def send_revocation_registry_entry(self, rev_reg_id: str): |
| 171 | + """ |
| 172 | + Send revocation registry entry to ledger. |
| 173 | + """ |
| 174 | + |
| 175 | + return await self.admin_POST(f"{self.base_url}/registry/{rev_reg_id}/entry") |
| 176 | + |
| 177 | + async def upload_revocation_registry_tails_file(self, rev_reg_id: str): |
| 178 | + """ |
| 179 | + Upload local tails file to server. |
| 180 | + """ |
| 181 | + |
| 182 | + return await self.admin_PUT(f"{self.base_url}/registry/{rev_reg_id}/tails-file") |
| 183 | + |
| 184 | + """ |
| 185 | + TODO: this API call downloads the Tails file as an octet stream. We need to consider if we |
| 186 | + want to build support for this |
| 187 | + """ |
| 188 | + # async def get_revocation_registry_tails_file(self, rev_reg_id: str): |
| 189 | + # """ |
| 190 | + # Download tails file. |
| 191 | + # """ |
| 192 | + # return await self.admin_GET(f"{self.base_url}/registry/{rev_reg_id}/tails-file") |
| 193 | + |
| 194 | + async def update_revocation_registry_state(self, rev_reg_id: str, state: str): |
| 195 | + """ |
| 196 | + Set revocation registry state manually. |
| 197 | + """ |
| 198 | + |
| 199 | + params = {} |
| 200 | + if state: |
| 201 | + if not self.__validate_revocation_registry_state(state): |
| 202 | + raise InputError("invalid revocation registry state input") |
| 203 | + params["state"] = state |
| 204 | + |
| 205 | + return await self.admin_PATCH(f"{self.base_url}/registry/{rev_reg_id}/set-state", params=params) |
| 206 | + |
| 207 | + """ |
| 208 | + Private utility methods. |
| 209 | + """ |
| 210 | + |
| 211 | + def __validate_revocation_registry_state(self, state: str) -> bool: |
| 212 | + """ |
| 213 | + Validate if state input is one of init, generated, posted, active, full. |
| 214 | + """ |
| 215 | + |
| 216 | + return state in self.revocation_states |
0 commit comments