simple tool to encode/decode content hash for EIP 1577 compliant ENS Resolvers
npm install content-hash
swarm-ns
ipfs-ns
ipns-ns
utf-8.
bash
$> npm install content-hash
`
* via Github : Download or clone this repo, then install the dependencies.
`bash
$> 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 :
`javascript
const contentHash = require('content-hash')
`
* Browser :
`html
`
> To rebuild the browser version of the package run npm run build into the root folder. Don't forget to also run npm run lint and npm test before building !
📕 API
> All hex string inputs can be prefixed with 0x, but it's not mandatory.
> ⚠️ All outputs are NOT prefixed with 0x
$3
This function takes a content hash as a hex string and returns the decoded content as a string.
`javascript
const encoded = 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
const content = contentHash.decode(encoded)
// 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
`
$3
This function takes an IPFS address as a base58 encoded string and returns the encoded content hash as a hex string.
> this function just call contentHash.encode() under the hood
`javascript
const ipfsHash = 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
const contentH = contentHash.fromIpfs(ipfsHash)
// 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
`
$3
This function takes a Swarm address as a hex string and returns the encoded content hash as a hex string.
> this function just call contentHash.encode() under the hood
`javascript
const swarmHash = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
const contentH = contentHash.fromSwarm(swarmHash)
// 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
`
$3
This function takes a supported codec as a string and a value as a string and returns coresponding content hash as a hex string.
`javascript
const onion = 'zqktlwi4fecvo6ri'
contentHash.encode('onion', onion);
// 'bc037a716b746c776934666563766f367269'
`
$3
This function takes a content hash as a hex string and returns the codec as a hex string.
`javascript
const encoded = 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
const codec = contentHash.getCodec(encoded) // 'swarm-ns'
codec === 'ipfs-ns' // false
`
$3
This object contain the following helpers functions :
- #### cidV0ToV1Base32( ipfsHash ) -> string
This function takes an ipfsHash and convert it to a CID v1 encoded in base32.
`javascript
const ipfs = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
const cidV1 = contentHash.helpers.cidV0ToV1Base32(ipfs)
// 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4'
``