Wallet Connect is an open protocol - not a wallet - built to create a communication link between DApps and Wallets. A wallet and an application supporting this protocol will enable a secure link through a shared key between the two peers. A connection is initiated by the DApp displaying a QR code with a standard WalletConnect URI and the connection is established when the wallet application approves the connection request. Further requests regarding funds transfer are confirmed on the wallet application itself.
1. Set up Web3
To set up your DApp to connect to user’s Polygon Wallet we can use Wallet Connect’s provider to directly connect to Polygon. Install the following in your DApp:
Install matic.js for Matic integration:
And add the following code in your App,
Next, we set up Polygon and Ropsten provider via Wallet Connect’s object:
We created the above two provider objects to instantiate our Web3 object with:
2. Instantiating contracts
Once we have our web3 object, the instantiating of contracts involves the same steps we followed for metamask.
Again, assuming you have your contract ABI and address already in place :)
3. Calling functions
Like discussed above, we have two types of functions in Ethereum, depending upon the interaction with the blockchain. We
call() when we read data and
send() when we write data.
Now reading data doesn’t require a signature, therefore the process is the same as discussed above:
Since writing to the blockchain requires a signature, we prompt the user on their wallet (that supports wallet connect) to sign the transaction.
This involves two steps:
- Constructing a transaction
- Getting a signature on the transaction
- Sending signed transaction
The above code creates a transaction object which is then sent to user’s wallet for signature:
signTransaction() function prompts the user for their signature and
sendSignedTransaction() sends the signed transaction over (returns a transaction receipt on success).
NOTE: all this while, the private key is in user’s wallet and the app does not access it any way. :)