Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anoncred support #759

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 65 additions & 52 deletions aries-backchannels/acapy/acapy_backchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,21 @@
import signal
import subprocess
import sys

from timeit import default_timer
from typing_extensions import Literal
from typing import Any, Dict, List, Mapping, Optional, Tuple, Union

from aiohttp import (
web,
ClientSession,
ClientRequest,
ClientError,
ClientTimeout,
)

from python.agent_backchannel import (
AgentBackchannel,
AgentPorts,
BackchannelCommand,
default_genesis_txns,
RUN_MODE,
START_TIMEOUT,
)
from python.utils import flatten, log_msg, output_reader, prompt_loop
from python.storage import (
get_resource,
push_resource,
pop_resource,
pop_resource_latest,
)

from acapy.routes.agent_routes import routes as agent_routes
from acapy.routes.mediation_routes import (
routes as mediation_routes,
get_mediation_record_by_connection_id,
)
from acapy.routes.mediation_routes import get_mediation_record_by_connection_id
from acapy.routes.mediation_routes import routes as mediation_routes
from aiohttp import (ClientError, ClientRequest, ClientSession, ClientTimeout,
web)
from python.agent_backchannel import (RUN_MODE, START_TIMEOUT,
AgentBackchannel, AgentPorts,
BackchannelCommand, default_genesis_txns)
from python.storage import (get_resource, pop_resource, pop_resource_latest,
push_resource)
from python.utils import flatten, log_msg, output_reader, prompt_loop
from typing_extensions import Literal

# from helpers.jsonmapper.json_mapper import JsonMapper

