Skip to main content

ContentHash for Python

version downloads license python

linux build windows build coverage quality

Python implementation of EIP 1577 content hash.

Description

This is a simple package made for encoding and decoding content hashes has specified in the EIP 1577. This package will be useful for every Ethereum developer wanting to interact with EIP 1577 compliant ENS resolvers.

For JavaScript implementation, see pldespaigne/content-hash.

Installation

Requirements

ContentHash requires Python 3.5 or higher.

From PyPI

The recommended way to install ContentHash is from PyPI with PIP.

pip install content-hash

From Source

Alternatively, you can also install it from the source.

git clone https://github.com/filips123/ContentHashPy.git
cd ContentHashPy
python setup.py install

Usage

Supported Codecs

The following codecs are currently supported:

  • swarm-ns
  • ipfs-ns
  • ipns-ns

Every other codec supported by multicodec will be encoded by default in utf-8. You can see the full list of the supported codecs here.

Getting Codec

You can use a get_codec function to get codec from the content hash.

It takes a content hash as a HEX string and returns the codec name. A content hash can be prefixed with a 0x, but it's not mandatory.

import content_hash

chash = 'bc037a716b746c776934666563766f367269'
codec = content_hash.get_codec(chash)

print(codec) # onion

Decoding

You can use a decode function to decode a content hash.

It takes a content hash as a HEX string and returns the decoded content as a string. A content hash can be prefixed with a 0x, but it's not mandatory.

import content_hash

chash = 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
value = content_hash.decode(chash)

print(value) # QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4

Encoding

You can use an encode function to encode a content hash.

It takes a supported codec as a string and a value as a string and returns the corresponding content hash as a HEX string. The output will not be prefixed with a 0x.

import content_hash

codec = 'swarm-ns'
value = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
chash = content_hash.encode(codec, value)

print(chash) # e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162

Creating Codecs

All supported codec profiles are available in content_hash/profiles/__init__.py, in PROFILES dictionary. You need to add a new profile there. You only need to add a new profile if your codec encoding and decoding are different from utf-8.

Each profile must have the same name as the corresponding codec in the multicodec library.

A profile must also have decode and encode function. They should be passed as a string containing the name of the module for required decode or encode. All such modules are available in content_hash/decodes and content_hash/encodes.

Each module name should describe it as much as possible. Its name can only contain valid characters for Python modules.

Each decode module must have a decode function. It must be a function that takes a bytes input and returns a str result.

Each encode module must have an encode function. It must be a function that takes a str input and returns a bytes result.

All inputs and outputs must be the same as in the JavaScript implementation. Multiple profiles can share the same decodes and encodes.

Versioning

This library uses SemVer for versioning. For the versions available, see the tags on this repository.

License

This library is licensed under the MIT license. See the LICENSE file for details.

Metadata

Release files for rotki-content-hash 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rotki-content-hash 0.0.3
File Size Uploaded Provenance Actions
rotki-content-hash-0.0.3.tar.gz 10.7 kB Without provenance Details for rotki-content-hash-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rotki-content-hash 0.0.3
File Interpreter ABI Platform Actions
rotki_content_hash-0.0.3-py3-none-any.whl Python 3 none any Details for rotki_content_hash-0.0.3-py3-none-any.whl

Total release size: 21.2 kB

Release files / rotki-content-hash-0.0.3.tar.gz

Download URL rotki-content-hash-0.0.3.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
392ae6c9f3f54f0c6984a40f9e81b7e045fdd5758079058478e83399d10f96d2
BLAKE2b-256 checksum
How to use checksums
de138da7f1df598a37235c8283118ca568ac9b611f27db3e4ea25294961f17b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.10

Release files / rotki_content_hash-0.0.3-py3-none-any.whl

Download URL rotki_content_hash-0.0.3-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b6ebc6b4a560588f4bb87b2ac97253753d283cb8e0e3937e31cdda55f65387f
BLAKE2b-256 checksum
How to use checksums
d4aecabdcda5473cfaff8e8e7b6158f5f3591cdd96f064b82d44365a66ae5a4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.10

Security for rotki-content-hash 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page