|
| 1 | +from .base import BaseController |
| 2 | +from aiohttp import ClientSession |
| 3 | +import logging |
| 4 | +from ..helpers.utils import extract_did, get_schema_details |
| 5 | + |
| 6 | +logger = logging.getLogger("aries_controller.issuer2") |
| 7 | + |
| 8 | +CRED_PREVIEW = ( |
| 9 | + "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/2.0/credential-preview" |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class IssuerV2Controller(BaseController): |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + admin_url: str, |
| 17 | + client_session: ClientSession, |
| 18 | + connection_controller, |
| 19 | + wallet_controller, |
| 20 | + definition_controller, |
| 21 | + ): |
| 22 | + super().__init__(admin_url, client_session) |
| 23 | + self.base_url = "/issue-credential-2.0" |
| 24 | + self.connections = connection_controller |
| 25 | + self.wallet = wallet_controller |
| 26 | + self.definitions = definition_controller |
| 27 | + |
| 28 | + # Fetch all credential exchange records |
| 29 | + async def get_records( |
| 30 | + self, |
| 31 | + connection_id: str = None, |
| 32 | + role: str = None, |
| 33 | + state: str = None, |
| 34 | + thread_id: str = None, |
| 35 | + ): |
| 36 | + params = {} |
| 37 | + if connection_id: |
| 38 | + params["connection_id"] = connection_id |
| 39 | + if role in ["issuer", "holder"]: |
| 40 | + params["role"] = role |
| 41 | + if state in [ |
| 42 | + "proposal-sent", |
| 43 | + "proposal-received", |
| 44 | + "offer-sent", |
| 45 | + "offer-received", |
| 46 | + "request-sent", |
| 47 | + "request-received", |
| 48 | + "credential-issued", |
| 49 | + "credential-received", |
| 50 | + "done", |
| 51 | + ]: |
| 52 | + params["state"] = state |
| 53 | + if thread_id: |
| 54 | + params["thread_id"] = thread_id |
| 55 | + return await self.admin_GET(f"{self.base_url}/records", params=params) |
| 56 | + |
| 57 | + async def get_record_by_id(self, cred_ex_id): |
| 58 | + return await self.admin_GET(f"{self.base_url}/records/{cred_ex_id}") |
| 59 | + |
| 60 | + async def create_credential(self, body): |
| 61 | + return await self.admin_POST(f"{self.base_url}/create", json_data=body) |
| 62 | + |
| 63 | + # Send holder a credential, automating the entire flow |
| 64 | + async def send_credential( |
| 65 | + self, |
| 66 | + connection_id, |
| 67 | + schema_id, |
| 68 | + cred_def_id, |
| 69 | + attributes, |
| 70 | + comment: str = "", |
| 71 | + auto_remove: bool = True, |
| 72 | + trace: bool = False, |
| 73 | + ): |
| 74 | + |
| 75 | + body = await self.create_credential_body( |
| 76 | + connection_id, |
| 77 | + schema_id, |
| 78 | + cred_def_id, |
| 79 | + attributes, |
| 80 | + comment, |
| 81 | + auto_remove, |
| 82 | + trace, |
| 83 | + ) |
| 84 | + return await self.admin_POST(f"{self.base_url}/send", json_data=body) |
| 85 | + |
| 86 | + # Send Issuer a credential proposal |
| 87 | + async def send_proposal( |
| 88 | + self, |
| 89 | + connection_id, |
| 90 | + schema_id, |
| 91 | + cred_def_id, |
| 92 | + attributes, |
| 93 | + comment: str = "", |
| 94 | + auto_remove: bool = True, |
| 95 | + trace: bool = False, |
| 96 | + ): |
| 97 | + |
| 98 | + body = await self.create_credential_body( |
| 99 | + connection_id, |
| 100 | + schema_id, |
| 101 | + cred_def_id, |
| 102 | + attributes, |
| 103 | + comment, |
| 104 | + auto_remove, |
| 105 | + trace, |
| 106 | + ) |
| 107 | + return await self.admin_POST(f"{self.base_url}/send-proposal", json_data=body) |
| 108 | + |
| 109 | + async def send_offer( |
| 110 | + self, |
| 111 | + connection_id, |
| 112 | + cred_def_id, |
| 113 | + attributes, |
| 114 | + comment: str = "", |
| 115 | + auto_issue: bool = True, |
| 116 | + auto_remove: bool = True, |
| 117 | + trace: bool = False, |
| 118 | + ): |
| 119 | + await self.connections.is_active(connection_id) |
| 120 | + offer_body = { |
| 121 | + "cred_def_id": cred_def_id, |
| 122 | + "auto_remove": auto_remove, |
| 123 | + "trace": trace, |
| 124 | + "comment": comment, |
| 125 | + "auto_issue": auto_issue, |
| 126 | + "credential_preview": {"@type": CRED_PREVIEW, "attributes": attributes}, |
| 127 | + "connection_id": connection_id, |
| 128 | + } |
| 129 | + return await self.admin_POST( |
| 130 | + f"{self.base_url}/send-offer", json_data=offer_body |
| 131 | + ) |
| 132 | + |
| 133 | + # Send holder a credential offer in reference to a proposal with preview |
| 134 | + async def send_offer_for_record(self, cred_ex_id): |
| 135 | + return await self.admin_POST(f"{self.base_url}/records/{cred_ex_id}/send-offer") |
| 136 | + |
| 137 | + # Send issuer a credential request |
| 138 | + async def send_request_for_record(self, cred_ex_id): |
| 139 | + return await self.admin_POST( |
| 140 | + f"{self.base_url}/records/{cred_ex_id}/send-request" |
| 141 | + ) |
| 142 | + |
| 143 | + # Send holder a credential |
| 144 | + async def issue_credential(self, cred_ex_id: str, comment: str = None): |
| 145 | + body = {} |
| 146 | + if comment: |
| 147 | + body = { |
| 148 | + "comment": comment, |
| 149 | + } |
| 150 | + return await self.admin_POST( |
| 151 | + f"{self.base_url}/records/{cred_ex_id}/issue", json_data=body |
| 152 | + ) |
| 153 | + |
| 154 | + # Store a received credential |
| 155 | + async def store_credential(self, cred_ex_id: str, credential_id: str = None): |
| 156 | + body = {} |
| 157 | + if credential_id: |
| 158 | + body["credential_id"] = credential_id |
| 159 | + return await self.admin_POST( |
| 160 | + f"{self.base_url}/records/{cred_ex_id}/store", json_data=body |
| 161 | + ) |
| 162 | + |
| 163 | + # Remove an existing credential exchange record |
| 164 | + async def remove_record(self, cred_ex_id): |
| 165 | + return await self.admin_DELETE(f"{self.base_url}/records/{cred_ex_id}") |
| 166 | + |
| 167 | + # Send a problem report for a credential exchange |
| 168 | + async def problem_report(self, cred_ex_id, explanation: str): |
| 169 | + body = {"explain_ltxt": explanation} |
| 170 | + |
| 171 | + return await self.admin_POST( |
| 172 | + f"{self.base_url}/records/{cred_ex_id}/problem-report", json_data=body |
| 173 | + ) |
| 174 | + |
| 175 | + # Used for both send and send-proposal bodies |
| 176 | + async def create_credential_body( |
| 177 | + self, |
| 178 | + connection_id, |
| 179 | + schema_id, |
| 180 | + cred_def_id, |
| 181 | + attributes, |
| 182 | + comment: str = "", |
| 183 | + auto_remove: bool = True, |
| 184 | + trace: bool = False, |
| 185 | + ): |
| 186 | + # raises error if connection not active |
| 187 | + await self.connections.is_active(connection_id) |
| 188 | + |
| 189 | + schema_details = get_schema_details(schema_id) |
| 190 | + |
| 191 | + issuer_did = extract_did(cred_def_id) |
| 192 | + |
| 193 | + body = { |
| 194 | + "issuer_did": issuer_did, |
| 195 | + "auto_remove": auto_remove, |
| 196 | + "credential_proposal": {"@type": CRED_PREVIEW, "attributes": attributes}, |
| 197 | + "connection_id": connection_id, |
| 198 | + "trace": trace, |
| 199 | + "comment": comment, |
| 200 | + "cred_def_id": cred_def_id, |
| 201 | + } |
| 202 | + |
| 203 | + credential_body = {**body, **schema_details} |
| 204 | + return credential_body |
0 commit comments