Expand Down Expand Up @@ -630,7 +611,7 @@ async def make_agent_POST_request(
agent_operation += f"?mediation_id={mediation_id}"

(resp_status, resp_text) = await self.admin_POST(
agent_operation, data=command.data
agent_operation, data=data
)
if resp_status == 200:
resp_text = self.agent_state_translation(command.topic, resp_text)
Expand All @@ -651,10 +632,10 @@ async def make_agent_POST_request(
if not await self.expected_agent_state(
f"/connections/{connection_id}", "request", wait_time=60.0
):
raise Exception(f"Expected state request but not received")
raise Exception("Expected state request but not received")

agent_operation = f"/connections/{connection_id}/{operation}"
log_msg("POST Request: ", agent_operation, command.data)
log_msg("POST Request: ", agent_operation, data)

if self.auto_accept_requests and operation == "accept-request":
resp_status = 200
Expand All @@ -676,14 +657,22 @@ async def make_agent_POST_request(

elif command.topic == "schema":
# POST operation is to create a new schema
agent_operation = "/schemas"
log_msg(agent_operation, command.data)
if command.anoncreds:
schema_name = data['schema'].get("name")
schema_version = data['schema'].get("version")
schema_get_endpoint = '/anoncreds/schemas'
schema_post_endpoint = '/anoncreds/schema'
else:
schema_name = data.get("schema_name")
schema_version = data.get("schema_version")
schema_get_endpoint = '/schemas/created'
schema_post_endpoint = '/schemas'

# Check if schema id already exists
schema_name = command.data.get("schema_name")
schema_version = command.data.get("schema_version")
log_msg(schema_post_endpoint, data)

(resp_status, resp_text) = await self.admin_GET(
"/schemas/created",
schema_get_endpoint,
params={"schema_version": schema_version, "schema_name": schema_name},
)
resp_json = json.loads(resp_text)
Expand All @@ -692,7 +681,7 @@ async def make_agent_POST_request(
return (200, json.dumps({"schema_id": schema_id}))

(resp_status, resp_text) = await self.admin_POST(
agent_operation, command.data
schema_post_endpoint, data
)

log_msg(resp_status, resp_text)
Expand All @@ -701,14 +690,23 @@ async def make_agent_POST_request(

elif command.topic == "credential-definition":
# POST operation is to create a new cred def
if command.anoncreds:
schema_id = data['credential_definition'].get("schemaId")
tag = data['credential_definition'].get("tag")
cred_defs_get_endpoint = '/anoncreds/credential-definitions'
cred_defs_post_endpoint = '/anoncreds/credential-definition'
else:
tag = data.get("tag")
schema_id = data.get("schema_id")
cred_defs_get_endpoint = '/credential-definitions/created'
cred_defs_post_endpoint = '/credential-definitions'

agent_operation = "/credential-definitions"
log_msg(agent_operation, command.data)
log_msg(cred_defs_post_endpoint, data)

# Check if credential definition id already exists
schema_id = command.data.get("schema_id")
tag = command.data.get("tag")
(resp_status, resp_text) = await self.admin_GET(
"/credential-definitions/created",
cred_defs_get_endpoint,
params={
"schema_id": schema_id,
},
Expand All @@ -725,7 +723,7 @@ async def make_agent_POST_request(
)

(resp_status, resp_text) = await self.admin_POST(
agent_operation, command.data
cred_defs_post_endpoint, data
)

log_msg(resp_status, resp_text)
Expand Down Expand Up @@ -788,7 +786,7 @@ async def make_agent_POST_request(
wait_time=60.0,
):
raise Exception(
f"Expected state request-received but not received"
"Expected state request-received but not received"
)

# Make Special provisions for revoke since it is passing multiple query params not just one id.
Expand Down Expand Up @@ -928,7 +926,7 @@ async def make_agent_POST_request(
wait_time=60.0,
):
raise Exception(
f"Expected state presentation-received but not received"
"Expected state presentation-received but not received"
)

else:
Expand Down Expand Up @@ -1452,7 +1450,10 @@ async def make_agent_GET_request(

elif command.topic == "schema":
schema_id = record_id
agent_operation = f"/schemas/{schema_id}"
if command.anoncreds:
agent_operation = f"/anoncreds/schema/{schema_id}"
else:
agent_operation = f"/schemas/{schema_id}"

(resp_status, resp_text) = await self.admin_GET(agent_operation)
if resp_status != 200:
Expand All @@ -1461,12 +1462,20 @@ async def make_agent_GET_request(
resp_json = json.loads(resp_text)
schema = resp_json["schema"]

# If anoncreds, add the id to the schema to use existing framework
if command.anoncreds:
schema["id"] = resp_json["schema_id"]

resp_text = json.dumps(schema)
return (resp_status, resp_text)

elif command.topic == "credential-definition":
cred_def_id = record_id
agent_operation = f"/credential-definitions/{cred_def_id}"

if command.anoncreds:
agent_operation = f"/anoncreds/credential-definition/{cred_def_id}"
else:
agent_operation = f"/credential-definitions/{cred_def_id}"

(resp_status, resp_text) = await self.admin_GET(agent_operation)
if resp_status != 200:
Expand All @@ -1475,6 +1484,10 @@ async def make_agent_GET_request(
resp_json = json.loads(resp_text)
credential_definition = resp_json["credential_definition"]

# If anoncreds, add the id to the credential definition to use existing framework
if command.anoncreds:
credential_definition["id"] = resp_json["credential_definition_id"]

resp_text = json.dumps(credential_definition)
return (resp_status, resp_text)

Expand Down Expand Up @@ -1836,8 +1849,8 @@ async def start_process_with_extra_args(
agent_args = self.get_process_args(bin_path) + args

# start agent sub-process
self.log(f"Starting agent sub-process ...")
self.log(f"agent starting with params: ")
self.log("Starting agent sub-process ...")
self.log("agent starting with params: ")
self.log(agent_args)
self.log("and environment:")
self.log(my_env)
Expand All @@ -1863,8 +1876,8 @@ async def start_process(
)

# start agent sub-process
self.log(f"Starting agent sub-process ...")
self.log(f"agent starting with params: ")
self.log("Starting agent sub-process ...")
self.log("agent starting with params: ")
self.log(agent_args)
loop = asyncio.get_event_loop()
self.proc = await loop.run_in_executor(
Expand Down
18 changes: 12 additions & 6 deletions aries-backchannels/python/agent_backchannel.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import json
import logging
import os
import traceback
import random

from typing_extensions import TypedDict, Literal
from typing import Any, Optional, Tuple
import traceback
from dataclasses import dataclass
from typing import Any, Optional, Tuple

from aiohttp import web, ClientSession
from aiohttp.typedefs import Handler
import ptvsd
from aiohttp import ClientSession, web
from aiohttp.typedefs import Handler
from typing_extensions import Literal, TypedDict

from .utils import log_msg

Expand Down Expand Up @@ -59,6 +58,7 @@ class BackchannelCommand:
operation: Optional[str]
record_id: Optional[str]
data: Optional[Any]
anoncreds: Optional[bool] = False


async def default_genesis_txns():
Expand Down Expand Up @@ -225,6 +225,11 @@ async def parse_request(self, request: web.Request) -> BackchannelCommand:
record_id = request.match_info.get("id", None)
operation = request.match_info.get("operation", None)
topic = request.match_info.get("topic", None)
anoncreds = request.query.get("anoncreds", False)

if anoncreds == 'True':
anoncreds = True

method = request.method

if not topic:
Expand All @@ -248,6 +253,7 @@ async def parse_request(self, request: web.Request) -> BackchannelCommand:
topic=topic,
method=method,
data=data,
anoncreds=anoncreds
)

def not_found_response(self, data: Any):
Expand Down
26 changes: 13 additions & 13 deletions aries-test-harness/agent_backchannel_client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import asyncio
from aiohttp import (
web,
ClientSession,
ClientRequest,
ClientResponse,
ClientError,
ClientTimeout,
)
import json
import os.path
from time import sleep

from aiohttp import ClientSession

######################################################################
# coroutine utilities
######################################################################
Expand Down Expand Up @@ -70,26 +64,30 @@ def sorted_payload(val, level=0):
with open('/aries-test-harness/logs/request.log', 'a') as fout:
print(f'Req: {method} {agent_url} {sorted_payload(payload)}', file=fout)
print(f'Res: {resp_status} {sorted_payload(resp_text)}', file=fout)
print(f'-----', file=fout)
print('-----', file=fout)

def agent_backchannel_GET(url, topic, operation=None, id=None) -> (int, str):
def agent_backchannel_GET(url, topic, operation=None, id=None, anoncreds=False) -> (int, str):
agent_url = url + topic + "/"
params = {}
if operation:
agent_url = agent_url + operation + "/"
if id:
agent_url = agent_url + id
if (anoncreds):
params["anoncreds"] = 'True'
(resp_status, resp_text) = run_coroutine_with_kwargs(
make_agent_backchannel_request, "GET", agent_url
make_agent_backchannel_request, "GET", agent_url, params=params
)
request_log("GET", agent_url, resp_status, resp_text)
return (resp_status, resp_text)


def agent_backchannel_POST(
url, topic, operation=None, id=None, data=None
url, topic, operation=None, id=None, data=None, anoncreds=False
) -> (int, str):
agent_url = url + topic + "/"
payload = {}
params = {}
if data:
payload["data"] = data
if operation:
Expand All @@ -99,8 +97,10 @@ def agent_backchannel_POST(
payload["cred_ex_id"] = id
else:
payload["id"] = id
if anoncreds:
params["anoncreds"] = 'True'
(resp_status, resp_text) = run_coroutine_with_kwargs(
make_agent_backchannel_request, "POST", agent_url, data=payload
make_agent_backchannel_request, "POST", agent_url, data=payload, params=params
)
request_log("POST", agent_url, resp_status, resp_text, payload)
return (resp_status, resp_text)
Expand Down
Loading