-
Notifications
You must be signed in to change notification settings - Fork 517
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
Add multiple kid per key feature, kid removal and get by kid routes #3472
Draft
PatStLouis
wants to merge
5
commits into
openwallet-foundation:main
Choose a base branch
from
OpSecId:enable-multiple-kid-per-keypair
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−10
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d80bf3
add multiple kid per key feature, kid removal and get by kid routes
PatStLouis 912f3b7
linting
PatStLouis d8a3be8
remove default kid, conflict with did key
PatStLouis 01c8086
comment unused import
PatStLouis a62e429
linting
PatStLouis 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
from .did_method import SOV, DIDMethod, DIDMethods | ||
from .did_parameters_validation import DIDParametersValidation | ||
from .error import WalletDuplicateError, WalletError, WalletNotFoundError | ||
|
||
# from .keys.manager import verkey_to_multikey | ||
from .key_type import BLS12381G2, ED25519, P256, X25519, KeyType, KeyTypes | ||
from .util import b58_to_bytes, bytes_to_b58 | ||
|
||
|
@@ -94,11 +96,13 @@ async def create_key( | |
if metadata is None: | ||
metadata = {} | ||
|
||
tags = {"kid": kid} if kid else None | ||
|
||
try: | ||
keypair = _create_keypair(key_type, seed) | ||
verkey = bytes_to_b58(keypair.get_public_bytes()) | ||
# multikey = verkey_to_multikey(verkey, alg=key_type.key_type) | ||
# default_kid = f"did:key:{multikey}#{multikey}" | ||
# tags = {"kid": [default_kid, kid]} if kid else [default_kid] | ||
tags = {"kid": [kid]} if kid else None | ||
await self._session.handle.insert_key( | ||
verkey, | ||
keypair, | ||
|
@@ -131,14 +135,58 @@ async def assign_kid_to_key(self, verkey: str, kid: str) -> KeyInfo: | |
if not key_entry: | ||
raise WalletNotFoundError(f"No key entry found for verkey {verkey}") | ||
|
||
try: | ||
existing_kid = key_entry.tags.get("kid") | ||
except Exception: | ||
existing_kid = [] | ||
|
||
existing_kid = existing_kid if isinstance(existing_kid, list) else [existing_kid] | ||
existing_kid.append(kid) | ||
tags = {"kid": existing_kid} | ||
|
||
key = cast(Key, key_entry.key) | ||
metadata = cast(dict, key_entry.metadata) | ||
key_types = self.session.inject(KeyTypes) | ||
key_type = key_types.from_key_type(key.algorithm.value) | ||
if not key_type: | ||
raise WalletError(f"Unknown key type {key.algorithm.value}") | ||
|
||
await self._session.handle.update_key(name=verkey, tags={"kid": kid}) | ||
await self._session.handle.update_key(name=verkey, tags=tags) | ||
return KeyInfo(verkey=verkey, metadata=metadata, key_type=key_type, kid=kid) | ||
|
||
async def remove_kid_from_key(self, kid: str) -> KeyInfo: | ||
"""Remove a kid association. | ||
|
||
Args: | ||
kid: the key identifier | ||
|
||
Returns: | ||
The key identified by kid | ||
|
||
""" | ||
key_entries = await self._session.handle.fetch_all_keys( | ||
tag_filter={"kid": kid}, limit=2 | ||
) | ||
if len(key_entries) > 1: | ||
raise WalletDuplicateError(f"More than one key found by kid {kid}") | ||
|
||
entry = key_entries[0] | ||
existing_kid = entry.tags.get("kid") | ||
key = cast(Key, entry.key) | ||
verkey = bytes_to_b58(key.get_public_bytes()) | ||
metadata = cast(dict, entry.metadata) | ||
key_types = self.session.inject(KeyTypes) | ||
key_type = key_types.from_key_type(key.algorithm.value) | ||
if not key_type: | ||
raise WalletError(f"Unknown key type {key.algorithm.value}") | ||
try: | ||
existing_kid.remove(kid) | ||
except Exception: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should catch the correct type of exception for removing entry that doesn't exist. Can't remember of the top of my head. In general try to only catch the base Exception class when nessecary. |
||
pass | ||
|
||
tags = {"kid": existing_kid} | ||
|
||
await self._session.handle.update_key(name=verkey, tags=tags) | ||
return KeyInfo(verkey=verkey, metadata=metadata, key_type=key_type, kid=kid) | ||
|
||
async def get_key_by_kid(self, kid: str) -> KeyInfo: | ||
|
@@ -158,6 +206,7 @@ async def get_key_by_kid(self, kid: str) -> KeyInfo: | |
raise WalletDuplicateError(f"More than one key found by kid {kid}") | ||
|
||
entry = key_entries[0] | ||
kid = entry.tags.get("kid") | ||
key = cast(Key, entry.key) | ||
verkey = bytes_to_b58(key.get_public_bytes()) | ||
metadata = cast(dict, entry.metadata) | ||
|
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
Oops, something went wrong.
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.
key_entry.tags.get("kid", [])