Skip to content

Commit

Permalink
Use contextWrapper to use context everywhere.
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 Nov 25, 2024
1 parent 225eed9 commit 9735f1c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/com/web3auth/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
selectedLoginProvider,
extraLoginOptions = extraLoginOptions,
mfaLevel = MFALevel.OPTIONAL
), this
)
)
loginCompletableFuture.whenComplete { _, error ->
if (error == null) {
Expand All @@ -106,7 +106,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
}

private fun signOut() {
val logoutCompletableFuture = web3Auth.logout(this)
val logoutCompletableFuture = web3Auth.logout()
logoutCompletableFuture.whenComplete { _, error ->
if (error == null) {
reRender()
Expand Down Expand Up @@ -195,10 +195,10 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
options, this
)

web3Auth.setResultUrl(intent.data, this)
web3Auth.setResultUrl(intent.data)

// for session response
val sessionResponse: CompletableFuture<Void> = web3Auth.initialize(this)
val sessionResponse: CompletableFuture<Void> = web3Auth.initialize()
sessionResponse.whenComplete { _, error ->
if (error == null) {
reRender()
Expand All @@ -224,7 +224,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
chainId = "0x89",
rpcTarget = "https://1rpc.io/matic",
chainNamespace = ChainNamespace.EIP155
), context = this
)
)
launchWalletCompletableFuture.whenComplete { _, error ->
if (error == null) {
Expand All @@ -248,7 +248,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
chainId = "0x89",
rpcTarget = "https://polygon-rpc.com/",
chainNamespace = ChainNamespace.EIP155
), "personal_sign", requestParams = params, appState = "web3Auth", context = this
), "personal_sign", requestParams = params, appState = "web3Auth"
)
signMsgCompletableFuture.whenComplete { signResult, error ->
if (error == null) {
Expand All @@ -261,7 +261,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {

val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
btnSetUpMfa.setOnClickListener {
val setupMfaCf = web3Auth.enableMFA(context = this)
val setupMfaCf = web3Auth.enableMFA()
setupMfaCf.whenComplete { _, error ->
if (error == null) {
Log.d("MainActivity_Web3Auth", "MFA setup successfully")
Expand All @@ -283,14 +283,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {

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

override fun onResume() {
super.onResume()
if (Web3Auth.getCustomTabsClosed()) {
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null, this)
web3Auth.setResultUrl(null)
Web3Auth.setCustomTabsClosed(false)
}
}
Expand Down
65 changes: 32 additions & 33 deletions core/src/main/java/com/web3auth/core/Web3Auth.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.web3auth.core

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.net.Uri
import android.os.Handler
Expand Down Expand Up @@ -40,7 +41,8 @@ import org.json.JSONObject
import java.util.Locale
import java.util.concurrent.CompletableFuture

class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResultCallback {
class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResultCallback,
ContextWrapper(context) {

private val gson = GsonBuilder().disableHtmlEscaping().create()

Expand Down Expand Up @@ -103,7 +105,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
* @param params The login parameters required for the request.
*/
private fun processRequest(
actionType: String, params: LoginParams?, context: Context
actionType: String, params: LoginParams?
) {
val sdkUrl = Uri.parse(web3AuthOption.sdkUrl)
val initOptions = JSONObject(gson.toJson(getInitOptions()))
Expand Down Expand Up @@ -132,7 +134,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
}
paramMap.put("params", initParams)

val loginIdCf = getLoginId(paramMap, context)
val loginIdCf = getLoginId(paramMap)
loginIdCf.whenComplete { loginId, error ->
if (error == null) {
val jsonObject = mapOf("loginId" to loginId)
Expand All @@ -143,9 +145,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
//print("url: => $url")
val intent = Intent(context, CustomChromeTabsActivity::class.java)
val intent = Intent(this, CustomChromeTabsActivity::class.java)
intent.putExtra(WEBVIEW_URL, url.toString())
context.startActivity(intent)
this.startActivity(intent)
}
}
}
Expand All @@ -155,19 +157,19 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
*
* @return A CompletableFuture<Void> representing the asynchronous operation.
*/
fun initialize(context: Context): CompletableFuture<Void> {
fun initialize(): CompletableFuture<Void> {
val initializeCf = CompletableFuture<Void>()
KeyStoreManagerUtils.initializePreferences(context.applicationContext)
KeyStoreManagerUtils.initializePreferences(this.applicationContext)

//initiate keyStore
initiateKeyStoreManager()

//fetch project config
fetchProjectConfig(context).whenComplete { _, err ->
fetchProjectConfig().whenComplete { _, err ->
if (err == null) {
//authorize session
sessionManager.setSessionId(SessionManager.getSessionIdFromStorage())
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
this.authorizeSession(web3AuthOption.redirectUrl.toString(), this)
.whenComplete { resp, error ->
runOnUIThread {
if (error == null) {
Expand All @@ -190,7 +192,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
*
* @param uri The URI representing the result URL.
*/
fun setResultUrl(uri: Uri?, context: Context) {
fun setResultUrl(uri: Uri?) {
val hash = uri?.fragment
if (hash == null) {
if (::loginCompletableFuture.isInitialized) {
Expand Down Expand Up @@ -223,7 +225,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
sessionManager.setSessionId(sessionId)

//Rehydrate Session
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
this.authorizeSession(web3AuthOption.redirectUrl.toString(), this)
.whenComplete { resp, error ->
runOnUIThread {
if (error == null) {
Expand Down Expand Up @@ -268,7 +270,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
* @param loginParams The login parameters required for authentication.
* @return A CompletableFuture<Web3AuthResponse> representing the asynchronous operation, containing the Web3AuthResponse upon successful login.
*/
fun login(loginParams: LoginParams, context: Context): CompletableFuture<Web3AuthResponse> {
fun login(loginParams: LoginParams): CompletableFuture<Web3AuthResponse> {
//check for share
if (web3AuthOption.loginConfig != null) {
val loginConfigItem: LoginConfigItem? = web3AuthOption.loginConfig?.values?.first()
Expand All @@ -280,7 +282,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
}

//login
processRequest("login", loginParams, context)
processRequest("login", loginParams)

loginCompletableFuture = CompletableFuture<Web3AuthResponse>()
return loginCompletableFuture
Expand All @@ -291,11 +293,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
*
* @return A CompletableFuture<Void> representing the asynchronous operation.
*/
fun logout(context: Context): CompletableFuture<Void> {
fun logout(): CompletableFuture<Void> {
val logoutCompletableFuture: CompletableFuture<Void> = CompletableFuture()
val sessionResponse: CompletableFuture<Boolean> =
sessionManager.invalidateSession(context)
sessionResponse.whenComplete { _, error ->
val sessionResponse: CompletableFuture<Boolean>? = sessionManager.invalidateSession(this)
sessionResponse?.whenComplete { _, error ->
SessionManager.deleteSessionIdFromStorage()
runOnUIThread {
if (error == null) {
Expand All @@ -315,7 +316,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
* @param loginParams The optional login parameters required for authentication. Default is null.
* @return A CompletableFuture<Boolean> representing the asynchronous operation, indicating whether MFA was successfully enabled.
*/
fun enableMFA(loginParams: LoginParams? = null, context: Context): CompletableFuture<Boolean> {
fun enableMFA(loginParams: LoginParams? = null): CompletableFuture<Boolean> {
enableMfaCompletableFuture = CompletableFuture()
if (web3AuthResponse?.userInfo?.isMfaEnabled == true) {
throwEnableMFAError(ErrorCode.MFA_ALREADY_ENABLED)
Expand All @@ -326,7 +327,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
throwEnableMFAError(ErrorCode.NOUSERFOUND)
return enableMfaCompletableFuture
}
processRequest("enable_mfa", loginParams, context)
processRequest("enable_mfa", loginParams)
return enableMfaCompletableFuture
}

Expand Down Expand Up @@ -382,11 +383,11 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
return sessionCompletableFuture
}

private fun fetchProjectConfig(context: Context): CompletableFuture<Boolean> {
private fun fetchProjectConfig(): CompletableFuture<Boolean> {
val projectConfigCompletableFuture: CompletableFuture<Boolean> = CompletableFuture()
val web3AuthApi =
ApiHelper.getInstance(web3AuthOption.network.name).create(ApiService::class.java)
if (!ApiHelper.isNetworkAvailable(context)) {
if (!ApiHelper.isNetworkAvailable(this)) {
throw Exception(
Web3AuthError.getError(ErrorCode.RUNTIME_ERROR)
)
Expand Down Expand Up @@ -441,12 +442,12 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
* @param jsonObject The JSONObject from which to retrieve the login ID.
* @return A CompletableFuture<String> representing the asynchronous operation, containing the login ID.
*/
private fun getLoginId(jsonObject: JSONObject, context: Context): CompletableFuture<String> {
private fun getLoginId(jsonObject: JSONObject): CompletableFuture<String> {
val sessionId = SessionManager.generateRandomSessionKey()
sessionManager.setSessionId(sessionId)
return sessionManager.createSession(
jsonObject.toString(),
context,
this,
)
}

Expand All @@ -459,8 +460,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
*/
fun launchWalletServices(
chainConfig: ChainConfig,
path: String? = "wallet",
context: Context
path: String? = "wallet"
): CompletableFuture<Void> {
val launchWalletServiceCF: CompletableFuture<Void> = CompletableFuture()
val sessionId = SessionManager.getSessionIdFromStorage()
Expand All @@ -477,7 +477,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
"options", initOptions
)

val loginIdCf = getLoginId(paramMap, context)
val loginIdCf = getLoginId(paramMap)

loginIdCf.whenComplete { loginId, error ->
if (error == null) {
Expand All @@ -498,9 +498,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
.encodedPath(sdkUrl.encodedPath).appendPath(path)
.fragment(walletHash).build()
//print("wallet launch url: => $url")
val intent = Intent(context, WebViewActivity::class.java)
val intent = Intent(this, WebViewActivity::class.java)
intent.putExtra(WEBVIEW_URL, url.toString())
context.startActivity(intent)
this.startActivity(intent)
launchWalletServiceCF.complete(null)
}
}
Expand All @@ -524,8 +524,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
method: String,
requestParams: JsonArray,
path: String? = "wallet/request",
appState: String? = null,
context: Context,
appState: String? = null
): CompletableFuture<SignResponse> {
signMsgCF = CompletableFuture()
WebViewActivity.webViewResultCallback = this
Expand All @@ -542,7 +541,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
"options", initOptions
)

val loginIdCf = getLoginId(paramMap, context)
val loginIdCf = getLoginId(paramMap)

loginIdCf.whenComplete { loginId, error ->
if (error == null) {
Expand All @@ -566,10 +565,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
.encodedPath(sdkUrl.encodedPath).appendEncodedPath(path)
.fragment(signMessageHash).build()
//print("message signing url: => $url")
val intent = Intent(context, WebViewActivity::class.java)
val intent = Intent(this, WebViewActivity::class.java)
intent.putExtra(WEBVIEW_URL, url.toString())
intent.putExtra(REDIRECT_URL, web3AuthOption.redirectUrl.toString())
context.startActivity(intent)
this.startActivity(intent)
}
}
} else {
Expand Down

0 comments on commit 9735f1c

Please sign in to comment.