Skip to content

Commit

Permalink
Replace all other dependencies with multiformats and others
Browse files Browse the repository at this point in the history
Basically replaces py-multicodec and py-multihash with multiformats
and adds base58check for base58 encoding/decoding and pysha3 so that
keccak-256 decoding can work.

Also temporarily pins typing extensions to 4.5.0 due to
hashberg-io/multiformats#10

All tests now pass again
  • Loading branch information
LefterisJP committed Jun 15, 2023
1 parent 61f3b9f commit 24ac291
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 31 deletions.
26 changes: 13 additions & 13 deletions content_hash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Python implementation of EIP 1577 content hash."""

import multihash
import multicodec
from multiformats import multicodec

from .profiles import get_profile

Expand All @@ -15,14 +14,14 @@ def decode(chash):
:return: the decoded content
:rtype: str
"""
buffer = bytes.fromhex(chash.lstrip('0x'))

buffer = multihash.from_hex_string(chash.lstrip('0x'))

codec = multicodec.get_codec(buffer)
value = multicodec.remove_prefix(buffer)

profile = get_profile(codec)
return profile.decode(value)
codec, value = multicodec.unwrap(buffer)
profile = get_profile(codec.name)
result = profile.decode(value)
if isinstance(result, bytes):
result = result.decode()
return result


def encode(codec, value):
Expand All @@ -39,8 +38,8 @@ def encode(codec, value):
profile = get_profile(codec)

value = profile.encode(value)
value = multicodec.add_prefix(codec, value)
return multihash.to_hex_string(value)
value = multicodec.wrap(codec, value)
return value.hex()


def get_codec(chash):
Expand All @@ -53,5 +52,6 @@ def get_codec(chash):
:rtype: str
"""

buffer = multihash.from_hex_string(chash.lstrip('0x'))
return multicodec.get_codec(buffer)
buffer = bytes.fromhex(chash.lstrip('0x'))
codec, _ = multicodec.unwrap(buffer)
return codec.name
6 changes: 3 additions & 3 deletions content_hash/decodes/b58_multi_hash.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Decode module for B58 multi hash."""

import multihash
from multiformats import multihash
from multiformats_cid import make_cid

from base58check import b58encode

def decode(value):
"""
Expand All @@ -15,4 +15,4 @@ def decode(value):
"""

cid = make_cid(value)
return multihash.to_b58_string(cid.multihash)
return b58encode(cid.multihash)
4 changes: 2 additions & 2 deletions content_hash/decodes/hex_multi_hash.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Decode module for HEX multi hash."""

import multihash
from multiformats import multihash
from multiformats_cid import make_cid


Expand All @@ -15,4 +15,4 @@ def decode(value):
"""

cid = make_cid(value)
return multihash.to_hex_string(multihash.decode(cid.multihash).digest)
return multihash.unwrap(cid.multihash).hex()
4 changes: 2 additions & 2 deletions content_hash/encodes/ipfs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Encode module for IPFS."""

import multihash
import base58check
from multiformats_cid import make_cid


Expand All @@ -14,5 +14,5 @@ def encode(value):
:rtype: str
"""

mhash = multihash.from_b58_string(value)
mhash = base58check.b58decode(value)
return make_cid(1, 'dag-pb', mhash).buffer
5 changes: 3 additions & 2 deletions content_hash/encodes/swarm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Encode module for Swarm."""

import multihash
from multiformats import multihash
from multiformats_cid import make_cid


Expand All @@ -14,5 +14,6 @@ def encode(value):
:rtype: str
"""

mhash = multihash.encode(multihash.from_hex_string(value), 'keccak-256')
# mhash = multihash.digest(bytes.fromhex(value), 'keccak-256')
mhash = multihash.wrap(bytes.fromhex(value), 'keccak-256')
return make_cid(1, 'swarm-manifest', mhash).buffer
6 changes: 3 additions & 3 deletions content_hash/profiles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def get_profile(name):


PROFILES = {
'ipfs-ns': Profile(decode='b58_multi_hash', encode='ipfs'),
'ipns-ns': Profile(decode='b58_multi_hash', encode='ipfs'),
'swarm-ns': Profile(decode='hex_multi_hash', encode='swarm'),
'ipfs': Profile(decode='b58_multi_hash', encode='ipfs'),
'ipns': Profile(decode='b58_multi_hash', encode='ipfs'),
'swarm': Profile(decode='hex_multi_hash', encode='swarm'),
'default': Profile(decode='utf8', encode='utf8'),
}
"""
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def readme():

install_requires = [
'py-multiformats-cid',
'py-multicodec>=0.2.1,<0.3.0',
'py-multihash>=0.2.3,<0.3.0',
'multiformats',
'base58check', # for b58 encode/decode
'pysha3', # for keccak-256
'typing_extensions==4.5.0', # due to https://github.com/hashberg-io/multiformats/issues/10
],

extras_require = {
Expand Down
2 changes: 1 addition & 1 deletion tests/profiles/test_ipfs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import content_hash


CODEC = 'ipfs-ns'
CODEC = 'ipfs'
DECODED = 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
ENCODED = 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'

Expand Down
2 changes: 1 addition & 1 deletion tests/profiles/test_ipns.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import content_hash


CODEC = 'ipns-ns'
CODEC = 'ipns'
DECODED = 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
ENCODED = 'e5010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'

Expand Down
2 changes: 1 addition & 1 deletion tests/profiles/test_swarm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import content_hash


CODEC = 'swarm-ns'
CODEC = 'swarm'
DECODED = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
ENCODED = 'e40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'

Expand Down
3 changes: 2 additions & 1 deletion tests/test_content_hash.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from multiformats.multicodec.err import MulticodecKeyError
import content_hash


Expand All @@ -14,5 +15,5 @@ def test_not_decode_nonexistent_codec():


def test_not_encode_nonexistent_codec():
with pytest.raises(ValueError):
with pytest.raises(MulticodecKeyError):
content_hash.encode('this-codec-does-not-exist', 'value')

0 comments on commit 24ac291

Please sign in to comment.