Skip to content

Commit

Permalink
feat: Handle error gracefully in initialize method.
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Goel <[email protected]>
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Sep 19, 2024
1 parent eb8f9b1 commit 16d4b5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
12 changes: 9 additions & 3 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ class MainActivity : AppCompatActivity() {

if (singleFactorAuth.isSessionIdExists()) {
val sfakey = singleFactorAuth.initialize(this.applicationContext)
val text =
"Public Address: ${sfakey.getPublicAddress()} , Private Key: ${sfakey.getPrivateKey()}"
tv.text = text
sfakey.whenComplete { response, error ->
if (error == null) {
val text =
"Public Address: ${response.getPublicAddress()} , Private Key: ${response.getPrivateKey()}"
tv.text = text
} else {
tv.text = error.message
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.web3auth.singlefactorauth

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.google.gson.GsonBuilder
import com.web3auth.session_manager_android.SessionManager
import com.web3auth.singlefactorauth.types.ErrorCode
Expand All @@ -20,6 +22,7 @@ import org.torusresearch.torusutils.types.common.TorusKey
import org.torusresearch.torusutils.types.common.TorusOptions
import org.torusresearch.torusutils.types.common.TorusPublicKey
import org.web3j.crypto.Hash
import java.util.concurrent.CompletableFuture

class SingleFactorAuth(
sfaParams: SFAParams,
Expand All @@ -42,9 +45,25 @@ class SingleFactorAuth(
sessionManager = SessionManager(ctx, sfaParams.getSessionTime(), ctx.packageName)
}

fun initialize(ctx: Context): SFAKey {
val data = sessionManager.authorizeSession(ctx.packageName, ctx).get()
return gson.fromJson(JSONObject(data).toString(), SFAKey::class.java)
fun initialize(ctx: Context): CompletableFuture<SFAKey> {
val sfaCF = CompletableFuture<SFAKey>()
val data = sessionManager.authorizeSession(ctx.packageName, ctx)
data.whenComplete { response, error ->
val mainHandler = Handler(Looper.getMainLooper())
mainHandler.post {
if (error != null) {
sfaCF.completeExceptionally(error)
} else {
sfaCF.complete(
gson.fromJson(
JSONObject(response).toString(),
SFAKey::class.java
)
)
}
}
}
return sfaCF
}

private fun getTorusNodeEndpoints(nodeDetails: NodeDetails): Array<String?> {
Expand Down

0 comments on commit 16d4b5a

Please sign in to comment.