WalletConnect 钱包连接

  1. 下载依赖
  npm install --save @walletconnect/sign-client @web3modal/standalone
  1. 配置命令
    utils/wallet.js
// 钱包连接-
import { SignClient } from '@walletconnect/sign-client'
import { Web3Modal } from '@web3modal/standalone'

// 1. Define constants
const projectId = 'xxxxxxxxxxxxxxxxxxxx'
const namespaces = {
  eip155: { methods: ['eth_sign'], chains: ['eip155:1'], events: ['accountsChanged'] }
}

// 3. Create modal client
const web3Modal = new Web3Modal({ projectId, standaloneChains: namespaces.eip155.chains })
let signClient = undefined

// 4. Initialise clients
export async function initClient() {
  setInterval(() => {
    console.log('keep alive')
  }, 2000)
  try {
    signClient = await SignClient.init({ projectId })
  } catch (err) {
    console.error(err)
  }
}

// 5. 打开钱包
export const openWallet = async () => {
  try {
    if (signClient) {
      const { uri, approval } = await signClient.connect({ requiredNamespaces: namespaces })
      if (uri) {
        await web3Modal.openModal({ uri })
        await approval()
        web3Modal.closeModal()
      }
    }
  } catch (err) {
    console.error(err)
  }
}

  1. 使用app.vue
<template>
  <a-button v-if="initWalletSuccess" type="primary" class="qr-code-btn" @click="onClickWallet">
    Open in wallet
  </a-button>
</template>

<script>
import { openWallet, initClient  } from '@/utils/wallet.js'
export default {
  data () {
    return {
      initWalletSuccess: false
    }
  },
  mounted() {
    this.initWallet()
  },
  methods: {
    async initWallet() { // 初始化wallet钱包
      console.log('init wallet ...')
      this.initWalletSuccess = false;
      await initClient()
      this.initWalletSuccess = true;
      console.log('init wallet success')
    },
    async onClickWallet() { // 使用wallet钱包
      console.log('onClickWallet')
      openWallet()
    }
  }
}
</script>
  1. 其他 - 报错

列如:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解决:

module.exports = {
  configureWebpack: {
    module: {
       rules: [{ test: /node_modules[\\/]@walletconnect/, loader: 'babel-loader' }, { test: /node_modules[\\/]@web3modal/, loader: 'babel-loader' }]
    }
  }
}

展示:

在这里插入图片描述
在这里插入图片描述

参考链接:

  • 官方文档 https://docs.walletconnect.com/2.0/web3modal/html-js/installation
  • Project id生成连接 https://cloud.walletconnect.com/sign-in
  • 解决报错 https://github.com/WalletConnect/walletconnect-monorepo/issues/1349
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值