@ensdomains/content-hash

中文文档
3.2.0发布于 2026/11/04MITTypeScriptES Module构建来源已验证近 30 天下载 9.8万

A simple tool to encode/decode content hash for EIP 1577 compliant ENS Resolvers (fork of pldespaigne/content-hash)

原始使用文档
文档目录10 节
  1. 🔠 Supported Codec
  2. 📥 Install
  3. 🛠 Usage
  4. 📕 API
  5. decode( contentHash ) -> string
  6. encode( codec, value) -> string
  7. getCodec( contentHash ) -> string
  8. Helpers
  9. 📝 License
  10. 👥 Acknowledgements

content-hash

npm package licence

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.

🔠 Supported Codec

  • swarm
  • ipfs
  • ipns
  • onion
  • onion3
  • skynet
  • arweave
  • adnl
  • ton-storage (tonstorage://<BagID>)

📥 Install

npm install @ensdomains/content-hash

🛠 Usage

import { decode } from "@ensdomains/content-hash";

const encoded =
  "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f";

const content = decode(encoded);
// 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'

📕 API

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

⚠️ All outputs are NOT prefixed with 0x

decode( contentHash ) -> string

This function takes a content hash as a hex string and returns the decoded content as a string.

import { decode } from "@ensdomains/content-hash";

const encoded =
  "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f";

const content = decode(encoded);
// 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'

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.

import { encode } from "@ensdomains/content-hash";

const onion = "zqktlwi4fecvo6ri";

const encoded = encode("onion", onion);
// 'bc037a716b746c776934666563766f367269'

getCodec( contentHash ) -> string

This function takes a content hash as a hex string and returns the codec as a hex string.

import { getCodec } from "@ensdomains/content-hash";

const encoded =
  "e40101701b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162";

const codec = getCodec(encoded);
// 'swarm'

Helpers

  • cidV0ToV1Base32( ipfsHash ) -> string

    This function takes an ipfsHash and converts it to a CID v1 encoded in base32.

    import { cidV0ToV1Base32 } from "@ensdomains/content-hash";
    
    const ipfs = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
    
    const cidV1 = cidV0ToV1Base32(ipfs);
    // 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4'
    
  • cidForWeb( ipfsHash ) -> string

    This function takes any ipfsHash and converts it to DNS-compatible CID.

    import { cidForWeb } from "@ensdomains/content-hash";
    
    const ipfs = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
    
    const cidV1 = cidForWeb(ipfs);
    // 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4'
    

📝 License

This project is licensed under the MIT License, you can find it here.

Note that the dependencies may have a different License

👥 Acknowledgements

This repo is forked from the original content-hash library, which was written by pldespaigne.

版本与发布记录

@ensdomains/content-hash 共发布 18 个版本,首次发布于 2020/10/05;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.2.0latest2026/11/049 天前
3.1.12026/06/184 个月前
3.1.02026/06/184 个月前
3.0.12025/11/2411 个月前
3.1.0-rc.1next2024/01/152 年前
3.1.0-rc.02023/11/103 年前

关于 @ensdomains/content-hash 的常见问题

@ensdomains/content-hash 是什么?

A simple tool to encode/decode content hash for EIP 1577 compliant ENS Resolvers (fork of pldespaigne/content-hash)

如何安装 @ensdomains/content-hash?

在项目目录运行 npm install @ensdomains/content-hash,或使用 pnpm add / yarn add / bun add。

@ensdomains/content-hash 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ensdomains/content-hash 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ensdomains/content-hash 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ensdomains/content-hash 可以商用吗?

@ensdomains/content-hash 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ensdomains/content-hash 的下载量有多少?

根据 npm 官方下载统计,@ensdomains/content-hash 在 2026/10/14 – 2026/11/12 这 30 天内被下载 97,832 次,日均约 3,261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ensdomains/content-hash 还在维护吗?

最新版本 3.2.0 发布于 2026/11/04(9 天前);至今共发布 18 个版本,其中过去 12 个月发布了 4 个;首次发布于 2020/10/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。