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

DIDComm v1 interfaces like those available for v2 #39

Merged
merged 8 commits into from
Jun 6, 2024

Conversation

dbluhm
Copy link
Member

@dbluhm dbluhm commented Jun 4, 2024

This PR adds interfaces for DIDComm v1 like the ones we have for v2.

The purpose of this addition is to help users of the library implement both v1 and v2 in the same code base more easily by providing a (relatively) consistent interface between v1 and v2. There are, of course, differences arising from the differences in the two versions but the style and patterns should be familiar at least.

Signed-off-by: Daniel Bluhm <[email protected]>
instead of through the old crypto module

Signed-off-by: Daniel Bluhm <[email protected]>
DIDComm v1 isn't quite legacy yet

Signed-off-by: Daniel Bluhm <[email protected]>
Signed-off-by: Daniel Bluhm <[email protected]>
Signed-off-by: Daniel Bluhm <[email protected]>
Copy link
Contributor

@mepeltier mepeltier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dbluhm dbluhm merged commit b826282 into main Jun 6, 2024
5 checks passed
@dbluhm dbluhm deleted the feature/didcomm-v1-interfaces branch June 6, 2024 16:19
Copy link
Contributor

@TheTechmage TheTechmage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, things are looking good in general from what I can tell.

"""Result of unpacking a message."""

unpacked: bytes
encrytped: bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo?

Comment on lines -35 to 38
return peer_4_pattern_short.match(did) or peer_4_pattern_long.match(did)
return bool(peer_4_pattern_short.match(did)) or bool(
peer_4_pattern_long.match(did)
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would surrounding the entire or in the bool be cleaner and/or make more sense to someone reading the code?

Comment on lines +75 to +80
enc_sender = None
nonce = None
enc_cek = crypto_box.crypto_box_seal(target_xk, cek_b)
builder.add_recipient(
JweRecipient(encrypted_key=enc_cek, header={"kid": target_vk.kid})
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonce & enc_sender set but not used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just to "zero out" the values? If so, wouldn't it make more sense to have it outside the if statement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to be used, refactored and then it was no longer used. Good catch!

Comment on lines +55 to +63
@property
def key(self) -> str:
"""Return base58 encoded key."""
return base58.b58encode(self.value).decode()

@property
def kid(self) -> str:
"""Get the key ID."""
return self.key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this hold true for the kid if the did was did:peer:1/2 during a DIDComm V1 exchange?

# Legacy formats
# V1 formats
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that Legacy here referred to something else besides DIDComm V1

Comment on lines +21 to +26
class V1UnpackResult(NamedTuple):
"""Result of unpacking."""

message: bytes
recip: str
sender: Optional[str]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should message here load the JSON to match the interface for DIDComm V2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, this is a "lower level" result object. I'll rename to make it's function clearer.

Comment on lines +208 to +211
if not isinstance(seed, bytes):
raise ValueError("Seed value is not a string or bytes")
if len(seed) != 32:
raise ValueError("Seed value must be 32 bytes in length")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this fail if it's a str? Won't a str's len() value also be off if unicode (particularly CJK) is used?
Method signature signifies that only bytes is allowed, but error says that str is as well.

Comment on lines +30 to +46
@dataclass
class V1UnpackResult:
"""Result of unpacking a message."""

unpacked: bytes
encrytped: bool
authenticated: bool
recipient_kid: str
sender_kid: Optional[str] = None

@property
def message(self) -> dict:
"""Return unpacked value as a dict.

This value is used to preserve backwards compatibility.
"""
return json.loads(self.unpacked)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class appears to have the same name as a NamedTuple in v1/base.py, leading to some confusion

@@ -1,31 +1,17 @@
"""Test Pack and Unpack."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this file need to stay here, or move to ../v1/?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should stay here; I'm treating the crypto module in the legacy folder as a "do not touch, backwards compat only" thing since it went out in a release already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it mimics exactly the dependency it replaces, aries-staticagent-python

"""Result of unpacking a message."""

unpacked: bytes
encrytped: bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this isn't a typo?

@dbluhm dbluhm mentioned this pull request Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants