Skip to content

Commit

Permalink
add methods to get and set the credential tagging policy for a cred d…
Browse files Browse the repository at this point in the history
…ef ID

Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
  • Loading branch information
andrewwhitehead committed Sep 17, 2019
1 parent b4275b1 commit 55cbf8d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 9 deletions.
12 changes: 6 additions & 6 deletions aries_cloudagent/ledger/indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ async def fetch_schema(self, schema_id: str):

return parsed_response

async def send_credential_definition(self, schema_id: str, tag: str = "default"):
async def send_credential_definition(
self, schema_id: str, tag: str = "default"
):
"""
Send credential definition to ledger and store relevant key matter in wallet.
Expand Down Expand Up @@ -430,7 +432,7 @@ async def send_credential_definition(self, schema_id: str, tag: str = "default")
try:
cred_def_id = re.search(
r"\w*:3:CL:(([1-9][0-9]*)|(.{21,22}:2:.+:[0-9.]+)):\w*",
error.message
error.message,
).group(0)
return cred_def_id
# The regex search failed so let the error bubble up
Expand Down Expand Up @@ -518,9 +520,7 @@ async def credential_definition_id2schema_id(self, credential_definition_id):

# get txn by sequence number, retrieve schema identifier components
request_json = await indy.ledger.build_get_txn_request(
None,
None,
seq_no=seq_no
None, None, seq_no=seq_no
)
response = json.loads(await self._submit(request_json))

Expand All @@ -530,7 +530,7 @@ async def credential_definition_id2schema_id(self, credential_definition_id):
(origin_did, name, version) = (
data_txn["metadata"]["from"],
data_txn["data"]["data"]["name"],
data_txn["data"]["data"]["version"]
data_txn["data"]["data"]["version"],
)
return f"{origin_did}:2:{name}:{version}"

Expand Down
35 changes: 35 additions & 0 deletions aries_cloudagent/wallet/indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,38 @@ async def unpack_message(self, enc_message: bytes) -> (str, str, str):
to_verkey = unpacked.get("recipient_verkey", None)
from_verkey = unpacked.get("sender_verkey", None)
return message, from_verkey, to_verkey

async def get_credential_definition_tag_policy(self, credential_definition_id: str):
"""Return the tag policy for a given credential definition ID."""
policy_json = await indy.anoncreds.prover_get_credential_attr_tag_policy(
self.handle, credential_definition_id
)
return json.loads(policy_json) if policy_json else None

async def set_credential_definition_tag_policy(
self,
credential_definition_id: str,
taggables: Sequence[str] = None,
retroactive: bool = True,
):
"""
Set the tag policy for a given credential definition ID.
Args:
credential_definition_id: The ID of the credential definition
taggables: A sequence of string values representing attribute names
retroactive: Whether to apply the policy to previously-stored credentials
"""

if taggables is not None:
self.logger.info(
"Set tagging policy: %s %s", credential_definition_id, taggables
)
await indy.anoncreds.prover_set_credential_attr_tag_policy(
self.handle,
credential_definition_id,
json.dumps(taggables),
retroactive,
)
else:
self.logger.info("Clear tagging policy: %s", credential_definition_id)
73 changes: 70 additions & 3 deletions aries_cloudagent/wallet/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Wallet admin routes."""

from aiohttp import web
from aiohttp_apispec import docs, response_schema
from aiohttp_apispec import docs, request_schema, response_schema

from marshmallow import fields, Schema

Expand Down Expand Up @@ -31,6 +31,18 @@ class DIDListSchema(Schema):
results = fields.List(fields.Nested(DIDSchema()))


class GetTagPolicyResultSchema(Schema):
"""Result schema for tagging policy get request."""

taggables = fields.List(fields.Str())


class SetTagPolicyRequestSchema(Schema):
"""Request schema for tagging policy set request."""

taggables = fields.List(fields.Str())


def format_did_info(info: DIDInfo):
"""Serialize a DIDInfo object."""
if info:
Expand Down Expand Up @@ -127,7 +139,7 @@ async def wallet_create_did(request: web.BaseRequest):
request: aiohttp request object
Returns:
The DID list response
The DID info
"""
context = request.app["request_context"]
Expand All @@ -148,7 +160,7 @@ async def wallet_get_public_did(request: web.BaseRequest):
request: aiohttp request object
Returns:
The DID list response
The DID info
"""
context = request.app["request_context"]
Expand Down Expand Up @@ -202,6 +214,59 @@ async def wallet_set_public_did(request: web.BaseRequest):
return web.json_response({"result": format_did_info(info)})


@docs(tags=["wallet"], summary="Get the tagging policy for a credential definition")
@response_schema(GetTagPolicyResultSchema())
async def wallet_get_tagging_policy(request: web.BaseRequest):
"""
Request handler for getting the tag policy associated with a cred def.
Args:
request: aiohttp request object
Returns:
A JSON object containing the tagging policy
"""
context = request.app["request_context"]

credential_definition_id = request.match_info["id"]

wallet: BaseWallet = await context.inject(BaseWallet, required=False)
if not wallet or wallet.WALLET_TYPE != "indy":
raise web.HTTPForbidden()
result = await wallet.get_credential_definition_tag_policy(credential_definition_id)
return web.json_response({"taggables": result})


@docs(tags=["wallet"], summary="Set the tagging policy for a credential definition")
@request_schema(SetTagPolicyRequestSchema())
async def wallet_set_tagging_policy(request: web.BaseRequest):
"""
Request handler for setting the tag policy associated with a cred def.
Args:
request: aiohttp request object
Returns:
An empty JSON response
"""
context = request.app["request_context"]

credential_definition_id = request.match_info["id"]

body = await request.json()
taggables = body.get("taggables")

wallet: BaseWallet = await context.inject(BaseWallet, required=False)
if not wallet or wallet.WALLET_TYPE != "indy":
raise web.HTTPForbidden()
await wallet.set_credential_definition_tag_policy(
credential_definition_id, taggables
)
return web.json_response({})


async def register(app: web.Application):
"""Register routes."""

Expand All @@ -211,5 +276,7 @@ async def register(app: web.Application):
web.post("/wallet/did/create", wallet_create_did),
web.get("/wallet/did/public", wallet_get_public_did),
web.post("/wallet/did/public", wallet_set_public_did),
web.get("/wallet/tag-policy/{id}", wallet_get_tagging_policy),
web.post("/wallet/tag-policy/{id}", wallet_set_tagging_policy),
]
)

0 comments on commit 55cbf8d

Please sign in to comment.