Wallet Integration

Implementation Checklist

The integration implementation checklist for dApp developers consists of three categories:

  1. Frontend features

  2. Transactions and wallet interactions

  3. Client-side provider

Frontend

Make sure to create a wallet-connection button for Metamask and/or Keplr on the frontend of the application. For instance, consider the "Connect to a wallet" button on the interface of Diffusion Finance.

Transactions

Developers enabling transactions on their dApp have to determine wallet type of the user, create the transaction, request signatures from the corresponding wallet, and finally broadcast the transaction to the network.

Determining Wallet Type

Developers should determine whether users are using Keplr or MetaMask. Whether MetaMask or Keplr is installed on the user device can be determined by checking the corresponding window.ethereum or window.keplr value.

  • For MetaMask: await window.ethereum.enable(chainId);

  • For Keplr: await window.keplr.enable(chainId);

If either window.ethereum or window.keplr returns undefined after document.load, then MetaMask (or, correspondingly, Keplr) is not installed. There are several ways to wait for the load event to check the status: for instance, developers can register functions to window.onload, or they can track the document's ready state through the document event listener.

After the user's wallet type has been determined, developers can proceed with creating, signing, and sending transactions.

Create the Transaction

The example below uses the BlockX Testnet chainID. For more info, check the BlockX Chain IDs reference document here.

Developers can create MsgSend transactions.

import { createMessageSend } from @tharsis/transactions

const chain = {
    chainId: 50,
    cosmosChainId: 'blockx_50-1',
}

const sender = {
    accountAddress: 'blockx1mx9nqk5agvlsvt2yc8259nwztmxq7zjq50mxkp',
    sequence: 1,
    accountNumber: 9,
    pubkey: 'AgTw+4v0daIrxsNSW4FcQ+IoingPseFwHO1DnssyoOqZ',
}

const fee = {
    amount: '20',
    denom: 'abcx',
    gas: '200000',
}

const memo = ''

const params = {
    destinationAddress: 'blockx1pmk2r32ssqwps42y3c9d4clqlca403yd9wymgr',
    amount: '1',
    denom: 'abcx',
}

const msg = createMessageSend(chain, sender, fee, memo, params)

// msg.signDirect is the transaction in Keplr format
// msg.legacyAmino is the transaction with legacy amino
// msg.eipToSign is the EIP712 data to sign with metamaskjs

Sign and Broadcast the Transaction

The example below uses an BlockX Testnet RPC node.

After creating the transaction, developers need to send the payload to the appropriate wallet to be signed (msg.signDirect is the transaction in Keplr format, and msg.eipToSign is the EIP712 data to sign with MetaMask).

Connections

For Ethereum RPC, BlockX gRPC, and/or REST queries, dApp developers should implement providers client-side, and store RPC details in the environment variable as secrets.

Last updated