Skip to content

Commit

Permalink
IN PROGRESS: did helpers: ecdsa p-384 key loading
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Nov 15, 2023
1 parent a4bc402 commit e3cc9ad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[pytest]
# https://docs.pytest.org/en/7.1.x/how-to/doctest.html#using-doctest-options
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
# Alternatively, options can be enabled by an inline comment in the doc test itself:
# >>> something_that_raises() # doctest: +IGNORE_EXCEPTION_DETAIL
# Traceback (most recent call last):
# ValueError: ...
addopts = --doctest-modules
51 changes: 47 additions & 4 deletions scitt_emulator/did_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import urllib.parse
from typing import Optional, Callable

import multiformats


def did_web_to_url(
did_web_string: str,
Expand All @@ -27,8 +29,42 @@ class DIDKeyDecoderNotFoundError(NotImplementedError):
DID_KEY_METHOD = "did:key:"


def did_key_to_jwk_dict_is_p_384_startswith_z82(did_key: str) -> dict[str, str]:
did_key = did_key.replace(DID_KEY_METHOD, "", 1)
def did_key_decode(
did_key_without_method_or_key_type_prefix: str,
) -> tuple[str, bytes]:
return


import snoop


@snoop
def did_key_decode_public_key(did_key: str) -> dict[str, str]:
"""
3.1.3 Decode Public Key Algorithm
The following algorithm can be used for expanding a multibase-encoded
multicodec value to decoded public key components. A multibaseValue and
options are the required inputs. A decodedPublicKey is the output.
"""

# Set decodedPublicKey to an empty object.
decoded_public_key = None

# Decode multibaseValue using the base58-btc multibase alphabet and set multicodecValue to the multicodec header for the decoded value. Implementers are cautioned to ensure that the multicodecValue is set to the result after performing varint decoding.
base58btc_decoded = multiformats.multibase.get("base58btc").decode(did_key)
# 3.1.2.3
multibase_value = base58btc_decoded[:2]
if multibase_value[0] != 0x12:
multibase_value = base58btc_decoded[:1]

# Set the rawPublicKeyBytes to the bytes remaining after the multicodec header.
raw_public_key_bytes = base58btc_decoded[len(multibase_value) :]

# Return multicodecValue and rawPublicKeyBytes as the decodedPublicKey.
return multibase_value, raw_public_key_bytes


def did_key_to_jwk_dict_is_p_384_startswith_z82(multibase_value, raw_public_key_bytes) -> dict[str, str]:
return


Expand All @@ -49,7 +85,7 @@ def did_key_to_jwk_dict(
>>> did_key_to_jwk_dict("did:key:invalid")
Traceback (most recent call last):
DIDKeyDecoderNotFoundError: ...
>>> did_key_to_jwk_dict("did:key:z82LkvCwHNreneWpsgPEbV3gu1C6NFJEBg4srfJ5gdxEsMGRJUz2sG9FE42shbn2xkZJh54")
>>> did_key_to_jwk_dict("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK")
"""
if decoders_by_prefix is None:
decoders_by_prefix = {
Expand All @@ -61,8 +97,15 @@ def did_key_to_jwk_dict(
)
}

try:
multibase_value, raw_public_key_bytes = did_key_decode_public_key(
did_key.replace(DID_KEY_METHOD, "", 1)
)
except multiformats.multibase.err.MultibaseKeyError as e:
raise DIDKeyDecoderNotFoundError(did_key) from e

for prefix, decoder in decoders_by_prefix.items():
if did_key.startswith(DID_KEY_METHOD + prefix):
return decoder(did_key)
return decoder(multibase_value, raw_public_key_bytes)

raise DIDKeyDecoderNotFoundError(did_key)

0 comments on commit e3cc9ad

Please sign in to comment.