Skip to content

Commit

Permalink
Merge pull request openwallet-foundation#83 from cjhowland/fix/attrib…
Browse files Browse the repository at this point in the history
…ute-retrieval

Fix/attribute retrieval
  • Loading branch information
dbluhm authored Sep 8, 2022
2 parents 6f2bbea + 1e0d23f commit 45f8df1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
14 changes: 8 additions & 6 deletions aries_cloudagent/protocols/issue_credential/v2_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,14 @@ async def store_credential(
await cred_format.handler(self.profile).store_credential(
cred_ex_record, cred_id
)
async with self.profile.session() as session:
await AttachmentDataRecord.save_attachments(
session=session,
supplements=supplements,
attachments=attachments,
)
if supplements:
async with self.profile.session() as session:
await AttachmentDataRecord.save_attachments(
session=session,
supplements=supplements,
attachments=attachments,
cred_id=cred_id,
)
# TODO: if storing multiple credentials we can't reuse the same id
cred_id = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

EXAMPLE_DATA = b"Hello World!"
EXAMPLE_LINK = "hl:zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e:zuh8iaLobXC8g9tfma1CSTtYBakXeSTkHrYA5hmD4F7dCLw8XYwZ1GWyJ3zwF"
EXAMPLE_DATA_SHA = bytes.fromhex("7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069")
EXAMPLE_DATA_SHA = bytes.fromhex(
"7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069"
)


def test_example_link():
Expand All @@ -25,12 +27,14 @@ def test_serialize_metadata():

assert hashlink._serialize_metadata() == EXAMPLE_LINK.split(":")[2]


def test_deserialize_data():

data = EXAMPLE_LINK.split(":")[1]

assert Hashlink._deserialize_data(data) == EXAMPLE_DATA_SHA


def test_verify_link():

assert Hashlink.verify(EXAMPLE_LINK, EXAMPLE_DATA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ def get_format_data(
)

async def create_bound_request(
self,
pres_ex_record: V20PresExRecord,
request_data: dict = None,
self, pres_ex_record: V20PresExRecord, request_data: dict = None
) -> Tuple[V20PresFormat, AttachDecorator]:
"""
Create a presentation request bound to a proposal.
Expand Down Expand Up @@ -129,9 +127,7 @@ async def create_bound_request(
return self.get_format_data(PRES_20_REQUEST, indy_proof_request)

async def create_pres(
self,
pres_ex_record: V20PresExRecord,
request_data: dict = None,
self, pres_ex_record: V20PresExRecord, request_data: dict = None
) -> Tuple[V20PresFormat, AttachDecorator]:
"""Create a presentation."""
requested_credentials = {}
Expand All @@ -141,12 +137,10 @@ async def create_pres(
indy_proof_request = proof_request.attachment(
IndyPresExchangeHandler.format
)
requested_credentials = (
await indy_proof_req_preview2indy_requested_creds(
indy_proof_request,
preview=None,
holder=self._profile.inject(IndyHolder),
)
requested_credentials = await indy_proof_req_preview2indy_requested_creds(
indy_proof_request,
preview=None,
holder=self._profile.inject(IndyHolder),
)
except ValueError as err:
LOGGER.warning(f"{err}")
Expand All @@ -163,8 +157,7 @@ async def create_pres(
}
indy_handler = IndyPresExchHandler(self._profile)
indy_proof = await indy_handler.return_presentation(
pres_ex_record=pres_ex_record,
requested_credentials=requested_credentials,
pres_ex_record=pres_ex_record, requested_credentials=requested_credentials
)
return self.get_format_data(PRES_20, indy_proof)

Expand Down
13 changes: 6 additions & 7 deletions aries_cloudagent/wallet/models/attachment_data_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ...messaging.valid import UUIDFour
from ...protocols.issue_credential.v2_0.messages.inner.supplement import (
Supplement,
SupplementAttribute,
SupplementSchema,
)
from ...messaging.decorators.attach_decorator import (
Expand All @@ -34,7 +35,7 @@ def __init__(
supplement: Supplement = None,
attachment: AttachDecoratorData = None,
cred_id: str = None,
attribute: str = None,
attribute: Sequence[SupplementAttribute] = None,
):
super().__init__()
self.attachment_id = attachment_id
Expand Down Expand Up @@ -69,34 +70,32 @@ def match_by_attachment_id(
supplements: Sequence[Supplement],
attachments: Sequence[AttachDecorator],
cred_id: str,
attribute: str,
):
"""Match supplement and attachment by attachment_id and store in
AttachmentDataRecord."""

ats: dict[str, AttachDecoratorData] = AttachmentDataRecord.attachment_lookup(
attachments
)

return [
AttachmentDataRecord(
attachment_id=sup.id,
supplement=sup,
attachment=ats[sup.id],
cred_id=cred_id,
attribute=attribute,
attribute=sup.attrs[0]["value"],
)
for sup in supplements
]

@classmethod
async def save_attachments(
cls, session, supplements, attachments, cred_id, attribute
):
async def save_attachments(cls, session, supplements, attachments, cred_id):
""" "Save all attachments"""
return [
await attachment.save(session)
for attachment in AttachmentDataRecord.match_by_attachment_id(
supplements, attachments, cred_id, attribute
supplements, attachments, cred_id
)
]

Expand Down
11 changes: 4 additions & 7 deletions aries_cloudagent/wallet/tests/test_attachment_data_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_supplement():
def _create_supplement(num):
return Supplement(
type="hashlink_data",
attrs={"key": "field", "value": "<fieldname>"},
attrs=[{"key": "field", "value": "<fieldname>"}],
ref=None,
id="attachment_id_" + str(num),
)
Expand Down Expand Up @@ -76,12 +76,13 @@ def test_match_by_attachment_id(create_supplement, create_attachment):
supplements = [create_supplement(1), create_supplement(2)]
attachments = [create_attachment(1), create_attachment(2)]
result = AttachmentDataRecord.match_by_attachment_id(
supplements, attachments, cred_id="test_cred_id", attribute="test_attribute"
supplements, attachments, cred_id="test_cred_id"
)

for record in result:
assert type(record) == AttachmentDataRecord
assert record.attachment_id == record.supplement.id
assert record.attribute == "<fieldname>"


@pytest.mark.asyncio
Expand All @@ -91,11 +92,7 @@ async def test_save_attachments(create_supplement, create_attachment, profile):
supplements = [create_supplement(1), create_supplement(2)]
attachments = [create_attachment(1), create_attachment(2)]
result = await AttachmentDataRecord.save_attachments(
session,
supplements,
attachments,
cred_id="test_cred_id",
attribute="test_attribute",
session, supplements, attachments, cred_id="test_cred_id"
)

assert type(result) == list
Expand Down

0 comments on commit 45f8df1

Please sign in to comment.