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

Add creation and resolution of DIDs using method 1 #46

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions peerdid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

from . import core, dids, errors, keys

from pydid import DID, DIDDocument
from pydid import BaseDIDDocument, DID, DIDDocument

__version__ = "0.5.1"

__all__ = ["__version__", "core", "errors", "dids", "keys", "DID", "DIDDocument"]
__all__ = [
"__version__",
"core",
"errors",
"dids",
"keys",
"BaseDIDDocument",
"DID",
"DIDDocument",
]
20 changes: 13 additions & 7 deletions peerdid/core/multicodec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Multicodec utility methods."""

from enum import Enum
from io import BytesIO
from typing import Tuple, Union

import varint
Expand All @@ -11,32 +12,37 @@ class Codec(Enum):

X25519 = 0xEC
ED25519 = 0xED
SHA256 = 0x12

def encode_multicodec(self, value: bytes) -> bytes:
"""Encode a value with this codec."""
prefix = varint.encode(self.value)
return prefix + value

def encode_multicodec_with_length(self, value: bytes) -> bytes:
"""Encode a value with this codec."""
prefix = varint.encode(self.value) + varint.encode(len(value))
return prefix + value


def from_multicodec(value: Union[str, bytes]) -> Tuple[bytes, Codec]:
"""Decode a multicodec value."""
if isinstance(value, str):
value = value.encode("utf-8")
buffer = BytesIO(value)
try:
prefix_int = varint.decode_bytes(value)
prefix_int = varint.decode_stream(buffer)
except Exception:
raise ValueError(
"Invalid key: Invalid multicodec prefix in {}".format(str(value))
)
raise ValueError("Invalid key: Invalid multicodec prefix in {}".format(value))
remain = buffer.read()

try:
codec = Codec(prefix_int)
except ValueError:
raise ValueError(
"Invalid key: Unknown multicodec prefix {} in {}".format(
str(prefix_int), str(value)
hex(prefix_int), value
)
)

prefix = varint.encode(prefix_int)
return value[len(prefix) :], codec
return remain, codec
2 changes: 1 addition & 1 deletion peerdid/core/peer_did_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def decode_service(service: str) -> Optional[List[Service]]:

def decode_multibase_numbasis(
multibase: str,
key_format: KeyFormat,
key_format: KeyFormat = None,
) -> BaseKey:
"""
Decode multibase-encoded numeric basis to a verification method for DID Document.
Expand Down
Loading