Skip to main content

Usage of PnP Android SDK

Web3Auth's Android SDK is a set of libraries and tools that allow you to easily integrate Web3 with Android applications.

Logging in a User

web3Auth.login(LoginParams)

Trigger login flow will navigate the user to a browser model allowing the user to login into the service. You can pass in the supported providers to the login method for specific social logins such as GOOGLE, APPLE, FACEBOOK, etc., and do whitelabel login. The login function takes in LoginParams as a required input.

Arguments

LoginParams

ParameterDescription
loginProviderIt sets the OAuth login method to be used. You can use any of the supported values are GOOGLE, FACEBOOK, REDDIT, DISCORD, TWITCH, APPLE, LINE, GITHUB, KAKAO, LINKEDIN, TWITTER, WEIBO, WECHAT, EMAIL_PASSWORDLESS.
extraLoginOptions?It can be used to set the OAuth login options for corresponding loginProvider. For instance, you'll need to pass user's email address as. Default value for the field is null, and it accepts ExtraLoginOptions as a value.
redirectUrl?Url where user will be redirected after successfull login. By default user will be redirected to same page where login will be initiated. Default value for the field is null, and accepts Uri as a value.
appState?It can be used to keep track of the app state when user will be redirected to app after login. Default is null, and accepts String as a value.
mfaLevel?Customize the MFA screen shown to the user during OAuth authentication. Default value for field is MFALevel.DEFAULT, which shows MFA screen every 3rd login. It accepts MFALevel as a value.
dappShare?Custom verifier logins can get a dapp share returned to them post successful login. This is useful if the dapps want to use this share to allow users to login seamlessly. It accepts String as a value.
curve?It will be used to determine the public key encoded in the jwt token which returned in getUserInfo function after user login. This parameter won't change format of private key returned by We3Auth. Private key returned by getPrivKey is always secp256k1. To get the ed25519 key you can use getEd25519PrivKey method. The default value is Curve.SECP256K1.

web3Auth.getPrivkey()

Use getPrivkey() to get the private key of the user. The method returns an EVM compatible private key which can be used to sign transactions on EVM compatible chains.

web3Auth.getEd25519PrivKey()

Use getEd25519PrivKey() to get the Ed25519 private key of the user. This private key can be used to sign transactions on Solana.

web3Auth.getUserInfo()

Use getUserInfo() to get the user info of the user.

UserInfo Response

{
"userInfo": {
"email": "w3a-heroes@web3auth.com",
"name": "Web3Auth Heroes",
"profileImage": "https://lh3.googleusercontent.com/a/Ajjjsdsmdjmnm...",
"verifier": "torus",
"verifierId": "w3a-heroes@web3auth.com",
"typeOfLogin": "google",
"aggregateVerifier": "w3a-google-sapphire",
"dappShare": "", // 24 words of seed phrase will be sent only incase of custom verifiers
"idToken": "<jwtToken issued by Web3Auth>",
"oAuthIdToken": "<jwtToken issued by OAuth Provider>", // will be sent only incase of custom verifiers
"oAuthAccessToken": "<accessToken issued by OAuth Provider>", // will be sent only incase of custom verifiers
"isMfaEnabled": false // Returns whether the user has enabled MFA or not
}
}
Usage
import android.widget.Button
import com.web3auth.core.Web3Auth
import android.os.Bundle

class MainActivity : AppCompatActivity() {
private lateinit var web3Auth: Web3Auth

private fun signIn() {
val selectedLoginProvider = Provider.GOOGLE
// Can be REDDIT, TWITTER, GITHUB etc.
val loginCompletableFuture: CompletableFuture<Web3AuthResponse> =
web3Auth.login(LoginParams(selectedLoginProvider))

loginCompletableFuture.whenComplete { loginResponse, error ->
if (error == null) {
// render logged in UI
println("PrivKey: " + web3Auth.getPrivkey())
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
} else {
// render error UI
}

}
}

override fun onCreate(savedInstanceState: Bundle?) {
...
// Setup UI and event handlers
val signInButton = findViewById<Button>(R.id.signInButton)
signInButton.setOnClickListener { signIn() }
...
}
...
}

Selecting Curve

The web3Auth.login() method accepts a curve parameter. This parameter can be used to select the elliptic curve to use for the signature.

web3Auth.login(LoginParams(selectedLoginProvider, curve = Curve.SECP256K1)) // Can be ED25519, SECP256K1
Usage
import android.widget.Button
import com.web3auth.core.Web3Auth
import android.os.Bundle

