-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2235 from sicpa-dlab/custom_default_verkey_strategy
refactor: Extract verification method ID generation to a separate class
- Loading branch information
Showing
7 changed files
with
138 additions
and
61 deletions.
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
"""Utilities for specifying which verification method is in use for a given DID.""" | ||
from abc import ABC, abstractmethod | ||
from typing import Optional, List | ||
|
||
from aries_cloudagent.wallet.key_type import KeyType | ||
|
||
from aries_cloudagent.did.did_key import DIDKey | ||
|
||
|
||
class BaseVerificationKeyStrategy(ABC): | ||
"""Base class for defining which verification method is in use.""" | ||
|
||
@abstractmethod | ||
def get_verification_method_id_for_did( | ||
self, | ||
did: str, | ||
allowed_verification_method_types: Optional[List[KeyType]] = None, | ||
proof_purpose: Optional[str] = None, | ||
) -> Optional[str]: | ||
"""Given a DID, returns the verification key ID in use. | ||
Returns None if no strategy is specified for this DID. | ||
:params did: the did | ||
:params allowed_verification_method_types: list of accepted key types | ||
:params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) | ||
:returns Optional[str]: the current verkey ID | ||
""" | ||
pass | ||
|
||
|
||
class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): | ||
"""A basic implementation for verkey strategy. | ||
Supports did:key: and did:sov only. | ||
""" | ||
|
||
def get_verification_method_id_for_did( | ||
self, | ||
did: str, | ||
allowed_verification_method_types: Optional[List[KeyType]] = None, | ||
proof_purpose: Optional[str] = None, | ||
) -> 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 | ||
:params allowed_verification_method_types: list of accepted key types | ||
:params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) | ||
: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 |