Skip to content

Commit

Permalink
Allows "hash" and "multihash" tags for multihashes
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
sg495 committed Dec 18, 2023
1 parent 8cb5f72 commit 41f4ece
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"esbonio.sphinx.confDir": "${workspaceFolder}\\docs",
"workbench.colorTheme": "Default Dark Modern"
"workbench.colorTheme": "Default Dark+"
}
5 changes: 5 additions & 0 deletions 0.3.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Requirement already satisfied: multiformats-config in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (0.3.1)
Requirement already satisfied: multiformats in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (from multiformats-config) (0.2.1.post7)
Requirement already satisfied: typing-extensions>=4.6.0 in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (from multiformats->multiformats-config) (4.9.0)
Requirement already satisfied: typing-validation>=1.1.0 in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (from multiformats->multiformats-config) (1.1.0)
Requirement already satisfied: bases>=0.3.0 in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (from multiformats->multiformats-config) (0.3.0)
4 changes: 2 additions & 2 deletions multiformats/multibase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

MultibaseStatus = Literal[
"draft", "final", "reserved", "experimental",
"candidate", "default" # legacy values
"candidate", "default" # FIXME: deprecated legacy values
]
"""
Literal type of possible values for the :attr:`Multibase.status` property.
Expand Down Expand Up @@ -287,7 +287,7 @@ def __repr__(self) -> str:
return f"Multibase({', '.join(f'{k}={repr(v)}' for k, v in self.to_json().items())})"

@property
def _as_tuple(self) -> Tuple[Type["Multibase"], str, str, Literal["draft", "candidate", "default"]]:
def _as_tuple(self) -> Tuple[Type["Multibase"], str, str, MultibaseStatus]:
return (Multibase, self.name, self.code, self.status)

def __hash__(self) -> int:
Expand Down
4 changes: 2 additions & 2 deletions multiformats/multicodec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def hexcode(self) -> str:
return _hexcode(self._code)

@property
def status(self) -> Literal["draft", "permanent"]:
def status(self) -> MulticodecStatus:
""" Multicodec status. """
return self._status

Expand Down Expand Up @@ -277,7 +277,7 @@ def __repr__(self) -> str:
return f"Multicodec({', '.join(f'{k}={repr(v)}' for k, v in self.to_json().items())})"

@property
def _as_tuple(self) -> Tuple[Type["Multicodec"], str, str, int, Literal["draft", "permanent"]]:
def _as_tuple(self) -> Tuple[Type["Multicodec"], str, str, int, MulticodecStatus]:
return (Multicodec, self.name, self.tag, self.code, self.status)

def __hash__(self) -> int:
Expand Down
12 changes: 10 additions & 2 deletions multiformats/multihash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def __new__(cls, codec: Union[str, int, Multicodec]) -> "Multihash":
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not the one given.")
codec = existing_codec
# check that the codec is a multihash multicodec:
if codec.tag != "multihash":
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not a multihash.")
if codec.tag not in ("multihash", "hash"):
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not a hash or multihash.")
if not raw.exists(codec.name):
raise MultihashKeyError(f"No implementation for multihash multicodec {repr(codec.name)}.")
_cache = Multihash._cache
Expand Down Expand Up @@ -124,6 +124,14 @@ def codec(self) -> Multicodec:
"""
return self._codec

@property
def is_cryptographic(self) -> bool:
"""
Whether this is a cryptographic hash or not, based on whether
the codec is tagged as ``'multihash'`` or just ``'hash'``.
"""
return self.codec.tag=="multihash"

@property
def max_digest_size(self) -> Optional[int]:
"""
Expand Down
4 changes: 2 additions & 2 deletions multiformats/multihash/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def register(name: str, hashfun: Hashfun, digest_size: Optional[int], *, overwri
raise MultihashValueError(f"An implementation for the multihash multicodec named {repr(name)} already exists.")
if name not in _hashfun:
multihash = multicodec.get(name)
if multihash.tag != "multihash":
raise MultihashValueError(f"Multicodec '{multihash.name}' exists, but it is not a multihash multicodec.")
if multihash.tag not in ("multihash", "hash"):
raise MultihashValueError(f"Multicodec '{multihash.name}' exists, but it is not a hash or multihash multicodec.")
_hashfun[name] = (hashfun, digest_size)


Expand Down
4 changes: 2 additions & 2 deletions test/test_02_multibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_get() -> None:
enc = multibase.get("base16")
assert enc.name == "base16"
assert enc.code == "f"
assert enc.status == "default"
assert enc.description == "hexadecimal"
assert enc.status == "final"
assert enc.description == "Hexadecimal (lowercase)"

def test_multibase_contructor() -> None:
""" Tests `Multibase.from_json`. """
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deps =
pyskein
mmh3
pycryptodomex
multiformats_config
multiformats_config>=0.3.1
rich # optional dependency of typing_validation

setenv =
Expand Down

0 comments on commit 41f4ece

Please sign in to comment.