DefaultSIWX Implementation
Here is a quick guide to get started with a custom SIWX feature using theDefaultSIWX
class.
Example
Next.js MultiChain SIWX Default Example
Check the Next.js example using DefaultSIWX in a multi-chain environment
Installation
Usage
ReownAuthentication
configuration will use the predefined components to handle the message generation, verification and storage of the sessions. You can customize the default implementation by providing your own components as in the following sections.
Customizing the DefaultSIWX Class
TheDefaultSIWX
class is divided in three main components: SIWXMessenger
, SIWXVerifier
and SIWXStorage
. The @reown/appkit-siwx
package have defined options to fulfill the parts when initializing the DefaultSIWX
configuration and you are also able to setup your own parts as required.
Predefined Components
The@reown/appkit-siwx
package provides some predefined components that you can use to quickly setup the DefaultSIWX
configuration.
Check the latest components over the SIWX repository
Customizing components
You may provide the parts to theDefaultSIWX
configuration using the predefined components exposed by @reown/appkit-siwx
package and customize the component params as needed.
Custom Components
You may create your own components to handle the message generation, verification and storage of the sessions. The following sections will guide you through the process of creating your own components.SIWXMessenger
The SIWXMessenger
is an abstract class which holds methods for generating the message to be signed.
Creating a Custom Messenger:
You may extend theSIWXMessenger
class adding the public attributes:
version
: a string that represents the version of the messenger;stringify
: a method that receives the message data and returns a string to be signed.
SIWXVerifier
The SIWXVerifier
is an abstract class that defines the verification logic for the signed message.
Creating a Custom Verifier:
You may extend theSIWXVerifier
class adding the public attributes:
chainNamespace
: a string that represents the chain namespace for the verifier;verify
: a method that receives the session data and returns a boolean indicating if the session is valid.
SIWXStorage
Next.js SIWX Default Example with supabase storage
Check the Next.js example using DefaultSIWX and supabase as default storage
SIWXStorage
is a interface that defines how the session data will be stored.
Creating a Custom Storage:
You may implement theSIWXStorage
interface with the following methods:
add
: This method will be called to store a new single session that is verified;set
: This method must replace all the sessions in the storage with the new ones;get
: This method must return all the sessions stored for a specific chain and address. Is expected that the sessions are already verified;delete
: This method must delete all the sessions stored for a specific chain and address.
Using custom components
You may provide your custom components to theDefaultSIWX
configuration.
If you omit any of the components, the default implementation will be used. Check here the default components.
SIWX Full Custom Implementation
SIWX is developed to work as a plugin system for AppKit and to enable correctly it you need to fulfill the expected interface within thecreateAppKit
function.
SIWXConfig interface
This is the interface that you need to implement to create your own SIWX feature.@reown/appkit-core
package. You may check the source code here.
Full Detailed Interfaces
Full Detailed Interfaces
Constraints
You are able to implement theSIWXConfig
in the way you would like, but some constraints MUST be followed to make sure that AppKit can interact with it correctly and it will work as expected:
createMessage
This method will be called to create a new message to be signed by the user.
- The message MUST be unique and contain all the necessary information to verify the user’s identity.
addSession
This method will be called to store a new single session.
- This method MUST verify if the session is valid and store it in the storage successfully.
revokeSession
This method will be called to revoke all the sessions stored for a specific chain and address.
- This method MUST delete all the sessions stored for the specific chain and address successfully.
setSessions
This method will be called to replace all the sessions in the storage with the new ones.
- This method MUST verify all the sessions before storing them in the storage;
- This method MUST replace all the sessions in the storage with the new ones successfully otherwise it MUST throw an error.
getSessions
This method will be called to get all the sessions stored for a specific chain and address.
- This method MUST return only sessions that are verified and valid;
- This method MUST NOT return expired sessions.
Example
Here is an example of how you can implement use yourSIWXConfig
interface: