Usage
This section provides instructions on how to initialize the WalletKit client, approve sessions with supported namespaces, and respond to session requests, enabling easy integration of Web3 wallets with dapps through a simple and intuitive interface.
Content
Links to sections on this page. Some sections are platform specific and are only visible when the platform is selected. To view a summary of useful platform specific topics, check out Extra (Platform Specific) under this section.
Initialization: Creating a new ReownWalletKit instance and initializing it with a projectId from Cloud.
Session: Connection between a dapp and a wallet.
- Namespace Builder: Namespace Builder is a helper utility that greatly reduces the complexity of parsing the required and optional namespaces. It accepts as parameters a session proposal along with your user’s chains/methods/events/accounts and returns a ready-to-use object
- Session Approval: Approving a session sent from a dapp
- Session Rejection: Rejecting a session sent from a dapp
- Responding to Session Requests: Responding to session requests sent from a dapp
- Updating a Session: Updating a session sent between a dapp and wallet
- Extending a Session: Extending a session between a dapp and wallet
- Session Disconnect: Disconnecting a session between a dapp and wallet
- Formatted Errors: A list of useful error objects to be used
Initialization
To create an instance of ReownWalletKit, you need to pass in the core
and metadata
parameters.
Session
A session is a connection between a dapp and a wallet. It is established when a user approves a session proposal from a dapp. A session is active until the user disconnects from the dapp or the session expires.
Namespace Builder
On flutter you don’t need to worry about Namespace Builder as Flutter SDK would handle that for you and generate a namespace object with the supported ones for you to approve.
All you have to do is make sure you register…
- wallet’s accounts with
_walletKit.registerAccount()
for accounts you want events and methods to be enabled on. This is essential if you want to properly form a session object between your wallet and the requester dapp. - request handlers with
_walletKit.registerRequestHandler()
for methods you want to support on your wallet. Optional but highly recommended if you want to seamlessly create a session object, as we will see in the coming section. - events emitters with
_walletKit.registerEventEmitter()
for events you want to support on your wallet. Optional but recommended if you plan to send events such aschainChanged
andaccountsChanged
.
And you’ll have to do this for every chain you want to support on your wallet.
When a dApp propose a session, with declared namespaces, your wallet will be able to approve an already generated set of namespaces based on your registered accounts, methods and events.
You can access this object in SessionProposalEvent during onSessionProposal
event by querying event.params.generatedNamespaces
. (See Session Approval below)
You can choose not to use registerRequestHandler
to configure your supported methods and rather define them during session approval (See Session Approval below)
By not using registerRequestHandler
your methods requests are going to be sent through onSessionRequest
event subscription.
If you do choose to use registerRequestHandler
(highly recommended) then onSessionRequest
event subscription is not going to be called.
Flutter SDK provides a handy MethodsConstants
and EventsConstants
for already defined set of required and optional values.
EVM methods & events
In @walletconnect/ethereum-provider, (our abstracted EVM SDK for apps) we support by default the following Ethereum methods and events:
Session Approval
As mentioned before, the SessionProposalEvent
is emitted when a dapp initiates a new session with your wallet. The event object will include the information about the dapp and requested namespaces. The wallet should display a prompt for the user to approve or reject the session.
To approve a session, subscribe to onSessionProposal
event and call approveSession()
passing in the event.id
and the namespaces object.
As mentioned before, namespaces:
should be either event.params.generatedNamespaces!
if you decided to use registerRequestHandler
method to configure your supported methods or a Map<String, Namespace>
object defined by yourself if you decided not to use registerRequestHandler
method
Pairing
The pair
method initiates a pairing process with a dapp using the given uri
(QR code from the dapps). To learn more about pairing, checkout out the docs.
Scan the QR code and parse the URI, and pair with the dapp.
Upon the first pairing, you will immediately receive onSessionProposal
and onAuthRequest
events.
Session Rejection
To reject the request, pass in an error code and reason according to protocol specs. See also Formatted Errors section.
To reject a session:
Responding to Session requests
To handle a session request, such as personal_sign
, you have two ways as explained before, and they are mutually exclusive, so, either you use onSessionRequest event subscription or your methods handlers configured with registerRequestHandler
.
- The recommended one is to register a request handler for the methods and chains you want to support. So let’s say your wallet supports
eip155:1
andeip155:137
. This would translate to:
- The other way is subscribing to
onSessionRequest
events (if you didn’t usegeneratedNamespaces
object) and handle the request based on the method that is firing the event.
Remember that if you have handlers registered these are going to be triggered instead of the onSessionRequest
event.
Updating a Session
If you wish to include new accounts, chains or methods in an existing session, updateSession
allows you to do so.
You need pass in the topic
and a new Namespaces
object that contains all of the existing namespaces as well as the new data you wish to include.
After you update the session, the dapp connected to your wallet will receive a SessionUpdate
event.
Extending a Session
To extend the session, call the extendSession
method and pass in the new topic
. The SessionUpdate
event will be emitted from the wallet.
Session Disconnect
To initiate a session disconnect, call the disconnectSession
method and pass in the topic
and a reason
.
When either the dapp or the wallet disconnects from a session, a SessionDelete
event will be emitted. It’s important to subscribe to this event so you could keep your state up-to-date.
Using disconnectSession()
alone will make the pairing topic persist, i.e, it can be re-used until it expires. If you want to disconnect (remove) the pairing topic as well you would have add another call as follows:
Supporting session events
In order to support session events, such as chainChanged
or accountChanged
, you would have to register an event emitter for such events, for every chain you want to emit an event for (similar to request handlers).
And to emit an event, call emitSessionEvent()
as follows:
For a better understanding please check out the example wallet and, in particular, the EVMService inside of it.
Formatted Errors
Our SDK exports a variety of ready-made error objects for you to use in the different situations. Most commonly used are…
But you can check the full list of available errors here