Created
January 11, 2022 21:18
-
-
Save miguelmota/a2475e0a900a6d737962b4e7bf3c773d to your computer and use it in GitHub Desktop.
JavaScript generate ENS contenthash (content) IPFS IPNS hash value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bs58 = require('bs58') | |
const contentHash = require('content-hash') | |
const domain = 'app.uniswap.org' | |
const text = `ipns://${domain}` | |
const matched = text.match(/^(ipfs|ipns|bzz|onion|onion3):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/) || text.match(/\/(ipns)\/(.*)/) | |
const contentType = matched[1] | |
const content = matched[2] | |
const bs58content = bs58.encode( | |
Buffer.concat([ | |
Buffer.from([0, content.length]), | |
Buffer.from(content) | |
]) | |
) | |
const ensContentHash = '0x' + contentHash.encode('ipns-ns', bs58content) | |
console.log(ensContentHash) // 0xe5010170000f6170702e756e69737761702e6f7267 | |
const decoded = contentHash.decode(ensContentHash) | |
console.log(decoded) // 12uA8M8Ku8mHUumxHcu7uee |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment