From e1719a3c77e204a8aad4d7b31975f149d136abed Mon Sep 17 00:00:00 2001 From: SAIKAT KARMAKAR Date: Fri, 10 Nov 2023 02:00:48 +0000 Subject: [PATCH] _encodeReference function fixed. test_encode_and_decode_to_same_reference test passed --- README.md | 18 +++++++++--------- src/swarm_cid/swarm_cid.py | 8 ++++++-- tests/conftest.py | 6 +++--- tests/test_swarm_cid.py | 8 ++++---- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fa1f1b1..b7eebe9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ GitHub top language ---- +______________________________________________________________________ ## ๐Ÿ“– Table of Contents @@ -32,13 +32,13 @@ - [๐Ÿค Contributing](#-contributing) - [๐Ÿ“„ License](#-license) ---- +______________________________________________________________________ ## ๐Ÿ“ Overview Utility library written in Python to convert Swarm hex references into Swarm CIDs ---- +______________________________________________________________________ ## ๐Ÿš€ Getting Started @@ -48,7 +48,7 @@ Utility library written in Python to convert Swarm hex references into Swarm CID py-multiformats-cid ``` ---- +______________________________________________________________________ ### ๐Ÿ”ง Installation @@ -56,7 +56,7 @@ py-multiformats-cid pip install swarm_cid_py ``` ---- +______________________________________________________________________ ### ๐Ÿค– Running swarm-cid-py @@ -64,7 +64,7 @@ pip install swarm_cid_py ``` ---- +______________________________________________________________________ ### ๐Ÿงช Tests @@ -72,7 +72,7 @@ pip install swarm_cid_py ``` ---- +______________________________________________________________________ ## ๐Ÿค Contributing @@ -111,7 +111,7 @@ Once your PR is reviewed and approved, it will be merged into the main branch. ---- +______________________________________________________________________ ## ๐Ÿ“„ License @@ -119,4 +119,4 @@ This project is protected under the [BSD-3-Clause](./LICENSE) License. [**Return**](#Top) ---- +______________________________________________________________________ diff --git a/src/swarm_cid/swarm_cid.py b/src/swarm_cid/swarm_cid.py index 920802c..ee0d505 100644 --- a/src/swarm_cid/swarm_cid.py +++ b/src/swarm_cid/swarm_cid.py @@ -58,14 +58,18 @@ def _encodeReference(ref: Union[str, Reference], codec: str, version: Optional[i """ hash_bytes = hexToBytes(ref) - return CIDv1(codec, multihash.digest(hash_bytes, KECCAK_256_CODEC)) + _hash = multihash.digest(hash_bytes, KECCAK_256_CODEC).hex() + new_hash = hexToBytes(_hash[:4] + ref) + + return CIDv1(codec, new_hash) def _decodeReference(cid: Union[CIDv0, CIDv1, str]) -> DecodeResult: if isinstance(cid, str): cid = parse(cid) - reference = bytesTohex(cid.multihash) + # remove the hashtype + lengh i.e. first 4 bytes + reference = bytesTohex(cid.multihash)[4:] content_type = TYPE_MAPPING.get(cid.codec, "") # type: ignore return DecodeResult(reference, content_type) diff --git a/tests/conftest.py b/tests/conftest.py index df7be66..f49164d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,14 +2,14 @@ @pytest.fixture -def test_reference(): +def test_swarm_reference(): return "4c949794d617238d928ef1dc544ee07cbdcfd6b946e5202fa06c4d32088d7e69" @pytest.fixture -def test_manifest_cid(): +def test_swarm_manifest_cid(): # return "bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a" - return "bah5acgzaukog5d75a3uftpeeikfy35gnw2mmvulh4ktwebzfbsbqxskto6ha" + return "bah5acgzajskjpfgwc4ry3euo6hofitxaps647vvzi3ssal5anrgtecenpzuq" @pytest.fixture diff --git a/tests/test_swarm_cid.py b/tests/test_swarm_cid.py index 97276b7..6435008 100644 --- a/tests/test_swarm_cid.py +++ b/tests/test_swarm_cid.py @@ -11,15 +11,15 @@ def test_encode_and_decode_to_same_reference( - test_reference, test_manifest_cid, SWARM_MANIFEST_CODEC + test_swarm_reference, test_swarm_manifest_cid, SWARM_MANIFEST_CODEC ): - cid = encodeReference(test_reference, ReferenceType.MANIFEST, 1) + cid = encodeReference(test_swarm_reference, ReferenceType.MANIFEST, 1) assert cid.codec == SWARM_MANIFEST_CODEC - assert cid.encode() == test_manifest_cid + assert cid.encode().decode() == test_swarm_manifest_cid assert decodeCid(cid).to_dict() == { - "reference": cid.multihash.hex(), + "reference": test_swarm_reference, "type": ReferenceType.MANIFEST, }