将 Moralis 与 WalletConnect 集成

WalletConnect ​让您可以通过二维码、移动钱包和桌面钱包进行连接。 您可以在 WalletConnect 文档中查看更多信息。

1. 添加 WalletConnect 提供商

根据moralis 导入项目的方式添加提供程序脚本 – ​CDN​、​npm ​或 ​yarn​。

<script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.7.1/web3-provider.min.js" rel="external nofollow" ></script>
npm install @walletconnect/web3-provider
yarn add @walletconnect/web3-provider

确保检查您是否使用最新的稳定版本的 ​WalletConnect web3-provider​,并相应地更新版本。 在 Github 上查看他们的最新版本

2.调用鉴权函数

调用身份验证函数,但带有​provider​选项,可以选择使用​JS​或者​React​:

const user = await Moralis.authenticate({ provider: "walletconnect" })
import { useMoralis } from "react-moralis";

function App() {

    const { authenticate, isAuthenticated, user } = useMoralis();

    const login = async () => {
      if (!isAuthenticated) {

        await authenticate({ provider: "walletconnect" })
          .then(function (user) {
            console.log(user!.get("ethAddress"));
          })
          .catch(function (error) {
            console.log(error);
          });
      }
    }
}

3.指定chainId

指定 ​WalletConnect ​默认使用的链 ID。 您可以通过提供 ​chainId ​作为额外选项来做到这一点,可以选择使用​JS​或者​React​:

const user = await Moralis.authenticate({ provider: "walletconnect", chainId: 56 })
import { useMoralis } from "react-moralis";

function App() {

    const { authenticate, isAuthenticated, user } = useMoralis();

    const login = async () => {
      if (!isAuthenticated) {

        await authenticate({ provider: "walletconnect", chainId: 56 })
          .then(function (user) {
            console.log(user!.get("ethAddress"));
          })
          .catch(function (error) {
            console.log(error);
          });
      }
    }
}

4.过滤移动链接选项

要减少移动链接选项的数量或自定义其顺序,请为 ​mobileLinks ​选项提供一组​wallet names​,可以选择使用​JS​或者​React​。

admin

这个人很懒,什么都没有留下~

Share
Published by
admin

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago