Skip to content

Commit

Permalink
_encodeReference function fixed. test_encode_and_decode_to_same_refer…
Browse files Browse the repository at this point in the history
…ence test passed
  • Loading branch information
Aviksaikat committed Nov 10, 2023
1 parent a53b810 commit e1719a3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<img src="https://img.shields.io/github/languages/top/Aviksaikat/swarm-cid-py?style=for-the-badge&color=5D6D7E" alt="GitHub top language" />
</div>

---
______________________________________________________________________

## 📖 Table of Contents

Expand All @@ -32,13 +32,13 @@
- [🤝 Contributing](#-contributing)
- [📄 License](#-license)

---
______________________________________________________________________

## 📍 Overview

Utility library written in Python to convert Swarm hex references into Swarm CIDs

---
______________________________________________________________________

## 🚀 Getting Started

Expand All @@ -48,31 +48,31 @@ Utility library written in Python to convert Swarm hex references into Swarm CID
py-multiformats-cid
```

---
______________________________________________________________________

### 🔧 Installation

```sh
pip install swarm_cid_py
```

---
______________________________________________________________________

### 🤖 Running swarm-cid-py

```sh

```

---
______________________________________________________________________

### 🧪 Tests

```sh

```

---
______________________________________________________________________

## 🤝 Contributing

Expand Down Expand Up @@ -111,12 +111,12 @@ Once your PR is reviewed and approved, it will be merged into the main branch.

</details>

---
______________________________________________________________________

## 📄 License

This project is protected under the [BSD-3-Clause](./LICENSE) License.

[**Return**](#Top)

---
______________________________________________________________________
8 changes: 6 additions & 2 deletions src/swarm_cid/swarm_cid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/test_swarm_cid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit e1719a3

Please sign in to comment.