Skip to content

Commit

Permalink
refactor: minor format/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arminveres committed Nov 15, 2023
1 parent 2580d76 commit d9aca7e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 236 deletions.
133 changes: 32 additions & 101 deletions impl/src/agents/agent_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def __init__(
)
)
if anoncreds_legacy_revocation:
extra_args.append(
f"--anoncreds-legacy-revocation={anoncreds_legacy_revocation}"
)
extra_args.append(f"--anoncreds-legacy-revocation={anoncreds_legacy_revocation}")
super().__init__(
ident,
http_port,
Expand Down Expand Up @@ -216,17 +214,13 @@ async def handle_issue_credential(self, message):
cred_attrs = self.cred_attrs[message["credential_definition_id"]]
cred_preview = {
"@type": CRED_PREVIEW_TYPE,
"attributes": [
{"name": n, "value": v} for (n, v) in cred_attrs.items()
],
"attributes": [{"name": n, "value": v} for (n, v) in cred_attrs.items()],
}
try:
cred_ex_rec = await self.admin_POST(
f"/issue-credential/records/{credential_exchange_id}/issue",
{
"comment": (
f"Issuing credential, exchange {credential_exchange_id}"
),
"comment": (f"Issuing credential, exchange {credential_exchange_id}"),
"credential_preview": cred_preview,
},
)
Expand Down Expand Up @@ -267,9 +261,7 @@ async def handle_issue_credential_v2_0(self, message):
if self.log_level == LogLevel.DEBUG:
log_status("#15 After receiving credential offer, send credential request")
if message["by_format"]["cred_offer"].get("indy"):
await self.admin_POST(
f"/issue-credential-2.0/records/{cred_ex_id}/send-request"
)
await self.admin_POST(f"/issue-credential-2.0/records/{cred_ex_id}/send-request")
elif message["by_format"]["cred_offer"].get("ld_proof"):
holder_did = await self.admin_POST(
"/wallet/did/create",
Expand Down Expand Up @@ -362,9 +354,7 @@ async def handle_present_proof(self, message):
for referent in presentation_request["requested_attributes"]:
if referent in credentials_by_reft:
revealed[referent] = {
"cred_id": credentials_by_reft[referent]["cred_info"][
"referent"
],
"cred_id": credentials_by_reft[referent]["cred_info"]["referent"],
"revealed": revealed_flag,
}
revealed_flag = True
Expand All @@ -374,9 +364,7 @@ async def handle_present_proof(self, message):
for referent in presentation_request["requested_predicates"]:
if referent in credentials_by_reft:
predicates[referent] = {
"cred_id": credentials_by_reft[referent]["cred_info"][
"referent"
]
"cred_id": credentials_by_reft[referent]["cred_info"]["referent"]
}

if self.log_level == LogLevel.DEBUG:
Expand All @@ -390,10 +378,7 @@ async def handle_present_proof(self, message):
if self.log_level == LogLevel.DEBUG:
log_status("#26 Send the proof to X")
await self.admin_POST(
(
"/present-proof/records/"
f"{presentation_exchange_id}/send-presentation"
),
("/present-proof/records/" f"{presentation_exchange_id}/send-presentation"),
request,
)
except ClientError:
Expand Down Expand Up @@ -463,9 +448,7 @@ async def handle_present_proof_v2_0(self, message):
for referent in pres_request_indy["requested_attributes"]:
if referent in creds_by_reft:
revealed[referent] = {
"cred_id": creds_by_reft[referent]["cred_info"][
"referent"
],
"cred_id": creds_by_reft[referent]["cred_info"]["referent"],
"revealed": revealed_flag,
}
revealed_flag = True
Expand All @@ -475,9 +458,7 @@ async def handle_present_proof_v2_0(self, message):
for referent in pres_request_indy["requested_predicates"]:
if referent in creds_by_reft:
predicates[referent] = {
"cred_id": creds_by_reft[referent]["cred_info"][
"referent"
]
"cred_id": creds_by_reft[referent]["cred_info"]["referent"]
}

if self.log_level == LogLevel.DEBUG:
Expand Down Expand Up @@ -530,9 +511,7 @@ async def handle_present_proof_v2_0(self, message):
input_descriptor_schema_uri, record
):
record_id = record["record_id"]
dif_request["dif"]["record_ids"][
input_descriptor["id"]
] = [
dif_request["dif"]["record_ids"][input_descriptor["id"]] = [
record_id,
]
break
Expand Down Expand Up @@ -644,11 +623,8 @@ async def input_invitation(self, invite_details: dict, wait: bool = False):
log_msg("Waiting for connection...")
await self.detect_connection()

async def create_schema_and_cred_def(
self, schema_name, schema_attrs, revocation, version=None
):
async def create_schema_and_cred_def(self, schema_name, schema_attrs, revocation, version=None):
with log_timer("Publish schema/cred def duration:"):
log_status("#3/4 Create a new schema/cred def on the ledger")
if self.log_level == LogLevel.DEBUG:
log_status("#3/4 Create a new schema/cred def on the ledger")
if not version:
Expand All @@ -660,10 +636,7 @@ async def create_schema_and_cred_def(
random.randint(1, 101),
)
)
(
_,
cred_def_id,
) = await self.register_schema_and_creddef( # schema id
(_, cred_def_id,) = await self.register_schema_and_creddef( # schema id
schema_name,
version,
schema_attrs,
Expand All @@ -672,9 +645,7 @@ async def create_schema_and_cred_def(
)
return cred_def_id

def check_input_descriptor_record_id(
self, input_descriptor_schema_uri, record
) -> bool:
def check_input_descriptor_record_id(self, input_descriptor_schema_uri, record) -> bool:
result = False
for uri in input_descriptor_schema_uri:
for record_type in record["type"]:
Expand Down Expand Up @@ -808,9 +779,7 @@ async def initialize(
raise Exception("Endorser agent returns None :-(")

# set the endorser invite so the agent can auto-connect
self.agent.endorser_invite = (
self.endorser_agent.endorser_multi_invitation_url
)
self.agent.endorser_invite = self.endorser_agent.endorser_multi_invitation_url
self.agent.endorser_did = self.endorser_agent.endorser_public_did
else:
self.endorser_agent = None
Expand Down Expand Up @@ -848,25 +817,17 @@ async def initialize(
if self.mediation:
# we need to pre-connect the agent to its mediator
self.agent.log("Connect wallet to mediator ...")
if not await connect_wallet_to_mediator(
self.agent, self.mediator_agent
):
if not await connect_wallet_to_mediator(self.agent, self.mediator_agent):
raise Exception("Mediation setup FAILED :-(")
if self.endorser_agent:
self.agent.log("Connect wallet to endorser ...")
if not await connect_wallet_to_endorser(
self.agent, self.endorser_agent
):
if not await connect_wallet_to_endorser(self.agent, self.endorser_agent):
raise Exception("Endorser setup FAILED :-(")
if self.taa_accept:
await self.agent.taa_accept()

# if we are an author, create our public DID here ...
if (
self.endorser_role
and self.endorser_role == "author"
and self.endorser_agent
):
if self.endorser_role and self.endorser_role == "author" and self.endorser_agent:
if self.public_did and self.cred_type != CRED_FORMAT_JSON_LD:
new_did = await self.agent.admin_POST("/wallet/did/create")
self.agent.did = new_did["result"]["did"]
Expand All @@ -887,9 +848,7 @@ async def initialize(

if schema_name and schema_attrs:
# Create a schema/cred def
self.cred_def_id = await self.create_schema_and_cred_def(
schema_name, schema_attrs
)
self.cred_def_id = await self.create_schema_and_cred_def(schema_name, schema_attrs)

async def create_schema_and_cred_def(
self,
Expand Down Expand Up @@ -983,12 +942,8 @@ async def request_proof(self, proof_request, explicit_revoc_required: bool = Fal

if self.cred_type == CRED_FORMAT_INDY:
indy_proof_request = {
"name": proof_request["name"]
if "name" in proof_request
else "Proof of stuff",
"version": proof_request["version"]
if "version" in proof_request
else "1.0",
"name": proof_request["name"] if "name" in proof_request else "Proof of stuff",
"version": proof_request["version"] if "version" in proof_request else "1.0",
"requested_attributes": proof_request["requested_attributes"],
"requested_predicates": proof_request["requested_predicates"],
}
Expand Down Expand Up @@ -1188,9 +1143,7 @@ async def agency_admin_POST(self, path, data=None, text=False, params=None) -> d
text = True if the expected response is text, False if json data
params = any additional parameters to pass with the request
"""
return await self.agent.agency_admin_POST(
path, data=data, text=text, params=params
)
return await self.agent.agency_admin_POST(path, data=data, text=text, params=params)


def arg_parser(ident: str = None, port: int = 8020):
Expand All @@ -1199,9 +1152,7 @@ def arg_parser(ident: str = None, port: int = 8020):
"ident", if specified, refers to one of the standard demo personas - alice, faber, acme or performance.
"""
parser = argparse.ArgumentParser(
description="Runs a " + (ident or "aries") + " demo agent."
)
parser = argparse.ArgumentParser(description="Runs a " + (ident or "aries") + " demo agent.")
if not ident:
parser.add_argument(
"--ident",
Expand Down Expand Up @@ -1233,9 +1184,7 @@ def arg_parser(ident: str = None, port: int = 8020):
action="store_true",
help="Use DID-Exchange protocol for connections",
)
parser.add_argument(
"--revocation", action="store_true", help="Enable credential revocation"
)
parser.add_argument("--revocation", action="store_true", help="Enable credential revocation")
parser.add_argument(
"--anoncreds-legacy-revocation",
type=str,
Expand Down Expand Up @@ -1263,15 +1212,9 @@ def arg_parser(ident: str = None, port: int = 8020):
metavar=("<api>"),
help="API level (10 or 20 (default))",
)
parser.add_argument(
"--timing", action="store_true", help="Enable timing information"
)
parser.add_argument(
"--multitenant", action="store_true", help="Enable multitenancy options"
)
parser.add_argument(
"--mediation", action="store_true", help="Enable mediation functionality"
)
parser.add_argument("--timing", action="store_true", help="Enable timing information")
parser.add_argument("--multitenant", action="store_true", help="Enable multitenancy options")
parser.add_argument("--mediation", action="store_true", help="Enable mediation functionality")
parser.add_argument(
"--multi-ledger",
action="store_true",
Expand Down Expand Up @@ -1334,16 +1277,12 @@ async def create_agent_with_args_list(in_args: list):

async def create_agent_with_args(args, ident: str = None):
if ("did_exchange" in args and args.did_exchange) and args.mediation:
raise Exception(
"DID-Exchange connection protocol is not (yet) compatible with mediation"
)
raise Exception("DID-Exchange connection protocol is not (yet) compatible with mediation")

check_requires(args)

if "revocation" in args and args.revocation:
tails_server_base_url = args.tails_server_base_url or os.getenv(
"PUBLIC_TAILS_URL"
)
tails_server_base_url = args.tails_server_base_url or os.getenv("PUBLIC_TAILS_URL")
else:
tails_server_base_url = None

Expand All @@ -1361,9 +1300,7 @@ async def create_agent_with_args(args, ident: str = None):
)

if ("revocation" in args and args.revocation) and not tails_server_base_url:
raise Exception(
"If revocation is enabled, --tails-server-base-url must be provided"
)
raise Exception("If revocation is enabled, --tails-server-base-url must be provided")

multi_ledger_config_path = None
genesis = None
Expand Down Expand Up @@ -1546,19 +1483,15 @@ async def test_main(
args = parser.parse_args()

if args.did_exchange and args.mediation:
raise Exception(
"DID-Exchange connection protocol is not (yet) compatible with mediation"
)
raise Exception("DID-Exchange connection protocol is not (yet) compatible with mediation")

ENABLE_PYDEVD_PYCHARM = os.getenv("ENABLE_PYDEVD_PYCHARM", "").lower()
ENABLE_PYDEVD_PYCHARM = ENABLE_PYDEVD_PYCHARM and ENABLE_PYDEVD_PYCHARM not in (
"false",
"0",
)
PYDEVD_PYCHARM_HOST = os.getenv("PYDEVD_PYCHARM_HOST", "localhost")
PYDEVD_PYCHARM_CONTROLLER_PORT = int(
os.getenv("PYDEVD_PYCHARM_CONTROLLER_PORT", 5001)
)
PYDEVD_PYCHARM_CONTROLLER_PORT = int(os.getenv("PYDEVD_PYCHARM_CONTROLLER_PORT", 5001))

if ENABLE_PYDEVD_PYCHARM:
try:
Expand All @@ -1583,9 +1516,7 @@ async def test_main(
tails_server_base_url = args.tails_server_base_url or os.getenv("PUBLIC_TAILS_URL")

if args.revocation and not tails_server_base_url:
raise Exception(
"If revocation is enabled, --tails-server-base-url must be provided"
)
raise Exception("If revocation is enabled, --tails-server-base-url must be provided")

try:
asyncio.get_event_loop().run_until_complete(
Expand Down
31 changes: 8 additions & 23 deletions impl/src/agents/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from support.agent import DEFAULT_EXTERNAL_HOST
from support.utils import log_msg, prompt, prompt_loop, log_json, LogLevel
from support.perf_analysis import log_time_to_file

# Our custom software to be updated
import shady_stuff
Expand Down Expand Up @@ -57,34 +58,18 @@ async def handle_revocation_notification(self, message):
"""
Handles incoming revocation, perpetrated by the auditor and issued by Issuer
"""

log_time_to_file(
"revocation", f"REV_RECEIVED: time: {time.perf_counter_ns()}, node: {self.ident}\n"
)

if self.log_level == LogLevel.DEBUG:
self.log("Received revocation notification message:")
self.log_json(message)
message["comment"] = json.loads(message["comment"])
diff = await self.get_update(message)
await self.notify_admin_of_update(diff)

async def handle_issue_credential_v2_0_indy(self, message):
rev_reg_id = message.get("rev_reg_id")
cred_rev_id = message.get("cred_rev_id")
cred_id_stored = message.get("cred_id_stored")

if cred_id_stored:
cred_id = message["cred_id_stored"]
log_status(f"#18.1 Stored credential {cred_id} in wallet")
self.cred_id = cred_id
cred = await self.admin_GET(f"/credential/{cred_id}")
log_json(cred, label="Credential details:")
self.log("credential_id", cred_id)
self.log("cred_def_id", cred["cred_def_id"])
self.log("schema_id", cred["schema_id"])
# track last successfully received credential
self.last_credential_received = cred

if rev_reg_id and cred_rev_id:
self.log(f"Revocation registry ID: {rev_reg_id}")
self.log(f"Credential revocation ID: {cred_rev_id}")

# =============================================================================================
# Additional methods
# =============================================================================================
Expand Down Expand Up @@ -221,8 +206,8 @@ async def create_node_agent(args):
with open(cache_path, "wb") as cache:
cache.write(bytes(_agent.seed, "utf-8"))
await agent_container.initialize(the_agent=_agent)
with open('.agent_cache/mass_onboarding', "ab") as cache:
output=f"name: {agent_container.ident}\ndid: {agent_container.agent.did}\n"
with open(".agent_cache/mass_onboarding", "ab") as cache:
output = f"name: {agent_container.ident}\ndid: {agent_container.agent.did}\n"
cache.write(bytes(output, "utf-8"))
return agent_container

Expand Down
Loading

0 comments on commit d9aca7e

Please sign in to comment.