Skip to content

Latest commit

 

History

History

README.md

MetaMask Embedded Wallets — Android Firebase Example

Android example demonstrating custom authentication with Firebase using MetaMask Embedded Wallets (formerly Web3Auth Plug and Play). Firebase authenticates the user, then the Firebase ID token is passed directly to Web3Auth to derive the user's EVM wallet.

Features

  • Firebase Email/Password authentication
  • Custom JWT flow: Firebase ID token → Web3Auth wallet derivation
  • EVM wallet creation and management
  • Get wallet address, Sepolia balance, sign messages, and send transactions
  • Session persistence across app restarts

Requirements

Setup

1. Clone the repository

git clone https://github.com/Web3Auth/web3auth-android-examples.git
cd web3auth-android-examples/android-firebase-example

2. Configure Firebase

  1. Create an Android app in Firebase Console.
  2. Enable Email/Password sign-in under Authentication → Sign-in methods.
  3. Download google-services.json and place it in the app/ directory.
  4. Add the SHA-1 fingerprint of your debug keystore in Firebase Console (Project Settings → Your App).

3. Configure the Web3Auth Dashboard

  1. Create a project on the Embedded Wallets Dashboard.
  2. Go to Authentication → create a custom connection with:
    • Type: Firebase
    • JWKS Endpoint: https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com
    • User ID field: sub
    • JWT Validation: aud field equals your Firebase project ID
  3. Note your Auth Connection ID (verifier name). This example uses w3a-firebase-demo.
  4. Allowlist your redirect URI: com.sbz.web3authdemoapp://auth

4. Set credentials in strings.xml

<string name="web3auth_project_id">YOUR_WEB3AUTH_CLIENT_ID</string>

5. Open in Android Studio and run

How It Works

Initialize Web3Auth with Firebase connection config

web3Auth = Web3Auth(
    Web3AuthOptions(
        clientId = getString(R.string.web3auth_project_id),
        web3AuthNetwork = Web3AuthNetwork.SAPPHIRE_MAINNET,
        redirectUrl = "com.sbz.web3authdemoapp://auth",
        authConnectionConfig = listOf(
            AuthConnectionConfig(
                authConnection = AuthConnection.CUSTOM,
                authConnectionId = "w3a-firebase-demo", // your Auth Connection ID from dashboard
                clientId = getString(R.string.web3auth_project_id)
            )
        )
    ), this
)

web3Auth.setResultUrl(intent?.data)

val sessionResponse: CompletableFuture<Void> = web3Auth.initialize()
sessionResponse.whenComplete { _, error ->
    if (error == null) {
        val credentials = Credentials.create(web3Auth.getPrivateKey())
    }
}

Firebase sign-in → Web3Auth wallet

// 1. Sign in with Firebase
Firebase.auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // 2. Get the Firebase ID token (must be fresh — max 60s old)
            Firebase.auth.currentUser?.getIdToken(true)
                ?.addOnSuccessListener { result ->
                    val idToken = result.token ?: return@addOnSuccessListener

                    // 3. Pass the ID token directly to Web3Auth
                    val loginFuture: CompletableFuture<Web3AuthResponse> =
                        web3Auth.connectTo(
                            LoginParams(
                                authConnection = AuthConnection.CUSTOM,
                                authConnectionId = "w3a-firebase-demo",
                                idToken = idToken
                            )
                        )

                    loginFuture.whenComplete { _, error ->
                        if (error == null) {
                            val credentials = Credentials.create(web3Auth.getPrivateKey())
                        }
                    }
                }
        }
    }

Handle deep link callbacks

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    web3Auth.setResultUrl(intent?.data)
}

override fun onResume() {
    super.onResume()
    if (Web3Auth.getCustomTabsClosed()) {
        web3Auth.setResultUrl(null)
        Web3Auth.setCustomTabsClosed(false)
    }
}

Resources

License

MIT — see LICENSE for details.