pldespaigne / content-hash

simple tool to encode/decode content hash for EIP 1577 compliant ENS Resolvers
ISC License
44 stars 28 forks source link

content-hash

npm packageCircleCIlicenceGitter chatBeerpay

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

Here you can find a live demo of this package.

🔠 Supported Codec

📥 Install

🛠 Usage

Import the module in order to use it :

📕 API

All hex string inputs can be prefixed with 0x, but it's not mandatory.

⚠️ All outputs are NOT prefixed with 0x

contentHash.decode( contentHash ) -> 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( ipfsHash ) -> string

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


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**.
> this function just call `contentHash.encode()` under the hood
```javascript
const swarmHash = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'

const contentH = contentHash.fromSwarm(swarmHash)
// 'e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'

contentHash.encode( codec, value) -> string

This function takes a supported codec as a string and a value as a string and returns coresponding content hash as a hex string.

const onion = 'zqktlwi4fecvo6ri'
contentHash.encode('onion', onion);
// 'bc037a716b746c776934666563766f367269'

contentHash.getCodec( 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.getCodec(encoded) // 'swarm-ns'
codec === 'ipfs-ns' // false

contentHash.helpers

This object contain the following helpers functions :

👨‍💻 Maintainer

🙌 Contributing

For any questions, discussions, bug report, or whatever I will be happy to answer through the issues or on my twitter 😁. PR (with tests) 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