Dapp Usage
Implementation
This library is compatible with Node.js, browsers and React Native applications (Node.js modules require polyfills for React Native).
Dapps will also need to install WalletConnectModal for the UI.
For an example implementation, please refer to our react-dapp-v2
example.
Install Packages
Dapps will also need to install WalletConnectModal
for the UI.
Create a Session
1. Initiate your WalletConnect client with the relay server, using your Project ID.
2. Add listeners for desired SignClient
events.
To listen to pairing-related events, please follow the guidance for Pairing API event listeners.
3. Create a new WalletConnectModal instance.
4. Connect the application and specify session permissions.
Session Authenticate with ReCaps
The authenticate() method enhances the WalletConnect protocol, offering EVM dApps a sophisticated mechanism to request wallet authentication and simultaneously establish a session. This innovative approach not only authenticates the user but also facilitates a seamless session creation, integrating the capabilities defined by ERC-5573, also known as ReCaps.
ReCaps extend the SIWE protocol, enabling users to give informed consent for dApps to exercise scoped capabilities on their behalf. This consent mechanism is crucial for authorizing a dApp to perform actions or access resources, thus ensuring security and trust in dApp interactions. These scoped capabilities are specified through ReCap URIs in the resources field of the AuthRequestParams, which translate to human-readable consent in the SIWE message, detailing the actions a dApp is authorized to undertake.
To initiate an authentication and authorization request, a dApp invokes the authenticate() method, passing in parameters that include desired capabilities as outlined in EIP-5573. The method generates a pairing URI for user interaction, facilitating a streamlined authentication and consent process.
Example of initiating an authentication request with ReCaps:
Making Requests
Once the session has been established successfully, you can start making JSON-RPC requests to be approved and signed by the wallet:
For more information on available JSON-RPC requests, see the JSON-RPC reference.
Restoring a Session
Sessions are saved to localstorage, meaning that even if the web page is reloaded, the session can still be retrieved, as demonstrated in the following code:
Finding a Specific Session
If you need to find a specific session, you can do so by passing in a known requiredNamespace
and calling find
.
This library is compatible with Node.js, browsers and React Native applications (Node.js modules require polyfills for React Native).
Dapps will also need to install WalletConnectModal for the UI.
For an example implementation, please refer to our react-dapp-v2
example.
Install Packages
Dapps will also need to install WalletConnectModal
for the UI.
Create a Session
1. Initiate your WalletConnect client with the relay server, using your Project ID.
2. Add listeners for desired SignClient
events.
To listen to pairing-related events, please follow the guidance for Pairing API event listeners.
3. Create a new WalletConnectModal instance.
4. Connect the application and specify session permissions.
Session Authenticate with ReCaps
The authenticate() method enhances the WalletConnect protocol, offering EVM dApps a sophisticated mechanism to request wallet authentication and simultaneously establish a session. This innovative approach not only authenticates the user but also facilitates a seamless session creation, integrating the capabilities defined by ERC-5573, also known as ReCaps.
ReCaps extend the SIWE protocol, enabling users to give informed consent for dApps to exercise scoped capabilities on their behalf. This consent mechanism is crucial for authorizing a dApp to perform actions or access resources, thus ensuring security and trust in dApp interactions. These scoped capabilities are specified through ReCap URIs in the resources field of the AuthRequestParams, which translate to human-readable consent in the SIWE message, detailing the actions a dApp is authorized to undertake.
To initiate an authentication and authorization request, a dApp invokes the authenticate() method, passing in parameters that include desired capabilities as outlined in EIP-5573. The method generates a pairing URI for user interaction, facilitating a streamlined authentication and consent process.
Example of initiating an authentication request with ReCaps:
Making Requests
Once the session has been established successfully, you can start making JSON-RPC requests to be approved and signed by the wallet:
For more information on available JSON-RPC requests, see the JSON-RPC reference.
Restoring a Session
Sessions are saved to localstorage, meaning that even if the web page is reloaded, the session can still be retrieved, as demonstrated in the following code:
Finding a Specific Session
If you need to find a specific session, you can do so by passing in a known requiredNamespace
and calling find
.
Configure Networking and Pair clients
Make sure that you properly configure Networking and Pair Clients first.
Configure Sign Client
In order to initialize a client, call a configure
method on the Sign instance
Subscribe for Sign publishers
When your Sign
instance receives requests from a peer it will publish related event. So you should set subscription to handle them.
To track sessions subscribe to sessionsPublisher
publisher
Following publishers are available to subscribe:
Connect Clients
- Prepare namespaces that constraints minimal requirements for your dApp:
To learn more on namespaces, check out our specs.
- Your App should generate a pairing URI and share it with a wallet. Uri can be presented as a QR code or sent via a universal link. Wallet begins subscribing for session proposals after receiving URI. In order to create a pairing and send a session proposal, you need to call the following:
Session Authenticate with ReCaps
The authenticate() method enhances the WalletConnect protocol, offering EVM dApps a sophisticated mechanism to request wallet authentication and simultaneously establish a session. This innovative approach not only authenticates the user but also facilitates a seamless session creation, integrating the capabilities defined by ERC-5573, also known as ReCaps.
ReCaps extend the SIWE protocol, enabling users to give informed consent for dApps to exercise scoped capabilities on their behalf. This consent mechanism is crucial for authorizing a dApp to perform actions or access resources, thus ensuring security and trust in dApp interactions. These scoped capabilities are specified through ReCap URIs in the resources field of the AuthRequestParams, which translate to human-readable consent in the SIWE message, detailing the actions a dApp is authorized to undertake.
To initiate an authentication and authorization request, a dApp invokes the authenticate() method, passing in parameters that include desired capabilities as outlined in EIP-5573. The method generates a pairing URI for user interaction, facilitating a streamlined authentication and consent process.
Example of initiating an authentication request with ReCaps:
Subscribe to Authentication Responses
Once you have initiated an authentication request, you need to listen for responses from wallets. Responses will indicate whether the authentication request was approved or rejected. Use the authResponsePublisher to subscribe to these events.
Example subscription to authentication responses:
In this setup, the authResponsePublisher notifies your dApp of the outcome of the authentication request. Your dApp can then proceed based on whether the authentication was successful, rejected, or failed due to an error.
Example of AuthRequestParams:
Send Request to the Wallet
Once the session has been established sessionSettlePublisher
will publish an event. Your dApp can start requesting wallet now.
When wallet respond sessionResponsePublisher
will publish an event so you can verify the response.
Extending a Session
By default, session lifetime is set for 7 days and after that time user’s session will expire. But if you consider that a session should be extended you can call:
Above method will extend a user’s session to a week.
Where to go from here
- Try our Example dApp that is part of WalletConnectSwiftV2 repository.
- Build API documentation in XCode: go to Product -> Build Documentation
Initialization
The Dapp client is responsible for initiating the connection with wallets and defining the required namespaces (CAIP-2) from the Wallet and is also in charge of sending requests. To initialize the Sign client, create a Sign.Params.Init
object in the Android Application class with the Core Client. The Sign.Params.Init
object will then be passed to the SignClient
initialize function.
Dapp
SignClient.DappDelegate
The SignClient needs a SignClient.DappDelegate
passed to it for it to be able to expose asynchronously updates sent from the Wallet.
Connect
More about optional and required namespaces can be found here
Authenticate
The authenticate() method enhances the WalletConnect protocol, offering EVM dApps a sophisticated mechanism to request wallet authentication and simultaneously establish a session. This innovative approach not only authenticates the user but also facilitates a seamless session creation, integrating the capabilities defined by ERC-5573, also known as ReCaps.
Capabilities are specified through ReCap URIs in the resources field of the Sign.Params.Authenticate, which translate to human-readable consent in the SIWE message, detailing the actions a dApp is authorized to undertake.
To initiate an authentication and authorization request, a dApp invokes the authenticate() method, passing in parameters that include desired capabilities as outlined in EIP-5573. The method generates a pairing URI for user interaction, facilitating a streamlined authentication and consent process.
Example of initiating an authentication request with ReCaps:
Once you have sent an authentication request, await for responses from wallets. Responses will indicate whether the authentication request was approved or rejected. Use the onSessionAuthenticateResponse callback to receive a response:
Get List of Settled Sessions
To get a list of the most current settled sessions, call SignClient.getListOfSettledSessions()
which will return a list of type Session
.
Get list of pending session requests for a topic
To get a list of pending session requests for a topic, call SignClient.getPendingRequests()
and pass a topic which will return
a PendingRequest
object containing requestId, method, chainIs and params for pending request.
Initialization
To create an instance of SignClient
, you need to pass in the core and metadata parameters.
Connection
To connect with specific parameters and display the returned URI, use connect
with the required namespaces.
You will use that URI to display a QR code or handle a deep link.
We recommend not handling deep linking yourself. If you want to deep link, then use the walletconnect_modal_flutter package.
Session Data
Once you’ve displayed the URI you can wait for the future and hide the QR code once you’ve received session data.
Request Signatures
Once the session had been created, you can request signatures.
Respond to Events
You can also respond to events from the wallet, like chain changed, using onSessionEvent
and registerEventHandler
.
To Test
Run tests using flutter test
.
Expected flutter version is: >3.3.10
Useful Commands
flutter pub run build_runner build --delete-conflicting-outputs
- Regenerates JSON Generatorsflutter doctor -v
- get paths of everything installed.flutter pub get
flutter upgrade
flutter clean
flutter pub cache clean
flutter pub deps
flutter pub run dependency_validator
- show unused dependencies and moredart format lib/* -l 120
flutter analyze
Setup
First you must setup SignClientOptions
which stores both the ProjectId
and Metadata
. You may also optionally specify the storage module to use. By default, the FileSystemStorage
module is used if none is specified.
Then, you must setup the ConnectOptions
which define what blockchain, RPC methods and events your dapp will use.
C# Constructor
Builder Functions Style
With both options defined, you can initialize and connect the SDK.
You can grab the Uri
for the connection request from connectData
.
Then await connection approval using the Approval
Task object.
This Task
will return the SessionStruct
when the session was approved, or throw an exception when the session request has either
- Timed out
- Been Rejected
Connected Address
To get the currently connected address, use the following function
WalletConnect Methods
All sign methods require the topic
of the session to be given. This can be found in the SessionStruct
object given when a session has been given approval by the user.
Update Session
Update a session, adding/removing additional namespaces in the given topic.
Extend Session
Extend a session’s expiry time so the session remains open
Ping
Send a ping to the session
Session Requests
Sending session requests as a dapp requires to build the request and response classes that the session request params
will be structured. C# is a statically typed language, so these types must be given whenever you do a session request (or do any querying for session requests).
Currently, WalletConnectSharp does not automatically assume the object type for params
is an array. This is very important, since most EVM RPC requests have params
as an array type. Use List<T>
to workaround this. For example, for eth_sendTransaction
, use List<Transaction>
instead of Transaction
.
Newtonsoft.Json is used for JSON serialization/deserialization, therefore you can use Newtonsoft.Json attributes when defining fields in your request/response classes.
Building a Request type
Create a class for the request and populate it with the JSON properties the request object has. For this example, we will use eth_sendTransaction
The params
field for eth_sendTransaction
has the object type
Now, let’s define the actual request class we’ll use in dappClient.Request
The RpcMethod
class attributes defines the rpc method this request uses. The RpcRequestOptions
class attributes define the expiry time and tag attached to the request. Both of these attributes are required
We use List<Transaction>
since the params
field for eth_sendTransaction
is actually sent as an object array. If the params
field was a normal object, then we could use Transaction
or define the fields directly into this class.
Sending a request
The response type for eth_sendTransaction
is a string
, so no response type is required to be made. You only need to create a response type if the response type is a custom object.
Disconnecting
To disconnect a session, use the Disconnect
function. You may optional provide a reason for the disconnect
Subscribe to session events
WalletConnectUnity is a wrapper for WalletConnectSharp. It simplifies managing a single active session, addressing a common challenge with the original library.
Features of WalletConnectUnity
-
Simplified Session Management: WalletConnectSharp is designed to support multiple sessions, requiring developers to manually track and restore the active session. WalletConnectUnity simplifies this process by focusing on a single session, making it easier to manage session restoration.
-
Session Restoration: WalletConnectUnity includes methods to easily access and restore the active session from storage.
-
Deep Linking Support: WalletConnectUnity automatically handles deep linking for mobile and desktop wallets.
-
QR Code Generation: WalletConnectUnity provides a utility for generating QR codes.
Usage
To use WalletConnectUnity in your project:
- Fill in the Project ID and Metadata fields in the
Assets/WalletConnectUnity/Resources/WalletConnectProjectConfig
asset.- If you don’t have a Project ID, you can create one at Reown Cloud.
- The
Redirect
fields are optional. They are used to redirect the user back to your app after they approve or reject the session.
- Initialize
WalletConnect
and connect the wallet:
All features of WalletConnectSharp are accessible in WalletConnectUnity.
For complex scenarios, the SignClient
can be accessed directly through WalletConnect.SignClient
.
Refer to the .NET
documentation for details on using the Sign API within WalletConnectUnity.
The usage of the WalletConnectSharp.Sign API remains consistent with .NET
.
Was this page helpful?