Skip to main content

Using Firebase with Web3Auth Android SFA SDK

mobileandroidethereumSFAWeb3Auth Team | May 19, 2024

In this guide, we'll talk about how we can use Web3Auth Single Factor Auth with Firebase in your Android application.

As an overview, the guide is quite simple, with functionality to log in, display user details, and perform blockchain interactions. The signing of the blockchain transactions is done through the Web3Auth embedded wallet. You can check out the infrastructure docs, "Web3Auth Wallet Management Infrastructure" for a high-level overview of the Web3Auth architecture and implementation. For those who want to skip straight to the code, you can find it on GitHub.

How to set up Web3Auth Dashboard

If you haven't already, sign up on the Web3Auth platform. It is free and gives you access to the Web3Auth's base plan. After the basic setup, explore other features and functionalities offered by the Web3Auth Dashboard. It includes custom verifiers, whitelabeling, analytics, and more. Head to Web3Auth's documentation page for detailed instructions on setting up the Web3Auth Dashboard.

How to set up Firebase for Android

If you haven't already setup the Firebase for your Android app, please setup the Firebase, as it's the prerequisites for the guide. Head to the Firebase's documentation for the details instructions.

How to set up Custom verifier

Once, you have set up the Web3Auth Dashboard, created a new project, and set up Firebase, it's time to create a Custom Verifier for your Firebase application. We already have detail instructions on how to create a Custom Verifier for Firebase, please head to our documentation.

Integrating Web3Auth SFA in Android

Once, you have set up the Custom Verifier, it's time to integrate Web3Auth in your Android application. For the implementation, we'll use the "single-factor-auth-android". This SDK facilitates integration with Web3Auth. This way you can easily manage embedded wallet in your Android application.

Installation

In your module-level build.gradle or settings.gradle file, add JitPack repository:

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" } // <-- Add this line
}
}

Once, you have added the JitPack repository, then in your app-level build.gradle dependencies section, add the single-factor-auth-android.

dependencies {
// ...
implementation 'com.github.web3auth:single-factor-auth-android:0.0.6'
}

For the prerequisites, and other mandatory configuration of the SDK, please head to our installation documentation.

Initialization

After successfully installing the package, the next step is to initialize SingleFactorAuth in your Android app. This sets up the necessary configurations using Web3Auth network and prepares the SDK. Learn more about SingleFactorAuth Initialization.

class MainActivity : AppCompatActivity() {
private lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs

// Additional code

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
}

// Additional code
}

Session Management

To check whether the user is authenticated, you can use the initialize method. For a user already authenticated, the result would be a non-nullable TorusKey. You can navigate to different views based on the result.

val sessionResponse: CompletableFuture<TorusKey> = singleFactorAuth.initialize(
this.applicationContext
)

sessionResponse.whenComplete {
torusKey, error ->
if (torusKey != null) {
publicAddress = torusKey?.publicAddress.toString()
println("""Private Key: ${torusKey.privateKey?.toString(16)}""".trimIndent())
} else {
Log.d("MainActivity_SFA", error.message ?: "Something went wrong")
}
}

Authentication

If the user is not authenticated, you should utilize the getKey method. For the guide, we will add Email Password login using Firebase. The getKey method is pretty straightforward in SingleFactorAuth and takes LoginParams as input. After successfully logging in, the method will return the TorusKey.

Learn more about SingleFactorAuth LoginParams. To more about Firebase login methods, please checkout the Firebase documentation.

private var auth: FirebaseAuthauth = Firebase.auth

// Take the input for email and password from user
auth.signInWithEmailAndPassword("android@firebase.com", "Android@Web3Auth")
.addOnCompleteListener(this) {
task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success")
val user = auth.currentUser

// Try to get a fresh idToken
user!!.getIdToken(true).addOnSuccessListener {
result ->
val idToken = result.token

Log.d(TAG, "GetTokenResult result = $idToken")

if (idToken != null) {
val sub = user!!.id

val loginParams = LoginParams(
// Replace with your custom verifier name
"web3auth-firebase-examples",
sub,
idToken
)

try {
// Save the TorusKey for future use to interact with Blockchain.
torusKey = singleFactorAuth.getKey(
loginParams,
this.applicationContext,
86400
).get()

} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

publicAddress = torusKey?.publicAddress.toString()

println("""Private Key: ${torusKey?.privateKey?.toString(16)}""".trimIndent())
println("""Public Address: $publicAddress""".trimIndent())

// Additional code
};
}
} else {
// Upon failur, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(
baseContext, "Authentication failed.",
Toast.LENGTH_SHORT
).show()
}
}

Set up Blockchain Providers

Once we have successfully authenticated the user, the next step would be to fetch the user details, retrieve wallet address and prepare blockchain providers for interactions. For this guide, we are supporting only Ethereum ecosystem, but the general idea can be used for any blockchain ecosystem.

For interacting with ethereum chains, we'll use the web3j SDK. For installation, in your app-level build.gradle's dependencies section, add the web3j dependency.

dependencies {
// ...
implementation 'org.web3j:core:4.8.7-android'
}

After successfully installing SDK, it's time to create Credentials instance to retrive user's EOA address, and interact with blockchain. To retrive the user's private key, we'll use the TorusKey instance.

val credentials: Credentials = Credentials.create(torusKey!.privateKey!.toString(16))

// User's EOA address
Log.d(TAG, credentials.address)

To retrive user's balance, and interacting with Blockcahin, you can follow our detailed guide on how to interact with EVM chain guides. Since, you already have created Credentials instance, and retrived the user's EOA address, you can skip that part. To interact with Solana blockchain, you can checkout our Solana blockchain guide.

Conclusion

Voila, you have learned how to use Web3Auth SFA SDK with Android application.

If you are interested in learning more about SFA SDK, please checkout our documentation for Android SFA SDK. You can find the code used for the guide on our examples repo.