能否用Requestly修改Coinbase Wallet Chrome扩展的eth_chainId请求?
问题描述
Coinbase Wallet Chrome扩展在添加自定义网络时,会发送格式不符合JSON-RPC规范的eth_chainId请求,缺失params和id字段。需要拦截并修改该请求,使其符合合规格式:
扩展发出的错误请求:
curl https://testnet.hashio.io/api \ -X POST \ -H "Content-Type: application/json" \ --data '{"jsonrpc":"2.0","method":"eth_chainId"}'
合规请求格式:
curl https://testnet.hashio.io/api \ -X POST \ -H "Content-Type: application/json" \ --data '{"method":"eth_chainId","params":[],"id":1,"jsonrpc":"2.0"}'
尝试使用Requestly拦截修改,但未成功,疑问是否可行或有其他替代方案。
Requestly的可行性分析
Requestly等常规网页请求修改工具无法拦截Chrome扩展的网络请求,原因是Chrome的扩展隔离机制:扩展的请求属于独立上下文,Requestly默认仅拥有拦截网页(http://*/*、https://*/*)请求的权限,无法访问其他扩展的请求上下文,因此无法捕获Coinbase Wallet发出的请求。
替代解决方案
1. 自定义Chrome扩展拦截修改
编写小型Chrome扩展,利用webRequest API拦截并修改目标请求,这是最可靠的方案。
实现步骤:
- 创建扩展目录,新建
manifest.json:
{ "manifest_version": 3, "name": "Fix CB Wallet RPC Request", "version": "1.0", "permissions": ["webRequest", "webRequestBlocking"], "host_permissions": ["https://testnet.hashio.io/api"], "background": { "service_worker": "background.js" } }
- 新建
background.js(服务工作脚本):
chrome.webRequest.onBeforeRequest.addListener( (details) => { // 仅处理目标URL的POST请求 if (details.method !== "POST") return; try { // 解析原始请求体 const rawBody = details.requestBody.raw[0].bytes; const bodyObj = JSON.parse(new TextDecoder().decode(rawBody)); // 仅修改eth_chainId请求 if (bodyObj.method === "eth_chainId") { // 补充缺失字段 const fixedBody = { ...bodyObj, params: [], id: 1 }; return { requestBody: JSON.stringify(fixedBody) }; } } catch (e) { console.error("Failed to modify request:", e); } }, { urls: ["https://testnet.hashio.io/api"] }, ["blocking", "requestBody"] );
- 加载扩展:Chrome浏览器进入
chrome://extensions/,开启开发者模式,点击“加载已解压的扩展程序”,选择创建的目录即可。
2. 修改Coinbase Wallet扩展源码
直接修改Coinbase Wallet扩展的请求发送逻辑,但需要注意:
- 找到扩展的安装目录(Chrome中可在
chrome://extensions/找到ID,然后在系统对应目录查找); - 定位到发送
eth_chainId请求的代码,补充params和id字段; - 重新打包扩展并加载,但Chrome会提示“未经验证的扩展”,且官方更新会覆盖修改。
3. 系统级代理工具拦截
使用Charles、MitmProxy等系统代理工具,在流量层面拦截并修改请求:
- Charles:开启代理后,添加Rewrite规则:
- 匹配URL为
https://testnet.hashio.io/api; - 选择“Request Body”修改类型;
- 使用正则替换,将
{"jsonrpc":"2.0","method":"eth_chainId"}替换为{"method":"eth_chainId","params":[],"id":1,"jsonrpc":"2.0"};
- 匹配URL为
- 该方案无需编写代码,但需要配置系统代理,且所有流量会经过代理。
内容的提问来源于stack exchange,提问作者MShakeG
相关产品推荐
相关产品推荐

