Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 11, 2022 21:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/a2475e0a900a6d737962b4e7bf3c773d to your computer and use it in GitHub Desktop.
Save miguelmota/a2475e0a900a6d737962b4e7bf3c773d to your computer and use it in GitHub Desktop.
JavaScript generate ENS contenthash (content) IPFS IPNS hash value
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