Package Exports
- content-hash
- content-hash/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (content-hash) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
content-hash
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.
⚠️ The EIP 1577 spec can change rapidlly, expect this lib to do the same !
Here you can find a live demo of this package.
📥 Install
- via npm :
$> npm install content-hash
- via Github : Download or clone this repo, then install the dependencies.
$> git clone https://github.com/pldespaigne/content-hash.git $> cd content-hash $> npm install
For browser only usage, installation is not required.
🛠 Usage
Import the module in order to use it :
- NodeJS :
const contentHash = require('content-hash')
- Browser :
<!--From CDN--> <script type="text/javascript" src="https://unpkg.com/content-hash/dist/index.js"></script> <!--From local module--> <script type="text/javascript" src="path/to/dist/index.js"></script>
To rebuild the browser version of the package run
npm run build
into the root folder.
📕 API
All hex string inputs can be prefixed with
0x
, but it's not mandatory
contentHash.Types
This is an object that defines the codec type constants.
- contentHash.Types.ipfs : codec for IPFS content hashes =
0xe3
- contentHash.Types.swarm : codec for Swarm content hashes =
0xe4
contentHash.Types
will be deprecated soon !
contentHash.decode( string ) -> string
This function takes a content hash as a hex string and returns the decoded content as a string.
const encoded = 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
const content = contentHash.decode(encoded)
// 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
contentHash.fromIpfs( string ) -> string
This function takes an IPFS address as a base58 encoded string and returns the encoded content hash as a hex string.
const ipfsHash = 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
const contentH = contentHash.fromIpfs(ipfsHash)
// 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
contentHash.fromSwarm( swarmHash ) -> string
This function takes a Swarm address as a hex string and returns the encoded content hash as a hex string.
const swarmHash = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
const contentH = contentHash.fromSwarm(swarmHash)
// 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
contentHash.getCodecType( contentHash ) -> string
This function takes a content hash as a hex string and returns the codec as a hex string.
const encoded = 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
const codec = contentHash.getCodecType(encoded) // 'e4'
// here codec is equal to contentHash.Types.swarm.toString('hex')
contentHash.isHashOfType( contentHash, type ) -> boolean
This function takes a content hash as a hex string and a codec as a contentHash.Types and returns a boolean depending on wether or not the content hash has the same codec as the type.
const encoded = 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
contentHash.isHashOfType(encoded, contentHash.Types.swarm) // true
🚫 contentHash.fromBuffer( codec, buffer ) -> string
DEPRECATED : will be reomved soon, use addPrefix() from the multicodec lib.
This function takes a codec as a hex string and a value as a hex string and returns the encoded content hash as a hex string.
const value = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
const contentH = contentHash.fromBuffer('00', value)
// '00d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
👨💻 Maintainer
🙌 Contributing
For any questions, discussions, bug report, or whatever I will be happy to answer through the issues or on my twitter 😁. PR are also welcome !
📝 License
This project is licensed under the ISC License, you can find it here.
Note that the dependencies may have a different License