Wallet Intergration

In this section, we will provide sample snippets for you to use to connect to your dApp on Zetrix. Here we provide support for both browser based interaction (Chromium-based browsers only) as well as mobile wallet interaction.

Browser-based connection

To use the following snippet, you will have to first install the Zetrix Chrome wallet here. Once you have installed, you can connect using the Zetrix object within the Window global object. The following snippet can be used to connect your dApp to the Zetrix wallet:

window.zetrix.authorize(
  { method: "changeAccounts" }, 
  (resp) => {
    if (resp.code === 0) {
      window.zetrix.authorize(
        { method: "sendRandom", param: { random: "blob" } }, 
        (resAuth) => {
          if (resAuth.code === 0) {
            // Retrieve the necessary info from resp.data and resAuth.data to retrieve the address, signData & publicKey
            sessionStorage.setItem("zetrixAccount", resp.data.address);
            sessionStorage.setItem("isLogin", "true");
          }
        }
      );
    } 
  }
);

Mobile wallet connection

For mobile wallet users, you will have to install the zetrix-connect-wallet-sdk. In order to test your code, you will have to install the Zetrix mobile wallet which are available on both iOS and Android. For mobile wallet connection to your dApp, the following piece of snippet can be referred (the following snippet are pieces of code written in Vue.js):

// Import the object from the zetrix-connect-wallet-sdk you have installed
import ZetrixWalletConnect from "zetrix-connect-wallet-sdk";

/* Your code in your JavaScript framework of choice */

// Initialize an instance of the object
this.zetrixWalletConnect = new ZetrixWalletConnect({ qrcode: true });

// Use the connect() function to connect to your mobile wallet
this.zetrixWalletConnect
  .connect()
  .then((res) => {
    this.zetrixWalletConnect
      .auth()
      .then((res) => {
        // Handle the response. The following are some sample code to handle the response:
        sessionStorage.setItem("zetrixAccount", res.data.address);
        sessionStorage.setItem("isLogin", this.isLogin);
        window.location.href = "/";
      })
      .catch(() => {
        return false;
      });
  })
  .catch(() => {
    return false;
  });

Last updated