Skip to content

Commit

Permalink
Merge pull request #2 from dbluhm/refactor/replace-multiformats
Browse files Browse the repository at this point in the history
refactor: replace multiformats
  • Loading branch information
dbluhm authored Oct 19, 2023
2 parents 1ebbbbc + ab1e88f commit 61035e8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 63 deletions.
45 changes: 34 additions & 11 deletions did_peer_4/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
import json
import re
from typing import Any, Dict, Optional
from multiformats import multibase, multicodec, multihash
from base58 import b58decode, b58encode
from hashlib import sha256

from .doc_visitor import DocVisitor

# Regex patterns
BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

LONG_PATTERN = re.compile(
r"^did:peer:4zQm[" + BASE58_ALPHABET + r"]{44}:z[" + BASE58_ALPHABET + r"]{6,}$"
)
SHORT_PATTERN = re.compile(r"^did:peer:4zQm[" + BASE58_ALPHABET + r"]{44}$")

# Multiformats constants
MULTICODEC_JSON = b"\x80\x04"
MULTICODEC_SHA2_256 = b"\x12\x20"
MULTIBASE_BASE58_BTC = "z"


def _encode_doc(document: Dict[str, Any]) -> str:
"""Encode the document."""
return multibase.encode(
multicodec.wrap("json", json.dumps(document, separators=(",", ":")).encode()),
"base58btc",
return (
MULTIBASE_BASE58_BTC
+ b58encode(
MULTICODEC_JSON + json.dumps(document, separators=(",", ":")).encode()
).decode()
)


def _decode_doc(encoded_doc: str) -> Dict[str, Any]:
"""Decode the document."""
encoding = encoded_doc[0]
encoded = encoded_doc[1:]
if encoding != MULTIBASE_BASE58_BTC:
raise ValueError(f"Unsupported encoding: {encoding}")

decoded_bytes = b58decode(encoded)
if not decoded_bytes.startswith(MULTICODEC_JSON):
raise ValueError(f"Unsupported multicodec: {decoded_bytes[:2]}...")

value = decoded_bytes[2:]
return json.loads(value)


def _hash_encoded_doc(encoded_doc: str) -> str:
"""Return multihash of encoded doc."""
return multibase.encode(
multihash.digest(encoded_doc.encode(), "sha2-256"), "base58btc"
return (
MULTIBASE_BASE58_BTC
+ b58encode(
MULTICODEC_SHA2_256 + sha256(encoded_doc.encode()).digest()
).decode()
)


Expand Down Expand Up @@ -61,10 +87,7 @@ def decode(did: str) -> Dict[str, Any]:
if _hash_encoded_doc(encoded_doc) != hashed:
raise ValueError(f"Hash is invalid for did: {did}")

decoded_bytes = multibase.decode(encoded_doc)
_, decoded = multicodec.unwrap(decoded_bytes)

return json.loads(decoded)
return _decode_doc(encoded_doc)


def decoded_to_resolved(did: str, document: dict) -> dict:
Expand Down
57 changes: 7 additions & 50 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ authors = [
{name = "Daniel Bluhm", email = "[email protected]"},
]
dependencies = [
"multiformats>=0.2.1",
"typing-extensions<4.6.0",
"base58>=2.1.1",
]
requires-python = ">=3.9"
readme = "README.md"
Expand Down

0 comments on commit 61035e8

Please sign in to comment.