@@ -32,44 +32,63 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
3232 val sdkUrl = Uri .parse(web3AuthOption.sdkUrl)
3333 val context = web3AuthOption.context
3434
35- val initParams = mutableMapOf (
35+ val initOptions = mutableMapOf (
3636 " clientId" to web3AuthOption.clientId,
3737 " network" to web3AuthOption.network.name.lowercase(Locale .ROOT )
3838 )
39- if (web3AuthOption.redirectUrl != null ) initParams [" redirectUrl" ] =
39+ if (web3AuthOption.redirectUrl != null ) initOptions [" redirectUrl" ] =
4040 web3AuthOption.redirectUrl.toString()
41- if (web3AuthOption.whiteLabel != null ) initParams [" whiteLabel" ] =
41+ if (web3AuthOption.whiteLabel != null ) initOptions [" whiteLabel" ] =
4242 gson.toJson(web3AuthOption.whiteLabel)
43- if (web3AuthOption.loginConfig != null ) initParams [" loginConfig" ] =
43+ if (web3AuthOption.loginConfig != null ) initOptions [" loginConfig" ] =
4444 gson.toJson(web3AuthOption.loginConfig)
45+ if (web3AuthOption.buildEnv != null ) initOptions[" buildEnv" ] =
46+ web3AuthOption.buildEnv.toString().lowercase()
4547
48+ val initParams = mutableMapOf (
49+ " loginProvider" to params?.loginProvider,
50+ " redirectUrl" to web3AuthOption.redirectUrl.toString()
51+ )
4652
4753 val paramMap = mapOf (
48- " init " to initParams , " params" to params
54+ " options " to initOptions , " params" to initParams, " actionType " to path
4955 )
56+
5057 extraParams?.let { paramMap.plus(" params" to extraParams) }
5158 val validParams = paramMap.filterValues { it != null }
5259
53- val hash = gson.toJson (validParams).toByteArray( Charsets . UTF_8 ).toBase64URLString( )
60+ val loginIdCf = getLoginId (validParams)
5461
55- val url = Uri .Builder ().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
56- .encodedPath(sdkUrl.encodedPath).appendPath(path).fragment(hash).build()
62+ loginIdCf.whenComplete { loginId, error ->
63+ if (error == null ) {
64+ val jsonObject = mapOf (
65+ " loginId" to loginId
66+ )
67+ val hash = " b64Params=" + gson.toJson(jsonObject).toByteArray(Charsets .UTF_8 )
68+ .toBase64URLString()
5769
58- val defaultBrowser = context.getDefaultBrowser()
59- val customTabsBrowsers = context.getCustomTabsBrowsers()
70+ val url =
71+ Uri .Builder ().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
72+ .encodedPath(sdkUrl.encodedPath).appendPath(" start" ).fragment(hash).build()
6073
61- if (customTabsBrowsers.contains(defaultBrowser)) {
62- val customTabs = CustomTabsIntent .Builder ().build()
63- customTabs.intent.setPackage(defaultBrowser)
64- customTabs.launchUrl(context, url)
65- } else if (customTabsBrowsers.isNotEmpty()) {
66- val customTabs = CustomTabsIntent .Builder ().build()
67- customTabs.intent.setPackage(customTabsBrowsers[0 ])
68- customTabs.launchUrl(context, url)
69- } else {
70- // Open in browser externally
71- context.startActivity(Intent (Intent .ACTION_VIEW , url))
74+ val defaultBrowser = context.getDefaultBrowser()
75+ val customTabsBrowsers = context.getCustomTabsBrowsers()
76+
77+ if (customTabsBrowsers.contains(defaultBrowser)) {
78+ val customTabs = CustomTabsIntent .Builder ().build()
79+ customTabs.intent.setPackage(defaultBrowser)
80+ customTabs.launchUrl(context, url)
81+ } else if (customTabsBrowsers.isNotEmpty()) {
82+ val customTabs = CustomTabsIntent .Builder ().build()
83+ customTabs.intent.setPackage(customTabsBrowsers[0 ])
84+ customTabs.launchUrl(context, url)
85+ } else {
86+ // Open in browser externally
87+ context.startActivity(Intent (Intent .ACTION_VIEW , url))
88+ }
89+ }
7290 }
91+
7392 }
7493
7594 fun initialize (): CompletableFuture <Void > {
@@ -104,29 +123,50 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
104123 loginCompletableFuture.completeExceptionally(UnKnownException (error))
105124 }
106125
107- web3AuthResponse = gson.fromJson(
108- decodeBase64URLString(hash).toString(Charsets .UTF_8 ), Web3AuthResponse ::class .java
109- )
126+ val sessionId = hash.split(" &" )[0 ].split(" =" )[1 ]
110127
111- if (web3AuthResponse?.error?.isNotBlank() == true ) {
112- loginCompletableFuture.completeExceptionally(
113- UnKnownException (
114- web3AuthResponse?.error ? : Web3AuthError .getError(ErrorCode .SOMETHING_WENT_WRONG )
115- )
116- )
117- } else if (web3AuthResponse?.privKey.isNullOrBlank()) {
118- loginCompletableFuture.completeExceptionally(Exception (Web3AuthError .getError(ErrorCode .SOMETHING_WENT_WRONG )))
119- } else {
120- web3AuthResponse?.sessionId?.let { sessionManager.saveSessionId(it) }
128+ if (sessionId != null ) {
129+ sessionManager.saveSessionId(sessionId)
121130
122- if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true ) {
123- KeyStoreManagerUtils .encryptData(
124- web3AuthResponse?.userInfo?.verifier.plus(" | " )
125- .plus(web3AuthResponse?.userInfo?.verifierId),
126- web3AuthResponse?.userInfo?.dappShare!! ,
127- )
131+ // Rehydrate Session
132+ if (ApiHelper .isNetworkAvailable(web3AuthOption.context)) {
133+ this .authorizeSession().whenComplete { resp, error ->
134+ if (error == null ) {
135+ web3AuthResponse = resp
136+ if (web3AuthResponse?.error?.isNotBlank() == true ) {
137+ loginCompletableFuture.completeExceptionally(
138+ UnKnownException (
139+ web3AuthResponse?.error
140+ ? : Web3AuthError .getError(ErrorCode .SOMETHING_WENT_WRONG )
141+ )
142+ )
143+ } else if (web3AuthResponse?.privKey.isNullOrBlank()) {
144+ loginCompletableFuture.completeExceptionally(
145+ Exception (
146+ Web3AuthError .getError(
147+ ErrorCode .SOMETHING_WENT_WRONG
148+ )
149+ )
150+ )
151+ } else {
152+ web3AuthResponse?.sessionId?.let { sessionManager.saveSessionId(it) }
153+
154+ if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true ) {
155+ KeyStoreManagerUtils .encryptData(
156+ web3AuthResponse?.userInfo?.verifier.plus(" | " )
157+ .plus(web3AuthResponse?.userInfo?.verifierId),
158+ web3AuthResponse?.userInfo?.dappShare!! ,
159+ )
160+ }
161+ loginCompletableFuture.complete(web3AuthResponse)
162+ }
163+ } else {
164+ print (error)
165+ }
166+ }
128167 }
129- loginCompletableFuture.complete(web3AuthResponse)
168+ } else {
169+ loginCompletableFuture.completeExceptionally(Exception (Web3AuthError .getError(ErrorCode .SOMETHING_WENT_WRONG )))
130170 }
131171 }
132172
@@ -171,7 +211,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
171211 */
172212 private fun authorizeSession (): CompletableFuture <Web3AuthResponse > {
173213 val sessionCompletableFuture: CompletableFuture <Web3AuthResponse > = CompletableFuture ()
174- val sessionResponse: CompletableFuture <String > = sessionManager.authorizeSession(true )
214+ val sessionResponse: CompletableFuture <String > = sessionManager.authorizeSession(false )
175215 sessionResponse.whenComplete { response, error ->
176216 if (error == null ) {
177217 val tempJson = JSONObject (response)
@@ -207,6 +247,23 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
207247 return sessionCompletableFuture
208248 }
209249
250+ private fun getLoginId (jsonObject : Map <String , Any ?>): CompletableFuture <String > {
251+ val createSessionCompletableFuture: CompletableFuture <String > = CompletableFuture ()
252+ if (this .sessionManager == null ) {
253+ createSessionCompletableFuture.completeExceptionally(Exception (" Session Manager is not initialized" ))
254+ }
255+ val sessionResponse: CompletableFuture <String > =
256+ sessionManager.createSession(gson.toJson(jsonObject), 600 )
257+ sessionResponse.whenComplete { response, error ->
258+ if (error == null ) {
259+ createSessionCompletableFuture.complete(response)
260+ } else {
261+ createSessionCompletableFuture.completeExceptionally(error)
262+ }
263+ }
264+ return createSessionCompletableFuture
265+ }
266+
210267 fun getPrivkey (): String {
211268 val privKey: String? = if (web3AuthResponse == null ) {
212269 " "
0 commit comments