WalletConnect Controller

WalletConnect controller handles the connections and transactions with dapps.

Create an instance

const wcController = new WCController({
        baseUrl,
        walletType,
        selectedChainName,
        environment = "mainnet",
        sessionSignatures,
        currentAccount,
        customAuthMethod,
        accessToken,
        sentryDns,
        aa,
        selectedWallet,
}: TriaArgs);

export type TriaArgs = {
  baseUrl: string;
  sentryDns?: string;
  walletType?: WalletType;
  selectedChainName?: ChainName;
  environment?: ENV;
  currentAccount?: IRelayPKP;
  sessionSignatures?: string;
  customAuthMethod?: LitCustomAuthMethod;
  accessToken?: string;
  aa?: AaDetails;
  selectedWallet?: "EOA" | "AA";
};

Initialize

This function initializes WalletConnect and start listening to the events.

await wcController.initialize();

Listen to events

Listen to connection event:

This event will be triggered when any dApp initiates a connection request.

// Listen to the connection request
wcController.onConnectionRequest(connectionReqCB);

// Sample connectionReqCB
function connectionReqCB({ id, params }) {
    const dAppName = params.proposer.metadata.name;
    const dAppIcon = params.proposer.metadata.icons[0];
    
    // ...show the connect wallet UI
}

// Accept the connection request
await wcController.acceptSessionProposal({ id, params });

// Reject the connection request
await wcController.rejectSessionProposal({ id });

Listen to dApp function calls:

This event will be triggered when any dApp initiates any function call

// Listen to the function call event
wcController.onFunctionRequest(functionReqCB);

// Sample functionReqCB
function functionReqCB({ topic, params, id }) {
    const method = params.request.method;
    const args = params.request.params;
    const chainId = params.chainId;
    
    // The screen to be displayed will depend on the value of method parameter.
    if (method === 'eth_sign') {
        const message === args[1];
        
        // Open the message sign UI with the message
    } else if (method === 'personal_sign') {
        const message === args[0];
        
        // Open the message sign UI with the message
    } else if (method === 'eth_sendTransaction') {
        const { to, value, data, from, gas } = args[0];
    
        const transaction = { to, value, data, from, gasLimit };
        
        // Use the above parameters to populate the transaction screen
    }
}

To execute the function calls,

const response = await wcController.executeFunction({ id, method, params: args, chainIdNamespace: chainId });

Last updated