You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发数字钱包时在Web扩展中使用JS IPFS遇错,求解决方案

Web扩展中IPFS节点创建错误的修复方案

1. 适配Web扩展环境调整IPFS节点配置

Web扩展的沙箱环境和普通网页存在差异,默认IPFS配置会触发网络或权限问题,需要精简配置,只启用兼容的传输方式:

try {
  const node = await Ipfs.create({
    // 生成唯一仓库名,避免扩展存储冲突
    repo: 'ipfs-' + Math.random().toString(36).substr(2, 9),
    config: {
      Addresses: {
        // 只启用WebRTC Star传输,适配浏览器环境
        Swarm: ['/webrtc-star/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star']
      },
      // 禁用默认Bootstrap节点,避免跨域请求被拦截
      Bootstrap: [],
      Discovery: {
        MDNS: { Enabled: false }, // 禁用本地MDNS发现,扩展环境不支持
        webRTCStar: { Enabled: true }
      }
    },
    libp2p: {
      config: {
        transports: {
          // 禁用不兼容的TCP、WebSocket传输
          '/ipfs/libp2p-tcp': false,
          '/ipfs/libp2p-websocket': false
        }
      }
    }
  })

  const results = await node.add('hello manlio')
  document.getElementById('demo').innerHTML = JSON.stringify(results)
} catch (err) {
  console.error('IPFS运行错误:', err)
  document.getElementById('demo').innerHTML = '错误详情: ' + err.message
}

2. 配置扩展权限

在扩展的manifest.json中添加必要权限,确保IPFS能正常访问网络和存储:

  • Manifest V2配置:
{
  "permissions": [
    "storage",
    "https://*/*"
  ]
}
  • Manifest V3配置:
{
  "permissions": ["storage"],
  "host_permissions": ["https://*/*"]
}

注意:Manifest V3中不要在Service Worker中运行IPFS代码,建议在Popup页面或Content Script的上下文执行。

3. 依赖包版本检查

确保使用适配浏览器环境的IPFS包,推荐使用ipfs-core(而非旧版ipfs),安装命令:

npm install ipfs-core

引入时使用:

import { create } from 'ipfs-core'

内容的提问来源于stack exchange,提问作者manlio98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:40:26