Package: content-hash
ContentHash for PythonPython implementation of EIP 1577 content hash.DescriptionThis is a simple package made for encoding and decoding content hashes has specified in theEIP 1577.
This package will be useful for everyEthereumdeveloper wanting to interact withEIP 1577compliantENS resolvers.For JavaScript implementation, seepldespaigne/content-hash.InstallationRequirementsContentHash requires Python 3.8 or higher.From PyPIThe recommended way to install ContentHash is from PyPI with PIP.pipinstallcontent-hashFrom SourceAlternatively, you can also install it from the source.gitclonehttps://github.com/filips123/ContentHashPy.gitcdContentHashPy
pipinstall.UsageSupported CodecsThe following codecs are currently supported:swarmipfsipnsEvery other codec supported bymulticodecwill be encoded by default inutf-8. You can see the full list of the supported codecshere.Getting CodecYou can use aget_codecfunction 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 a0x, but it's not mandatory.importcontent_hashchash='bc037a716b746c776934666563766f367269'codec=content_hash.get_codec(chash)print(codec)# onionDecodingYou can use adecodefunction 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 a0x, but it's not mandatory.importcontent_hashchash='e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'value=content_hash.decode(chash)print(value)# QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4EncodingYou can use anencodefunction 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 a0x.importcontent_hashcodec='swarm-ns'value='d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'chash=content_hash.encode(codec,value)print(chash)# e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162Creating CodecsAll supported codec profiles are available incontent_hash/profiles/__init__.py, inPROFILESdictionary. You need to add a new profile there. You only need to add a new profile if your codec encoding and decoding are different fromutf-8.Each profile must have the same name as the corresponding codec in themulticodeclibrary.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 incontent_hash/decodesandcontent_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 adecodefunction. It must be a function that takes abytesinput and returns astrresult.Each encode module must have anencodefunction. It must be a function that takes astrinput and returns abytesresult.All inputs and outputs must be the same as in theJavaScript implementation. Multiple profiles can share the same decodes and encodes.VersioningThis library usesSemVerfor versioning. For the versions available, seethe tagson this repository.LicenseThis library is licensed under the MIT license. See theLICENSEfile for details.