Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arminveres committed Nov 15, 2023
1 parent 64d6e12 commit f9026bc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
2 changes: 2 additions & 0 deletions impl/src/agents/agent_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def __init__(
self.last_proof_received = None

async def detect_connection(self):
self._connection_ready = asyncio.Future()
log_msg("Waiting for connection...")
await self._connection_ready
self._connection_ready = None

Expand Down
130 changes: 89 additions & 41 deletions impl/src/agents/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PERF_LOG = "./.agent_cache/time_log"


def log_time_to_file(text):
async def log_time_to_file(text):
with open(PERF_LOG, mode="a", encoding="utf-8") as perf_log:
perf_log.write(text)

Expand Down Expand Up @@ -146,8 +146,8 @@ async def handle_notify_vulnerability(self, message):
# mark devices to be revoked
for device in self.db_client.db_keys[vuln_db_name]:
db_result = await self.db_client.query_key(vuln_db_name, device)
log_json(db_result)
log_json(vulnerability)
# log_json(db_result)
# log_json(vulnerability)

for component_key, component_value in db_result["components"].items():
for (
Expand Down Expand Up @@ -213,7 +213,7 @@ async def handle_node_updated(self, message):

await self.issue_credential(node_did, node_name, node_cred, DB_NAME)
await self.db_client.record_key(DB_NAME, node_name, db_entry)
log_time_to_file(f"UPDATE: time: {time.perf_counter_ns()}, node: {node_name}\n")
await log_time_to_file(f"UPDATE: time: {time.perf_counter_ns()}, node: {node_name}\n")

# =============================================================================================
# Additional methods
Expand All @@ -228,23 +228,18 @@ async def __set_connection_id(self, db_name: str, node_name: str):
recipient_key = await self.send_invitation(node_did)
# we set the recipient key for later identification
self.db_client.db_keys[db_name][node_name]["recipient_key"] = recipient_key

self._connection_ready = asyncio.Future()
log_msg("Waiting for connection...")
# wait for conneciton
await self.detect_connection()

# Set the connection id for each controller
response = await self.admin_GET("/connections")
log_json(response)
# log_json(response)
for conn in response["results"]:
if conn["invitation_key"] == recipient_key:
conn_id = conn["connection_id"]
self.db_client.db_keys[db_name][node_name]["connection_id"] = conn_id
# remove recipient/invitation key
self.db_client.db_keys[db_name][node_name].pop("recipient_key")

self.reset_connection()

return conn_id

async def revoke_credential(
Expand All @@ -258,7 +253,6 @@ async def revoke_credential(
Revoke a credentials and publish it.
"""
response = await self.db_client.query_key(db_name, node_name)

cred_ex_id = response.get("cred_ex_id")

# update database with removed credential id
Expand All @@ -270,36 +264,71 @@ async def revoke_credential(
if cred_ex_id is None or cred_ex_id == "":
return

try:
conn_id = await self.__set_connection_id(db_name, node_name)
await self.admin_POST(
"/revocation/revoke",
{
"cred_ex_id": cred_ex_id,
"publish": True,
"connection_id": conn_id,
"comment": json.dumps(revocation_reason),
},
)
except:
# TODO: (aver) implement mechanism to notify offline devices.
self.log(f"Certificate revoked for {node_name} but it was offline")
await self.admin_POST(
"/revocation/revoke",
{
"cred_ex_id": cred_ex_id,
"publish": False,
"comment": json.dumps(revocation_reason),
},
)
log_time_to_file(f"REVOCATION: time: {time.perf_counter_ns()}, node: {node_name}\n")
# try:
# conn_id = await self.__set_connection_id(db_name, node_name)
# await self.admin_POST(
# "/revocation/revoke",
# {
# "cred_ex_id": cred_ex_id,
# "publish": True,
# "connection_id": conn_id,
# "comment": json.dumps(revocation_reason),
# },
# )
# except Exception as e:
# self.log(f"\n\nCaptured Exception: {e}\n\n")
# # TODO: (aver) implement mechanism to notify offline devices.
# self.log(f"Certificate revoked for {node_name} but it was offline")
# await self.admin_POST(
# "/revocation/revoke",
# {
# "cred_ex_id": cred_ex_id,
# "publish": False,
# "comment": json.dumps(revocation_reason),
# },
# )
# conn_id = await self.__set_connection_id(db_name, node_name)

node_did = self.db_client.db_keys[db_name][node_name].get("controller_did")
if node_did is None:
response = await self.db_client.query_key(db_name, node_name)
self.db_client.db_keys[db_name][node_name].update(response)
node_did = self.db_client.db_keys[db_name][node_name].get("controller_did")

recipient_key = await self.send_invitation(node_did)
# we set the recipient key for later identification
self.db_client.db_keys[db_name][node_name]["recipient_key"] = recipient_key
# wait for conneciton
await self.detect_connection()
# Set the connection id for each controller
response = await self.admin_GET("/connections")
# log_json(response)
for conn in response["results"]:
if conn["invitation_key"] == recipient_key:
conn_id = conn["connection_id"]
self.db_client.db_keys[db_name][node_name]["connection_id"] = conn_id
# remove recipient/invitation key
self.db_client.db_keys[db_name][node_name].pop("recipient_key")
self.reset_connection()

await self.admin_POST(
"/revocation/revoke",
{
"cred_ex_id": cred_ex_id,
"publish": True,
"connection_id": conn_id,
"comment": json.dumps(revocation_reason),
},
)

await log_time_to_file(f"REVOCATION: time: {time.perf_counter_ns()}, node: {node_name}\n")

async def issue_credential(
self,
node_did: str,
node_name: str,
node_cred: dict,
domain: str,
db_name: str,
):
"""
Issue a predetermined credential to a node
Expand All @@ -309,7 +338,28 @@ async def issue_credential(
node_cred: credential to be issued
domain: databse name where it will be stored
"""
conn_id = await self.__set_connection_id(domain, node_name)
# conn_id = await self.__set_connection_id(domain, node_name)
node_did = self.db_client.db_keys[db_name][node_name].get("controller_did")
if node_did is None:
response = await self.db_client.query_key(db_name, node_name)
self.db_client.db_keys[db_name][node_name].update(response)
node_did = self.db_client.db_keys[db_name][node_name].get("controller_did")

recipient_key = await self.send_invitation(node_did)
# we set the recipient key for later identification
self.db_client.db_keys[db_name][node_name]["recipient_key"] = recipient_key
# wait for conneciton
await self.detect_connection()
# Set the connection id for each controller
response = await self.admin_GET("/connections")
# log_json(response)
for conn in response["results"]:
if conn["invitation_key"] == recipient_key:
conn_id = conn["connection_id"]
self.db_client.db_keys[db_name][node_name]["connection_id"] = conn_id
# remove recipient/invitation key
self.db_client.db_keys[db_name][node_name].pop("recipient_key")
self.reset_connection()

log_status(f"# Issuing credential offer to {node_name}")
self.cred_attrs[self.cred_def_id] = node_cred
Expand All @@ -325,7 +375,7 @@ async def issue_credential(
"credential_preview": cred_preview,
"filter": {"indy": {"cred_def_id": self.cred_def_id}},
}
response = await self.admin_POST("/issue-credential-2.0/send-offer", offer_request)
_ = await self.admin_POST("/issue-credential-2.0/send-offer", offer_request)

async def onboard_node(self, domain: str, node_name: str, node_did: str):
"""
Expand Down Expand Up @@ -517,9 +567,7 @@ def get_prompt():
prompt_options = add_option(
prompt_options, "onboard", " [3]: Onboard node with public DID\n"
)
prompt_options = add_option(
prompt_options, "mass_onboard", " [4]: Onboard fleet with public DID\n"
)
prompt_options = add_option(prompt_options, "mass_onboard", " [4]: Mass Onboard fleet\n")

async for option in prompt_loop(get_prompt):
if option is not None:
Expand Down

0 comments on commit f9026bc

Please sign in to comment.