Web3 接入 WalletConnect1.0
Web3 接入 WalletConnect1.0
安装依赖
pnpm add web3 @walletconnect/web3-provider |
创建 walletConnetct provider
import WalletConnectProvider from "@walletconnect/web3-provider" |
创建 web3 实例
import Web3 from 'web3' |
断开连接
const killSession = () => { |
可能存在的问题
-
使用 vite 报 Buffer process EventEmitter global 不存在的错误
解决方案一(推荐)
# 安装以下依赖
pnpm add -D rollup-plugin-polyfill-node修改 vite.config.ts
import nodePolyfills from 'rollup-plugin-polyfill-node'
export default defineConfig({
// ...
plugins: [
{
...nodePolyfills ({ include: ['node_modules/**/*.js', /node_modules\/.vite\/.*js/] }),
apply: 'serve'
},
],
build: {
rollupOptions: {
plugins: [nodePolyfills()]
},
commonjsOptions: {
transformMixedEsModules: true
}
}
// ...
})解决方案二
# 安装以下依赖
pnpm add buffer buffer process util在 index.html 中添加以下代码
<head>
<script>
window.global = window;
</script>
<script type="module">
import process from "process";
import { Buffer } from "buffer";
import EventEmitter from "events";
window.Buffer = Buffer;
window.process = process;
window.EventEmitter = EventEmitter;
</script>
</head>在 vite.config.ts 中添加以下代码
// 解决 build 后报错
export default defineConfig({
// ...
build: {
commonjsOptions: {
transformMixedEsModules: true
}
}
// ...
}) -
无法调起 walletConnect QR code modal
walletConnect 必须在 https 环境下使用
// 配置 vite.config.ts
export default defineConfig({
// ...
server: {
https: true
}
// ...
})或者使用 nginx 配置 https
# nginx.conf
server {
listen 443 ssl http2;
server_name example.io;
# 引入 ssl 证书
include ssl/example.io/ssl.conf;
location / {
proxy_pass https://127.0.0.1:3000;
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 日常小记!
评论
TwikooDisqus