Skip to content

Commit c553ffa

Browse files
Merge pull request #747 from Web3Auth/sfa-mobile-guides
Add Mobile SFA guides
2 parents 97abad6 + 5601d73 commit c553ffa

8 files changed

Lines changed: 791 additions & 5 deletions

File tree

docs/sdk/core-kit/sfa-flutter/install.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ displayed_sidebar: sdk
55
description: "Web3Auth Core Kit Single Factor Auth Flutter SDK - Install | Documentation - Web3Auth"
66
---
77

8-
## `single_factor_auth_flutter`
8+
## [single_factor_auth_flutter](https://pub.dev/packages/single_factor_auth_flutter)
99

10-
To install the `single_factor_auth_flutter` package, you have two options. You can either manually add the package in the `pubspec.yaml` file, or you
11-
can use the `flutter pub add` command.
10+
To install the `single_factor_auth_flutter` package, you have two options. You can either manually
11+
add the package in the `pubspec.yaml` file, or you can use the `flutter pub add` command.
1212

1313
Add `single_factor_auth_flutter` as a dependency to your `pubspec.yaml`.
1414

@@ -17,7 +17,7 @@ dependencies:
1717
single_factor_auth_flutter: ^2.0.1
1818
```
1919
20-
Add `single_factor_auth_flutter` using `flutter pub add command`.
20+
Add `single_factor_auth_flutter` using `flutter pub add` command.
2121

2222
```sh
2323
flutter pub add single_factor_auth_flutter

docs/sdk/core-kit/sfa-ios/install.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: "Installing Web3Auth SFA iOS SDK | Documentation - Web3Auth"
1111

1212
1. In Xcode, with your app project open, navigate to **File > Add Packages**.
1313

14-
1. When prompted, add the tKey iOS SDK repository:
14+
1. When prompted, add the single-factor-auth-swift iOS SDK repository:
1515

