Skip to content

Commit d48e4a9

Browse files
committed
feat: code refactored
Signed-off-by: Gaurav Goel <[email protected]>
1 parent 1421cc5 commit d48e4a9

File tree

2 files changed

+48
-53
lines changed

2 files changed

+48
-53
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
9191
selectedLoginProvider,
9292
extraLoginOptions = extraLoginOptions,
9393
mfaLevel = MFALevel.OPTIONAL
94-
)
94+
), this
9595
)
9696
loginCompletableFuture.whenComplete { _, error ->
9797
if (error == null) {
@@ -106,7 +106,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
106106
}
107107

108108
private fun signOut() {
109-
val logoutCompletableFuture = web3Auth.logout()
109+
val logoutCompletableFuture = web3Auth.logout(this)
110110
logoutCompletableFuture.whenComplete { _, error ->
111111
if (error == null) {
112112
reRender()
@@ -197,20 +197,18 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
197197
options, this
198198
)
199199

200-
web3Auth.setResultUrl(intent.data)
200+
web3Auth.setResultUrl(intent.data, this)
201201

202202
// for session response
203-
if (web3Auth.isSessionIdExists()) {
204-
val sessionResponse: CompletableFuture<Void> = web3Auth.initialize()
205-
sessionResponse.whenComplete { _, error ->
206-
if (error == null) {
207-
reRender()
208-
println("PrivKey: " + web3Auth.getPrivkey())
209-
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
210-
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
211-
} else {
212-
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
213-
}
203+
val sessionResponse: CompletableFuture<Void> = web3Auth.initialize(this)
204+
sessionResponse.whenComplete { _, error ->
205+
if (error == null) {
206+
reRender()
207+
println("PrivKey: " + web3Auth.getPrivkey())
208+
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
209+
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
210+
} else {
211+
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
214212
}
215213
}
216214

@@ -228,7 +226,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
228226
chainId = "0x89",
229227
rpcTarget = "https://1rpc.io/matic",
230228
chainNamespace = ChainNamespace.EIP155
231-
)
229+
), context = this
232230
)
233231
launchWalletCompletableFuture.whenComplete { _, error ->
234232
if (error == null) {
@@ -253,7 +251,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
253251
chainId = "0x89",
254252
rpcTarget = "https://polygon-rpc.com/",
255253
chainNamespace = ChainNamespace.EIP155
256-
), "personal_sign", requestParams = params
254+
), "personal_sign", requestParams = params, context = this
257255
)
258256
signMsgCompletableFuture.whenComplete { _, error ->
259257
if (error == null) {
@@ -273,7 +271,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
273271

274272
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
275273
btnSetUpMfa.setOnClickListener {
276-
val setupMfaCf = web3Auth.enableMFA()
274+
val setupMfaCf = web3Auth.enableMFA(context = this)
277275
setupMfaCf.whenComplete { _, error ->
278276
if (error == null) {
279277
Log.d("MainActivity_Web3Auth", "MFA setup successfully")
@@ -295,14 +293,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
295293

296294
override fun onNewIntent(intent: Intent?) {
297295
super.onNewIntent(intent)
298-
web3Auth.setResultUrl(intent?.data)
296+
web3Auth.setResultUrl(intent?.data, this)
299297
}
300298

301299
override fun onResume() {
302300
super.onResume()
303301
if (Web3Auth.getCustomTabsClosed()) {
304302
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
305-
web3Auth.setResultUrl(null)
303+
web3Auth.setResultUrl(null, this)
306304
Web3Auth.setCustomTabsClosed(false)
307305
}
308306
}

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
4848

4949
private var web3AuthResponse: Web3AuthResponse? = null
5050
private var web3AuthOption = web3AuthOptions
51-
private var appContext: Context = context
5251
private var sessionManager: SessionManager = SessionManager(
5352
context,
5453
web3AuthOptions.sessionTime ?: 600,
@@ -100,7 +99,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
10099
* @param params The login parameters required for the request.
101100
*/
102101
private fun processRequest(
103-
actionType: String, params: LoginParams?
102+
actionType: String, params: LoginParams?, context: Context
104103
) {
105104
val sdkUrl = Uri.parse(web3AuthOption.sdkUrl)
106105
val initOptions = JSONObject(gson.toJson(getInitOptions()))
@@ -129,7 +128,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
129128
}
130129
paramMap.put("params", initParams)
131130

132-
val loginIdCf = getLoginId(paramMap)
131+
val loginIdCf = getLoginId(paramMap, context)
133132
loginIdCf.whenComplete { loginId, error ->
134133
if (error == null) {
135134
val jsonObject = mapOf("loginId" to loginId)
@@ -140,9 +139,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
140139
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
141140
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
142141
//print("url: => $url")
143-
val intent = Intent(appContext, CustomChromeTabsActivity::class.java)
142+
val intent = Intent(context, CustomChromeTabsActivity::class.java)
144143
intent.putExtra(WEBVIEW_URL, url.toString())
145-
appContext.startActivity(intent)
144+
context.startActivity(intent)
146145
}
147146
}
148147
}
@@ -152,18 +151,18 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
152151
*
153152
* @return A CompletableFuture<Void> representing the asynchronous operation.
154153
*/
155-
fun initialize(): CompletableFuture<Void> {
154+
fun initialize(context: Context): CompletableFuture<Void> {
156155
val initializeCf = CompletableFuture<Void>()
157-
KeyStoreManagerUtils.initializePreferences(appContext.applicationContext)
156+
KeyStoreManagerUtils.initializePreferences(context.applicationContext)
158157

159158
//initiate keyStore
160159
initiateKeyStoreManager()
161160

162161
//fetch project config
163-
fetchProjectConfig().whenComplete { _, err ->
162+
fetchProjectConfig(context).whenComplete { _, err ->
164163
if (err == null) {
165164
//authorize session
166-
this.authorizeSession(web3AuthOption.redirectUrl.toString())
165+
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
167166
.whenComplete { resp, error ->
168167
runOnUIThread {
169168
if (error == null) {
@@ -186,7 +185,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
186185
*
187186
* @param uri The URI representing the result URL.
188187
*/
189-
fun setResultUrl(uri: Uri?) {
188+
fun setResultUrl(uri: Uri?, context: Context) {
190189
val hash = uri?.fragment
191190
if (hash == null) {
192191
if (::loginCompletableFuture.isInitialized) {
@@ -218,7 +217,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
218217
sessionManager.saveSessionId(sessionId)
219218

220219
//Rehydrate Session
221-
this.authorizeSession(web3AuthOption.redirectUrl.toString())
220+
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
222221
.whenComplete { resp, error ->
223222
runOnUIThread {
224223
if (error == null) {
@@ -260,7 +259,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
260259
* @param loginParams The login parameters required for authentication.
261260
* @return A CompletableFuture<Web3AuthResponse> representing the asynchronous operation, containing the Web3AuthResponse upon successful login.
262261
*/
263-
fun login(loginParams: LoginParams): CompletableFuture<Web3AuthResponse> {
262+
fun login(loginParams: LoginParams, context: Context): CompletableFuture<Web3AuthResponse> {
264263
//check for share
265264
if (web3AuthOption.loginConfig != null) {
266265
val loginConfigItem: LoginConfigItem? = web3AuthOption.loginConfig?.values?.first()
@@ -272,7 +271,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
272271
}
273272

274273
//login
275-
processRequest("login", loginParams)
274+
processRequest("login", loginParams, context)
276275

277276
loginCompletableFuture = CompletableFuture<Web3AuthResponse>()
278277
return loginCompletableFuture
@@ -283,10 +282,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
283282
*
284283
* @return A CompletableFuture<Void> representing the asynchronous operation.
285284
*/
286-
fun logout(): CompletableFuture<Void> {
285+
fun logout(context: Context): CompletableFuture<Void> {
287286
val logoutCompletableFuture: CompletableFuture<Void> = CompletableFuture()
288287
val sessionResponse: CompletableFuture<Boolean> =
289-
sessionManager.invalidateSession(appContext)
288+
sessionManager.invalidateSession(context)
290289
sessionResponse.whenComplete { _, error ->
291290
runOnUIThread {
292291
if (error == null) {
@@ -306,7 +305,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
306305
* @param loginParams The optional login parameters required for authentication. Default is null.
307306
* @return A CompletableFuture<Boolean> representing the asynchronous operation, indicating whether MFA was successfully enabled.
308307
*/
309-
fun enableMFA(loginParams: LoginParams? = null): CompletableFuture<Boolean> {
308+
fun enableMFA(loginParams: LoginParams? = null, context: Context): CompletableFuture<Boolean> {
310309
enableMfaCompletableFuture = CompletableFuture()
311310
if (web3AuthResponse?.userInfo?.isMfaEnabled == true) {
312311
throwEnableMFAError(ErrorCode.MFA_ALREADY_ENABLED)
@@ -317,19 +316,20 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
317316
throwEnableMFAError(ErrorCode.NOUSERFOUND)
318317
return enableMfaCompletableFuture
319318
}
320-
processRequest("enable_mfa", loginParams)
319+
processRequest("enable_mfa", loginParams, context)
321320
return enableMfaCompletableFuture
322321
}
323322

324323
/**
325324
* Authorize User session in order to avoid re-login
326325
*/
327326
private fun authorizeSession(
328-
origin: String
327+
origin: String,
328+
context: Context
329329
): CompletableFuture<Web3AuthResponse> {
330330
val sessionCompletableFuture: CompletableFuture<Web3AuthResponse> = CompletableFuture()
331331
val sessionResponse: CompletableFuture<String> =
332-
sessionManager.authorizeSession(origin, appContext)
332+
sessionManager.authorizeSession(origin, context)
333333
sessionResponse.whenComplete { response, error ->
334334
if (error == null) {
335335
val tempJson = JSONObject(response)
@@ -364,11 +364,11 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
364364
return sessionCompletableFuture
365365
}
366366

367-
private fun fetchProjectConfig(): CompletableFuture<Boolean> {
367+
private fun fetchProjectConfig(context: Context): CompletableFuture<Boolean> {
368368
val projectConfigCompletableFuture: CompletableFuture<Boolean> = CompletableFuture()
369369
val web3AuthApi =
370370
ApiHelper.getInstance(web3AuthOption.network.name).create(ApiService::class.java)
371-
if (!ApiHelper.isNetworkAvailable(appContext)) {
371+
if (!ApiHelper.isNetworkAvailable(context)) {
372372
throw Exception(
373373
Web3AuthError.getError(ErrorCode.RUNTIME_ERROR)
374374
)
@@ -423,10 +423,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
423423
* @param jsonObject The JSONObject from which to retrieve the login ID.
424424
* @return A CompletableFuture<String> representing the asynchronous operation, containing the login ID.
425425
*/
426-
private fun getLoginId(jsonObject: JSONObject): CompletableFuture<String> {
426+
private fun getLoginId(jsonObject: JSONObject, context: Context): CompletableFuture<String> {
427427
return sessionManager.createSession(
428428
jsonObject.toString(),
429-
appContext
429+
context
430430
)
431431
}
432432

@@ -440,6 +440,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
440440
fun launchWalletServices(
441441
chainConfig: ChainConfig,
442442
path: String? = "wallet",
443+
context: Context
443444
): CompletableFuture<Void> {
444445
val launchWalletServiceCF: CompletableFuture<Void> = CompletableFuture()
445446
val sessionId = sessionManager.getSessionId()
@@ -456,7 +457,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
456457
"options", initOptions
457458
)
458459

459-
val loginIdCf = getLoginId(paramMap)
460+
val loginIdCf = getLoginId(paramMap, context)
460461

461462
loginIdCf.whenComplete { loginId, error ->
462463
if (error == null) {
@@ -477,9 +478,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
477478
.encodedPath(sdkUrl.encodedPath).appendPath(path)
478479
.fragment(walletHash).build()
479480
//print("wallet launch url: => $url")
480-
val intent = Intent(appContext, WebViewActivity::class.java)
481+
val intent = Intent(context, WebViewActivity::class.java)
481482
intent.putExtra(WEBVIEW_URL, url.toString())
482-
appContext.startActivity(intent)
483+
context.startActivity(intent)
483484
launchWalletServiceCF.complete(null)
484485
}
485486
}
@@ -503,6 +504,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
503504
method: String,
504505
requestParams: JsonArray,
505506
path: String? = "wallet/request",
507+
context: Context
506508
): CompletableFuture<Void> {
507509
val signMsgCF: CompletableFuture<Void> = CompletableFuture()
508510
val sessionId = sessionManager.getSessionId()
@@ -517,7 +519,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
517519
"options", initOptions
518520
)
519521

520-
val loginIdCf = getLoginId(paramMap)
522+
val loginIdCf = getLoginId(paramMap, context)
521523

522524
loginIdCf.whenComplete { loginId, error ->
523525
if (error == null) {
@@ -540,10 +542,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
540542
.encodedPath(sdkUrl.encodedPath).appendEncodedPath(path)
541543
.fragment(signMessageHash).build()
542544
//print("message signing url: => $url")
543-
val intent = Intent(appContext, WebViewActivity::class.java)
545+
val intent = Intent(context, WebViewActivity::class.java)
544546
intent.putExtra(WEBVIEW_URL, url.toString())
545547
intent.putExtra(REDIRECT_URL, web3AuthOption.redirectUrl.toString())
546-
appContext.startActivity(intent)
548+
context.startActivity(intent)
547549
runOnUIThread {
548550
signMsgCF.complete(null)
549551
}
@@ -557,11 +559,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) {
557559
return signMsgCF
558560
}
559561

560-
fun isSessionIdExists(): Boolean {
561-
val sessionId = sessionManager.getSessionId()
562-
return !sessionId.isNullOrEmpty()
563-
}
564-
565562
private fun runOnUIThread(action: () -> Unit) {
566563
val mainHandler = Handler(Looper.getMainLooper())
567564
mainHandler.post(action)

0 commit comments

Comments
 (0)