Using Single Factor Auth Android SDK
After successfully installing and initializing SingleFactorAuth, you can use it to authenticate your users and obtain their private and public keys.
Web3Auth SFA Android SDK only works for users who have not enabled MFA. For MFA enabled users, you'll see an Error message.
Functionality Overview
The SingleFactorAuth instance natively provides the following methods:
Method | Description |
---|---|
connect | Use to login user and retrieve private key pair. |
logout | Use to logout existing user. |
connected | Use to check whether the user is logged in or not. |
getSessionData | This method helps to get the session data for valid session. |
showWalletUI | Use to open templated the wallet UI in WebView. |
request | Use to open templated transaction screens for signing transactions. |
Login User
Please refer to the Authentication section for more details on the setting up your verifier and other authentication parameters.
To obtain a user's private key, and relevant session data using the Web3Auth SFA Android SDK, you
can call the connect
method. The method accepts LoginParams
, and returns SessionData
.
Please checkout the SessionData for more details.
Parameters
- Table
- Type
Parameter | Description |
---|---|
verifier | The verifier parameter takes the name of the custom verifier from the Web3Auth Dashboard. This is a required field that must be a String . If you're using an aggregate verifier, make sure to pass the aggregate verifier name. |
verifierId | The verifierID takes the JWT verifier ID to be used for JWT/ID token verification. It can be an email, sub, or custom value available in the JWT token. |
idToken | The idToken accepts a JWT token obtained from the user's login provider. |
subVerifierInfoArray? | Sub verifier info. Usually used during the aggregate verifier. It takes Array<TorusSubVerifierInfo> as a value. |
class LoginParams {
val verifier: String
val verifierId: String
val idToken: String
var subVerifierInfoArray: Array<TorusSubVerifierInfo>? = null
constructor(verifier: String, verifierId: String, idToken: String) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
}
constructor(
verifier: String,
verifierId: String,
idToken: String,
subVerifierInfoArray: Array<TorusSubVerifierInfo>
) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
this.subVerifierInfoArray = subVerifierInfoArray
}
}
class TorusSubVerifierInfo(
var verifier: String,
var idToken: String
)
Usage
import com.web3auth.singlefactorauth.types.SessionData
import com.web3auth.singlefactorauth.types.LoginParams
import android.content.Context
val loginParams = LoginParams(
"YOUR_VERIFIER_NAME",
"YOUR_VERIFIER_ID_VALUE",
"YOUR_ID_TOKEN"
);
val context: Context = "YOUR_APPLICATION_CONTEXT"
val sessionData: SessionData = singleFactorAuth.connect(loginParams, context);
Logout User
To logout the current user, you can use the logout
method. Please note, the method will not logout
the user from the authentication provider, it'll only logout and invalidate the Web3Auth session.
Usage
import android.content.Context
val context: Context = "YOUR_APPLICATION_CONTEXT"
singleFactorAuth.logout()
Check User's Logged In Status
You can use the connected
method to check whether the user is logged in Web3Auth or not. Please
note, you should call this method after the initialize
method if you want to check the user's
connection status for an existing session.
Usage
val isConnected = singleFactorAuth.connected()
Get Session Data
We have included Session Management in this SDK, so calling the getSessionData
will retrive the
user's SessionData
without re-logging in the user if a user has an active session. Otherwise, it
will return null
.
Please note, you should use this method after the initialize
method.
Usage
val sessionData = singleFactorAuth.getSessionData()
if(sessionData != null) {
// User is logged in
} else {
// User is not logged in
}
Response
The SessionData
has the following properties to retrive the relevant session information.
Name | Description |
---|---|
privateKey | Retrieves the user's private key. |
publicAddress | Retrieves the user's public address. |
userInfo? | Retrieves the user's information like email, name, verifier id, and more. |
signatures? | Retrieves the node's signatures that are returned for request. |
Show Wallet UI
The showWalletUI
method launches a WebView which allows you to use the templated wallet UI
services. The method takes ChainConfig
as the required input. Wallet Services is currently only
available for EVM chains.
Access to Wallet Services is gated. You can use this feature in sapphire_devnet
for free. The
minimum pricing plan to use this feature in a production
environment is the Scale Plan.
Parameters
- Table
- Class
Parameter | Description |
---|---|
chainNamespace | Custom configuration for your preferred blockchain. As of now only EVM supported. Default value is ChainNamespace.EIP155 . |
decimals? | Number of decimals for the currency ticker. Default value is 18, and accepts Int as value. |
blockExplorerUrl? | Blockchain's explorer URL. (eg: https://etherscan.io ) |
chainId | The chain id of the selected blockchain in hex String . |
displayName? | Display Name for the chain. |
logo? | Logo for the selected chainNamespace & chainId . |
rpcTarget | RPC Target URL for the selected chainNamespace & chainId . |
ticker? | Default currency ticker of the network (e.g: ETH ) |
tickerName? | Name for currency ticker (e.g: Ethereum ) |
data class ChainConfig(
val chainNamespace: ChainNamespace = ChainNamespace.EIP155,
val decimals: Int? = 18,
val blockExplorerUrl: String? = null,
val chainId: String,
val displayName: String? = null,
val logo: String? = null,
val rpcTarget: String,
val ticker: String? = null,
val tickerName: String? = null,
)
Usage
val chainConfig = ChainConfig(
chainId = "0x1",
rpcTarget = "https://rpc.ethereum.org",
ticker = "ETH",
chainNamespace = ChainNamespace.EIP155
)
val completableFuture = singleFactorAuth.showWalletUI(
chainConfig
)
completableFuture.whenComplete { _, error ->
if (error == null) {
Log.d("Successful", "Wallet launched successfully")
} else {
Log.d("Error", error.message ?: "Something went wrong")
}
}
Request signature
The request
method facilitates the use of templated transaction screens for signing transactions.
These screens can be used to sign transactions for any EVM chain and can be whitelabeled to your
branding.
Please check the list of JSON RPC methods, noting that the request method currently supports only the signing methods.
Parameters
Parameter | Description |
---|---|
chainConifg | Defines the chain to be used for signature request. Learn more about ChainConfig. |
method | JSON RPC method name in String . Currently, the request method only supports the singing methods. |
requestParams | Parameters for the corresponding method. The parameters should be in the list and correct sequence. Take a look at RPC methods to know more. |
Usage
val params = JsonArray().apply {
// Message to be signed
add("Hello, World!")
// User's EOA address
add(address)
}
val chainConfig = ChainConfig(
chainId = "0x1",
rpcTarget = "https://rpc.ethereum.org",
ticker = "ETH",
chainNamespace = ChainNamespace.EIP155
)
val signMsgCompletableFuture = singleFactorAuth.request(
chainConfig = chainConfig,
"personal_sign",
requestParams = params
)
signMsgCompletableFuture.whenComplete { signResult, error ->
if (error == null) {
Log.d("Sign Result", signResult.toString())
} else {
Log.d("Sign Error", error.message ?: "Something went wrong")
}
}
SignResponse
Name | Description |
---|---|
success | Determines whether the request was successful or not. |
result? | Holds the signature for the request when success is true . |
error? | Holds the error for the request when success is false . |