@@ -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