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

Update the tool to use more modern dependencies #2

Closed
Closed
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
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
9 changes: 3 additions & 6 deletions content_hash/decodes/b58_multi_hash.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Decode module for B58 multi hash."""

from cid import make_cid
import multihash

from multiformats import multihash, CID
from base58check import b58encode

def decode(value):
"""
Expand All @@ -13,6 +12,4 @@ def decode(value):
:return: the decoded content
:rtype: str
"""

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

from cid import make_cid
import multihash
from multiformats import multihash, CID


def decode(value):
Expand All @@ -13,6 +12,4 @@ def decode(value):
:return: the decoded content
:rtype: str
"""

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

from cid import make_cid
import multihash
import base58check
from multiformats import CID, multicodec
from content_hash.utils import raw_cid_value

def encode(value):
"""
Expand All @@ -10,8 +11,8 @@ def encode(value):
:param bytes value: a decoded content

:return: the encoded content
:rtype: str
:rtype: bytes
"""

mhash = multihash.from_b58_string(value)
return make_cid(1, 'dag-pb', mhash).buffer
mhash = base58check.b58decode(value)
cid = CID(base='base58btc', codec='dag-pb', version=1, digest=mhash)
return raw_cid_value(cid)
12 changes: 6 additions & 6 deletions content_hash/encodes/swarm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Encode module for Swarm."""

from cid import make_cid
import multihash
from multiformats import multihash, CID
from content_hash.utils import raw_cid_value

def encode(value):
"""
Expand All @@ -10,8 +10,8 @@ def encode(value):
:param bytes value: a decoded content

:return: the encoded content
:rtype: str
:rtype: bytes
"""

mhash = multihash.encode(multihash.from_hex_string(value), 'keccak-256')
return make_cid(1, 'swarm-manifest', mhash).buffer
mhash = multihash.wrap(bytes.fromhex(value), 'keccak-256')
cid = CID(base='base58btc', codec='swarm-manifest', version=1, digest=mhash)
return raw_cid_value(cid)
1 change: 0 additions & 1 deletion content_hash/encodes/utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ def encode(value):
:return: the encoded content
:rtype: str
"""

return value.encode('utf-8')
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
5 changes: 5 additions & 0 deletions content_hash/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from multiformats import multicodec

def raw_cid_value(cid):
"""Return raw representation of the CID as seen in multiformats_cid in bytes"""
return b''.join([cid.version.to_bytes(1, byteorder='big'), multicodec.wrap(cid.codec, cid.digest)])
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from setuptools import setup


def readme():
with open('README.md') as f:
return f.read()
Expand All @@ -21,9 +22,10 @@ def readme():
},

install_requires = [
'py-cid>=0.3.0,<0.4.0',
'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 All @@ -49,6 +51,5 @@ def readme():
'Topic :: Software Development :: Libraries',
'Topic :: Utilities',
],

include_package_data = True,
)
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')