Here we learn how to connect the user's wallet with the web application and sign messages.
A completed version of this part can be found in the example repository (01_frontend). This example uses the browser console to print messages, so it should be actively monitored.
To sign in with Ethereum we only need to send two pieces of information:
The message
The signature of the message
On the previous page, we wrote a function that gives us the means to create messages, so now we only need the means to sign messages.
So we must first connect the web application and the user's wallet so that the application can request information about the Ethereum account and sign messages.
1. In order to do that we will need to add some new dependencies to our print project:
6. Now run the following command and visit the URL printed to the console. After you connect your wallet and sign the message, the signature should appear in the console.
yarn start
Explanation of Components
ethers.js is a library that provides functionality for interacting with the Ethereum blockchain. We use it here for connecting the webpage to extension wallets.
The Metamask extension injects the window.ethereum object into every webpage, and the ethers library provides a convenient provider class to wrap it. We then use this provider to connect to the wallet, and access signing capabilities:
Running the connectWallet function below will send a request to the MetaMask extension to ask permission to view information about the Ethereum accounts that it controls. MetaMask will then show a window to the user asking them to authorize our application to do so. If they authorize the request then we've connected their account: