-
Notifications
You must be signed in to change notification settings - Fork 516
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
refactor: Extract verification method ID generation to a separate class #2235
Merged
dbluhm
merged 10 commits into
openwallet-foundation:main
from
sicpa-dlab:custom_default_verkey_strategy
Jun 13, 2023
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
27c3b3c
refactor: put _get_verification_method in an injectable class
yvgny 6001328
fix: use given verification_method when given + renaming
yvgny 6ec4805
fix: set up context correctly in pres_exch tests
yvgny a68c065
Merge branch 'main' into custom_default_verkey_strategy
dbluhm 7c01bd0
Merge branch 'main' into custom_default_verkey_strategy
dbluhm 89a53a8
Merge branch 'main' into custom_default_verkey_strategy
dkulic 466a946
Merge branch 'main' into custom_default_verkey_strategy
yvgny 73d313f
chore: rename injection token and classes
yvgny 19ff653
feat: add proof_purpose and allowed_method_types
yvgny e63f517
Merge branch 'main' into custom_default_verkey_strategy
dbluhm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
aries_cloudagent/wallet/default_verification_key_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Utilities for specifying which verification method is in use for a given DID.""" | ||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
from aries_cloudagent.did.did_key import DIDKey | ||
|
||
|
||
class DefaultVerificationKeyStrategyBase(ABC): | ||
"""Base class for defining which verification method is in use.""" | ||
|
||
@abstractmethod | ||
def get_verification_method_id_for_did(self, did) -> Optional[str]: | ||
"""Given a DID, returns the verification key ID in use. | ||
|
||
Returns None if no strategy is specified for this DID. | ||
|
||
:params str did: the did | ||
:returns Optional[str]: the current verkey ID | ||
""" | ||
pass | ||
|
||
|
||
class DefaultVerificationKeyStrategy(DefaultVerificationKeyStrategyBase): | ||
"""A basic implementation for verkey strategy. | ||
|
||
Supports did:key: and did:sov only. | ||
""" | ||
|
||
def get_verification_method_id_for_did(self, did) -> Optional[str]: | ||
"""Given a did:key or did:sov, returns the verification key ID in use. | ||
|
||
Returns None if no strategy is specified for this DID. | ||
|
||
:params str did: the did | ||
:returns Optional[str]: the current verkey ID | ||
""" | ||
if did.startswith("did:key:"): | ||
return DIDKey.from_did(did).key_id | ||
elif did.startswith("did:sov:"): | ||
# key-1 is what uniresolver uses for key id | ||
return did + "#key-1" | ||
|
||
return None |
30 changes: 30 additions & 0 deletions
30
aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from unittest import TestCase | ||
|
||
from aries_cloudagent.did.did_key import DIDKey | ||
|
||
from aries_cloudagent.wallet.default_verification_key_strategy import ( | ||
DefaultVerificationKeyStrategy, | ||
) | ||
|
||
TEST_DID_SOV = "did:sov:LjgpST2rjsoxYegQDRm7EL" | ||
TEST_DID_KEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" | ||
|
||
|
||
class TestDefaultVerificationKeyStrategy(TestCase): | ||
def test_with_did_sov(self): | ||
strategy = DefaultVerificationKeyStrategy() | ||
assert ( | ||
strategy.get_verification_method_id_for_did(TEST_DID_SOV) | ||
== TEST_DID_SOV + "#key-1" | ||
) | ||
|
||
def test_with_did_key(self): | ||
strategy = DefaultVerificationKeyStrategy() | ||
assert ( | ||
strategy.get_verification_method_id_for_did(TEST_DID_KEY) | ||
== DIDKey.from_did(TEST_DID_KEY).key_id | ||
) | ||
|
||
def test_unsupported_did_method(self): | ||
strategy = DefaultVerificationKeyStrategy() | ||
assert strategy.get_verification_method_id_for_did("did:test:test") is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
Base
instead of Default, as you can override the default with a custom one? It would't make sense to make the injection token the default implementation IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TimoGlastra So the injection token should be renamed to
BaseVerificationKeyStrategy
? This would mean the base (abstract) class would be namedBaseVerificationKeyStrategy
and the default implementationDefaultVerificationKeyStrategy
, is this correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yvgny that is correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done