Moralis WalletConnect

2022-06-02 17:16 更新

将 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​。


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号