class MainActivity : AppCompatActivity() {
private lateinit var web3Auth: Web3Auth

private fun signIn() {

val selectedLoginProvider = Provider.GOOGLE
// Can be GOOGLE, FACEBOOK, TWITCH etc.
val loginCompletableFuture: CompletableFuture<Web3AuthResponse> =
web3Auth.login(LoginParams(selectedLoginProvider,
curve = Curve.SECP256K1))

loginCompletableFuture.whenComplete { loginResponse, error ->
if (error == null) {
// render logged in UI
} else {
// render error UI
}

}
}

override fun onCreate(savedInstanceState: Bundle?) {
...
// Setup UI and event handlers
val signInButton = findViewById<Button>(R.id.signInButton)
signInButton.setOnClickListener { signIn() }
...
}
...
}

Logging out a user

Logging out your user is as simple as calling the logout() function. This method will clear the session data and the user will be logged out from Web3Auth.

private fun logOut() {
val logoutCompletableFuture = web3Auth.logout()
logoutCompletableFuture.whenComplete { _, error ->
if (error == null) {
// render logout UI
} else {
// render error UI
}
}
}

Enable MFA for a user

The enableMFA method is used to trigger MFA setup flow for users. The method takes LoginParams which will used during custom verifiers. If you are using default login providers, you don't need to pass LoginParams. If you are using custom jwt verifiers, you need to pass the JWT token in loginParams as well.

Usage
import android.widget.Button
import com.web3auth.core.Web3Auth
import android.os.Bundle

class MainActivity : AppCompatActivity() {
private lateinit var web3Auth: Web3Auth

private fun enableMFA() {
val completableFuture = web3Auth.enableMFA()

completableFuture.whenComplete{_, error ->
if (error == null) {
Log.d("MainActivity_Web3Auth", "Launched successfully")
// Add your logic
} else {
// Add your logic on error
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
...
// Setup UI and event handlers
val enableMFAButton = findViewById<Button>(R.id.enableMFAButton)
enableMFAButton.setOnClickListener { enableMFA() }
...
}
...
}

Launch Wallet Services

The launchWalletServices method launches a WebView which allows you to use the templated wallet UI services. The method takes LoginParams and ChainConfig as the required input.Wallet Services is currently only available for EVM chains.

Minimum Scale plan required

Access to Wallet Services is gated. You can use this feature in the development environment for free. The minimum pricing plan to use this feature in a production environment is the Scale Plan.

Arguments

ChainConfig

ParameterDescription
chainNamespaceCustom 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)
chainIdThe chain id of the selected blockchain in hex String.
displayName?Display Name for the chain.
logo?Logo for the selected chainNamespace & chainId.
rpcTargetRPC 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)
Usage
val loginParams = LoginParams(Provider.GOOGLE)

val chainConfig = ChainConfig(
chainId = "0x1",
rpcTarget = "https://rpc.ankr.com/eth",
ticker = "ETH",
chainNamespace = ChainNamespace.EIP155
)

val completableFuture = web3Auth.launchWalletServices(
loginParams, chainConfig
)

completableFuture.whenComplete{_, error ->
if(error == null) {
// Add your logic
Log.d("MainActivity_Web3Auth", "Wallet services launched successfully")
} else {
// Add your logic for error
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
}
}

Request signature

The request method facilitates the use of templated transaction screens for signing transactions. Upon successful completion, you can retrieve the signature for the request using the getSignResponse static method.

Please check the list of JSON RPC methods, noting that the request method currently supports only the signing methods.

Arguments

ArgumentsDescription
loginParamsTakes in LoginParams used for authenticating the user. If you are using a custom verifier, please make sure to pass the valid JWT token in LoginParams.
methodJSON RPC method name in String. Currently, the request method only supports the singing methods.
requestParamsParameters for the corresponding method. The parameters should be in the list and correct sequence. Take a look at RPC methods to know more.

Usage

Usage

val params = JsonArray().apply {
// Message to be signed
add("Hello, World!")
// User's EOA address
add(address)
}

val loginParams = LoginParams(Provider.GOOGLE)

val signMsgCompletableFuture = web3Auth.request(
loginParams = loginParams,
"personal_sign",
requestParams = params
)

signMsgCompletableFuture.whenComplete { _, error ->
if (error == null) {
Log.d("MainActivity_Web3Auth", "Message signed successfully")
val signResult = Web3Auth.getSignResponse()
Log.d("MainActivity_Web3Auth", signResult.toString())

} else {
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
}
}