WalletConnect is an open source protocol for connecting DAPPs to mobile wallets with QR code scanning or deep linking.
There are two common ways to integrate:
1. Web3Modal
Web3Modal is an easy-to-use library to help developers add support for multiple providers (including WalletConnect) in their apps with a simple customizable configuration.
Sample code for configuring WalletConnect with Bitrock Blockchain
import Web3 from"web3";import WalletConnectProvider from"@walletconnect/web3-provider";import Web3Modal from"web3modal";// set chain id and rpc mapping in provider optionsconstproviderOptions= { walletconnect: { package: WalletConnectProvider, options: { rpc: {7171:"https://connect.bit-rock.io", }, chainId:7171, }, },};constweb3Modal=newWeb3Modal({ network:"mainnet",// optional cacheProvider:true,// optional providerOptions,// required});constprovider=awaitweb3Modal.connect();awaitweb3Modal.toggleModal();// regular web3 provider methodsconstnewWeb3=newWeb3(provider);constaccounts=awaitnewWeb3.eth.getAccounts();console.log(accounts);
2. Standalone Client
Bitrock Wallet extends WalletConnect 1.x with aditional JSON-RPC methods to support multi-chain dApps. Currently, you can get all accounts and sign transactions for Bitrock Blockchain implements signJSON method in wallet core.
Before you can sign transactions, you have to initiate a connection to a WalletConnect bridge server, and handle all possible states:
import WalletConnect from"@walletconnect/client";import QRCodeModal from"@walletconnect/qrcode-modal";// Create a connectorconstconnector=newWalletConnect({ bridge:"https://bridge.walletconnect.org",// Required qrcodeModal: QRCodeModal,});// Check if connection is already establishedif (!connector.connected) {// create new sessionconnector.createSession();}// Subscribe to connection eventsconnector.on("connect", (error, payload) => {if (error) {throw error; }// Get provided accounts and chainIdconst { accounts,chainId } =payload.params[0];});connector.on("session_update", (error, payload) => {if (error) {throw error; }// Get updated accounts and chainIdconst { accounts,chainId } =payload.params[0];});connector.on("disconnect", (error, payload) => {if (error) {throw error; }// Delete connector});
code snippet above is copied from https://docs.walletconnect.org/quick-start/dapps/client#initiate-connection, please check out the original link for standard methods.
Get chain accounts from Bitrock Wallet
Once you have walletconnect client set up, you will be able to get user's accounts:
constrequest=connector._formatRequest({ method:"get_accounts",});connector._sendCallRequest(request).then((result) => {// Returns the accountsconsole.log(result); }).catch((error) => {// Error returned when rejectedconsole.error(error); });
The result is an array with following structure:
[ { network: number, address: string, },];
Need more details about Wallet Connect set up for your dApp to connect to Bitrock Wallet smoothly find it HERE