Skip to content

Commit 41f4ece

Browse files
committed
Allows "hash" and "multihash" tags for multihashes
Closes #15
1 parent 8cb5f72 commit 41f4ece

File tree

8 files changed

+25
-12
lines changed

8 files changed

+25
-12
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"esbonio.sphinx.confDir": "${workspaceFolder}\\docs",
3-
"workbench.colorTheme": "Default Dark Modern"
3+
"workbench.colorTheme": "Default Dark+"
44
}

0.3.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Requirement already satisfied: multiformats-config in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (0.3.1)
2+
Requirement already satisfied: multiformats in c:\users\stefa\appdata\local\programs\python\python312\lib\site-packages (from multiformats-config) (0.2.1.post7)
3+
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)
4+
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)
5+
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)

multiformats/multibase/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

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

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

293293
def __hash__(self) -> int:

multiformats/multicodec/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def hexcode(self) -> str:
165165
return _hexcode(self._code)
166166

167167
@property
168-
def status(self) -> Literal["draft", "permanent"]:
168+
def status(self) -> MulticodecStatus:
169169
""" Multicodec status. """
170170
return self._status
171171

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

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

283283
def __hash__(self) -> int:

multiformats/multihash/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def __new__(cls, codec: Union[str, int, Multicodec]) -> "Multihash":
5858
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not the one given.")
5959
codec = existing_codec
6060
# check that the codec is a multihash multicodec:
61-
if codec.tag != "multihash":
62-
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not a multihash.")
61+
if codec.tag not in ("multihash", "hash"):
62+
raise MultihashValueError(f"Multicodec named {repr(codec.name)} exists, but is not a hash or multihash.")
6363
if not raw.exists(codec.name):
6464
raise MultihashKeyError(f"No implementation for multihash multicodec {repr(codec.name)}.")
6565
_cache = Multihash._cache
@@ -124,6 +124,14 @@ def codec(self) -> Multicodec:
124124
"""
125125
return self._codec
126126

127+
@property
128+
def is_cryptographic(self) -> bool:
129+
"""
130+
Whether this is a cryptographic hash or not, based on whether
131+
the codec is tagged as ``'multihash'`` or just ``'hash'``.
132+
"""
133+
return self.codec.tag=="multihash"
134+
127135
@property
128136
def max_digest_size(self) -> Optional[int]:
129137
"""

multiformats/multihash/raw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def register(name: str, hashfun: Hashfun, digest_size: Optional[int], *, overwri
121121
raise MultihashValueError(f"An implementation for the multihash multicodec named {repr(name)} already exists.")
122122
if name not in _hashfun:
123123
multihash = multicodec.get(name)
124-
if multihash.tag != "multihash":
125-
raise MultihashValueError(f"Multicodec '{multihash.name}' exists, but it is not a multihash multicodec.")
124+
if multihash.tag not in ("multihash", "hash"):
125+
raise MultihashValueError(f"Multicodec '{multihash.name}' exists, but it is not a hash or multihash multicodec.")
126126
_hashfun[name] = (hashfun, digest_size)
127127

128128

test/test_02_multibase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def test_get() -> None:
2121
enc = multibase.get("base16")
2222
assert enc.name == "base16"
2323
assert enc.code == "f"
24-
assert enc.status == "default"
25-
assert enc.description == "hexadecimal"
24+
assert enc.status == "final"
25+
assert enc.description == "Hexadecimal (lowercase)"
2626

2727
def test_multibase_contructor() -> None:
2828
""" Tests `Multibase.from_json`. """

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ deps =
1515
pyskein
1616
mmh3
1717
pycryptodomex
18-
multiformats_config
18+
multiformats_config>=0.3.1
1919
rich # optional dependency of typing_validation
2020

2121
setenv =

0 commit comments

Comments
 (0)