1616
```sh
1717
https://github.com/Web3Auth/single-factor-auth-swift
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
---
2+
title: Using Firebase with Web3Auth Android SFA SDK
3+
image: "guides/banners/sfa-android-firebase.png"
4+
description:
5+
Web3Auth Single Factor Authentication (SFA) Android SDK with Firebase enables secure blockchain
6+
authentication using Email and Password login. The process involves setting up Firebase and
7+
Web3Auth, initializing the SDK, and interacting with the Ethereum blockchain.
8+
type: guide
9+
tags: [mobile, android, ethereum, SFA]
10+
date: May 19, 2024
11+
author: Web3Auth Team
12+
---
13+
14+
import SEO from "@site/src/components/SEO";
15+
import TabItem from "@theme/TabItem";
16+
import Tabs from "@theme/Tabs";
17+
18+
<SEO
19+
title="Using Firebase with Web3Auth Android SFA SDK"
20+
description="Learn how to use Web3Auth Single Factor Auth with Firebase in your Android Application."
21+
image="https://web3auth.io/docs/guides/banners/sfa-android-firebase.png"
22+
slug="/guides/sfa-android-firebase"
23+
/>
24+
25+
In this guide, we'll talk about how we can use Web3Auth Single Factor Auth with Firebase in your
26+
Android application.
27+
28+
As an overview, the guide is quite simple, with functionality to log in, display user details, and
29+
perform blockchain interactions. The signing of the blockchain transactions is done through the
30+
Web3Auth embedded wallet. You can check out the infrastructure docs,
31+
["Web3Auth Wallet Management Infrastructure"](/docs/infrastructure/infrastructure) for a high-level
32+
overview of the Web3Auth architecture and implementation. For those who want to skip straight to the
33+
code, you can find it on
34+
[GitHub](https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-android/sfa-android-quick-start).
35+
36+
## How to set up Web3Auth Dashboard
37+
38+
If you haven't already, sign up on the Web3Auth platform. It is free and gives you access to the
39+
Web3Auth's base plan. After the basic setup, explore other features and functionalities offered by
40+
the Web3Auth Dashboard. It includes custom verifiers, whitelabeling, analytics, and more. Head to
41+
[Web3Auth's documentation](/docs/dashboard-setup) page for detailed instructions on setting up the
42+
Web3Auth Dashboard.
43+
44+
## How to set up Firebase for Android
45+
46+
If you haven't already setup the Firebase for your Android app, please setup the Firebase, as it's
47+
the prerequisites for the guide. Head to the
48+
[Firebase's documentation](https://firebase.google.com/docs/android/setup) for the details
49+
instructions.
50+
51+
## How to set up Custom verifier
52+
53+
Once, you have set up the Web3Auth Dashboard, created a new project, and set up Firebase, it's time
54+
to create a Custom Verifier for your Firebase application. We already have detail instructions on
55+
how to create a Custom Verifier for Firebase, please head to our
56+
[documentation](https://web3auth.io/docs/auth-provider-setup/authentication-service-providers#firebase).
57+
58+
## Integrating Web3Auth SFA in Android
59+
60+
Once, you have set up the Custom Verifier, it's time to integrate Web3Auth in your Android
61+
application. For the implementation, we'll use the "single-factor-auth-android". This SDK
62+
facilitates integration with Web3Auth. This way you can easily manage embedded wallet in your
63+
Android application.
64+
65+
### Installation
66+
67+
In your module-level `build.gradle` or `settings.gradle` file, add JitPack repository:
68+
69+
```groovy
70+
dependencyResolutionManagement {
71+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
72+
repositories {
73+
google()
74+
mavenCentral()
75+
// highlight-next-line
76+
maven { url "https://jitpack.io" } // <-- Add this line
77+
}
78+
}
79+
```
80+
81+
Once, you have added the JitPack repository, then in your app-level `build.gradle` dependencies
82+
section, add the `single-factor-auth-android`.
83+
84+
```groovy
85+
dependencies {
86+
// ...
87+
// highlight-next-line
88+
implementation 'com.github.web3auth:single-factor-auth-android:0.0.6'
89+
}
90+
```
91+
92+
For the prerequisites, and other mandatory configuration of the SDK, please head to our
93+
[installation documentation](/docs/sdk/core-kit/sfa-android/install).
94+
95+
### Initialization
96+
97+
After successfully installing the package, the next step is to initialize `SingleFactorAuth` in your
98+
Android app. This sets up the necessary configurations using Web3Auth network and prepares the SDK.
99+
[Learn more about SingleFactorAuth Initialization](/docs/sdk/core-kit/sfa-android/initialize).
100+
101+
```kotlin
102+
class MainActivity : AppCompatActivity() {
103+
private lateinit var singleFactorAuth: SingleFactorAuth
104+
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
105+
106+
// Additional code
107+
108+
override fun onCreate(savedInstanceState: Bundle?) {
109+
super.onCreate(savedInstanceState)
110+
setContentView(R.layout.activity_main)
111+
112+
// highlight-start
113+
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
114+
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
115+
// highlight-end
116+
}
117+
118+
// Additional code
119+
}
120+
```
121+
122+
### Session Management
123+
124+
To check whether the user is authenticated, you can use the `initialize` method. For a user already
125+
authenticated, the result would be a non-nullable `TorusKey`. You can navigate to different views
126+
based on the result.
127+
128+
```kotlin
129+
val sessionResponse: CompletableFuture<TorusKey> = singleFactorAuth.initialize(
130+
this.applicationContext
131+
)
132+
133+
sessionResponse.whenComplete {
134+
torusKey, error ->
135+
if (torusKey != null) {
136+
publicAddress = torusKey?.publicAddress.toString()
137+
println("""Private Key: ${torusKey.privateKey?.toString(16)}""".trimIndent())
138+
} else {
139+
Log.d("MainActivity_SFA", error.message ?: "Something went wrong")
140+
}
141+
}
142+
```
143+
144+
### Authentication
145+
146+
If the user is not authenticated, you should utilize the `getKey` method. For the guide, we will add
147+
Email Password login using Firebase. The `getKey` method is pretty straightforward in
148+
SingleFactorAuth and takes `LoginParams` as input. After successfully logging in, the method will
149+
return the `TorusKey`.
150+
151+
Learn more about [SingleFactorAuth LoginParams](/docs/sdk/core-kit/sfa-android/usage#loginparams).
152+
To more about Firebase login methods, please
153+
[checkout the Firebase documentation](https://firebase.google.com/docs/auth/android/start).
154+
155+
```kotlin
156+
private var auth: FirebaseAuthauth = Firebase.auth
157+
158+
// Take the input for email and password from user
159+
auth.signInWithEmailAndPassword("android@firebase.com", "Android@Web3Auth")
160+
.addOnCompleteListener(this) {
161+
task ->
162+
if (task.isSuccessful) {
163+
// Sign in success, update UI with the signed-in user's information
164+
Log.d(TAG, "signInWithEmail:success")
165+
val user = auth.currentUser
166+
167+
// Try to get a fresh idToken
168+
user!!.getIdToken(true).addOnSuccessListener {
169+
result ->
170+
val idToken = result.token
171+
172+
Log.d(TAG, "GetTokenResult result = $idToken")
173+
174+
if (idToken != null) {
175+
val sub = user!!.id
176+
177+
val loginParams = LoginParams(
178+
// Replace with your custom verifier name
179+
"web3auth-firebase-examples",
180+
sub,
181+
idToken
182+
)
183+
184+
try {
185+
// Save the TorusKey for future use to interact with Blockchain.
186+
// highlight-start
187+
torusKey = singleFactorAuth.getKey(
188+
loginParams,
189+
this.applicationContext,
190+
86400
191+
).get()
192+
// highlight-end
193+
194+
} catch (e: ExecutionException) {
195+
e.printStackTrace()
196+
} catch (e: InterruptedException) {
197+
e.printStackTrace()
198+
}
199+
200+
publicAddress = torusKey?.publicAddress.toString()
201+
202+
println("""Private Key: ${torusKey?.privateKey?.toString(16)}""".trimIndent())
203+
println("""Public Address: $publicAddress""".trimIndent())
204+
205+
// Additional code
206+
};
207+
}
208+
} else {
209+
// Upon failur, display a message to the user.
210+
Log.w(TAG, "signInWithEmail:failure", task.exception)
211+
Toast.makeText(
212+
baseContext, "Authentication failed.",
213+
Toast.LENGTH_SHORT
214+
).show()
215+
}
216+
}
217+
```
218+
219+
## Set up Blockchain Providers
220+
221+
Once we have successfully authenticated the user, the next step would be to fetch the user details,
222+
retrieve wallet address and prepare blockchain providers for interactions. For this guide, we are
223+
supporting only Ethereum ecosystem, but the general idea can be used for any blockchain ecosystem.
224+
225+
For interacting with ethereum chains, we'll use the [web3j](https://github.com/hyperledger/web3j)
226+
SDK. For installation, in your app-level build.gradle's dependencies section, add the web3j
227+
dependency.
228+
229+
```groovy
230+
dependencies {
231+
// ...
232+
implementation 'org.web3j:core:4.8.7-android'
233+
}
234+
```
235+
236+
After successfully installing SDK, it's time to create `Credentials` instance to retrive user's EOA
237+
address, and interact with blockchain. To retrive the user's private key, we'll use the `TorusKey`
238+
instance.
239+
240+
```kotlin
241+
// highlight-next-line
242+
val credentials: Credentials = Credentials.create(torusKey!.privateKey!.toString(16))
243+
244+
// User's EOA address
245+
Log.d(TAG, credentials.address)
246+
```
247+
248+
To retrive user's balance, and interacting with Blockcahin, you can follow our detailed guide on how
249+
to [interact with EVM chain guides](/docs/connect-blockchain/evm/ethereum/android#get-balance).
250+
Since, you already have created `Credentials` instance, and retrived the user's EOA address, you can
251+
skip that part. To interact with Solana blockchain, you can checkout our
252+
[Solana blockchain guide](/docs/connect-blockchain/solana/android).
253+
254+
## Conclusion
255+
256+
Voila, you have learned how to use Web3Auth SFA SDK with Android application.
257+
258+
If you are interested in learning more about SFA SDK, please checkout our
259+
[documentation for Android SFA SDK](/docs/sdk/core-kit/sfa-android/). You can find the code used for
260+
the guide on our
261+
[examples repo](https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-android/sfa-android-quick-start).

0 commit comments

Comments
 (0)