This library is compatible with Node.js, browsers and React Native
applications (Node.js modules require polyfills for React Native).
Migrating from v1.x
We recommend you install v1 and v2 together for maximum compatibility. If your wallet already uses@walletconnect/client@1.x.x
,
you should be able to add @walletconnect/sign-client@2.x.x
without any issues.If you experience dependency clashes or you require both @walletconnect/types@1.x.x
and @walletconnect/types@2.x.x
in parallel
in your wallet’s top-level dependencies, please refer to the legacy
packages which were published explicitly for this purpose.In the above scenario, you would replace @walletconnect/types@1.x.x
with @walletconnect/legacy-types
and then install @walletconnect/types@2.x.x
.Initializing the client
Initialize client as a controller using your Project ID.Setting up event listeners
WalletConnect v2.0 emits events related to the current session. The listeners listed in the following code snippet represent typical events in a session’s lifecycle that you can listen for to synchronise your application accordingly.Example: when asession_delete
event is emitted, it makes sense to change the UI from an active session state to
an inactive/disconnected state.1. Add listeners for desired SignClient
events.To listen to pairing-related events, please follow the guidance for Pairing
API event listeners.
Pairing and session permissions
URI
The pairing proposal between a wallet and a dapp is made using an URI. In WalletConnect v2.0 the session and pairing are decoupled from each other. This means that a URI is shared to construct a pairing proposal, and only after settling the pairing the dapp can propose a session using that pairing. In simpler words, the dapp generates an URI that can be used by the wallet for pairing.Namespaces
Thenamespaces
parameter is used to specify the namespaces and chains that are intended to be used in the session. The following is an example:Pairing with uri
To create a pairing proposal, simply pass the uri
received from the dapp into the signClient.core.pairing.pair()
function.As of 2.0.0 (stable), calling pairing-specific methods (such as
signClient.pair()
) directly on signClient
will continue to work, but is considered deprecated and will be removed in a future major version.It is recommended to instead call these methods directly via the Pairing API., e.g.: signClient.core.pairing.pair()
.Pairing with QR Codes
To facilitate better user experience, it is possible to pair wallets with dapps by scanning QR codes. This can be implemented by using any QR code scanning library (example, react-qr-reader). After scanning the QR code, pass the obtaineduri
into the signClient.pair()
function. A useful reference for implementing QR codes for pairing is the react wallet example.Authenticated Session
This section outlines an innovative protocol method that facilitates the initiation of a Sign session and the authentication of a wallet through a Sign-In with Ethereum (SIWE) message, enhanced by ReCaps (ReCap Capabilities). This enhancement not only offers immediate authentication for dApps, paving the way for prompt user logins, but also integrates informed consent for authorization. Through this mechanism, dApps can request the delegation of specific capabilities to perform actions on behalf of the wallet user. These capabilities, encapsulated within SIWE messages as ReCap URIs, detail the scope of actions authorized by the user in an explicit and human-readable form.By incorporating ReCaps, this method extends the utility of SIWE messages, allowing dApps to combine authentication with a nuanced authorization model. This model specifies the actions a dApp is authorized to execute on the user’s behalf, enhancing security and user autonomy by providing clear consent for each delegated capability. As a result, dApps can utilize these consent-backed messages to perform predetermined actions, significantly enriching the interaction between dApps, wallets, and users within the Ethereum ecosystem.Handling Authentication Requests
To handle incoming authentication requests, subscribe to thesession_authenticate
event. This will notify you of any authentication requests that need to be processed, allowing you to either approve or reject them based on your application logic.Populate Authentication Payload
Approving Authentication Requests
Note
- The recommended approach for secure authentication across multiple chains involves signing a SIWE (Sign-In with Ethereum) message for each chain and account. However, at a minimum, one SIWE message must be signed to establish a session. It is possible to create a session for multiple chains with just one issued authentication object.
- Sometimes a dapp may want to only authenticate the user without creating a session, not every approval will result with a new session.